Is TRIM enabled on my Ubuntu 18.04 installation?

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

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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

Solution 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.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply