Wednesday, January 9, 2013

How to Enable TRIM? Ubuntu 12.04

So you have upgraded your old 7.2K RPM hard drive to a new solid state drive (SSD) at either SATA II or SATA III link speed. Awesome! This is becoming very common because of the enormously high read and write speeds available with SSD's, and their extremely low latency -- great for music production applications, gaming, etc. These benefits can even be seen when using a SATA II link (3GB/s).
To prevent the dwindling of these enhancements to daily computing life we rely on TRIM. Trim is not an acronym, although it is commonly thought of as one. Trim is a command used by the operating system to inform the SSD of unused data blocks that can be deleted.

This helps improve overall performance, endurance, and compatibility of solid-state drives. 
As of version 2.6.33 of the Linux kernel TRIM support exits in LInux flavors built upon this; Ubuntu 10.10 and later. There are two ways to perform/enable TRIM under Ubuntu linux -- manual vs. automatic. 
Manual TRIM:

In Ubuntu, enabling manual TRIM can be performed using the fstrim module in a terminal session.

sudo fstrim /

This, however, is not needed if automatic TRIM is currently enabled. Ubuntu 11.04 and earlier releases are not packaged with the fstrim module. wiper.sh must be used and is supplied by 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, it needs 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=000000000-0000-0000-0000-0000000000  /  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:


cd / # Replace with SSD file system path
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 in the previous step.



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 few minutes and run the command again.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...