User Tools

Site Tools


linux:system:disk:fstrim

Enable SSD Trim automatically

Continues TRIM

The real reason Ubuntu doesn’t TRIM SSDs by default is because the Linux kernel’s implementation of TRIM is slow and results in poor performance in normal use. Windows sends a TRIM command each time it deletes a file, telling the drive to immediately delete the bits of the file. Linux supports this when file systems are mounted with the “discard” option. However, Debian, Ubuntu — and other distributions — don’t do this by default for performance reasons. Continuous TRIM is not the most preferred way to issue TRIM commands in Linux. Continuous TRIM is enabled by the discard option for a mount in /etc/fstab: enables continuous TRIM in device operations:

/dev/sda1  /           ext4  defaults,discard   0  1

Periodic trim

Copy the available templates to the systemctl directory and enable and start them.

# cp /usr/lib/systemd/system/fstrim.{service,timer} /etc/systemd/system
# systemctl enable fstrim.timer
# systemctl start fstrim.timer

In case these templates are not available, see the contents below:

# cat fstrim.service

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

[Service]
Type=oneshot
ExecStart=/sbin/fstrim -Av
# cat fstrim.timer

[Unit]
Description=Discard unused blocks once a week
Documentation=man:fstrim

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

[Install]
WantedBy=timers.target

Check the status of the .timer with:

# systemctl list-timers

The output is quite useful:

EXT                         LEFT          LAST                         PASSED       UNIT                         ACTIVATES
Sun 2017-12-10 00:11:06 CET  6h left       Sat 2017-12-09 14:48:34 CET  2h 56min ago apt-daily.timer              apt-daily.service
Sun 2017-12-10 06:11:27 CET  12h left      Sat 2017-12-09 06:47:46 CET  10h ago      apt-daily-upgrade.timer      apt-daily-upgrade.servi
Sun 2017-12-10 16:28:34 CET  22h left      Sat 2017-12-09 16:28:34 CET  1h 16min ago systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.
Mon 2017-12-11 00:00:00 CET  1 day 6h left n/a                          n/a          fstrim.timer                 fstrim.service
linux/system/disk/fstrim.txt · Last modified: by 127.0.0.1