New in libguestfs 1.13.17 is a tool for making virtual machines or disk images sparse, also known as thin provisioning.
This tool restores sparseness lost after you’ve created and deleted files. It works for virtually any guest OS, and supports almost any filesystem.
It can also do some clever things like finding sparseness in the unused parts of LVM physical volumes, or converting between formats (eg. raw to thin-provisioned qcow2).
It’s also (like virt-resize) a temporary tool: hopefully one day KVM and guests will be able to resize and trim themselves. That day isn’t here yet.
So hows does it work? I’ve been doing something like this by running a program that writes a file full of zeros till it runs out of disk space, then converting that fully expanded disk image back to a sparse one by converting it to qcow2. Is this more efficient? Does it actually understand the internal filesystem structure and get rid of unused blocks that way?
http://git.annexia.org/?p=libguestfs.git;a=blob;f=sparsify/sparsify.ml;hb=HEAD
It currently has two strategies for turning free space into zeroes: write a file full of zeroes as you said, and creating an LV in each volume group and filling it with zeroes. The file and the LV are of course temporary and deleted afterwards.
We then use
qemu-img convert
(even when not converting the format) since this has built-in zero detection and creates sparse output correctly, at least, it creates sparse output correctly for raw and qcow2 formats.I have tested this on a variety of Linux and Windows guests and it is very effective.
In future we would like to do something with swap space.