KVM: Convert non-sparse RAW image to sparse qcow2
I recently had a raw KVM image which was accidentally created with virt-install without sparse options. So this image was unnecessary big and wasted a lot of disk space. I also wanted to convert the image to qcow2 format to use the additional features. The usual way to do that is the tool virt-convert. The problem is, virt-convert copies everything 1:1, so the image is qcow2 after conversion but still the same size.
There is an easy way to fix this: The tool virt-sparsify.
I converted the raw non-sparse image file to a sparse qcow2 image:
# virt-sparsify disk.raw –convert qcow2 disk.qcow2
Depending on how big the disk is, this can take a long time.
And also important the warning from the man pages:
Using
virt-sparsify
on live virtual machines, or concurrently with other disk editing tools, can be dangerous, potentially causing disk corruption. The virtual machine must be shut down before you use this command, and disk images must not be edited concurrently.