14

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.

GParted screenshot

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?

2
  • Do you actually need to downsize the virtual disk, or would compacting it suffice? You could do the latter with something like qemu-img -O qcow2 vm1.img vm1-compact.qcow2. Oct 14, 2012 at 10:38
  • @AnsgarWiechers, Yes, I still need to use the disk, and I've allocated far more space than I'll ever need.
    – Cerin
    Oct 14, 2012 at 14:26

6 Answers 6

13

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:

sudo qemu-img resize /var/lib/libvirt/images/myvm.img 255G
2
  • qemu-img is a great tools when it comes to working with disk images for kvm. 3 years later your link seems broken, here the manual page from die.net linux.die.net/man/1/qemu-img Dec 26, 2017 at 3:35
  • 1
    And if anyone wants this convenience, and is stuck with (does not want) a .qcow2 VM, convert it first qemu-img convert -f qcow2 -O raw image.qcow2 image.img and then you can run the above command.
    – oemb1905
    Apr 10, 2019 at 6:16
4

What you need to do,

  • Take a backup
  • Shrink the file system(s) *don't do this on a live system, I recommend using a live cd.
  • Create a new disk image of desired size.
  • Run a live os, with the new and old images attached as (virtual) hard disks (not mounted)
  • Create the new partition(s) the same size as the resized partitions on the old disk
  • Use dd to mirror the data to the new partition.
  • You'll possibly need to purge / regenerate grub(2) to boot successfully.

This can be accomplished through both GUI and CLI

Resources

Tools

  • gparted ( link omitted due to Spam, not enough rep )
8
  • The hard part is "Shrink the filesystem". I'm using an LVM, which no tool I've found support resizing. Not even a GParted Live CD.
    – Cerin
    Oct 14, 2012 at 0:34
  • I think this covers it, I've not used it so please report good or bad tcpdump.com/kb/os/linux/lvm-resizing-guide/shrink.html
    – daxroc
    Oct 14, 2012 at 0:51
  • There are 2 main points to observe: 1) Do things in order (first shrink the filesystem, then shrink the partition, then shrink the disk). 2) The procedure/tools for resizing a filesystem depend heavily on the actual filesystem (which the OP chose not to reveal). The article @daxroc posted won't work for filesystems other than ext[234]. Oct 14, 2012 at 10:32
  • @daxroc, I need to reduce both the logical and physical volumes. Your link only covers reducing the logical volume.
    – Cerin
    Oct 14, 2012 at 16:29
  • agreed. backup, new disk, restore is safest and easiest
    – dyasny
    Oct 14, 2012 at 17:14
2

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.

qemu-img convert -c -O qcow2 vmdisk.qcow2 vmdisk-comp.qcow2
0
sudo qemu-img resize --shrink /var/lib/libvirt/images/[operating sys filename].qcow2 20G

This should give an absolute value to resize smaller, down to 20G in this example.

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Nov 30, 2021 at 18:19
0

@daxroc's solution just worked for me, expanding on it below since I cannot upvote. In my example, my host machine is Unraid (Linux) and my guest is ArchLinux (the official basic vm). Their images are set to 40GB which is too much for my use case. I successfully resized (shrunk) the image to 8GB using the below steps.

On your host machine:

  1. Take a backup of existing image cp vdisk1.img vdiskbak.img
  2. Create a new target image with reduced size qemu-img create -f raw vdisknew.img 8G
  3. Mount GParted Live CD, start VM, and shrink the file system(s) using GParted on LIVE CD (In my example, I only used 1.2GB of Root Partition and my image was 40GB)
  4. Shutdown VM and mount Clonezilla Live CD, start VM (with the new and old images attached i.e. vdisk1.img & vdisknew.img)
  5. Mirror disk over using disk to disk the EXPERT mode and enable the -icds option (Leave all defaults - can hit Enter on each first selection) - ubackup
2
-5

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:

1
  • 7
    He is trying to make the image smaller, not larger. Oct 13, 2012 at 20:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .