Skip to content

Instantly share code, notes, and snippets.

@nimbosa
Created August 14, 2014 07:51
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save nimbosa/09cc3580c1edcb5ede19 to your computer and use it in GitHub Desktop.
Save nimbosa/09cc3580c1edcb5ede19 to your computer and use it in GitHub Desktop.
shrink + compress virtual disk image in Qemu/KVM

shrink + compress virtual disk image in Qemu/KVM

originally from Martin Kopta's blog

Tue Apr 22 10:41:10 UTC 2014

In order to shrink virtual disk of your virtual machine (process called disk space reclamation) in Qemu/KVM, you can go with following. In your running virtual machine, fill all remaining empty space with zeroes. Then, shut down virtual machine and convert the disk image from original format to the same format (esentially doing nothing). In the process of conversion, Qemu will skip the zeroes and save you some space. You can even add some compression to save even more space.

root@vm# dd if=/dev/zero of=/file
root@vm# rm /file
root@vm# poweroff
user@host$ mv image.qcow2 image.qcow2.bk
user@host$ qemu-img convert -p -c -f qcow2 image.qcow2.bk image.qcow2

Option -p will display progress bar. See qemu-img(1) for more information.

@tomer
Copy link

tomer commented Feb 4, 2018

You should add -O qcow2 to qeum-img or else the generated image might be formatted as RAW.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment