Ubuntu 14.10 onwards
In Ubuntu 14.10 and 15.04, TRIMming happens automatically every week on all SSDs supported by fstrim
.
$ tail -n1 /etc/cron.weekly/fstrim
/sbin/fstrim --all || true
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:
Manual TRIM
In Ubuntu this can be performed with fstrim
:
sudo 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 with hdparm
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:
gksudo gedit /etc/cron.weekly/fstrim
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:
sudo cp /etc/fstab ~/fstab-backup
gksudo gedit /etc/fstab
Add discard
to the fstab options entry (comma separated) for the SSD drive or each partition.
UUID=00000000-0000-0000-0000-000000000000 / ext4 discard,errors=remount-ro 0 1
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.
Best Answer
I see that this is an old question ... but it comes up early in searches on the topic, so it seemed to be worth giving an answer from the kernel sources.
There are two different ways a Linux filesystem implementation can "support" TRIM:
It can support the
-o discard
mount option, so blocks that become free are trimmed immediatelyIt can support the
FITRIM
ioctl, which is what the userlandfstrim
command uses to trim all currently-free blocks in bulk when requestedA given filesystem implementation can support one, or the other, or both (or none).
As it turns out, the FAT implementation has been able to do
discard
since kernel 2.6.28. That even predates the mount option, which didn't appear until 2.6.33. From 2.6.28 through 2.6.32, FAT just was written todiscard
unconditionally. Since 2.6.33, you can control whether it does or not.As for the
FITRIM
ioctl, well, that became a thing in 2.6.37, but I do not see any sign that the FAT filesystem code supports it, even in the latest, greatest (as of today) 4.13rc5. Several other filesystems have implemented it, but no one seems to have bothered for FAT.That's sort of too bad, because there's at least one perfectly common scenario where you would want a way to TRIM all the existing free blocks at once: you've given your ancient Banana 6000 a nice upgrade by
dd
-ing the full disk image from the old spinning drive to a shiny new SSD, so now, as far as the SSD is concerned, all of those blocks have been written, whether they matter to the filesystem or not. So, of course, you'd like the ones considered free by the filesystem to be trimmed.But there may be hope. While I haven't had a chance to try it yet, I think this might work:
-o discard
).fallocate -n
(which allocates the blocks without writing anything to them, so will not put much extra wear on the SSD). ForFAT32
a file is limited to just under 4 GiB, so that's why you may need a bunch of temporary files to use up all the free space. (For this step to work, you need kernel 4.5 or later, where FAT hasfallocate
support.)discard
ed.This, of course, ought to work as a brute-force
fstrim
alternative for any filesystem implementation that is able to dodiscard
but notFITRIM
.p.s. I do like the alternate links offered below in Tom Yan's comment. I was working from the more-or-less official git repo, but what I like about the service Tom linked to is that you can more easily switch between different versions of a file to compare.