201

I know that the Linux kernel supports TRIM as of version 2.6.33, so there should be TRIM support in ubuntu.

Is TRIM enabled by default or do I need to change some settings or install additional software for it to work? If so, how?

Jorge Castro
  • 71,754
Uli
  • 5,166
  • I just followed these instructions on webupd8 which gives some really good options :-) http://www.webupd8.org/2013/01/enable-trim-on-ssd-solid-state-drives.html I tried the option for automatic trim as described above in "Automatic TRIM" but found that setting up a daily cron worked alot better with my setup. – bmbaker Jan 21 '13 at 09:38

2 Answers2

205

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.

Uli
  • 5,166
  • 5
    Testing TRIM may not actually work like that: http://serverfault.com/a/401506/60525 – chrishiestand Jun 22 '12 at 21:53
  • 1
    @Uli Please someone help me. It doesnt seem to work for me. I have 4 partitions in my ssd / /boot /usr and /opt and I have used another hdd for my /home. I go to cd / #sdc and I continue with the instructions but I never get zeros. Thanks in advance! – BugShotGG Mar 27 '13 at 01:00
  • @GeoPapas Are you sure your SSD supports TRIM? You might also be experiencing the issue linked to in the comment above yours. – Uli Apr 05 '13 at 13:20
  • @Uli It supports TRIM. Does it have to do with journaling on/off ? I have it enabled with ext4. In the comment i cant see any solution. I remember that i never had any issues with enabling TRIM in the past on the same ssd. I ll check cat /sys/block/sda/queue/discard_zeroes_data and see what i can find out... – BugShotGG Apr 05 '13 at 14:28
  • 1
    @lightrush: Great edit! Thanks for keeping the answer up to date. – Uli Dec 04 '13 at 12:54
  • I should update my blog post too. Quite a few people end up there. :) – Nicolay Doytchev Dec 04 '13 at 15:32
  • 1
    You might want to add a sync after the dd command, since at least on my system the tempfile is not created right away, so hdparm reports nothing. – ajdev8 Dec 09 '13 at 18:40
  • feel free to edit ;-) – Uli Dec 11 '13 at 19:35
  • According to http://bazaar.launchpad.net/~ubuntu-branches/ubuntu/trusty/util-linux/trusty/view/head:/debian/fstrim-all fstrim-all will also work for OCZ, Patriot and Sandisk and is automatically activated on Ubuntu 14.04 Trusty (See /etc/cron.weekly/fstrim) – Christian Benke Oct 07 '14 at 09:11
  • Note: "System->Administration->Disk Utility" means launch the "Disks" application from the Unity Dash. From this utility, make a note of the device name of your SSD (e.g. sda, sdb, sdc ...). The value to use in [ADDRESS] should be the first begin_LBA number listed (with a byte_offset = 0) after executing "sudo hdparm --fibmap tempfile". – Enterprise Oct 12 '14 at 01:10
  • after i upgrade to ssd and upgrade distro to 17.10 i decide to check and there is no fstrim file in /etc/cron.weekly, do i have to create it manually, and what is the correct code to add inside? – Vasil Valchev Oct 24 '17 at 09:07
  • 3
    systemd handles this now. sudo systemctl status fstrim.timer – balloons Jun 03 '18 at 23:05
29

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.

SSD: /dev/sda

sudo fdisk -l /dev/sda
/dev/sda1               # boot partition (83) (unencrypted, ext4)
/dev/sda2               # extended partition
/dev/sda5               # logical partition (83) with LUKS encryption

ls /dev/mapper
/dev/mapper/sda5_crypt         # encrypted LUKS device in physical /dev/sda5 partition
/dev/mapper/volumegroup-root   # rootpartition sda5_crypt
/dev/mapper/volumegroup-swap   # swap partition sda5_crypt

HowTo

  1. Make a backup of all your data. You're messing with your file system so having a backup is simply a good idea.
  2. 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).
  3. 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.

    /dev/mapper/volumegroup-root    /   ext4 discard,noatime,nodiratime,errors=remount-ro    0    1
    
  4. 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.

    sda5_crypt UUID=e364d03f-[...]6cd7e none luks,discard
    
  5. Rebuild your initramfs. The crypttab options are stored there and used on boot.

    sudo update-initramfs -c -k all
    
  6. Reboot.

  7. Check if TRIM is now active.

    sudo dmsetup table /dev/mapper/sda5_crypt --showkeys
    
  8. If the last command shows a result like this (1 allow_discards at the end) you're all set.

    0  77656056  crypt  aes-cbc-essiv:sha256  abc[...]c7a0c  0  8:5  2056  1  allow_discards
    

Result

TRIM is activated. Enjoy your hassle-free SSD!

Lekensteyn
  • 174,277
nem75
  • 1,669
  • @Marco Ceppi any specific reason for the edit, something I overlooked? – nem75 Jun 04 '12 at 09:15
  • Just tidying up the markdown and removed the "signature" line (it's assumed that you'll get feedback via comments and voting on the site. – Marco Ceppi Jun 04 '12 at 12:49
  • 7
    The guide should be included here in case that site/page is unavailable. – Cas Jan 08 '13 at 13:22
  • Besides that this is effectively a link-only answer, it's not working at all. Most of you will use LVM with LUKS, which isn't capable of propagating TRIM in 12.04. See this answer for more info. – gertvdijk May 04 '13 at 11:17
  • 1
    After following these instructions, is it still necessary to set up a cron job to trim the disk on 14.04? – ajn May 21 '15 at 13:45
  • Notice: enabling TRIM on an encrypted partition reduces the security of the encryption because it reveals which parts of the partition contain data and which don't. This can aid in some types of analysis. To put this in perspective this does not immediately make your data visible but it would be a similar security trade-off to using a sparse file as the encrypted block, or of setting up an encrypted partition but not initialising the free space with random data, as is usually recommended. – thomasrutter Oct 01 '15 at 00:01
  • @Is TRIM still needed with SSDs (with LVM & LUKS) nowadays? – Filbuntu Aug 17 '19 at 06:04