Shrinking size of qcow2 qemu images


You have provisioned virtual disk/image for your guest that is thin-provisioned, size 50GB.

image on host is /var/lib/libvirt/images/aa0d036a-f814-4cd8-991f-d0a0ad21a7d4 = 0MB currenty, since no data writen from inside VM

You install OS in your VM, write lot of data to diks (benchmark diks via DD ?) and then delete that data.

You current amount of disk space as seen from inside VM is 870 MB (CentOS minimal installed) ?

But size of image /var/lib/libvirt/images/aa0d036a-f814-4cd8-991f-d0a0ad21a7d4 on host is i.e. 40GB – how is that possible ?

You want to shirnk your image on hosts, since you really have only 1GB os data inside VM.

So :

From inside VM, fill the disk with zeros  – later we will use qemu-img to convert image, and qemu-img does nice job of compressing the zeros inside qcow2 files:

root@vm.local# dd if=/dev/zero of=/root/zeros bs=4m

and let this run until it completely fills your disk space inside VM.

then remove the file, and shutdown the guest VM:

root@vm.local# rm -f /root/zeros
root@vm.local# shutdown -h now

Then, on the host we convert existing image to new one, and qemu-img will compress all those zeros inside our image:

root@HOST# mv /var/lib/libvirt/images/aa0d036a-f814-4cd8-991f-d0a0ad21a7d4 /var/lib/libvirt/images/oldimage
root@HOST# qemu-img convert -O qcow2 /var/lib/libvirt/images/oldimage /var/lib/libvirt/images/aa0d036a-f814-4cd8-991f-d0a0ad21a7d4

will take a while, and remove old image

root@HOST# rm -f /var/lib/libvirt/images/oldimage
root@HOST#  du -hs aa0d036a-f814-4cd8-991f-d0a0ad21a7d4
871M aa0d036a-f814-4cd8-991f-d0a0ad21a7d4

There you go.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: