HOWTO: Configure Ext4 to Enable TRIM Support for SSDs on Ubuntu and Other Distributions

Post date: 18-Oct-2010 05:11:13

New Method

A new method to do scheduled TRIM has been established to be better than sending TRIM commands on every delete. The method is described under the Scheduled TRIM section here.

Changelog

  • Deprecated this HOWTO.
  • Added backup step in the beginning and restore step at the end in case of failure.
  • Updated the layout for better readability.
  • Added new benchmark of the SSD after 3 months of use for performance reference.
  • Modified some highlighting in the code to be more visible.
  • Added a link to the HOWTO page for testing TRIM.

Notes

  • This HOWTO should work for other GNU/Linux distributions as long as they are running Linux 2.6.33 or later and they have Gedit. If Gedit is not available - any text editor will do as long as we know how to use it.
  • The software used to benchmark the OCZ Agility 2 drive I have attached is Disk Utility. Its package name in Ubuntu is "gnome-disk-utility" and it ships by default with the desktop version of Ubuntu.
  • CAUTION: Some people recommend the use of other options to the Ext4 filesystem such as removing its journal or removing write barriers. Both of those can lead to filesystem corruption and loss of data on systems without UPS. You should use those only if you really know what you are doing. For the sake of stability and data health - just use the discard option to turn TRIM on as described below. TRIM and the SSD's controller should be enough to keep it in good shape in terms of speed and longevity. I personally use an OCZ Agility 2 60GB with discard only and as you can see the average read speed is still well sustained at ~250MB/s after around a bit over month of use (image to the right).

HOWTO (Deprecated)

Most current SSDs support the ATA_TRIM command for sustained long-term performance and wear-leveling. On Linux TRIM is supported by the Ext4 and Btrfs filesystems but the latter is out of the scope of this tutorial. We need two things in order to enable TRIM:

Having met the two requirements, all we need to do to enable TRIM is the following:

  1. We make a backup of the current /etc/fstab for disaster recovery purposes:
      1. sudo cp /etc/fstab /etc/fstab_bak-notrim
  2. We edit the /etc/fstab file:
      1. gksudo gedit /etc/fstab
  3. For every partition using Ext4 we add the word discard to the list of options. That is we prepend or append the word discard to the existing list of words separating it with comma without leaving space. We do not change anything else. So for example if we have only one partition mounted at root "/":
      1. UUID=[NUMS-AND-LETTERS] / ext4 errors=remount-ro 0 1
    1. we change the entry to:
      1. UUID=[NUMS-AND-LETTERS] / ext4 discard,errors=remount-ro 0 1
  4. We save and exit Gedit.
  5. We reboot the computer.
  6. In case of failure you can always use Ubuntu Live CD or other live Linux media, mount the root partition of your SSD and revert the changes by raplacing [MOUNT-POINT]/etc/fstab with the backed up one - [MOUNT-POINT]/etc/fstab_bak-notrim.

That's it. Now we should have working TRIM. If we wish to test that it works we can follow this HOWTO.

Benchmarks

This is a benchmark of OCZ Agility 60GB SSD used in a laptop on day-to-day basis for a month:

This is a benchmark of OCZ Agility 60GB SSD used in a laptop on day-to-day basis for 3 months:

As you can see the read performance after 3 months of use is well sustained as well as the access time is still just 0.2ms. This is after the drive was filled above 90% several times with virtual machines. The drive seems to successfully recover its performance after files are deleted which means TRIM works as intended.