Skip to content
November 12, 2011 / Alex Nedoboi

Tiny Tips: SSD and Linux. Enable TRIM and check if it works.


What is TRIM?

On hard disks, with spinning magnetic platters, you don’t care if a particular sector is being used by the operating system or not. If you want to use it again, just ask the controller to write into it, and the controller will just overwrite whatever was in that sector.

With SSD, if the sector already contains some information and you want to write into it, the old data needs to be deleted (zeroed out) first, and then the new data written. Zeroing out the old data takes considerable time, so it would make sense if we could tell the controller that the sector is no longer being used, allowing the controller to zero out the sector data in the background, and then when we need to use the sector again, it will be ready to be written into immediately, saving us a lot of time.

This is what the TRIM command does, it tells the controller that the sector is no longer used and can be zeroed out.

For the TRIM command to work, we need ext4 and the kernel to be 2.6.33 or higher. In this example we use Fedora 15.

Install Fedora 15 x86_64

If you don’t have it installed yet, or want to make sure that it’s installed correctly for the TRIM command to work, here is a very brief guide.

Installing Fedora is very much a matter of clicking the Next button.

The only important step where you would not click the Next button straight away, is the storage step.

Select the Create Custom Layout option. The reason we are not going with the default here is because we can’t use LVM with SSD.

Create two standard partitions, one swap and one ext4 (mounted as /). Don’t use ext2 or ext3 as they don’t support SSD either, particularly the TRIM command.

Enable TRIM

In default Fedora installation TRIM is not enabled by default. So let’s enable it.

As root, edit the /etc/fstab file and add the “discard” option to the root line.

UUID=... / ext4 discard 1 1

Restard the machine. Check if / has been mounted with “discard”.

[root@f15] mount
...
/dev/sda1 on / type ext4 (discard,rw)
...

Looks good. It is now time to test the TRIM functionality.

Check if TRIM works

We are going to create a file, check the sector data, then delete the file, and see if the sector data has been zeroed out.

We will be using the hdparm tool, so the following steps need to be done as root.

First, create a file.

[root@f15] for i in {10001..10200}; do echo "$i test line" >> testfile.txt; done

This will create a file with 200 lines, about 4k in size. Now make sure it is flushed out to the disk.

[root@f15] sync

Now let’s find out where the file is physically stored on the disk.

[root@f15] hdparm --fibmap testfile.txt

filesystem blocksize 4096, begins at LBA 0; assuming 512 byte sectors.
byte_offset  begin_LBA    end_LBA    sectors
          0   14927624   14927631          8

You can see the file starts at sector 14927624 and spans 8 sectors. Let’s check what’s in there.

[root@f15] hdparm --read-sector 14927624 /dev/sda

3031 3030 2031 6574 7473 6c20 6e69 0a65
3031 3030 2032 6574 7473 6c20 6e69 0a65
3031 3030 2033 6574 7473 6c20 6e69 0a65
...
3031 3330 2032 6574 7473 6c20 6e69 0a65

That’s our sector. “3031 3030 2031 6574 7473 6c20 6e69 0a65″ is a hex representation of “10000 test line”.

Now let’s delete the file. Make sure the change is flushed to the disk.

[root@f15] rm testfile.txt
[root@f15] sync

We are now expecting the sector to contain nothing but zeroes.

[root@f15] hdparm --read-sector 14927624 /dev/sda

0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
...
0000 0000 0000 0000 0000 0000 0000 0000

Perfect, the TRIM command works in Fedora 15.

Recent releases of most popular distributions (Ubuntu, Mint, Sabayon, etc) should also be fine.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: