Working with QCOW2 images often presents a catch-22 situation: we need to reclaim disk space by shrinking images, but converting to RAW format requires temporary space we don't have. This is particularly problematic in cloud environments or constrained development setups where storage is precious.
Before attempting to shrink, it's crucial to understand that QCOW2 is a sparse format with two types of "empty" space:
- Unallocated blocks (never written to)
- Zero-filled blocks (previously used but now empty)
Traditional tools like qemu-img
often require RAW conversion because they operate at the block level. However, we can work within the QCOW2 format itself.
First, verify your system has:
sudo apt-get install qemu-utils libguestfs-tools
1. Preparing the Image
Mount the image and zero out free space:
sudo guestmount -a image.qcow2 -i /mnt
sudo dd if=/dev/zero of=/mnt/zero.fill bs=1M
sudo rm -f /mnt/zero.fill
sudo guestunmount /mnt
2. Reclaiming Space
Use virt-sparsify to optimize while staying in QCOW2:
virt-sparsify --in-place image.qcow2
For older systems without --in-place support:
virt-sparsify image.qcow2 --compress -o qcow2:image_shrunk.qcow2
For systems without libguestfs:
qemu-img convert -O qcow2 -c image.qcow2 image_shrunk.qcow2
Check the new size:
qemu-img info image_shrunk.qcow2
Compare before/after:
ls -lh *.qcow2
du -sh *.qcow2
- Permission errors: Use sudo or ensure proper libvirt permissions
- Filesystem corruption: Always backup before shrinking
- Unexpected growth: Some filesystems reserve space (ext4 has 5% by default)
For LVM-based images, additional steps are needed:
virt-sparsify --in-place --format qcow2 \
--machine-readable \
--compress \
--tmp /dev/shm \
image.qcow2
Remember that in-place operations still require some temporary space, just significantly less than full RAW conversion.
Many developers and sysadmins face storage constraints when working with QCOW2 disk images. The common suggestion of converting to RAW format for shrinking isn't always feasible due to disk space limitations. Here's how to shrink QCOW2 files directly.
Before attempting to shrink your image:
- Ensure the guest filesystem is already minimized
- Zero out free space inside the guest OS
- Have qemu-img 1.1 or later installed
The most effective approach uses qemu-img's convert functionality with special parameters:
qemu-img convert -O qcow2 -c source.qcow2 shrunk.qcow2
This command:
- Maintains QCOW2 format (-O qcow2)
- Compresses the data (-c)
- Creates a new optimized image
For better results, combine multiple optimizations:
qemu-img convert -O qcow2 -c \
-o cluster_size=64K,preallocation=metadata \
source.qcow2 optimized.qcow2
Always verify the integrity after shrinking:
qemu-img check shrunk.qcow2
virt-df -h -a shrunk.qcow2
When even temporary space is limited, use a sparse conversion:
qemu-img convert -O qcow2 -S 0 \
source.qcow2 sparse-shrunk.qcow2
- This process doesn't actually shrink the filesystem - just the image container
- Always keep backups before shrinking operations
- The process may take significant time for large images