LXC disk trimming (discard)

adhoore

New Member
Nov 18, 2024
2
0
1
I have a few Proxmox clusters, all running a mix of LXCs and VMs. Most of them have their disks on Ceph, but some are on ZFS or LVM-thin. Almost all of the containers/VMs are running debian 12/13.

I know how disk trimming works when it comes to VMs:
- I enable the "discard" option of the virtual disk
- Inside the debian VM, the service "fstrim.timer" runs once a week and trims the disk
- I can see the fstrim service run each week in the logs, it does the thing

Now my question: how does this work for LXC containers? How can I verify that it's working? I can find very little documentation, nor forum posts. Most info is about VMs and not LXC containers.

Old post i found: https://forum.proxmox.com/threads/is-it-possible-to-auto-trim-for-lxc-disks.57195/

I know that the fstrim.timer is disabled inside the LXC, because the container doesn't have enough rights to run fstrim on the file system. So we need to do it from the host and Proxmox provides a command for this: pct fstrim <id>

BUT I'm just wondering whether Proxmox runs this automatically somehow? Like the weekly fstrim.timer for VMs. I know the Proxmox host also has an fstrim.timer, but I don't see that trimming any LXC containers in the logs.

If Proxmox doesn't run this weekly, what is the recommended way of doing it? Cron job? Systemd timer?

Personally, I feel like this should be a Proxmox feature. VMs have it... so why not LXC containers?

Kind regards,
Alexander
 
Trimming works not from inside of LXC but from host and it is not automatic. You must do it manualy. There is dedicated command for it: pct fstrim <vmid>. I have custom systemd timer and custom service which trim my lxc every week. Something like this: ExecStart=/usr/bin/bash -c "for i in $(`which pct` list | awk '/^[0-9]/ {print $1}'); do `which pct` fstrim $i 2>&1; done"
 
  • Like
Reactions: adhoore
You can enable the "discard" mount option on a container disk. This will automatically trim the disk when deleting files in the container. Just try it by creating a new container with the mount option "discard" set on the root disk. Create a big file in the container and see the storage usage increase. Delete the file in the container and the storage usage should decrease.
 
  • Like
Reactions: adhoore
So the "discard" mount option (under resources > disk > advanced) is what I'm looking for? Just enable that on all my LXC disks and they will automatically be trimmed? But only when I delete files? But I guess debian will delete files frequently enough (logs and such) that I can just rely on this happening from time to time. It does sound like this might add a lot of overhead, doing this on each file delete. I will probably not go this route, just for performance concerns.

Google AI tells me this (which justifies my fears):

> To mount a Debian filesystem with discard support, you can add the discard option to the mount command or the /etc/fstab file. A more modern and often recommended approach is to use the fstrim command to trim unused blocks, which can be scheduled weekly via a timer. Adding discard to /etc/fstab may negatively affect the lifetime of some SSDs and is often unnecessary if you schedule fstrim.