Extending LVM Partition on Linux
I recently ran out of storage on one of my VMs and a great thing about LVM is that everything can be done on the fly. Provisioned additional storage to a running machine in vSphere, and with few simple commands extended my currently full root partition. LVM has a hierarchy-like structure where all your Physical Volumes can be pooled into Volume Groups, which hold your Logical Volumes. So, first of all we have to deal with physical side of things by adding Physical Volume to Volume Group.
Adding Physical Volume
Start with adding drive space to the VM in your hypervisor, boot it up, run your favourite partitioning tool, like cfdisk.
- cfdisk /dev/sdX
Where X is a drive you hold your free space in. Usually it's /dev/sda, since in a simple VM environment you just assign one virtual disk. Then create new partition, set size and select 8e type (Linux LVM). Take note of partition index value Y (/dev/sdXY) as we will need it later on. Write the table, restart the system, then create the Physical Volume:
- pvcreate /dev/sdXY
You should get a message: 'Physical volume "/dev/sdXY" successfully created'.
Extending Volume Group
Now you must add Physical Volume to Volume Group by finding out its name:
On the top of the list, find 'VG Name' and use its value to extend Volume Group:
- vgextend <VG Name> /dev/sdXY
Message: 'Volume group "<VG Name>" successfully extended' verifies the completion.
Extending Logical Volume
Find out names of current Logical Volumes:
It lists current partitions, find the one you want to extend and use 'LV Path' (a directory like /dev/<VG Name>/root) for:
- lvextend -l+100%FREE <LV Path>
5G represents the extension size value of five gigabytes. Message: 'Logical volume root successfully resized' should appear.
Enter below command to finalize the extension process by resizing the filesystem:
- resize2fs <LV Path>
Check free disk space to make sure everything is in order:
- df -h
If you're extending root partition, some people advise to turn off machine altogether and use live CD. Not sure if that is necessary, but backing it up before extension would be a right decision.