[TUTORIAL] How to Make Fstrim Run More Often in LXC Containers and Linux VMs + the Host (for GRUB and Systemd Proxmox VE Installations)

Minionguyjpro

New Member
Oct 24, 2023
17
1
3
This is a tutorial on how to make fstrim run more often or on days that you wish. This could be handy if you have stuff continuously writing to the drives which then later gets deleted, which would cause it to create a large amount of unused data. For example, I have a Minecraft server running in Docker in an LXC container. It creates backups every 15 minutes and generates a lot of unused data at the end. Fstrim didn't run and left me with fully used space which interrupted my Minecraft server by locking it at read only. This was a pain and by doing this I was finally able to get back up without this issue!

Let's get started.

Step 1: Editing the Systemd Service Files of fstrim.timer (and fstrim.service if Container)​

The first step would be to enter the shell on your guest or host and log into the machine using root (or add a sudo prefix before each command here if you'd like to). Run the following command to edit the fstrim.timer file:
Bash:
systemctl edit --full fstrim.timer

Here, first check if it doesn't contain ConditionVirtualization=!<THISCOULDBEANYTHINGLIKECONTAINERKVMORNO> when doing this for a container or host. If it does, you should comment it out, e.g. like this for a container:
Code:
#ConditionVirtualization=!container

After that, you can modify the OnCalendar option to set the schedule as you wish. For example, we change it from weekly to run on Tuesday, Friday and Sunday (so thrice a week, you may change the description accordingly as well or generalize it):
Code:
OnCalendar=Tue,Fri,Sun

You're probably editing using nano, so use Ctrl+O and then press Enter to save the edited file. Press Ctrl+X to exit. If you are somehow using another text editor then you'd have to make sure to save it the proper way and exit there.

Step 2: Reloading and Properly Loading the New Changes​

The second step would be to do the final steps to reload things and check whether it runs the proper way according to the schedule. Still from the shell, we run the following command to reload systemd's daemon to make sure the changes are properly detected by systemd:
Bash:
systemctl daemon-reload

Then, we restart the fstrim timer using:
Bash:
systemctl restart fstrim.timer

As final step, we'd check if the schedule was properly set by checking the timer's status:
Bash:
systemctl status fstrim.timer

Check here if it runs successfully at all and importantly also check the Trigger part. This will tell you when it will trigger the fstrim service for the next time. This should be according to your set up schedule. If it's not set properly or incorrectly put, then you should check for more information on it or ask me here!

If this was done for a host only then you're done, congratulations!

Step 3 (Only for Containers and VMs): Making the fstrim Service Run​

The final step for containers and VMs would be to allow the fstrim service to run. By default or in some cases, due to a condition it seems to be disabled (same one as above). So just like above, you should edit the fstrim file but this time not the timer but the actual fstrim service:
Bash:
systemctl edit --full fstrim.service

We'd then as before find something like ConditionVirtualization=!<THISCOULDBEANYTHINGLIKECONTAINERKVMORNO> and then comment it out, e.g. for Proxmox VE VMs which use the KVM hypervisor:
Code:
#ConditionVirtualization=!kvm

Then we do the reloading of the systemd daemon once again:
Bash:
systemctl daemon-reload

We restart the fstrim service:
Bash:
systemctl restart fstrim.service

And finally, we check the status of the fstrim service to see if it runs properly:
Bash:
systemctl status fstrim.service

If it now runs without any errors, then you're good to go and you're done!



I hope y'all enjoyed this little guide and it helps you further to customize how often fstrim runs and allowing it to run in containers and VMs automatically too.
 
  • Like
Reactions: Veidit

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!