Tell me more ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question

3 Answers

up vote 86 down vote accepted

There are two ways to perform TRIM, manual and automatic:

Manual TRIM

In Ubuntu this can be performed with fstrim:

sudo fstrim /

however is not needed when automatic TRIM is enabled, as detailed in Automatic section 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

Automatic TRIM

Automatic TRIM has been supported since Ubuntu 10.10 (kernel 2.6.33) with the EXT4 file system.

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.

share|improve this answer
4  
Testing TRIM may not actually work like that: serverfault.com/a/401506/60525 – chrishiestand Jun 22 '12 at 21:53
@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! – Geo Papas Mar 27 at 1: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 5 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... – Geo Papas Apr 5 at 14:28

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

share|improve this answer
@Marco Ceppi any specific reason for the edit, something I overlooked? – nem75 Jun 4 '12 at 9: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 4 '12 at 12:49
4  
The guide should be included here in case that site/page is unavailable. – Cas Jan 8 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 4 at 11:17

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.

share|improve this answer

protected by Community Jul 8 '11 at 14:11

This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.

Not the answer you're looking for? Browse other questions tagged or ask your own question.