Saturday, October 15, 2022

Extending Virtual Disk in a Linux Virtual Machine Using LVM

To increase a disk size in a virtual machine with Linux operating system configured with LVM, below are the steps (these steps were tested using virtualbox):


1. Power off the virtual machine

2. Increase the virtual disk size in the virtual machine using the virtual machine manager

3. Restart the virtual machine

4. Even though the virtual disk has been increased in size, but LVM is not aware of the change. To make LVM aware of the change, run pvresize command
$ sudo pvresize <pv name>

You can get the physical volume (PV) name by running "pvs" command
$ sudo pvs

5. Once the physical volume (PV) has been resized, run "vgs" to see the new volume group (VG) size
$ sudo vgs

6. Resize logical volume (LV) to make the new size available for us to use. Use below command to resize LV to use 100% of the free space available in VG
$ sudo lvextend -l +100%FREE <LV path>

You can get the LV path by running lvdisplay
$ sudo lvdisplay | grep Path

7. Now the partition is aware of the size change, but not the filesystem. Check the filesystem type and mountpoint using below command
$ df -Th

8. Depending on the filesystem type, extend your filesystem to suit the new partition size. 
For xfs:
$ sudo xfs_growfs <mountpoint>

and for ext4:
$ sudo resize2fs <mountpoint>

9. Verify that your filesystem is now using the new size
$ df -Th

No comments: