Is there any way to shrink qcow2 image without converting it raw?

I cannot convert it to raw because I don't have disk space enough for raw image.


Noop conversion (qcow2-to-qcow2) removes sparse space:

qemu-img convert -O qcow2 source.qcow2 shrunk.qcow2

You can also try add compression (-c) to the output image:

qemu-img convert -c -O qcow2 source.qcow2 shrunk.qcow2

Before running above command be sure to shutdown the VM. After you are satisfied with the result, you can rename shrunk.qcow2 to original name your VM config wants.

|improve this answer|||||
  • 3
    the -c flag in qemu-img convert -c -O is for compression, not shrink... and it can decrease the disk speed... – Anton Anikin Jul 4 '14 at 14:41
  • This creates a second file, though, so you need twice as much free space as the image? Is there a way to shrink it in-place? – endolith Feb 13 '19 at 16:37

Try virt-sparsify - it will zerofill the unused blocks in the image and then deduplicate the zeroes.

|improve this answer|||||
  • Thanks for you answer, but virt-sparsify said it cannot resize disk images and I found virt-resize from that, but looks like I still need to allocate spaces to expand (that I don't have enough spaces on my harddisk). Anyway, Thank you very much for you info. – YOU Sep 27 '12 at 8:17
  • 2
    @YOU Current versions of virt-sparsify can work in-place without allocating space for a complete copy of the disk image. – Michael Hampton Feb 20 '18 at 5:07

I use virt-sparsify:

virt-sparsify /path/to/source.qcow2 --compress /path/to/output.qcow2
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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