By Default, Ubuntu (20.04) runs fstrim on /etc/fstab entries; NOT all mounted devices.

I’ve been Chia mining and one of the staples of Chia mining, to keep your SSDs from imploding, is to constantly trim the drives.

Fortunately in Ubuntu, this is handled for you already by the systemd service manager and the fstrim service – but out of the box it runs once a week. We want it to run hourly.

The way systemd works is it maintains 2 files per each configured task/service it is scheduled to run – a .service file that describes how to execute the service and a .timer file that describes how often it should run.

First, let’s edit the /usr/lib/systemd/system/fstrim.timer file:

[Unit]
Description=Discard unused blocks hourly
Documentation=man:fstrim
ConditionVirtualization=!container

[Timer]
OnCalendar=hourly
AccuracySec=30s
Persistent=true

[Install]
WantedBy=timers.target

The values I changed are on lines 2, 7 & 8 and are as follows:

2: Changed text description from 'weekly' to 'hourly'.
7: Changed "weekly" to "hourly"
8: Changed accuracy from "1h" to "30s"

TIP: The units of time described in the fstrim.timer file are all defined by systemd.time. You can check that man page for references on other formats you can use.

Now save and exit that file.

Next, open the /usr/lib/systemd/system/fstrim.service file for editing:

[Unit]
Description=Discard unused blocks on filesystems from /etc/fstab
Documentation=man:fstrim(8)
ConditionVirtualization=!container

[Service]
Type=oneshot
ExecStart=/sbin/fstrim -av
ProtectSystem=strict
ProtectHome=yes
PrivateDevices=no
PrivateNetwork=yes
PrivateUsers=no
ProtectKernelTunables=yes
ProtectKernelModules=yes
ProtectControlGroups=yes
MemoryDenyWriteExecute=yes
SystemCallFilter=@default @file-system @basic-io @system-service

The only thing we are going to do here are replace all the default arguments to fstrim on line 8, to simply -av and save the file.

IMPORTANT: By default Ubuntu ships the fstrim service configured to use the --fstab --verbose --quiet arguments – BUT this also means any drives dynamically loaded after /etc/fstab are read won’t be included in the trim operations. This was exactly what was happening on my Plotting machine; only my boot drive was getting trimmed, but the plotting drives were slowly filling up with garbage.

After you’ve made those changes, save the file.

Now we need to ask systemctl to reload all it’s configuration files AND restart the fstrim.timer to pickup the changes with these 2 commands:

sudo systemctl daemon-reload
sudo systemctl restart fstrim.timer

You are all set! Any SSD mounted to your machine will be trimming hourly now!

4 thoughts on “By Default, Ubuntu (20.04) runs fstrim on /etc/fstab entries; NOT all mounted devices.”

  1. Hello,
    I appreciate your write up and explanation. I am a beginner at Ubuntu and I am having trouble figuring out how to edit the config fstrim.timer. If I try in the GUI it says read only file and I only have the option to “save as”. I have tried in Terminal using Sudo edit fstrim.timer and this opens a blank window file edit (not sure the name of the actual window it opens) I have tried with Sudo Nano too and I still can’t figure it out. I have been able to run fstrim -av which helped, but I would like to automate it like you suggest. Can you please assist?

    Reply
    • Skeg, this can all be a bit overwhelming if you are new to Linux. Just to set some high level goal posts, in Linux (and Unix), the `root` user is a special user that has access to EVERYTHING. It’s generally seen as VERY dangerous to log into any Linux machine as `root` and just start running commands because you can wipe the whole system out very easily… I’ve actually done this before on a production server… the Windows equivalent would be similar to selected the “C:” drive and then deleting it 🙂

      SO when you setup Linux, it always has you create a user account that it gives limited permissions to. Things are much safer that way, BUT, if you want to edit system-level files like `fstrim.timer`, then you need to use the `sudo` command when running command line operations to tell the operating system “Ok, run this command, but don’t run it as `Skeg`, run it as `root` – because otherwise it won’t work.”

      `sudo` literally means “super-user do” or “do this command as the super-user” – which is `root`.

      `nano` is just a text-based editors that is found on most all Linux distributions in the last 10 years… back in the old days it was either `vi` or `emacs`, but those were pretty complex (and powerful) editors – they are still used quite a bit, but most distributions of Linux started to ship `nano` as a simple, easy to understand editor as default – and then you can just install whatever more complex one you want later.

      So when you see a command `sudo nano /directory/somefile.txt` that just means “open nano as root and then open the file at location /directory/somefile.txt”

      The last piece that might be a bit confusing is where you TYPE all these commands… you type them into the `Terminal` application – literally run the `Terminal` application from the Ubuntu apps list, it will open a window and show you a command line – this is the most powerful aspect of Linux (and Mac actually) – the command line is where most people try and do most of their work in the Linux world BUT it’s not very approachable for someone coming from Windows.

      The equivalent in windows is the `cmd` app to popup a command terminal – in the old days of Windows, this was the `DOS` environment. These were all attempts to copy the strength of the Linux/Unix command line environment. More recently in Windows 10, they have the “Powershell” environment.

      So using GUI programs like ‘Text Editor’ is much easier, BUT they will get run as your normal user and won’t have permission to access the fstrim.timer file – that’s why all the instructions in this post are meant for the command line. The editing is relatively simple and it’s just faster to do it from there.

      Last tip – when you say “I tried in Terminal using “sudo edit fstrim.timer” – if you literally typed only that and not `sudo nano /usr/lib/systemd/system/fstrim.timer` – then the reason you got a blank file is because it’s just creating a NEW file for you, with the name `fstrim.timer` in whatever directory you were in when you ran the command (you can check the current dir with the `pwd` command) and NOT opening the system file that is in the `/usr/lib/systemd/system/` directory as directed in the blog post.

      In Linux the directories all start at the root, `/`, and use the front slash to denote sub directories from there… so sort of like C:\usr\lib\systemd\system if you were trying to map this to the Windows world.

      Hope that helps!

      Reply
  2. Hi Riyad,

    thanks for your tutorial! But I have a question, do I need to specify something in the mount of the drive to activate trimming?

    Thanks again.

    Reply

Leave a Comment