Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adityatelange/637598d64caac73ef81f5be021c87f77 to your computer and use it in GitHub Desktop.
Save adityatelange/637598d64caac73ef81f5be021c87f77 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.

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