Resizing Linux Logical Volumes With LVM

Expanding an Existing Disk

If you are using a virtualization platform such as VMWare/Hyper-V/Xen/KVM/Citrix Xen/etc you will need to shutdown the VM and extend the virtual drive. Once you have finished doing this power the machine back on and do the following:

pvdisplay

This will display the physical volume group and what is currently in it. If you are working with a system that has a single disk you will likely be working off of /dev/sda of /dev/xvda in Citrix Xenserver. We will need to create a new partition on this disk to extend the logical volume in question

fdisk /dev/sda
p
n
p
3
accept default begin block
accept default end block
t
3
8e
p
w
q

The sequence of steps above opens fdisk for /dev/sda. The p denotes the printing of the current partition table, which can be useful to see how many partition numbers there are, usually this will be /dev/sda1 for the boot partition and /dev/sda2 for the filesystem partition. Assuming this is the case pressing n will create a new partition, select p for primary, then select the next available digit (in this case 3 so we can create /dev/sda3). Then accept the default starting block and the default ending block. Once this is complete we need to set the type by pressing t then select the partition number (3) then choose 8e as the identifier for Linux LVM. Once this is done press p to confirm everything looks correct then w to write changes and q to quit.

Once you press w you have committed the changes if you make a mistake press q and try again, this will prevent any erroneous writes from occurring.

At this point we can proceed to extend the volume

partprobe

running partprobe will resync the partition table to the OS so that it can see the new partition, a reboot will do the same thing and may be required if the next step fails, however if possible to avoid a reboot it would be advisable.

pvcreate /dev/sda3

This creates /dev/sda3 as a new physical volume in the physical volume group. Next we will need to see what the volume group name is and then extend the volume group.

vgdisplay

Using the output of the above command we can then extend that volume group. For this example I’ll call it vg_example, yours will be different

vgextend vg_example /dev/sda3

Next we will need to identify the name of your logical volume you wish to extend by running the following command

lvdisplay

Once you have acquired this name we can proceed to the next example. Please note I’ve used lv-root as the name, yours may differ, and I have also used 50Gb as the amount to extend by, please use the correct amount for your use case

lvextend -L +50G /dev/mapper/vg_example/lv-root

Now we have arrived at the final step which is to resize the filesystem

resize2fs /dev/mapper/vg_example/lv-root

If you are using CentOS 7 or another OS that defaults to an XFS filesystem you will need to use xfs_growfs instead of resize2fs

Adding an Additional Disk

If you are working with a physical machine or a VM that you are adding another disk to the process is similar to the above but moderately easier. Assuming the new device we are adding is /dev/sdb, if you are doing this without restarting the machine run partprobe before attempting the below command. If this doesn’t work a reboot may be required.

pvcreate /dev/sdb

This creates /dev/sdb as a new physical volume in the physical volume group. Next we will need to see what the volume group name is and then extend the volume group.

vgdisplay

Using the output of the above command we can then extend that volume group. For this example I’ll call it vg_example, yours will be different

vgextend vg_example /dev/sdb

Next we will need to identify the name of your logical volume you wish to extend by running the following command

lvdisplay

Once you have acquired this name we can proceed to the next example. Please note I’ve used lv-root as the name, yours may differ, and I have also used 50Gb as the amount to extend by, please use the correct amount for your use case

lvextend -L +50G /dev/mapper/vg_example/lv-root

Now we have arrived at the final step which is to resize the filesystem

resize2fs /dev/mapper/vg_example/lv-root

If you are using CentOS 7 or another OS that defaults to an XFS filesystem you will need to use xfs_growfs instead of resize2fs

 

Leave a Reply

Your email address will not be published. Required fields are marked *

17 − 8 =