ENABLE TRIM ON SSD Solid State Drives

Enable Trim on SSD Solid State Drives

Enable Trim on SSD Solid State Drives in Ubuntu and Mint for better performance.  SSD Drive picture

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.

Note: Make a backup of your system before you start. At the least make a copy of the /etc/fstab file so just in case things go wrong you can restore it back to the original state.

Before enabling TRIM, you must make sure:
Linux Kernel is 2.6.33 or newer
Your SSD supports TRIM
The partition(s) are EXT4 or BTRFS*

* Since most people are not using the BTRFS type filing system, 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 in side a terminal window:

sudo fstrim -v /

The output should look similar to this:
andrei@ubuntu-desktop:~$ sudo fstrim -v /
/: 1159715924 bytes were trimmed  (this tells you Trim is working)

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:

So if you don’t see something like: /: 1159715924 bytes were trimmed then Trim is not supported.

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 in a Terminal window:
sudo fdisk -l

Example that sudo fdisk -l will show you:

Disk /dev/sda: 240.1 GB, 240057409536 bytes
255 heads, 63 sectors/track, 29185 cylinders, total 468862128 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x8718cc60

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 76982271 38490112 83 Linux
/dev/sda2 76982272 108728319 15873024 83 Linux
/dev/sda3 108730366 468860927 180065281 5 Extended
Partition 3 does not start on physical sector boundary.
/dev/sda5 108730368 187578024 39423828+ 83 Linux
/dev/sda6 187580416 227514367 19966976 83 Linux
/dev/sda7 227516416 268477353 20480469 83 Linux
/dev/sda8 268478464 301238271 16379904 83 Linux
/dev/sda9 301240320 343935632 21347656+ 83 Linux
/dev/sda10 343937024 366462975 11262976 83 Linux
/dev/sda11 366465024 429949398 31742187+ 83 Linux
/dev/sda12 429950976 468860927 19454976 83 Linux

Disk /dev/sdb: 2000.4 GB, 2000398934016 bytes
255 heads, 63 sectors/track, 243201 cylinders, total 3907029168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xdd8e50e

This shows us that it’s /dev/sda drive where working with.

If your system does not support Trim, then just edit your /etc/fstab file (in root mode) and add the discard as show in the example below.

gksu gedit /etc/fstab
 
<file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/sda1 during installation
UUID=1ef2fc4f-7d89-4c7a-8nc7-9f9a2d5e5912  /     ext4     discard,errors=remount-ro    0     1
UUID=1cf2fc4f-9d89-9c7a-4nc7-9f9a2d5e6014  /home     ext4     discard,errors=remount-ro    0     1
(Note: you should not have a swap file on a SSD, if you have another drive that is not a SSD, put it on that drive)

Don’t forget, your UUID will be a different ID number just add the discard as shown above and change nothing else.

 

Now if Trim is Supported: Enable TRIM for solid-state drives (SSD) in Linux with a cron job.

Type in a Terminal windows gksu gedit /etc/fstab and add the following and only the following:

<file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/sda1 during installation
UUID=1ef2fc4f-7d89-4c7a-8nc7-9f9a2d5e5912  /     ext4     noatime,errors=remount-ro 0 1
UUID=1cf2fc4f-9d89-9c7a-4nc7-9f9a2d5e6014  /home     ext4     noatime,errors=remount-ro    0     1
(Note: you should not have a swap file on a SSD, if you have another drive that is not a SSD, put it on that drive)

Don’t forget your UUID will be a different ID number just add the noatime, as shown above and change nothing else.

Note: If your using Linux Mint Cinnamon the substitute gedit with xed, Xed is the default text editor in Cinnamon

To use a daily cron job for TRIM, type in a Terminal windows gksu gedit /etc/cron.daily/fstrim (/etc/cron.daily/fstrim doesn’t exist so this will create the file):
gksu gedit /etc/cron.daily/fstrim (gedit may not be your OS text editor. Substitute with the name of your text editor for your OS.
and paste this into the text editor then save and close it:
#!/bin/sh
LOG=/var/log/trim.log
echo “*** $(date -R) ***” >> $LOG
fstrim -v /
fstrim -v /home

The last two commands in the code above preform 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).

Once you’ve added your SSD partitions, save the file and make it executable using the following command:

sudo chmod +x /etc/cron.daily/fstrim

Here is a link how to change the cron job timing

Don’t want to go through all this, found a great article and program that doe it all for you.

Click here for more information

Hope this helps you

Wikipedia Link

Leave a Comment

You must be logged in to post a comment.