Reclaiming space on qcow2 disk image

Update 2018-11-11:


Recommended method is to use fstrim. See:

"The recommended version is to pass TRIM commands (known from SSDs) from the VM to the backing storage. This has the advantage that it works automatically, does not need to write the whole free parts of all disks to zero and must only be setup once."

The alternative manual method outlined below, is slow and painful - but seemingly necessary for the occasional apparent failure of fstrim, as experienced first-hand.

Convert the storage 'driver' from virtio-blck to virtio-scsi

Use VMM (Virtual Machine Manager) in a vncserver session on the hypervisor, over the VPN.

NB: Shut the VM down, first!

Remove the current virtio-blk disk image:

And add it back as virtio-scsi:


Then, to enable TRIM in the guest, first edit the VM's XML configuration file on the hypervisor:

virsh edit MACHINE_NAME

And add discard='unmap' to the driver.

    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2' discard='unmap'/>
      <source file='/var/lib/libvirt/images/new-ff.qcow2'/>
      <target dev='sda' bus='scsi'/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>


Start the VM and TRIM

virsh start new-ff

Then, SSH to the VM, and as root, run the following command:

fstrim -v /

Add to root's crontab to perform a trim weekly.

crontab -e

# Trim filesystem daily to recover space
0 2 * * 1      /sbin/fstrim -v / >> /home/jeff/fstrim.log

Alternative method:

Shutdown and backup the VM's qcow2 disk image.

Mount the qcow2 image using this guide.

  1. Zero-fill the drive (dd if=/dev/zero of=/some/file until you run out of space)
  2. delete /some/file

qemu-img convert -O qcow2 original_image.qcow2 deduplicated_image.qcow2


In a terminal on the VM, run the dd if=zero commands until you run out of disk space. Before running this, be sure to stop any applications running on the guest otherwise errors may result.