Libvirt TRIM/UNMAP

From MalinWiKi
Jump to navigation Jump to search

KVM/Libvirt supports UNMAP, similar to TRIM. That way you can free space used by a VM on a qemu2 vdisk not currently in use.

First of all, your disk images need to be in QCOW2 format. If they are raw, convert them first. To allow for deallocation of unused space, you'll need to use UNMAP, similar to TRIM, except that the latter is the ATA counterpart to SCSI's UNMAP. KVM/Libvirt doesn't support the ATA version, so we'll change the driver to SCSI. Usually drives are added as VirtIO. If they are, change this, using virt-manager. Obviously, you can do this on the commandline, but I'm lazy so I just use the GUI. On my box (Debian Jessie), I can't change VirtIO to VirtIO SCSI, so I'll have to remove and re-add the drives. If your version of KVM/Libvirt support changing this, skip steps 2 and 3 and just change it. Otherwise, do as follows

  1. Shut down the VM
  2. Start virt-manager and remove the disk(s) from the VM
  3. Add them in the same order, but as VirtIO SCSI

Now, you need to add a flag to the driver to make it support UNMAP. Changing the XML config file itself may not work too well, so in my experience, I find it easier to just dump the config, change it, undefine (that is, delete, not touching the data) the old VM and recreating one.

virsh dumpxml myvm > myvm.xml

Then change myvm.xml, adding the discard part

<driver name='qemu' type='qcow2' discard='unmap'/>

Undefine and redefine the VM

virsh undefine myvm
virsh define myvm.xml

Start the vm with virsh start myvm or with virt-manager and wait for it to start. When done, try du -sh /var/lib/libvirt/images/myvm.qcow2 and run fstrim / inside the VM and check the du output again. If there are a lot of wasted space, it should be freed by now.

roy