Since 15.04 Ubuntu uses systemd and its timer (man systemd.timer, Arch wiki)
systemctl list-timers
systemctl status fstrim.timer
Ubuntu 14.04
As of Ubuntu 14.04, scheduled TRIM is enabled by default for Intel, SAMSUNG, OCZ, Patriot and Sandisk SSDs. If you have another brand, you could disable the vendor check by running the following command:
sed -i 's/exec fstrim-all/exec fstrim-all --no-model-check/g' /etc/cron.weekly/fstrim
(or just edit the file /etc/cron.weekly/fstrim and add --no-model-check)
Ubuntu 13.10 and Earlier
There are three ways to perform TRIM, manual, scheduled, and automatic:
however it is not needed when scheduled or automatic TRIM are enabled, as detailed in the sections below.
Note: For Ubuntu 11.04 and earlier systems, fstrim is not available so you have to use wiper.sh supplied with hdparm in /usr/share/doc/hdparm/contrib/wiper.sh.gz
Paste the following code in the file, then save and close the file:
#! /bin/sh
# By default we assume only / is on an SSD.
# You can add more SSD mount points, separated by spaces.
# Make sure all mount points are within the quotes. For example:
# SSD_MOUNT_POINTS='/ /boot /home /media/my_other_ssd'
SSD_MOUNT_POINTS='/'
for mount_point in $SSD_MOUNT_POINTS
do
fstrim $mount_point
done
Note that the above assumes that only your root filesystem / is located on an SSD. If you have more mount points that reside on one or more SSDs, add them to SSD_MOUNT_POINTS as explained in the code.
Make the script executable:
sudo chmod +x /etc/cron.weekly/fstrim
And finally test it:
sudo /etc/cron.weekly/fstrim
If you see no errors, your cron job should be working fine.
Automatic TRIM (Deprecated, Slow)
Automatic TRIM has been supported since Ubuntu 10.10 (kernel 2.6.33) with the EXT4 file system. However, sending TRIM commands to the SSD in real-time - after every delete - has been recognized to make deletion much slower than usual on some drives. Therefore a weekly scheduled TRIM via a cron job (described above) is recomended.
To enable automatic TRIM on a drive or partition, they need to be mounted with the discard option in fstab. Firstly backup your fstab then open it for editing:
Close and save fstab, then reboot and automatic TRIM should now be working.
Testing automatic TRIM
To test if TRIM is working issue the following commands (source):
cd / # Replace with SSD file system
sudo dd if=/dev/urandom of=tempfile count=100 bs=512k oflag=direct
sudo hdparm --fibmap tempfile
From the output copy the number under begin_LBA and verify the device name of your SSD: System->Administration->Disk Utility e.g. sda, sdb, sdc ...
Run the following but replace [ADDRESS] (begin_LBA) and sdX (SSD device name) with the details obtained above.
sudo hdparm --read-sector [ADDRESS] /dev/sdX
the output should be a long string of characters for those sectors
sudo rm tempfile
sync
Repeat the hdparm command from above:
sudo hdparm --read-sector [ADDRESS] /dev/sdX
If you get only zeros then automatic TRIM is working. However if after removing the file the sectors are still not empty then wait a while and run the command again.
Note that if you are using encryption the solutions posted here so far won't help you, because you need to enable TRIM support in the encryption layer as well. This can be done with kernel versions 3.1+ and cryptsetup versions 1.4+, both of which are included in Ubuntu 12.04.
Find my guide on setting TRIM up for LUKS encrypted partitions here
How to Activate TRIM on LUKS Encrypted Partitions in Ubuntu & Debian
This step by step walkthrough will let you take advantage of the TRIM technology for your encrypted SSD partitions for cryptsetup 1.4 or higher and kernel 3.1 or higher. This leads to a hassle-free SSD experience because
"TRIM enables the SSD to handle garbage collection overhead, that
would otherwise significantly slow down future write operations to the
involved blocks, in advance."
Example Setup
Notebook with SSD as the single drive, Linux installed in single ext4 LVM root partition with LVM swap partition, both over LUKS encrypted logical partition.
Make a backup of all your data. You're messing with your file system
so having a backup is simply a good idea.
Make sure you have the required kernel and cryptsetup versions (3.1
& 1.4, e.g. in Ubuntu 12.04, beware though, at time of writing 12.04
is still beta).
Add discard parameter to the file system options of the encrypted
LVM volume(s) in your /etc/fstab file. This makes the file system of
your LVM partition aware that you want to use TRIM.
The last step is not enough though. As long as LUKS is not aware
that you want to use TRIM it will effectively block all TRIM
operations coming from the LVM partition's file system, for security
reasons. Add discard parameter to the cryptdevice options in
/etc/crypttab to make LUKS accept the discard behavior of the LVM
partition.
Ubuntu 14.10 onwards
In Ubuntu 14.10 and 15.04, TRIMming happens automatically every week on all SSDs supported by
fstrim
.Since 15.04 Ubuntu uses systemd and its timer (
man systemd.timer
, Arch wiki)Ubuntu 14.04
As of Ubuntu 14.04, scheduled TRIM is enabled by default for Intel, SAMSUNG, OCZ, Patriot and Sandisk SSDs. If you have another brand, you could disable the vendor check by running the following command:
(or just edit the file
/etc/cron.weekly/fstrim
and add--no-model-check
)Ubuntu 13.10 and Earlier
There are three ways to perform TRIM, manual, scheduled, and automatic:
Manual TRIM
In Ubuntu this can be performed with
fstrim
:however it is not needed when scheduled or automatic TRIM are enabled, as detailed in the sections below.
Note: For Ubuntu 11.04 and earlier systems, fstrim is not available so you have to use
wiper.sh
supplied withhdparm
in/usr/share/doc/hdparm/contrib/wiper.sh.gz
Scheduled TRIM (Recommended)
This is the currently recomended method, and is planed to be activated per default for Ubuntu 14.04. Here's how to activate it manually in older versions of ubuntu (11.10 to 13.10):
Create a weekly CRON job script file:
Paste the following code in the file, then save and close the file:
Note that the above assumes that only your root filesystem
/
is located on an SSD. If you have more mount points that reside on one or more SSDs, add them toSSD_MOUNT_POINTS
as explained in the code.Make the script executable:
And finally test it:
If you see no errors, your cron job should be working fine.
Automatic TRIM (Deprecated, Slow)
Automatic TRIM has been supported since Ubuntu 10.10 (kernel 2.6.33) with the EXT4 file system. However, sending TRIM commands to the SSD in real-time - after every delete - has been recognized to make deletion much slower than usual on some drives. Therefore a weekly scheduled TRIM via a cron job (described above) is recomended.
To enable automatic TRIM on a drive or partition, they need to be mounted with the
discard
option infstab
. Firstly backup your fstab then open it for editing:Add
discard
to the fstab options entry (comma separated) for the SSD drive or each partition.Close and save fstab, then reboot and automatic TRIM should now be working.
Testing automatic TRIM
To test if TRIM is working issue the following commands (source):
From the output copy the number under
begin_LBA
and verify the device name of your SSD:System->Administration->Disk Utility
e.g. sda, sdb, sdc ...Run the following but replace
[ADDRESS]
(begin_LBA) andsdX
(SSD device name) with the details obtained above.the output should be a long string of characters for those sectors
Repeat the
hdparm
command from above:If you get only zeros then automatic TRIM is working. However if after removing the file the sectors are still not empty then wait a while and run the command again.
Note that if you are using encryption the solutions posted here so far won't help you, because you need to enable TRIM support in the encryption layer as well. This can be done with kernel versions 3.1+ and cryptsetup versions 1.4+, both of which are included in Ubuntu 12.04.
Find my guide on setting TRIM up for LUKS encrypted partitions here
How to Activate TRIM on LUKS Encrypted Partitions in Ubuntu & Debian
This step by step walkthrough will let you take advantage of the TRIM technology for your encrypted SSD partitions for cryptsetup 1.4 or higher and kernel 3.1 or higher. This leads to a hassle-free SSD experience because
Example Setup
Notebook with SSD as the single drive, Linux installed in single ext4 LVM root partition with LVM swap partition, both over LUKS encrypted logical partition.
SSD: /dev/sda
HowTo
Add discard parameter to the file system options of the encrypted LVM volume(s) in your /etc/fstab file. This makes the file system of your LVM partition aware that you want to use TRIM.
The last step is not enough though. As long as LUKS is not aware that you want to use TRIM it will effectively block all TRIM operations coming from the LVM partition's file system, for security reasons. Add discard parameter to the cryptdevice options in /etc/crypttab to make LUKS accept the discard behavior of the LVM partition.
Rebuild your initramfs. The crypttab options are stored there and used on boot.
Reboot.
Check if TRIM is now active.
If the last command shows a result like this (1 allow_discards at the end) you're all set.
Result
TRIM is activated. Enjoy your hassle-free SSD!