We are in the process of migrating this forum. A new space will be available soon. We are sorry for the inconvenience.

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

29/12/2013, 13:25
Buenos días !

Por si se pierde la fuente...

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:

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="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 /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.

Un saludo !