virt-resize --shrink : Resizing a KVM VM Storage Image (which is a LVM partition)

I had to do some work relating to kvm last week. Including resizing (shrinking) a existing VM and then making a clone of it. I had to do a fair bit of googling and tinkering around to figure out how this can be done. Below is a summary of what I ended up doing.

The fact that the storage images of the VMs were infact LVM partitions played a part in how I had to go about doing the resizing. In retrospect, if LVM was out of the picture (and the if the images were just files) things would have been much easier. Of course this does not mean that LVM + KVM is a bad thing. It makes things a little bit more complicated though.


  • Use virt-df to see what's inside the image we want to resize (shrink in this case)
virt-df /dev/xendisks/gandalf
Filesystem                           1K-blocks       Used  Available  Use%
gandalf:/dev/sda1                     52324100    2521784   47180880    5%
  • Run guestfish and add the image
guestfish -a /dev/xendisks/gandalf
- Once inside guestfish run the following commands
> run
- To see the filesystems inside rub
> list-filesystems
/dev/vda1: ext4
/dev/vda2: unknown
/dev/vda5: swap
  • Now, if you want to resize the vga1 parttition (in this case, we do). Run the follwoing:
> e2fsck-f /dev/vga1
> resize2fs-size /dev/vga1 50G
  • You have now resized the partition.

  • Get information about the parttions inside the image and their size

virt-filesystems --long --parts --blkdevs -h -a /dev/xendisks/gandalf
Name       Type       MBR  Size  Parent
/dev/sda1  partition  83   96G   /dev/sda
/dev/sda2  partition  05   1.0K  /dev/sda
/dev/sda5  partition  82   2.0G  /dev/sda
/dev/sda   device     -    98G   -
  • Get a set of available storage groups
virsh pool-list
Name                 State      Autostart
default              active     yes
virtual_machines_lvm active     yes
  • Create a new volume to where the original image will be copied to.
virsh vol-create-as virtual_machines_lvm gandalf2 50G
Vol gandalf2 created
- Use virt-resize to create a new 'resized' storage (note that if the parttition that's been resized has a ext4 filesystem it might limit how small you can make the parttition)
virt-resize --resize /dev/sda1=-40G /dev/xendisks/gandalf /dev/xendisks/gandalf3
- Edit the Domain gandalf XML configuration:
edit gandalf

and change