Resize storage on KVM guest (LVM)

This is how I managed to resize a ext4 filesystem (KVM virtualised Ubuntu 16.04 guest) on a LVM (Logical Volume Manager).

It's very likely, that your devices, physical and virtual volumes are differently named. So you have to adapt the commands properly. In this tutorial I will use the names based on my setting (e.g. the device is /dev/vda). And don't forget to make backups before you start to "play" with your filesystem: You don't want to lose files!

Resizing storange on kvm is quite easy (It's a single command, named img-resize). But the second part, resizing the file system on the guest is not just done with a oneliner. So I wasn't in a good mood: I never used LVM before so the first attempt was to simply open gparted - But working with LVM is really nice (although a bit different) and has several benefits!

First I created a new partition with the size of the free space on the resized HDD. This can be done with cfdisk:

sudo cfdisk /dev/vda

To create a new partition I did the follwoing steps:

  1. select the free space with the cursor
  2. click on "new"
  3. click on "write" to save changes
  4. quit cfdisk

Now I created a pyhsical volume with the following command:

sudo pvcreate /dev/vda3

In a next step I added the new physical volume (/dev/vda3) to a virtual group:

sudo vgextend stats-imsm-vg /dev/vda3

Now I was able to resize the logical volume (I had several logical volumes like root, but I wanted to resize just /home):

sudo lvextend -l +100%FREE /dev/mapper/stats--imsm--vg-home

In the last step I had to update the filesystem, which is streight forward if you use ext3 or ext4 (like in my case). Just run df -hT, if you don't know your filesystem.

sudo resize2fs -p /dev/mapper/stats--imsm--vg-home