The biggest advantage of LVM over traditional disk partitions is its support for "dynamic partitions"; you can create and resize (grow or shrink) LVM volumes dynamically as needed. There is no notion of physical disk boundary in LVM logical volumes, so you can create a large LVM volume that spans across multiple smaller physical disks. Such flexible partitioning allows you to manage storage space more efficiently as disk usage patterns change over time.
If you want to add new disks to an existing LVM volume to expand its size, you can easily do it, and here is how.
First, use fdisk to check what disks you have on your system.
In this example, three disks are detected (/dev/sda, /dev/sdb, /dev/sdc). The first disk (/dev/sda) is used by LVM, while the other two (/dev/sdb and /dev/sdc) are not added to LVM, and do not have any partition in them.
Check available LVM volume groups and logical volumes with the lvs command.
In the example, one volume group ("yoda-vg") exists, and two logical volumes ("root" and "swap") are created in this volume group.
According to the df output, the "root" logical volume is mapped to /dev/mapper/yoda--vg-root by the device mapper.
Using this information, I will show how to add two disks /dev/sdb and /dev/sdc to the "root" logical volume in the rest of the tutorial.
Step One: Create Partitions on New Disks (Optional)
The first step is to create a partition on each new disk before adding them to LVM. This step is needed only if you want to allocate only part of the disk to LVM. If you want to add the whole disk to LVM, creating a partition is not necessary, and you can skip this step.
Create a primary-type partition which takes up the whole disk (2TB).
Repeat the same step with /dev/sdc.
Use fdisk to verify newly created partitions (/dev/sdb1 and /dev/sdc1).
Step Two: Create Physical Volumes on New Disks
Next, create a physical volume on each new disk partition (/dev/sdb1 and /dev/sdc1).
$ sudo pvcreate /dev/sdc1
If you haven't created any partition on each disk, run the following commands instead.
$ sudo pvcreate /dev/sdc
Using lvmdiskscan, verify that physical volumes are created successfully.
Newly created LVM physical volumes are named the same as the original partitions (e.g., /dev/sdb1, /dev/sdc1).
Step Three: Extend an Existing Logical Volume
Next, find the volume group which contains the logical volume to expand, and extend the group by adding newly created physical volumes to it.
In our example, the "root" logical volume belongs to the "yoda-vg" volume group. Let's add a physical volume /dev/sdb1 to this group first.
Again, if there is no partition in /dev/sdb, the following command instead.
Given the resized volume group, now extend the "root" logical volume itself:
The above command tells the "root" logical volume to use all available additional free space in its volume group. Note that /dev/yoda-vg/root is the device the "root" volume is mapped to.
The final step is to enlarge the filesystem created inside the "root" volume. Otherwise, the filesystem will not recognize additional free space in the volume.
The resize2fs command supports resizing ext2/ext3/ext4 filesystems. It also supports online resizing in case you expand a filesystem. No need to unmount the filesystem.
At this point, the filesystem should be expanded to take up 100% of the resized "root" logical volume.
Repeat the step three with /dev/sdc.
Subscribe to Ask Xmodulo
Do you want to receive Linux related questions & answers published at Ask Xmodulo? Enter your email address below, and we will deliver our Linux Q&A straight to your email box, for free. Delivery powered by Google Feedburner.
Did you find this tutorial helpful? Then please be generous and support Xmodulo!