Reclaiming space on QCOW2 files


I’m currently running some VMs on my home server with libvirt + Kimchi.

The VMs are stored in QCOW2 files and I noticed that over time they tend do expand beyond the size specified for the VM.

I did a quick Googling [1, 2] and I found that you can setup the VMs using the VirtIO-SCSI controller and run fstrim on the guest periodically to reclaim the unused space. But, apparently there are some limitations (e.g. it doesn’t seem to work if the VM has snapshots).

I also noticed that once fstrim has ran ls -l doesn’t show the reduced filesize on the host, but if you look at du or qemu-img info the filesize has been reduced. Not sure why.

How are you guys dealing with this issue when setting up VMs?


I found it also to be dependant on the fs you use on the guest and the way that fs utilizes free space. normal ext4 for instance first writes to former unused blocks, which makes sparse images grow steadily, even if you delete files on the guest. fstrim isn’t always correctly supported on top of that :confused:

xfs on the other hand does first overwrite former used blocks, so won’t neccessarily grow the sparse as fast as ext4 might do. so using that might help with the need to use trim or rewrite the sparse images, but ofc xfs does have its own caveats.


Thank you @Falzo.

I wonder how providers deal with this issue.

Depending on the guests disk activity, a dozen VMs on a host and you’ll be running out of space at some point. And you can’t count on clients running trim on their environments. Unless that is being built-in in the OS images offered.

For Proxmox, I have FSTrim installed on all VMs. They are setup with VirtIO SCSI for the SCSI controller under options. Under the hardware section I have the hard drive as a SCSI device with discard enabled. Make sure to restart the VMs, and then run FSTrim -a -v. Also, some VMs need the good old hole punching method.

More info can be found here:

1 Like

the list command to see how sparse a file is: ls -lash

[email protected]:/var/lib/vz/images/101# ls -lash
total 31G
4.0K drwxr----- 2 root root 4.0K May 19 00:46 .
4.0K drwxr-xr-x 4 root root 4.0K May 18 22:48 ..
 31G -rw-r----- 1 root root  50G May 20 03:20 vm-101-disk-1.raw
[email protected]:/var/lib/vz/images/101# qemu-img info vm-101-disk-1.raw 
image: vm-101-disk-1.raw
file format: raw
virtual size: 50G (53687091200 bytes)
disk size: 30G