Stop QEMU Disk Images From Silently Hogging Your Storage
By Saket Jain Published Linux/Unix
Stop QEMU Disk Images From Silently Hogging Your Storage
Technical Briefing | 11/24/2026
You’ve been there, right? Spun up a new VM, allocated a 100GB qcow2 disk, loaded it with some data, then cleaned things up, dropping a good 50GB of files. You’d expect that storage to be reclaimed, or at least for the qcow2 image on the host filesystem to shrink a bit. But you check du -sh on the image file and… nothing. It’s still huge, maybe even bigger than what the guest thinks it’s using. This isn’t just annoying; I’ve seen this silently eat through entire storage arrays in production, leading to unexpected outages.
Why Your Virtual Disks Are Like a Black Hole (and how to fix it)
What’s actually happening here isn’t a bug, it’s just how qcow2 (and similar sparse disk formats) works by default. When a guest OS deletes a file, it marks those blocks as free *within its own filesystem structure*. But the underlying qcow2 image file on your host doesn’t know anything about that. Those blocks in the qcow2 file are still allocated, holding what it *thinks* is valid data (even if it’s just zeroes or old deleted file contents). And because qcow2 tries to only write new data to new blocks to prevent fragmentation and optimize for copy-on-write, it rarely reclaims those old blocks on its own.
This is where TRIM, or more accurately, the UNMAP command, comes in. TRIM is an ATA command, UNMAP is its SCSI counterpart. Its purpose is simple: when the guest OS deletes data, it sends these commands down the storage stack, telling the underlying device (in our case, the QEMU virtual disk) that these blocks are no longer in use and can be freed. The fstrim command inside your Linux guests is what initiates this process. But just running fstrim in the guest isn’t enough. QEMU and libvirt need to be configured to actually *pass* those UNMAP commands through to the host filesystem. This is typically done by adding discard=’unmap’ to your disk definition in the libvirt XML. You’re looking for the <driver> tag within your <disk> definition. This bit me hard once when I assumed a modern virtio-scsi setup would just ‘know’ to do this; turns out, explicit is better.
fstrim -v /
systemctl enable --now fstrim.timer
Dealing with Bloat: The virt-sparsify Hammer
What if you’re dealing with an old, bloated image where TRIM was never enabled? Or maybe you just need to reclaim space aggressively *right now*? That’s where virt-sparsify from the libguestfs-tools package is a godsend. It works offline, so you’ll need to power off your VM first. It reads the disk image, identifies unused blocks, and writes a new, smaller qcow2 image. You then replace the old image with the new one. It needs temporary space for the new image, so keep that in mind – you’ll need enough free space for at least one full copy of the *actual used data* on your biggest disk, not the full provisioned size.
- Ensure your guest OS supports and runs fstrim (or an equivalent) regularly.
- Verify your libvirt disk definition uses discard=’unmap’ for qcow2 images, especially with virtio-scsi.
- Confirm your host filesystem (e.g., ext4, XFS) supports TRIM/DISCARD on the underlying storage.
- Remember virt-sparsify is your offline friend for emergency cleanup, but plan for temporary space.
Getting this right means happier storage admins, faster backups (because there’s less actual data to copy), and avoiding those embarrassing ‘disk full’ alarms for reasons that feel entirely avoidable. It’s not a one-and-done setup; it’s an operational pattern to keep in mind as you provision new VMs and troubleshoot storage issues. This isn’t just about saving a few GB; it’s about predictable storage usage and efficient infrastructure.
