72

I know that weekly TRIM is enabled by default from 14.10 onwards. Source: How to enable TRIM? But running sudo nano /etc/cron.weekly/fstrim returns an empty file. Also tail -n1 /etc/cron.weekly/fstrim says that this file does not exist.

Running lsblk -D returns non zero values for DISC-GRAN and DISC-MAX so TRIM is supported on my SSD. Is weekly TRIM actually enabled for my SSD or not?

I am using a Kingston SSD

2 Answers 2

110

From 18.04 fstrim (discard unused blocks) is enabled to run weekly by default for all mounted filesystems on devices that support the discard operation.

fstrim is managed by systemctl, not cron, and is defined by a systemd service unit (fstrim.service) and timer unit (fstrim.timer)

View configuration of fstrim.service:

$ systemctl cat fstrim.service
# /lib/systemd/system/fstrim.service
[Unit]
Description=Discard unused blocks

[Service]
Type=oneshot
ExecStart=/sbin/fstrim -av

Note: ExecStart=/sbin/fstrim -av
Where:
-a, Trim all mounted filesystems on devices that support the discard operation.
-v, Verbose execution. Output the number of bytes passed from the filesystem down the block stack to the device for potential discard.

View configuration of fstrim.timer:

$ systemctl cat fstrim.timer
# /lib/systemd/system/fstrim.timer
[Unit]
Description=Discard unused blocks once a week
Documentation=man:fstrim

[Timer]
OnCalendar=weekly
AccuracySec=1h
Persistent=true

[Install]
WantedBy=timers.target

View status of fstrim.timer:

$ systemctl status fstrim.timer
● fstrim.timer - Discard unused blocks once a week
   Loaded: loaded (/lib/systemd/system/fstrim.timer; enabled; vendor preset: enabled)
   Active: active (waiting) since Thu 2018-07-05 05:45:11 BST; 4h 42min ago
  Trigger: Mon 2018-07-09 00:00:00 BST; 3 days left
     Docs: man:fstrim

Start/Stop/Restart fstrim.timer:
(does not change startup status)

$ sudo systemctl [start/stop/restart] fstrim.timer

Enable/Disable fstrim.timer:
(add to/remove from startup, does not change current active status)

$ sudo systemctl [enable/disable] fstrim.timer

View journal entries for fstrim.service:

$ journalctl -u fstrim.service
...
May 01 07:57:48 user-laptop systemd[1]: Starting Discard unused blocks...
May 01 07:57:51 user-laptop fstrim[810]: /: 94.5 GiB (101432025088 bytes) trimmed
May 01 07:57:51 user-laptop systemd[1]: Started Discard unused blocks.
-- Reboot --
May 07 05:29:03 user-laptop systemd[1]: Starting Discard unused blocks...
May 07 05:29:05 user-laptop fstrim[776]: /: 94.3 GiB (101260668928 bytes) trimmed
May 07 05:29:05 user-laptop systemd[1]: Started Discard unused blocks.
-- Reboot --
etc...

View journal entries for fstrim.timer:

$ journalctl -u fstrim.timer
...
May 01 07:57:48 user-laptop systemd[1]: Started Discard unused blocks once a week.
May 01 20:21:43 user-laptop systemd[1]: Stopped Discard unused blocks once a week.
-- Reboot --
May 02 06:21:59 user-laptop systemd[1]: Started Discard unused blocks once a week.
May 02 20:37:11 user-laptop systemd[1]: Stopped Discard unused blocks once a week.
-- Reboot --
etc...
6
  • 4
    For reference, the when is controlled by /lib/systemd/system/fstrim.timer (OnCalendar=weekly) and the what by /lib/systemd/system/fstrim.service (ExecStart=/sbin/fstrim -av). Jul 5, 2018 at 8:46
  • 2
    Fine post! I suggest you change the "cat" commands by the dedicated systemctl commands: cat /lib/systemd/system/fstrim.timer -> systemctl cat fstrim.timer. In general, that command shows the version of the service that is in effect. That might be the one under /lib/systemd, but it might also be a modified version under /etc/systemd.
    – vanadium
    Jul 14, 2018 at 6:15
  • When it is active, does it do it for all drivers? For example, I have ubuntu installed in one SSD but have another SSD mounted as an ext4 partition. Will it trim both automatically? (status says it is active)
    – Eduardo
    Apr 30, 2019 at 21:15
  • how to activate it on demand? (for example now, i want to go for a lunch)
    – razor
    Oct 11, 2019 at 9:02
  • 1
    You can manually start/stop/restart any systemd service with: $ sudo systemctl [start/stop/restart] [service name].service Oct 11, 2019 at 11:56
2

Edit: Please read the comments, this answer mixes up two mechanisms!

Old answer

there is an important point which needs to be added to the answer of @Broadsworde to make it complete.

While on my laptop all the timers and services were enabled, the fstrim log entry was missing (only: starting… stopping… reboot… starting… etc.).

Missing step

You might need to mark the file systems as discardable. If a file system is not marked as discardable, the trim will skip it[1].

To mark a filesystem as discardable, you have two options:

1. Option: tune2fs

sudo tune2fs -o discard /dev/mapper/ubuntu--vg-root

This will set the discard option as default for my ext4 device. If you don't use encryption, try /dev/sda instead.

2. Option: /etc/fstab

Make sure to prepend or append the option discard to your existing mount options. For example like this:

/dev/mapper/ubuntu--vg-root / ext4 discard,relatime,errors=remount-ro 0 1

Only after this the timer service will really do something.

Footnotes

  • [1] actually, the device needs to support the TRIM operation. But on linux, this is a file system flag. Still, the device the file system is running on needs to support the TRIM operation. To see if your device supports it, use: sudo hdparm -I /dev/sda | grep -i TRIM.
4
  • In my installation (originally 18.10; later upgraded to 19.04) the filesystems did not have the discard option enabled. I use only SSD in my system; and it never had any mechanical HD. I enabled them with tune2fs. Any idea or explanation of why the system did not enabled them by default?
    – FedKad
    May 1, 2019 at 9:19
  • 5
    I don't think this is correct. The discard option trims blocks when the files are deleted from the file system. The systemd service trims the unused blocks periodically on the file system. These are different things. The discard option can negatively impact performance on some systems, so the periodic one is arguably better.
    – xioxox
    May 31, 2019 at 10:58
  • 2
    @xioxox is right. Recent mount's man page states that «The discard function issues frequent commands to let the block device reclaim space freed by the filesystem. [...] may have a significant performance impact. (The fstrim command is also available to initiate batch trims from userspace.)»
    – Fabio A.
    Jul 19, 2019 at 12:58
  • Thanks, I edited the answer acoordingly. Please do not continue to downvote this answer: I want to keep it as your comments are valuable! Jul 21, 2019 at 11:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .