11 June 2015

Are you aware that, in many cases the size of virtual machine images can be reduced dramatically. This blog studies a case that reduced the image size of a VM from 43GB to only 10GB using two techniques in combination. (The actual compression ratio shall vary from case to case as the contents in the VMs vary).

This is a VM under the control of openstack. It has a 43GB hard disk, only 19GB of which is actually used. We noticed that the image of this virtual machine is about 33GB, however.

Zero unused blocks

The first technique is to "zero unused blocks". Obviously, openstack employs Sparse File or something alike to store VM images. The basic idea is that, if a file contains a large amount of continuous zero blocks then we can save much space by storing meta data of those blocks instead of actually reserve that much blocks. Therefore, we ran the following commands in the VM to erase garbage in its file systems.

# for each file system
dd if=/dev/zero of=./zero bs=1M
rm ./zero
sync
  • The first command is to create a file (named "zero") to take all free space, and this file contains only zero (NULL bytes).
  • The remaining two commands are to delete the file and sync changes onto disk.

After running these commands reduced the image size to around 19GB.

Compress image

The second technique we used is:

qemu-img convert -c original.qcow2 -O qcow2 compressed.qcow2

This reduced the size further to 10GB.



blog comments powered by Disqus