Enabling Trim Support in (K)ubuntu

I have purchased two SSDs so far. The first, an Intel X25-V series, was somewhat of a performance disappointment.  It really didn’t perform much better than a good 7200RPM drive. To replace this drive I bought an OCZ Agility 2 which has not disappointed me so far. I dropped the old Intel X25-V into my netbook and have seen a insignificant performance over the slow hard drive that came with it.

Since I actually saw an increase in performance by the OCZ drive, I didn’t want to lose that over time as sectors get used up and aren’t zeroed out.  The time lost comes when new writes have to wait for the sectors to be zeroed before they can be written. The TRIM command is supposed to fix that by zeroing sectors as they are freed.

I found instructions on how to enable TRIM on AskUbuntu. What follows is the steps I followed, in case the original article ever disapears.

First you have to make sure the drive you want to use is formatted with EXT4. you can check this by looking in the /etc/fstab file. For example here is the line that describes my SSD:

UUID=13d165a1-ec32-42ce-bb58-10911e907ce1 / ext4 errors=remount-ro 0 1

Notice how after the mount point of “/” is specified the next parameter says ext4. To enable TRIM you simple need to add “discard” to the list of options like so:

UUID=13d165a1-ec32-42ce-bb58-10911e907ce1 / ext4 discard,errors=remount-ro 0 1

Then reboot.

The next step is to verify that TRIM is indeed working. The way that you demonstrate this is pretty neat; it shows you exactly what TRIM does. Basically you create a file filled with random data on the root of the SSD. Then you find the starting sector of the file you just created and read some raw data from the file. Then you remove the file and read the data from the sector again. Without TRIM, you’d see the same data, with TRIM you’ll see all zeros.

benjamen@lisa:~$ cd /
benjamen@lisa:/$ sudo dd if=/dev/urandom of=tempfile count=100 bs=512k oflag=direct
[sudo] password for benjamen:
100+0 records in
100+0 records out
52428800 bytes (52 MB) copied, 10.92 s, 4.8 MB/s
benjamen@lisa:/$ sudo hdparm --fibmap tempfile

tempfile:
filesystem blocksize 4096, begins at LBA 2048; assuming 512 byte sectors.
byte_offset begin_LBA end_LBA sectors
0 13706240 13707263 1024
524288 13730816 13731839 1024
1048576 13748224 13750271 2048
2097152 13711360 13715455 4096
4194304 13772800 13867007 94208
benjamen@lisa:/$ sudo hdparm --read-sector 13706240 /dev/sda

/dev/sda:
reading sector 13706240: succeeded
1228 8669 f83b a5aa 73cf a136 966a bc25
cdfb 8745 7f5d d4fc 5dbe 39c1 a30d a127
f8b6 74cd 648a b3d4 96d1 2421 691f e4eb
b81a ce47 d934 1f32 c99d ebb8 a7e3 03f5
b384 3321 6473 cc88 0f81 8f76 80f0 de6a
49e4 be9f 3095 de58 fd8f 1a0d 86a0 a27d
8c7f b98c 8bba 68e4 b12a 1fbb d899 2a55
82a7 07df 43db a95e 75d5 9460 3dc8 dd9f
7dfc c420 0b08 670b be78 9ca8 25ec 921d
f9d4 9ea7 2f35 9802 091f e2fc 912b e572
d24d 858e 1847 e01b 577c 8dcc 8cd1 3c0e
03f3 f5d8 d789 5431 36cf 2d9f 3b6d 68c4
365d a2e8 c987 c07c d389 4ca1 c3fc 70d3
65fd 1c97 c335 e332 f7b7 c791 27ec 5f14
50cb c804 114d 6379 6876 97df 8693 72e4
edbc a50f 5a9b 5c4f d028 0bb3 1327 2318
7292 884e 3494 4b99 46fd 0259 fe76 d9a6
e7a1 2ec5 131d 8d69 2a28 2f8a add4 2541
b953 06bc ba90 8f1e 7505 6baa 0bc2 f704
4e63 2dea 4c88 8f31 6971 c5d5 d2e5 7f7f
6ae4 e8dd c3cb d289 b1a6 f571 15c3 c944
e077 e4b3 141b 4db5 a5ec 76dc ef82 5ae0
8770 620e 9df7 b1a2 c40e a305 1272 19e0
c5f8 0fe7 dbe8 1b91 2556 8c22 a195 8d26
a297 baad 8b3b 53b5 8ce0 f408 bcd7 200b
553b 6121 9f2c 7fcd bc4e 874e 6955 a1ee
3a93 a3c1 75ad a987 a597 e6bf 25f8 3ad7
5b86 0b38 6747 61d5 2c9a 2ae3 e775 4283
e81f cb20 df29 de08 0c01 0d39 9b0c ccb4
cf95 a77a 564b 583d bc60 dd4d 70ff 8653
8f18 0f53 1476 4a27 1c4c 0b58 7109 4456
b65a c374 d399 eb08 6a4f caf6 f9f5 24f1
benjamen@lisa:/$ sudo rm tempfile
benjamen@lisa:/$ sync
benjamen@lisa:/$ sudo hdparm --read-sector 13706240 /dev/sda

/dev/sda:
reading sector 13706240: succeeded
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
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
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
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
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
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
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
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
benjamen@lisa:/$

One thing that I’m not sure about is what happens if you have the SSD drive partitioned. So to keep it simple my advice is to format the whole SSD as one partition.

COMMENTS