How do you decrease or shrink the size of a KVM virtual machine disk?
I allocated a virtual disk of 500GB (stored at /var/lib/libvirt/images/vm1.img), and I'm finding that overkill, so now I'd like to free up some of that space for use with other virtual machines. There seems to be a lot answers on how to increase image storage, but not decrease it. I found the virt-resize tool, but it only seems to work with raw disk partitions, not disk images.
Edit: I'm using an LVM with an Ext4 formatted partition.
Edit: GParted screenshot showing my LVM parition layout. I need to do a lot more then just resize the filesystem. I know of no safe way to resize an LVM. And please don't tell me to use pvresize
. Despite its name, it does not support resizing LVMs.
I did try sudo pvresize /dev/vda5
, but it just says physical volume "/dev/vda5" changed
but doesn't actually reduce the size.
I tried start parted
to manually set the partition size (very dangerous), but doing print all
just gives me the error "/dev/sr0 unrecognised disk label".
Edit: By following these instructions, I was able to successfully shrink both my logical and physical volumes (although I had to remember to activate and deactivate lvm before and after certain commands, which the instructions omit.
Now GParted is showing 250G of unallocated free space. How do I remove this from the KVM disk image and give it back to the underlying hypervisor?
Thanks to those who posted, but your answers were way too vague to be of any help.
After hours of Googling, I finally found a guide(link redacted) providing step-by-step instructions on how to shrink my filesystem, logical volumes, and physical volumes. The trick that most guides miss is the need to actually delete the physical partitions, and recreate them with the correct size, because parted is unable to resize lvm partitions.
I then found this documentation on qemu-img, which explains how to shrink a raw-formatted virtual disk image by running:
What you need to do,
This can be accomplished through both GUI and CLI
Resources
Tools
This worked really well for me. It compresses and shrinks the 0 blocks in the disk image. The image will expand over time as it's over written to again.
This should give an absolute value to resize smaller, down to 20G in this example.
I recommend before doing any of this you take a complete copy of the disk image as it is, then when it all breaks you can simply copy it back to start over.
There's 3 things you need to do:
1) Make the disk image bigger. In your host:
qemu-img resize foo.qcow2 +32G
Now your guest can see a bigger disk, but still has old partitions and filesystems.
2) Make the partition inside the disk image bigger. You need to boot off a LiveCD in your guest for this, since you won't be able to mess with a mounted partition. This is quite involved and probably the most dangerous part. It's quite a lot to copy here, so I'll just link instead for now. You want to do something like this:
http://www.howtoforge.com/linux_resizing_ext3_partitions_p2
OR 2b) creating a new partition would be simpler (and safer) if you just want more storage space. Use fdisk or cfdisk, or whatever you feel comfortable with - you should see a whole bunch of unallocated space on your guest disk now.
3) Finally, if you resized your existing partition, make the filesystem inside the new bigger partition bigger (this is actually in the guide linked above anyway). Inside your guest: