Random Culprits‎ > ‎

HOWTO: Check If TRIM On Ext4 Is Enabled And Working On Ubuntu And Other Distributions

posted Sep 21, 2010, 11:03 AM by Nicolay Doytchev   [ updated May 6, 2012, 11:13 AM ]
IMPORTANT: Many modern SSDs will not reclaim the TRIMmed space. For this reason you may not see zeros at the end of the test even though TRIM could be working. This weakens the reliability of the test when showing that TRIM is not working. However if you see zeros at the end, then TRIM is definitely working. To put it simply - if the test tells you that TRIM is working (zeros show up), then TRIM is working; if the test tells you that TRIM may not be working (zeros do not show up), then TRIM may or may not be working. In the latter case, simply double check that you have used the right mount options in /etc/fstab and if they look correct, TRIM most probably works. Simply your SSD is not reclaiming the space immediately, so nothing to worry about.

If you were looking for instructions on how to enable TRIM rather than test it, look at this HOWTO.
NOTE 2: This HOWTO may not work if the filesystem we store the tempfile on is encrypted (e.g. Encrypted Home on Ubuntu).
NOTE 3: /dev/sdX is the SSD drive you are testing.

If you have an SSD and using Ubuntu or another flavour of GNU/Linux you probably have enabled TRIM in Ext4. However there is no obvious way to check whether it actually works. Here are several steps that can help you verify that:
  1. Become root:
    sudo -i
  2. Make a 50MB file with random data:
    dd if=/dev/urandom of=tempfile count=100 bs=512k oflag=direct
  3. Check the starting LBA address of the file:
    hdparm --fibmap tempfile
  4. Read the first address of the file, note that you need to put the first LBA in place of [ADDRESS]:
    hdparm --read-sector [ADDRESS] /dev/sdX
  5. Now remove the file and synchronize the filesystem:
    rm tempfile
  6. Use the same command as in 4 to re-read the LBA:
    hdparm --read-sector [ADDRESS] /dev/sdX
Alternatively you can use the script provided by Dorian Bolivar that automates this process. In order to do that follow these steps:
  1. Download the script:
    wget -O /tmp/test_trim.sh "https://sites.google.com/site/lightrush/random-1/checkiftrimonext4isenabledandworking/test_trim.sh?attredirects=0&d=1"
  2. Make it executable:
    chmod +x /tmp/test_trim.sh
  3. Run the script with a tempfile on your device. The parameters to the script are subject to the same notes mentioned above:
    sudo /tmp/test_trim.sh tempfile 50 /dev/sdX

If TRIM is properly working the result of the last command should be a bunch of zeros. If the output is not zeros, then something could be wrong with your kernel/Ext4 configuration and TRIM may not be working. For simple instructions of how to enable TRIM, look at this HOWTO.

Nicolay Doytchev,
Jan 30, 2012, 7:37 PM