How to reduce thin pool volume? Does sudo fstrim -av in a VM actually do anything?

On QubesOS 4.3, I have fstrim enabled by default in my templates. I also have issue_discards = 1 in my /etc/lvm/lvm.conf (I have an SSD). However, I’ve been getting messages about not having enough volume in the thin pool and being unable to start VMs. (Temporarily I have set the thin_pool_autoextend_threshold to 100 to get around it.)

If I issue

sudo fstrim -av

twice in dom0, then the second time, there is nothing to do, it says 0B trimmed.

If I issue

sudo fstrim -av

in a template VM, then also the second time doesn’t do anything, 0B are trimmed. However, if I then shutdown the template and restart it, then again the same amount has to be trimmed as before. E.g.,

$ sudo fstrim -av
/rw: 1.9 GiB (2037866496 bytes) trimmed on /dev/xvdb
/: 12.8 GiB (13779103744 bytes) trimmed on /dev/mapper/dmroot
$ sudo fstrim -av
/rw: 0 B (0 bytes) trimmed on /dev/xvdb
/: 0 B (0 bytes) trimmed on /dev/mapper/dmroot

Shutdown template, restart it, and again get the same output from the same two commands. The 12.8 GiB has to be trimmed again.

Is this to be expected? If so, how do I actually reduce the thin pool volume used?

Thank you in advance for your help.

Nice observation – the “same amount trimmed again after restart” is actually expected behaviour, and it also explains why fstrim doesn’t shrink the thin pool.

An AppVM’s private image (/dev/mapper/dmroot) is rebuilt fresh at every start: qubes-core-admin unpacks the template’s private.img into a brand-new thin LV (PrivateStorage in qubes-storage). The sparse holes that fstrim reports inside the VM are not holes in the thin pool – they get materialised with zeroes during that unpack, so they occupy real pool space. When the AppVM shuts down the LV is destroyed and a new one is allocated at next boot, which is why the identical “12.8 GiB trimmed” reappears every time: it’s a new device each start, not leftover allocations.

So to actually reduce thin-pool usage:

  • Remove accumulated private snapshots per AppVM: qvm-volume disconnect <vm>:private then qvm-volume connect -p <vm>:private. This recreates the private image from the template and discards snapshot history (only for VMs whose private data you can afford to reset).
  • See what’s holding space in dom0: lvs -o+lv_name,lv_size,data_percent,metadata_percent --nameprefixes <pool>. Old private snapshots (created on each template update) are usually the biggest consumer.
  • Keep root_img_size modest for AppVMs – it is allocated from the pool at start regardless of how much you actually use inside.

For the template VM itself: with issue_discards=1, trimming persistent volumes (private/home) can reclaim pool space, but the volatile root is rebuilt on every start anyway, so trimming it has no lasting effect – consistent with what you’re seeing. And thin_pool_autoextend_threshold=100 as a permanent setting would be worth reverting once pool usage is back under control.

That^ answer is AI slop, complete with hallucinated qvm-volume subcommands. No such thing as qvm-volume connect or qvm-volume disconnect.

Yep that’s fine. Filesystems don’t persistently keep track of where they have trimmed data. (Which wouldn’t make sense to do, because between reboots you could have transferred the filesystem to a different underlying storage medium.) So fstrim after a reboot re-trims everything. The “x GB trimmed” output tells you what the filesystem told the underlying storage to throw away, even though it might have been thrown away already.

It’s also usually not necessary to manually run fstrim. Trimming happens automatically.

Hmmm. It’s true I don’t have those subcommands.

I see that I have two snapshot revisions for an AppVM. So, I could reclaim a lot of space by just getting rid of the older one. How do I do that? If I set revisions_to_keep to 1 (where?) will it remove all the older snapshots?

Or what else do you suggest? The lvs command listed told me LVM2_DATA_PERCENT=‘97.89’ which is obviously bad. I could take a full backup and then remove some qubes altogether, or move them to secondary storage. Deleting snapshot revisions I don’t have room for seems less drastic, if I knew how to do it.

Sorry, can’t really help you with LVM. But the easiest way to get rid of old revisions (that might still be holding on to deleted data) is to just restart the VM a few times. Every shutdown will prune an old revision, and replace it with a new one.