I use rsync to back up my workstation, including virtual machine disk images. Due to a known issue with sparse file handling during backups, rsync would expand sparse files, inflating an 11 GB disk image to its full 64 GB provisioned size on the backup server.
To solve this, I converted the disk images to compressed qcow2 format:
1. Convert Existing Raw Images to qcow2
This also enables live snapshot support in virt-manager:
# Convert raw disk to qcow2
qemu-img convert -O qcow2 orig.img new.qcow2
# Replace image
mv orig.img orig.img.bak
mv new.qcow2 orig.qcow2
# Update VM domain XML to use qcow2 driver instead of raw
sudo sed -i "s/type='raw'/type='qcow2'/" /etc/libvirt/qemu/orig.xml
2. Zero-out Free Space Inside the Guest VM
Boot the guest VM and zero all unused blocks so the file can be compacted:
# Inside the guest:
dd if=/dev/zero of=/mytempfile status=progress bs=1M
rm -f /mytempfile
3. Compact the Image on the Host
Shut down the VM cleanly, then recompress the disk image from the host machine:
mv original_image.qcow2 original_image.qcow2_backup
qemu-img convert -O qcow2 -c original_image.qcow2_backup original_image.qcow2