we are using kvm/qemu with qcow2-images for our virtual machines.

qcow2 has this nice feature where the image file only allocates the actually needed space by the virtual-machine. but how do i shrink back the image file, if the virtual machine's allocated space gets smaller?


1.) i create a new image with qcow2 format, size 100GB

2.) i use this image to install ubuntu. installation needs about 10 gb, the image-file grows up to about 10GB. nothing unexpected so far.

3.) i fill up the image with about 40 GB of additional data. the image-file grows up to 50GB. i am ok with that :-)

4.) this is where it gets strange: i delete all of the 40GB data on the image, but the image-size still eats up 50GB.

question: how do i free up that 40GB of data and shrink the image to the only needed 10 GB?

thanks in advance, berni


The image will not shrink automatically, since when you delete files, you don't actually delete data (this is why undelete works). Qemu has a facility to shrink qcow2 images back, but what the utility does is really deduplicate the zeroes from the disk, leaving all other information intact. So the idea would be to:

  1. Zero-fill the drive (dd if=/dev/zero of=/some/file until you run out of space)
  2. delete /some/file
  3. shut down the VM
  4. cd to where the images for the VM are kept and run qemu-img convert -O qcow2 original_image.qcow2 deduplicated_image.qcow2
  5. change the VM settings to use the new deduplicated_image.qcow2, test the VM is working, and remove the old image

This, afaik, will only work with qcow2 images, I haven't tested other formats.

  • ah, ok. this was very helpful - thank you! – bmaeser Nov 10 '11 at 18:09
  • 2
    If you have a windows guest run SDelete -z to zero up all free space (steps 1,2 above) – ndemou Jul 1 '16 at 6:57

virt-sparsify can do all this with less hassle on your part: http://libguestfs.org/virt-sparsify.1.html

  • Not so sure it was there in 2011 – dyasny Aug 21 '14 at 15:17
  • 1
    @dyasny If the existing answers to old questions have become obsolete over time, it is worth writing a new answer. This particular new answer might however not be up to standards for this site. – kasperd Aug 21 '14 at 19:08
  • @kasperd you are correct in general, reality does change and over years new software and solutions appear. However, maintaining every QnA thread up to date is pretty much impossible. The old solution still works, the new one is better, no doubt, but what will we do when a 3rd gen solution comes out? – dyasny Aug 21 '14 at 19:40
  • 3
    @dyasny If somebody want to spend the effort on writing a new quality answer, each time the old one is obsolete, they should be encouraged to do so. A more thorough discussion on how to deal with outdated answers is better suited for meta. – kasperd Aug 21 '14 at 19:52

virt-sparsify fileA fileB will do all that, but will not actually reduce the size of the image. For this you need to add --compress.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.