Using fstrim to “TRIM” your SSD instead of “delete” in fstab

Step 1: Creating and scheduling a script to trim your SSD

For this effort we will be using the program fstrim to clear unused blocks on an SSD. This will allow us to avoid using the discard option in /etc/fstab. We’ll do this because the discard option in /etc/fstab is very slow for deleting large numbers of small files.

First open a terminal and test fstrim.

sudo fstrim -v /

This should produce an output simething like:

/: 3875201024 bytes were trimmed

In other words 3.6 GiB were processed by fstrim. Note: this does not necessarily mean mean that 3.6 GiB needed to be trimmed. It means that 3.6GiB were examined and any blocks that needed trimming were trimmed.

Assuming fstrim worked, continue and create a daily cron job to schedule trimming. So:

gksudo gedit /etc/cron.daily/trim

and enter the following:

#!/bin/sh
LOG=/var/log/trim.log
echo "*** $(date -R) ***" >> $LOG
fstrim -v / >> $LOG
fstrim -v /home >> $LOG

The last two commands perform the actual trimming for my root (/) and home (/home) partition and log the results in /var/log/trim.log. You may need to edit these commands to fit your specific case. If you don’t have a separate /home partition you’ll need to delete the last line. Or if you have additional partitions to trim, you’ll need to add additional lines for those partitions.Once you’ve made the needed edits, if any, save the file and close gedit. Then make the file executable using the following command:

sudo chmod +x /etc/cron.daily/trim

If there are:

no encrypted partitions on your SSD (Step 2 below).
your system is routinely running (not suspended) at the time cron.daily is run (Step 3 below), then you are done for now. Do step 4 tomorrow to make sure your script is working properly. Otherwise there’s a bit more to do.

Step 2: Making the script work with encrypted filesystems.

If you’re using one or more encrypted partitions:

gksu gedit /etc/default/grub

And add the following options under GRUB_CMDLINE_LINUX=”” (it should be around line 12 in that file):
“allow-discards root_trim=yes”

After editing the file, it should look like this:

GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
GRUB_CMDLINE_LINUX="allow-discards root_trim=yes"

Then, save the file close gedit and update GRUB:

sudo update-grub

Next add the “discard” option for the encrypted partitions on your SSD, as follows:

gksu gedit /etc/crypttab

And add

#
var UUID=the-uuid-of-your-encrypted-partition none luks,discard

Finally, run the following command:

sudo update-initramfs -u -k all

Step 3: Making sure the trim script gets run after resuming from suspend:

If your system is regularly suspended at 6:25AM when Ubuntu executes, the daily cron will not be run. One way to to get around this problem is to edit your BIOS to wake your system up at 6:15 every day. That would work okay on a desktop or a laptop that’s always plugged in. But I elected to use a different approach which is running the trim script upon resume from suspend using.

sudo ln -s /etc/cron.daily/trim /etc/apm/resume.d/trim

Step 4: Making sure all is well

Sometime after cron.daily has been run on your system say 7:15 or later, check the /var/log/trim.log log file to see the fstrim output.

the contents should look something like:

*** Thu, 17 Jan 2013 07:03:25 -0600 ***
/: some-number bytes were trimmed
/home: some-other-number bytes were trimmed
*** Thu, 17 Jan 2013 07:06:15 -0600 ***

If there is no trim.log file then fstrim did not run. Do not continue with step 5 until you know fstrim is working.

Step 5: Removing the discard option from /etc/fstab.

If your /etc/fstab file uses the discard option to trim the partition(s) on your SSD, it’s time to remove it. Open a terminal and:

gksudo gedit /etc/fstab
and find the lines that look like:

# / was on /dev/sda1 during installation
UUID=the-uuid-of-your-root-partition / ext4 noatime,discard,errors=remount-ro 0 1
#
# /home was on /dev/sda2 during installation
UUID=the-uuid-of-your-home-partition /home ext4 noatime,discard,defaults 0 2

and remove the discard option from to produce lines like:

# / was on /dev/sda1 during installation
UUID=the-uuid-of-your-root-partition / ext4 noatime,errors=remount-ro 0 1
#
# /home was on /dev/sda2 during installation
UUID=the-uuid-of-your-home-partition /home ext4 noatime,defaults 0 2

then save the file and exit.

Advertisements
  1. Here I prepared fstrim demon instead of corn
    https://github.com/dobek/fstrimDemon

    It seems to be more reliable to execute fstrim based on up time then cron table.

    • I tried your demon, but I see two issues for my system. Most important, /etc/init.d/fstrimDemon refers to !#/sbin/runscript which is not on my ubuntu 12.04 system. Second, my system has a separate /home partition and your demon appears to only support one partition.

  2. Step 5 should state: “gksudo gedit /etc/fstab” or any other graphical editor.

    Good article, helped alot for cron noobs like me.

  3. Hi bruce. I have made a semi-automatic cronjob available on github called SSDcronTRIM (http://chmatse.github.io/SSDcronTRIM/ ) which has the following features:

    * Distribution Independent script (should also work on your Ubuntu System)
    * The script decides every time depending on the disk usage how often (monthly, weekly, daily, hourly) each partition has to be trimmed.
    * Recognizes if it should install itself into /etc/cron.{monthly,weekly,daily,hourly}, /etc/cron.d or any other defined directory and if it should make an entry into crontab.
    * Checks if the kernel meets the requirements, the filesystem is able to and if your ssd supports trimming.
    * simply install it by running it once without any option and deinstall it with the -d option

    One thing it lacks is an automatic way of enabling discard when using an encrypted fs. But maybe a future version will add this. What i also like is your way on how you trim after resuming from suspend. Maybe i will also add something similar 😉

  4. Jennie Fernandez

    Adding noatime and nodiratime

    noatime and nodiratime are mount options in linux for linux file system. noatime disables atime updates on file system, and nodiratime will disables atime updates on directory system. By adding noatime and nodiratime will greatly reduce ssd load means performance gains.

    Edit fstab file
    # nano /etc/fstab

    Add noatime,nodiratime to your ssd drives or partitions, after ext4
    UUID=bef10b86-494d-41c6-aa46-af72cfba90fd / ext4 discard,noatime,nodiratime,errors=remount-ro 0 1

    http://namhuy.net/1563/how-to-tweak-and-optimize-ssd-for-ubuntu-linux-mint.html

  1. Pingback: SSD trimming as a cron job | slackalaxy

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: