Ubuntu / Linux news and application reviews.

kingston ssd 120gb v200

TRIM allows the OS to "inform a solid-state drive (SSD) which blocks of data are no longer considered in use and can be wiped internally". Without using TRIM, the SSD speed decreases after a while so if you have a solid-state drive that supports TRIM, you should enable it so your SSD remains fast over time.

This isn't easy to benchmark because the performance decreases over time so you'd neet to check the SSD speed constantly for a few months to see exactly how the SSD is affected when TRIM is not enabled. But if your SSD read / write speed decreases a lot over time and you haven't enabled TRIM, this may be way.

Some articles mention using online discard - enabling TRIM by adding the "discard" option to /etc/fstab -, but there are many who say this isn't a good idea for most solid-state drives and you'll get a pretty significant performance hit when trying to delete a large number of small files. So below I'll let you know how to use both of these methods: online discard (not recommended) and the recommended way: using fstrim and a cron (anacron) job.

Note: the instructions below have been tested in Ubuntu, but they may (most probably) work with other Linux distributions as well.

Before enabling TRIM, you must make sure:
  • you're using the Linux Kernel 2.6.33 or newer
  • your SSD supports TRIM
  • the partition(s) are EXT4 or BTRFS*

* Since not many people are using BTRFS, this post will only cover enabling TRIM on EXT4 partitions.

If you're unsure if your SSD supports TRIM, you can run the following command:
sudo hdparm -I /dev/sda | grep "TRIM supported"

Where "/dev/sda" is the solid-state drive (it may be /dev/sdb, /dev/sdc, etc. for you), and the command should return something like this: "Data Set Management TRIM supported (limit 8 blocks)" (if there's no output, your SSD doesn't support TRIM).  If you don't know what to use here, you can get a list of hard disks and their partitions by using the following command:
sudo fdisk -l

Enable TRIM for solid-state drives (SSD) in Linux

Using online discard (fstab) - not recommended

If the conditions above are met, you can proceed to enable TRIM for your SSD. If you want to use the online discard option (not recommended!), open /etc/fstab as root with a text editor:
gksu gedit /etc/fstab

And add the "discard" option (separated by a comma and no space!) to the SSD partitions you want to enable TRIM for. Here's an example:
<file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sdb1 during installation
UUID=1cd2fc4f-7d99-4c7a-8ea7-6f9a2d5e5960 /               ext4    discard,errors=remount-ro 0       1

Using a daily cron job - recommended

Using a daily cron job, the SSD trimming will occur once every day.

To use a daily cron job (so the trimming will occur once a day) for TRIM (fstrim), open /etc/cron.daily/trim as root with a text editor (/etc/cron.daily/trim doesn't exist so this will create the file):
gksu gedit /etc/cron.daily/trim
 and paste this:
echo "*** $(date -R) ***" >> $LOG
fstrim -v / >> $LOG
fstrim -v /home >> $LOG

The last two commands in the code above perform the actual trimming for the root (/) and home (/home) partition and you need to edit them: here, add the SSD partitions for which you want to enable the daily TRIM job (usually, you must add "/" if the root partition is on the SSD and "/home" if you've set up a separate home partition).

Before saving the file, you can check if the fstrim command works:
sudo fstrim -v /

The output should look similar to this:
andrei@ubuntu-desktop:~$ sudo fstrim -v /
/: 8158715904 bytes were trimmed

Once you've added your SSD partitions, save the file and make it executable using the following command:
sudo chmod +x /etc/cron.daily/trim

Ubuntu executes the daily cron (we're using anacron, so even if your computer is turned off at that time, the job will still be performed later on) jobs at around 06:25 so each day after that time, you can check the /var/log/trim.log log file to see the fstrim output.

For encrypted partitions

If you're using an encrypted partition, you'll have to perform some extra steps for this to work (not tested because I don't have any encrypted partitions!): edit /etc/default/grub as root:
gksu gedit /etc/default/grub

And add the following options under GRUB_CMDLINE_LINUX="" (it should be around line 12 in that file):
"allow-discards root_trim=yes"

After editing the file, it should look like this:

GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX="allow-discards root_trim=yes"

Then, save the file and update GRUB:
sudo update-grub

Now edit /etc/crypttab as root:
gksu gedit /etc/crypttab

And add the "discard" option for the SSD partitions, like this (example):
#<target name>    <source device>            <key file>  <options>
var  UUID=01234567-89ab-cdef-0123-456789abcdef  none  luks,discard

And finally, run the following command:
sudo update-initramfs -u -k all

The above instructions (except for the encrypted partition part, which I can't test) have been tested on Ubuntu 12.10 and 13.04 (SSD: KINGSTON SVP200S 120 GB).

References / further reading: