Extend, Expand or Resize an LVM root partition ‘/’ in linux

If your linux root partition ‘/’ is an LVM, you could easily extend the drive. Make use of the commands listed later in this post.

First let me explain how an LVM is setup.

Different components in an LVM setup
Physical Volumes These are real physical hard drives or partitions attached to the machine.Favorite commands: pvdisplay, pvcreate, pvresize
Volume Groups Several physical volumes are combined to form a volume group. Multiple Volume groups are also possible. Additional PVs are added to a VG to expand it anytime online.Favorite commands: vgdisplay, vgextend, vgcreate
Logical Volumes  Logical Volumes are carved out of a Volume Group. Logical Volumes could be expanded anytime if its VG has enough free space availableFavorite commands: lvdisplay, lvextend, lvcreate

In short, several physical volumes(PV) are first created. Volume groups(VG) are then built on top of multiple PVs. Logical Volumes(LV) are then carved out of any VG until the VGs run out of storage. Adding more PVs to a VG would help increase the storage in the VG.

Now lets get to work. Continue reading.

Follow below steps to get your LVM extended.

To get to know free space on each drive and the filesystem names, execute “df -h”.

From above output, “/” mount point file system has 1.8G Available space, 9.0 G Size, 80% used and filesystem name is /dev/mapper/ubuntu–vg-root

Now that we have the filesystem name. Lets see the “lvscan” to view all LVMs.

Oops. The lvm names doesn’t match with those in “df” command output.
Here is how you get to know the right LVM.

First lets try the command with /dev/sda1 filesystem

/dev/sda1 is not an LVM and we got the message as expected. Now lets try with /dev/mapper/ubuntu–vg-root

Yeah. It’s an LVM named “/dev/ubuntu-vg/root”. And the LV belong to VG named “ubuntu-vg”. Lets see the vgdisplay.

From this vgdisplay output, (Free PE / Size: 1535 / 6.00 GiB) we have enough freespace in the VG. If there was no space available in the VG, follow below steps. Else you could skip a few.

Have a look at all the PVs we have with command “pvscan”

We have 4 Physical Volumes as listed above.

Before attaching new hard drive to the box, note that “sdd” is the last one in the above list that starts with letter ‘s’. When you add a new hard drive, It would be shown next to it hopefully as “sde”. Now shutdown your machine. Attach the hard drive and turn it back on. Execute the same command again.

Yes. Got the same output as expected. The new drive has appeared there as “sde”. Now lets create a partition table in the new disk.

fdisk says the disk does not have a valid partition table. Don’t worry. Simply type ‘w’ and hit enter.

A valid partition table is now written to the new disk. This step could be skipped. I have added PVs and used them without this step in my past without any issues. I am explaining this here to avoid improper/lengthy/confusing output for “sudo fdisk -l” command afterwards.
Next, the disk “sde” has to be converted to a Physical Volume(PV).

To verify, execute “pvscan” to view the new PV.

Add the newly created PV to VG ubuntu-vg

VG is now extended successfully over the new PV. Lets see vgdisplay below.

Wow. Now we have ample space in ubuntu-vg. Free PE/Size = 3582/13.99 GiB and Cur PV = 3.
Lets now continue with the steps to extend the LVM “/dev/ubuntu-vg/root” mounted on “/” as filesystem “/dev/mapper/ubuntu–vg-root”

/dev/ubuntu-vg/root LV is now resized to 10.26 GB. Now verify the lvdisplay output.

Good. We have additional 1 GB available in LVM. That means the LV is extended successfully. But wait. Lets see if the space is now usable by the Operating system.

Oops. Its not usable yet. “df -h” output still shows only 9.0G for the LVM “/” partition. “resize2fs” command is gonna help us now.

Cool. The space should now be usage. Lets have a look at “df -h” once again.

Yes. The LVM “/” partition is now with size 10G.

Job Well Done !!! 😀

Now suppose by mistake you accidentally did too much “lvextend” before “resize2fs”, you could reduce it back with “lvreduce”. CAUTION! there are chances of data loss. Hence be ready with all necessary backups before proceeding. Also this becomes impossible once “resize2fs” command is executed.

Author: Sherbaz

Being a gadget enthusiast, He always felt good to try new programming languages and techniques. But never goes in-depth anywhere. www.SplitExpense.in was found when he tried php, html, javascript and mysql. Being an electronics engineer, he is also interested to build small gadgets and tools in embedded and digital electronics. As a profession, he handles microsoft sql server database and calls himself a database administrator

Leave a Reply