How to fix “fstrim: Operation not supported” under KVM?

Maybe someone out there will have some ideas.

I have a KVM host running wheezy, with wheezy-backports versions of libvirt and qemu. I have defined a guest, properly set discard=unmap in the domain XML file for it, verified that’s being passed to the guest, but TRIM/DISCARD is just not working.

Mounting the ext4 filesystem with discard has no effect, and fstrim / always reports:

fstrim: /: FITRIM ioctl failed: Operation not supported

Every single time.

I’ve tried with the virtio, IDE, and SCSI (both default and virtio-scsi) backend drivers. The guest is also running wheezy (i386 version; the host is amd64) and I’ve tried the latest 3.12 backported kernel for it. No dice.

If I shut down the VM and mount the filesystem on the host, fstrim works fine.

Everything says this should work. But it doesn’t.

Any ideas?

9 thoughts on “How to fix “fstrim: Operation not supported” under KVM?

  1. I may have found part of the answer…

    virsh edit the domain. At the tag at the top, add xmlns:qemu=’http://libvirt.org/schemas/domain/qemu/1.0′

    Then, add lines like:

    qemu:commandline>
    qemu:arg value='-set'/>
    qemu:arg value='device.scsi0-0-0-0.discard_granularity=4096'/>
    qemu:arg value='-set'/>
    qemu:arg value='device.scsi0-0-1-0.discard_granularity=4096'/>
    /qemu:commandline>

    (adding appropriate characters to make it work)

  2. Umh, I think it will take a while before you can do this. TRIM is offered by specific hardware controllers, that is, by Flash-based media. What you see inside your KVM is an idealized block device, which can be backed on any kind of media — And to be able ot implement TRIM, KVM would have to be sure that the underlying structure also supports it :-(

    You could try doing a device pass-through, completely disregarding it from your host system… Although I really don’t know if there’s support for doing so.

  3. I’m running CentOS 6 and so far have been completely unsuccessful in getting KVM to pass the discard=’on’ option for the disk. I’ve upgraded qemu and libvirt to their latest versions from source, and even compiled a newer kernel (2.6.39.4). I’ll virsh edit the guest, insert discard=’on’ in the driver definition, save/quit, but the option doesn’t stick in the XML (I’ll virsh edit again and the edit I made is gone)

    Any ideas?

  4. Hey,
    1) under write something like:
    2) ensure that you are using a “TRIM friendly controller”. For instance: virtio-scsi (not the normal virtio) or ide.

    That should do it! Good luck :o)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Find out more about Webmentions.)