How to check TRIM is working on your SSD running Linux

A hairy highland cow

Does your drive need a trim?

SSDs really spank traditional disks. But due to a quirk of how they work, they can lose some speed over time. To make sure your disk stays at the same blazing speeds as when you first got it you need to have TRIM enabled. TRIM is a slight optimisation that allows the disk to do some housekeeping behind the scenes.

What you need for TRIM:

  1. An SSD with firmware that supports TRIM
  2. Linux kernel version 2.6.33 or higher. That means Ubuntu 10.10, Fedora 13 or better.
  3. The “discard” option in your /etc/fstab

If you haven’t already done this you can find out how here.

To check that TRIM is working the way it should we’ll create a small file on your SSD, inspect it, then delete it and make sure TRIM has zeroed all the data out.

Open a root terminal, or if on an Ubuntu-based system become root with:

sudo -i

Create a small file in /root (this is all one line):

dd if=/dev/urandom of=tempfile bs=1M count=3

Find the start of the file:

hdparm --fibmap tempfile

Note the address that the file starts at and then inspect that address (if you have more than one disk you should substitute sda for the disk you are checking):

hdparm --read-sector [ADDRESS] /dev/sda

You should see random data. Now delete the file, sync the filesystem, and wait a couple of minutes for the disk to do it’s thing:

rm tempfile && sync && sleep 120

Now let’s inspect that piece of the drive again to make sure the data is gone:

hdparm --read-sector [ADDRESS] /dev/sda

If TRIM is working, you should see all zeros. If you see anything except a sea of zeros then try updating the drive’s firmware. That’s pretty straightforward on recent drives. On Intel SSDs you just burn a small utility to a CD and boot up from it. Make sure you back up your data before you do that, just in case.

Tags: , , ,

22 Responses to “How to check TRIM is working on your SSD running Linux”

  1. Mark Says:

    I’m new to Linux Mint 10 and using an OCZ SSD.

    Trim now enabled and verified!!

    Thanks for the instructions…

  2. bobbux Says:

    Thanks a lot for this useful tutorial !

  3. Joe Says:

    Hi, is there any possibility to make TRIM work while using disc encryption (at least /home folder)? I’ve read a lot and seems this is not possible. Thanks

  4. David Says:

    Great! Thanks for the info, all works perfectly on Ubuntu 11.04 with my Vertex 3 SSD.

  5. stqn Says:

    Please note that if the temporary file doesn’t start on an erase block boundary, the starting sector might not be filled with zeroes after the file is erased. It can also take a few minutes to happen.

    This should be a bit more foolproof:

    dd if=/dev/urandom of=tmpfile bs=1M count=3
    (Some SSDs have erase blocks of up to 1M. By writing 3 times this size, we can be sure that at least the middle of the file will be TRIMmed – that is, if the filesystem is not fragmented.)
    hdparm –fibmap tmpfile
    rm tmpfile && sync && sleep 120
    hdparm –read-sector /dev/sda

  6. Andy Says:

    Thanks stqn, that’s some good info. I’ve amended the instructions with your suggestions.

  7. Verify TRIM support with BtrFS on SSD - Admins Goodies Says:

    […] my testing methodology (initially adopted from http://andyduffell.com/techblog/?p=852, with modifications to work with […]

  8. Verify TRIM support with BtrFS on SSD - Admins Goodies Says:

    […] my testing methodology (initially adopted from http://andyduffell.com/techblog/?p=852, with modifications to work with […]

  9. Nathan Says:

    Not working for me. Default install Fedora 15 on laptop I just get all 0’s, reproducably. There is some information missing from this walkthrough. Please contact me if you need more details, via email.

  10. GeekParadise | Trim testen unter Linux Says:

    […] Andy Dufell’s Blog Linux LVM […]

  11. Linux SSD partition alignment tips | Into.the.Void. Says:

    […] Drives 5. SSD performance tips for RHEL6 and Fedora 6. Re: [dm-devel] trim support (discard) 7. How to check TRIM is working on your SSD running Linux 8. Impact of ext4?s discard option on my SSD (very useful insight on TRIM command, read the […]

  12. Linux SSD partition alignment tips » ssd Says:

    […] Drives 5. SSD performance tips for RHEL6 and Fedora 6. Re: [dm-devel] trim support (discard) 7. How to check TRIM is working on your SSD running Linux 8. Impact of ext4?s discard option on my SSD (very useful insight on TRIM command, read the […]

  13. linux???SSD TRIM????? » ssd Says:

    […] How to check TRIM is working on your SSD running Linux […]

  14. trenuletzul Says:

    @nathan (if you still read this): on newer systems the write isn’t performed right away, you might want to do a sync before you do the firsthdparm read (and the second sync after removing the file of course)

  15. cracket Says:

    reading sectors on LVM devices (like RAID) does not work:
    # hdparm –read-sector 77697024 /dev/mapper/sil_biaidgddagcd1

    /dev/mapper/sil_biaidgddagcd1:
    reading sector 77697024: FAILED: Inappropriate ioctl for device

    use dd instead:
    # dd if=/dev/mapper/sil_biaidgddagcd1 of=~/sec1 bs=512 skip=77697024 count=64

  16. ssd trim - Page 2 Says:

    […] /boot ext4 noatime,discard,acl,user_xattr 1 2 I try the test described at Techblog […]

  17. sdgsg Says:

    just because the content is still there does not mean discard is not working AFAIK.
    the output of “lsblk -D” is better. it should print values >0 for DISC-GRAN and DISC-MAX for all layers involved.

  18. OpenSuse 12.2 installation notes from a new Linux user Says:

    […] cache, see Running Ubuntu and other Linux flavors on an SSD Verify TRIM working Just go here: Techblog Verify SSD Block Alignment With fdisk -l,and this post Linux Mint Forums • View topic […]

  19. Resolved: Verify TRIM support with BtrFS on SSD #fix #development #solution | SevenNet Says:

    […] my testing methodology (initially adopted from http://andyduffell.com/techblog/?p=852, with modifications to work with […]

  20. Solution: Verify TRIM support with BtrFS on SSD #development #answer #fix | Good Answer Says:

    […] my testing methodology (initially adopted from http://andyduffell.com/techblog/?p=852, with modifications to work with […]

  21. Answer: Verify TRIM support with BtrFS on SSD #programming #computers #dev | IT Info Says:

    […] my testing methodology (initially adopted from http://andyduffell.com/techblog/?p=852, with modifications to work with […]

  22. Ram Says:

    There is no use checking whether the file is trimmed after deletion of file since in Ubuntu 14.04, the trim job runs weekly and if we want to really check, then we have to manually trigger trim job.

Leave a Reply

This page last updated 18 October 2011