Linux LVM (Logical Volume Manager)
Linux LVM (Logical Volume Manager) is a robust tool for managing storage in Linux systems, offering flexibility and advanced features:
- Physical Volume (PV): PVs are physical storage devices or partitions (like hard disks, SSDs, or even RAID arrays) that are initialized for use by LVM. PVs are typically formatted with the LVM physical volume format using tools like pvcreate.
- Volume Group (VG): VGs are created by aggregating one or more PVs together. They act as a pool of storage from which logical volumes can be allocated. VGs are managed with commands like vgcreate to create them, vgextend to add PVs to an existing VG, and vgreduce to remove PVs.
- Logical Volume (LV): LVs are virtual partitions created within VGs. They behave like traditional partitions but offer greater flexibility. LVs can be resized dynamically using lvresize, moved between PVs with pvmove, and snapshots can be created using lvcreate –snapshot for backup or testing purposes.
Extending Disk
Here if we run “fdisk -l”, we can see that the physical disk /dev/sda has a 30 GiB virtual disk with a GPT partition table. It contains three partitions: /dev/sda1, a 1 MiB BIOS boot partition; /dev/sda2, a 2 GiB Linux filesystem partition; and /dev/sda3, a 28 GiB Linux filesystem partition. Additionally, there is a logical volume /dev/mapper/ubuntu–vg-ubuntu–lv of 28 GiB created using LVM, which utilizes the space from the third partition (/dev/sda3).
Running “df -h” shows the disk Usage as follows :
- /dev/mapper/ubuntu–vg-ubuntu–lv: This is the main logical volume for the root filesystem (/). It has a total size of 28 GiB, with 4.3 GiB used, 22 GiB available, and a usage of 17%.
- /dev/sda2: This partition is used for /boot. It has a total size of 2.0 GiB, with 95 MiB used, 1.7 GiB available, and a usage of 6%.
Now we’re extending the existing disk from 30GB to 45GB
Running “fdisk -l” shows that the Disk /dev/sda1 now is 45GB
We can also run “lsblk” to see this information
Next we create a new partition on the sda disk by running “fdisk /dev/sda”, then type “n” to create new
Next type “t” to change the partition type to “8e” which is a hexadecimal code representing Linux LVM (Logical Volume Manager) partition type.
Then type “w” to write the changes
Running “lsblk” shows that the new sda4 partition has been created
Now lets get back a second to the LVM structure as shown below
Here if we run “lvdisplay” to show the Logical Volume, we can see the LV “ubuntu-lv” has a size of 28GB and is a member of Volume Group “ubuntu-vg”
To see the Volume Group, run “vgdisplay”. Here we can see the VG “ubuntu-vg” with the size of 28GB and 0 free Physical Extend (PE) Size
Next to see the Physical Volume, run “pvdisplay”. Here we can see this PV uses partition “dev/sda3” and being used by VG “ubuntu-vg” with the size of 28GB
Now that those are out of the way, lets reverse the order and we start with the Physical Volume first.
We have created a new partition “dev/sda4” earlier, lets use that to create a new PV with “pvcreate /dev/sda4”
Next we extend the size of “ubuntu-vg” VG using the newly created PV, with the command “vgextend ubuntu-vg /dev/sda4”
We can see that this VG now has 15GB of free Physical Extend (PE)
Next run “pvscan” to scan all available physical volumes (PV) on the system, which shows now we have two PV supporting the same VG
After that to extend the Logical Volume, run “lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv”. This command will take all 100% of available free space in the VG
Running “lvdisplay” shows that now we have 43GB of LV Size
And running “vgdisplay” again confirms that all free space has been allocated
Lastly, we need to adjust the filesystem size to match the size of the underlying block device, to do that run “resize2fs /dev/ubuntu-vg/ubuntu-lv”
Running “lsblk” also shows that now the root (/) directory has the size of 43GB taking the space from two partition
Adding new Disk
What if we want to add a new disk? the process if pretty much the same with slight differences
Running “lsblk” shows the new disk is added with the name “sdb”
Firstly we need to create a new partition out of this disk, run the fdisk utility to do that
Next we create a new Physical Volume (PV) using thew newly created “dev/sdb1” partition
Then extend the Volume Group (VG) using the new PV
And then we can extend the Logical Volume (LV) using the free space on VG, but this time we will only extend it by 5GB
Here we can see that now this LV has size of 48GB
And on the VG, we have 15GB of free space after 5GB is used to extend the LV
Lastly, adjust the filesystem to match the new block size
And we’re done here