How it works:
guest: rm a big file the guest file system marks the blocks free
guest: fstrim the guest sends TRIM to the disk: "these blocks aren't needed"
Proxmox: discard=on on the disk QEMU passes TRIM on to the storage
storage: LVM-thin, ZFS, qcow2 the blocks go back to the pool
Without discard=on the second line goes nowhere: pool usage only grows until it equals the size of the disk
given to the guest. On a thin pool, where guests are given more than there is space, that’s the road to a full
pool (Proxmox storage).
Enable it on every disk of the VM: Hardware › disk › Edit › Discard, or
qm set 101 --scsi0 local-lvm:vm-101-disk-0,discard=on,size=30G
Take the line from qm config 101 and append discard=on without dropping anything. The cloud-init template
has it from the start (VM template with cloud-init). The SSD emulation checkbox next to it is about something else:
the guest sees the disk as an SSD rather than a spinning drive and won’t, for example, defragment it.
Inside the guest, TRIM has to be run. On Ubuntu fstrim.timer does it once a week and is enabled by default;
on other systems check systemctl status fstrim.timer. Mounting with the discard option isn’t needed: TRIM on
every delete loads the disk more than one batch a week.
For LXC containers the host runs TRIM for them: pct fstrim 201 — for all of the container’s volumes.
If it doesn’t work
Discard is on, fstrim -av ran, but Data% in lvs didn’t go down
- Cause
- The option was enabled on a running VM: it’s
[PENDING]and takes effect after the VM’s process restarts. Or the volume has snapshots — they hold on to the old blocks. - Fix
qm reboot 101, thenfstrim -avin the guest. Delete snapshots you no longer need.
How to check
lvs pve/data # Data% before
qm guest exec 101 -- fstrim -av # or sudo fstrim -av in the VM itself: /: 18.2 GiB trimmed
lvs pve/data # Data% after — lower