Latest News Posts

Social
Latest Forum Posts

Enabling and Testing SSD TRIM Support Under Linux
Bookmark and Share

enabling_trim_linux_050511_article_logo.gif
Print
by Rob Williams on May 6, 2011 in Linux, Solid-State Drives

Owning an SSD that supports TRIM is great, but while Windows users have the benefit of having TRIM enabled for them, Linux users need to take the manual route – at least, at this point in time. In this article, we tackle the simple process of doing so, and also show how to verify that TRIM is indeed working as it should be.

Checking for TRIM Success; Final Thoughts

If the SSD in question does in fact support TRIM, and ‘discard’ is properly added to the /etc/fstab file, then it can be assumed that TRIM is in fact working. But for assurance, there’s a simple test that can be performed, though it does require a little more effort in the command-line than enabling TRIM in the first place did.

Because TRIM purges all data found in affected ‘sectors’ after a file is deleted, the result should be sectors that read a bunch of 0s, not some sort of data. On a non-TRIM drive, data found in a sector would remain the same even after the file is deleted, which is the reason performance can find itself degraded over time. With TRIM purging out the data properly, any new writes to the drive will target clean, like-new sectors.

For testing, we’ll have to create a small file on the affected SSD partition, as we’ll be deleting it to see if TRIM in fact worked. After opening up a command-line, enter su or sudo mode (the latter using ‘ sudo -s ‘). Then, create a small file:


techgage Desktop # seq 1 1000 > testfile

That command creates a simple text file with numbers ranging from 1 to 1000, and in total, it weighs in at about 4KB (a typical sector size). With that file created, it’s time to find out which sector(s) on the drive it’s using:


techgage Desktop # hdparm –fibmap testfile

You will see this sort of result:

Enabling TRIM & Secure Erasing Under Linux

Take note of the first ‘begin_LBA’ value, which in this case is 49598792. That’s the beginning sector for the file, and one we can read from for this test. With the sector number in hand, we can check for its contents using these commands:


techgage Desktop # sync
techgage Desktop # hdparm –read-sector 49598792 /dev/sda

(If your drive is not /dev/sda, replace it with the correct value.)

Enabling TRIM & Secure Erasing Under Linux

The ‘sync’ command must be run prior to the hdparm one for the sake of flushing out the file system buffer, and once run, the hdparm command will show all of the contents of the given sector in hexadecimal format. Time to delete the file, and then run the same commands as before:


techgage Desktop # rm testfile
techgage Desktop # sync
techgage Desktop # hdparm –read-sector 49598792 /dev/sda

Enabling TRIM & Secure Erasing Under Linux

That’s just what we wanted to see… nothing! If all 0s appear, then TRIM worked, but if the same data as before is still there, be sure that the “sync’ command was in fact run, as it should be in order to flush out the buffer and give you accurate information.

Final Thoughts

It took a little while after Windows 7’s launch for robust support of TRIM to hit Linux, but it’s here, and as you can see, enabling and testing for it couldn’t be much simpler. Had I known that enabling it was as simple as adding ‘discard’ to the /etc/fstab file, I would have upgraded to an SSD months ago! And since we’re able to use hdparm to actually check to see that TRIM is in fact working, there’s no doubt left in our minds that once setup, TRIM will keep our SSDs in tip-top shape.

Although a rather simple article, I wanted to get the method out there and explain in as easy terms as possible how to get things done. If any of this guide proved a little confusing, or more information would be desired, please feel free to post in our related thread below, and we’ll consider updating the article.

Discuss this article in our forums!

Have a comment you wish to make on this article? Recommendations? Criticism? Feel free to head over to our related thread and put your words to our virtual paper! There is no requirement to register in order to respond to these threads, but it sure doesn’t hurt!

Page List:
Top

1. Introduction
2. Checking for TRIM Success; Final Thoughts


  • fsk141

    Thanks for this, been planning on setting up trim support with JFS on the new 3.7 kernel. Now I’ll know how to easily test that it’s working.

    Appreciate it!

    • http://techgage.com/ Rob Williams

      I’m glad the article proved useful!

  • matuog

    a very nice article! thank you!

  • http://twitter.com/juliobetta Julio BĂȘtta

    thanks!!!

  • http://www.facebook.com/tiago.venturamendes Tiago Ventura Mendes

    Good evening.

    I got this instead:

    reading sector 119807832: succeeded

    ffff ffff ffff ffff ffff ffff ffff ffff

    ffff ffff ffff ffff ffff ffff ffff ffff

    ffff ffff ffff ffff ffff ffff ffff ffff

    ffff ffff ffff ffff ffff ffff ffff ffff

    ffff ffff ffff ffff ffff ffff ffff ffff

    ffff ffff ffff ffff ffff ffff ffff ffff

    ffff ffff ffff ffff ffff ffff ffff ffff

    ffff ffff ffff ffff ffff ffff ffff ffff

    ffff ffff ffff ffff ffff ffff ffff ffff

    ffff ffff ffff ffff ffff ffff ffff ffff

    ffff ffff ffff ffff ffff ffff ffff ffff

    ffff ffff ffff ffff ffff ffff ffff ffff

    ffff ffff ffff ffff ffff ffff ffff ffff

    ffff ffff ffff ffff ffff ffff ffff ffff

    ffff ffff ffff ffff ffff ffff ffff ffff

    ffff ffff ffff ffff ffff ffff ffff ffff

    ffff ffff ffff ffff ffff ffff ffff ffff

    ffff ffff ffff ffff ffff ffff ffff ffff

    ffff ffff ffff ffff ffff ffff ffff ffff

    ffff ffff ffff ffff ffff ffff ffff ffff

    ffff ffff ffff ffff ffff ffff ffff ffff

    ffff ffff ffff ffff ffff ffff ffff ffff

    ffff ffff ffff ffff ffff ffff ffff ffff

    ffff ffff ffff ffff ffff ffff ffff ffff

    ffff ffff ffff ffff ffff ffff ffff ffff

    ffff ffff ffff ffff ffff ffff ffff ffff

    ffff ffff ffff ffff ffff ffff ffff ffff

    ffff ffff ffff ffff ffff ffff ffff ffff

    ffff ffff ffff ffff ffff ffff ffff ffff

    ffff ffff ffff ffff ffff ffff ffff ffff

    ffff ffff ffff ffff ffff ffff ffff ffff

    ffff ffff ffff ffff ffff ffff ffff ffff

    IS it enabled?

    Thank you.

    • http://www.facebook.com/deathspawner Rob Williams

      Those should be 0s, so no, I don’t think so. The fact that the characters are all the same leads me to believe it’s just bunk data (as if the drive was purposely filled).

      You may wish to look at the fstrim tool instead:

      http://manpages.ubuntu.com/manpages/oneiric/en/man8/fstrim.8.html

      I’ve heard that it’s recommended over the “discard” option mentioned in this article, but I haven’t had a chance to give it a good test. The idea would be to set up a scrip that would run once per day to clean all deleted blocks.

      • http://twitter.com/codeboy2k codeboy

        If it used to have valid testfile data before you deleted it, and after you delete the file it shows either all 00 or all FF then trim is working. The difference is due to different technologies, NOR flash vs NAND flash.

        NOR flash must write all data to 00 before it can be erased (to FF)

        NAND flash does not need to do this, and can quickly erase to FF.

        Finally, both technologies require any block to be in the erased state before it can be rewritten with data.

        The drive that shows all 00 is probably using NOR flash and wrote all 00 but did not erase it yet. That might come later, when the controller’s garbage collector erases the blocks.

        The drive that shows all FF is probably using NAND flash and quickly erased the block and marked it free to use.

        In both cases, it would appear to me that TRIM is working as intended.

        For some flash technologies, an empty cell is a 0 bit. For other flash technologies, and empty cell is a 1 bit. Thus an empty flash block would be filled with 1’s and would show up as FF after trim.

  • http://www.facebook.com/tiago.venturamendes Tiago Ventura Mendes

    Good evening.

    I got “f” instead of “0”

    IS it enabled?

    Thank you.

  • http://www.facebook.com/zachary.sonicfast Zachary Sonicfast

    Why not just have discard already built into Linux? That way no manual manipulation would be needed.

    It would be nice to have a point and click method to run trim (discard) manually without all the typing. Right click on a drive and left click on trim sorta thing.

    I have discard put into my fstab but the system seems to be getting really slow on startup.