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

Post date: 21-Sep-2010 18:03:55

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.

NOTE: 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:
      1. sudo -i
  2. Make a 50MB file with random data:
      1. dd if=/dev/urandom of=tempfile count=100 bs=512k oflag=direct
  3. Check the starting LBA address of the file:
      1. hdparm --fibmap tempfile
  4. Read the first address of the file, note that you need to put the first LBA in place of [ADDRESS]:
      1. hdparm --read-sector [ADDRESS] /dev/sdX
  5. Now remove the file and synchronize the filesystem:
      1. rm tempfile
      2. sync
  6. Use the same command as in 4 to re-read the LBA:
      1. 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:
      1. 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:
      1. 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:
      1. 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.