Does VirtIO storage support discard (fstrim)

fstrimlibvirtvirtio

$ uname -r
5.0.9-301.fc30.x86_64
$ findmnt /
TARGET SOURCE    FSTYPE OPTIONS
/      /dev/vda3 ext4   rw,relatime,seclabel
$ sudo fstrim -v /
fstrim: /: the discard operation is not supported

Same VM, but after switching the disk from VirtIO to SATA:

$ findmnt /
TARGET SOURCE    FSTYPE OPTIONS
/      /dev/sda3 ext4   rw,relatime,seclabel
$ sudo fstrim -v /
/: 5.3 GiB (5699264512 bytes) trimmed

The virtual disk is backed by a QCOW2 file. I am using virt-manager / libvirt. libvirt-daemon is version 4.7.0-2.fc29.x86_64. My host is currently running a vanilla kernel build 5.1 (ish), so it's a little bit "customized" at the moment, but I built it starting from a stock Fedora kernel configuration.

Is there a way to enable discard support on VirtIO somehow? Or does the code just not support it yet? I don't necessarily require the exact instructions how to enable it, but I am surprised and curious and I would like a solid answer :-).

Best Answer

Apparently discard wasn't supported on that setting. However it can work if you change the disk from "VirtIO" to "SCSI", and change the SCSI controller to "VirtIO". I found a walkthrough. There are several walkthroughs; that was just the first search result. This new option is called virtio-scsi. The other, older system is called virtio-block or virtio-blk.

I also found a great thread on the Ubuntu bug tracker. It points out that virtio-blk starts supporting discard requests in Linux 5.0. It says this also requires support in QEMU, which was committed on 22 Feb 2019. Therefore in future versions, I think we will automatically get both VirtIO and discard support.


Currently my virt-manager does not create virtio-scsi disks by default, even when it knows I am installing Fedora 29; it only creates the basic "VirtIO" disks. I do not know if there is any disadvantage of switching to virtio-scsi.

I guess virtio-scsi provides the same sort of performance advantage as virtio-blk, when compared to emulated SATA. (I do not see an option to use NVME protocol anywhere in virt-manager :-P, with or without VirtIO).

The oVirt website has some nice propaganda, which mentions

  1. some limitations in virtio-blk
  2. virtio-scsi can be used in pass-through mode to a SCSI LUN, and can use various new SCSI command features without needing modifications in virtio-scsi . If you are not specifically using SCSI pass-through, then any new commands will require new support in QEMU, but not in the virtio-scsi code.
  3. virtio-scsi includes support for multiple queues. (I am not clear whether this is also helpful for efficiency on single-queue hardware, or not).
Related Question