Resizing a kvm image from ubuntu

Because the ubuntu install uses LVM it is not as simple to resize a KVM image.

First, shutdown the original image, and make a back up of that image. This is important!

Next, increase the size of the image. For example, this will add 5GB to your image.

qemu-img resize image.img +5G

start the image running, and log into it.

virsh start image

Use parted to edit the drive. Note: don’t use fdisk for this, as the sizes never work out. Parted allows you to just increase the size of a partition, whereas in fdisk you delete the partition and create a new one (if you do this it is critical the new ones start at the same point as the old ones, and I couldn’t get fdisk to do that).

parted /dev/vda

Set the units to sectors

(parted) unit s

Display the current partition table

(parted) print

Increase the size of the extended partition . Set the end to 1 less than the disk size listed in print. Mine looked like this: Disk /dev/vda: 52428800s and so I set it to 52428799s

(parted) resizepart 2

Increase the size of the logical (lvm) partition

(parted) resizepart 5

use the same end as before.

Now we need to expand the size of the physical and logical volumes. First, lets see their current size:

pvdisplay

and now resize the the physical volume to occupy the whole disk:

pvresize /dev/vda5

Similarly, for the logical volume:

lvdisplay

extend the size of the disk. We do this in two steps. The first one causes an error and then we use that for the second command. Here the key is to use a size bigger than you added to the disk (I only increased my disk by 5G).

lvextend -L+10G /dev/vname/root

This will cause an error:

 Extending logical volume root to 28.74 GiB
 Insufficient free space: 2560 extents needed, but only 1224 available

So we use lvextend again to add the number of extents that are available:

lvextend -l+1224 /dev/vname/root

and finally extend the disk:

resize2fs /dev/vname/root

You have now extended the size of the disk!