38

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

59

Trim (fstrim/discard) is enabled to run weekly by default in 18.04.
It is set to run for all mounted filesystems on devices that support the discard operation.

It is a systemd service managed via systemctl, not CRON.

View fstrim.timer status:

$ 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 fstrim.timer configuration:

$ 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 fstrim.service configuration:

$ 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 related systemd journal entries:

$ journalctl -u fstrim.timer
Jul 04 14:18:41 user-laptop systemd[1]: Started Discard unused blocks once a week.
Jul 04 21:59:26 user-laptop systemd[1]: Stopped Discard unused blocks once a week.
etc...

$ journalctl -u fstrim.service
Jun 25 10:59:44 user-laptop systemd[1]: Starting Discard unused blocks...
Jun 25 10:59:48 user-laptop fstrim[955]: /: 92.5 GiB (99335237632 bytes) trimmed
Jun 25 10:59:48 user-laptop systemd[1]: Started Discard unused blocks.
-- Reboot --
Jul 02 04:27:41 user-laptop systemd[1]: Starting Discard unused blocks...
Jul 02 04:27:46 user-laptop fstrim[1032]: /: 92.3 GiB (99150807040 bytes) trimmed
Jul 02 04:27:46 user-laptop systemd[1]: Started Discard unused blocks.
etc...
  • 2
    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). – Moilleadóir Jul 5 '18 at 8:46
  • 1
    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 '18 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 at 21:15
  • how to activate it on demand? (for example now, i want to go for a lunch) – razor Oct 11 at 9:02
  • You can manually start/stop/restart any systemd service with: $ sudo systemctl [start/stop/restart] [service name].service – Broadsworde Oct 11 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.
  • 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? – FedonKadifeli May 1 at 9:19
  • 1
    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 at 10:58
  • 1
    @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 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! – Ben Jul 21 at 11:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.