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.
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
[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!