sparse a virtual machine disk

I’ve made a test to find the best “compressed” solution for thin provisioning on virtual machines.

f18.qcow2 is a default minimal Fedora 18 installation.

$ qemu-img info f18.qcow2
image: f18.qcow2
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 1.1G
cluster_size: 65536

Below is the result, sorted by size

1170252 f18.qcow2
 746188 f18.qcow2.spars
 253552 f18.qcow2.spars.compressed
 236860 f18.qcow2.spars.compressed.lrz
 236076 f18.qcow2.spars.compressed.xz
 151028 f18.qcow2.xz
 146172 f18.qcow2.spars.xz
 141276 f18.qcow2.spars.lrz
 141048 f18.qcow2.lrz

It seems that Con Kolivas has made an extraordinary work with lrzip

And these are the commands i’ve run (sorted by the result of above output):

f18.qcow2 qemu-kvm f18.qcow2 -cdrom Fedora-18-x86_64-netinst.iso -cpu host -usbdevice tablet -m 2048
f18.qcow2.spars virt-sparsify f18.qcow2 f18.qcow2.spars
f18.qcow2.spars.compressed virt-sparsify - -compress f18.qcow2 f18.qcow2.spars.compressed
f18.qcow2.spars.compressed.lrz lrzip f18.qcow2.spars.compressed
f18.qcow2.spars.compressed.xz xz -k f18.qcow2.spars.compressed
f18.qcow2.xz xz -k f18.qcow2
f18.qcow2.spars.xz xz -k f18.qcow2.spars
f18.qcow2.spars.lrz lrzip f18.qcow2.spars
f18.qcow2.lrz lrzip f18.qcow2

My specs are:

$ grep 'model name' /proc/cpuinfo | head -1
model name : Intel(R) Core(TM) i7-3517U CPU @ 1.90GHz

$ grep -c processor /proc/cpuinfo 

$ grep MemTotal /proc/meminfo 
MemTotal:        8038060 kB

Shrink your backup images

I am using LVM for storage to virtual machines.
One simply way to make backups (even if the machine is online) is through dd.

(but if you are using a database, online is out of question).

dd will backup the entire partition to a raw image.

Lets make an example:

# time dd if=/dev/vg01/winxp of=winxp.img bs=1024 
5242880+0 records in
5242880+0 records out
5368709120 bytes (5,4 GB) copied, 111,164 s, 48,3 MB/s

So we have a 5Gb backup raw image with sparse files into.
If only there was a way to remove sparse files to reduce the size!

Wait … it does exist: virt-sparsify

# virt-sparsify winxp.img winxp.img.spars
Sparsify operation completed with no errors.  Before deleting the old 
disk, carefully check that the target disk boots and works correctly.

# ls -ltr

-rw-r--r-- 1 root root 5368709120 Αύγ  28 11:55 winxp.img
-rw-r--r-- 1 root root 5368709120 Αύγ  28 11:59 winxp.img.spars

# du -h * 
5,1G    winxp.img
3,1G    winxp.img.spars

2gb sparse files have just removed !

Now you can compress the backup image to reduce the size even more:

# du -h winxp.img*

5,1G    winxp.img
3,1G    winxp.img.spars
1,4G    winxp.img.spars.lrz

We can now remove winxp.img & winxp.img.spars (8.2Gb) from our backup storage.