KVM SCSI unmap / discard

Hi,

I was trying to get this to work, but can’t.
Below is one of the templates I ended up with, rest set from GUI
The RAW bit was added by me following i.e. this:
https://chrisirwin.ca/posts/discard-with-kvm/

  CONTEXT = [
  NETWORK = "YES",
  SET_HOSTNAME = "$NAME",
  SSH_PUBLIC_KEY = "hellomeow" ]
  CPU = "1"
  DISK = [
    CACHE = "writeback",
    DEV_PREFIX = "sd",
    DISCARD = "unmap",
    IMAGE = "HardenedBSD 11.0",
    IMAGE_UNAME = "oneadmin" ]
  GRAPHICS = [
    LISTEN = "0.0.0.0",
    TYPE = "VNC" ]
  LOGO = "images/logos/hardenedbsd.png"
  MEMORY = "768"
  NIC = [
    NETWORK = "net",
    NETWORK_UNAME = "oneadmin" ]
  OS = [
    ARCH = "x86_64" ]
  RAW = [
    DATA = "<controller type='scsi' index='0' model='virtio-scsi'>
  <driver queues='2' />
  </controller>",
    TYPE = "kvm" ]

If I check from within the OS I get indicated there’s no support for trim/unmap/discard/bunnies.

Does one of you have discard tested working? Is the GUI setting supposed to really be enough?
From what I saw it won’t switch to virtio-scsi, which quite a few places tell us to be needed.

You need to add <devices> tag:

  RAW = [
    DATA = "<devices><controller type='scsi' index='0' model='virtio-scsi'>
  <driver queues='2' />
  </controller></devices>",
    TYPE = "kvm" ]

I’ve tested fstrim command and it works.

Thank you!

I’ll post back once fixed :slight_smile:

At the moment it’s still failing, but I suppose I’m closer now.

on the OS side, I don’t see the virtio disks in camcontrol, maybe this PV driver is partial.

What I can query is this:

root@zz:~ # sysctl -a | grep _trim
kstat.zfs.misc.zio_trim.failed: 0
kstat.zfs.misc.zio_trim.unsupported: 153
kstat.zfs.misc.zio_trim.success: 0
kstat.zfs.misc.zio_trim.bytes: 0

This might be a ZFS-side thing though as there is NO way to trigger a TRIM.

"Freed blocks are not TRIMed immediately, there is a low priority thread that TRIMs ranges when the time comes."

I’m gonna give it a day or two and see if the counters do go up.
Would appreciate if you let me know if i got the config in the storage tab right.

Hi Florian,

Here is the setup I am using:

You could use lsblk -D to check VM disk:

Hope this helps.

Best Regards,
Anton Todorov

This DID help!

root@zz:~ # sysctl -a |grep _trim
kstat.zfs.misc.zio_trim.failed: 0
kstat.zfs.misc.zio_trim.unsupported: 0
kstat.zfs.misc.zio_trim.success: 160
kstat.zfs.misc.zio_trim.bytes: 2560512

Consider this solved.
Hopefully that’ll will now enable me to flatten ZFS based qcow2 images.
that would make the FreeBSD/HardenedBSD images lose about
4GB of size.
and THAT would make the marketplace downloads faster once I submit them
and THAT would make me happy :slight_smile:

TY @atodorov_storpool

To repack the qcow2 images you can use qemu-img convert to qcow2, it skips non used blocks. And even better for marketplace is compressing the result qcow2:

$ qemu-img convert -c -p -O qcow2 fat.qcow2 slim.qcow2

In current CPUs decompressing those blocks is pretty fast.

Thank you for the exact command.
I think I had tried trimming it with something else (the one you gave a talk about) but it did not work due to ZFS not having freed the space yet. Even heavy compression did not help at all. That’s the fun part of CoW filesystems :slight_smile: