5 crucial optimizations for SSD usage in Ubuntu Linux

msataI bought my first SSD more than 5 years ago (late 2007), for my white MacBook Core2Duo 2.0 Ghz. It may be needless to say that my MacBook ran faster than many MacBook Pro’s at that time. The look on peoples faces when they saw my MacBook boot faster than their MacBook Pro was unforgettable.

By that time I was also experimenting with running 2 x 8GB CompactFlash with SATA converters in RAID-0 mode in my completely silent (but old and under-clocked) Pentium 3 Linux desktop. When people would visit my home they couldn’t believe my PC was actually turned on: it was dead silent.

Even today I still enjoy building low-power, silent and ultra-fast (Linux) computers with an SSD as the primary drive. Nowadays the new mSATA standard (see picture) is being used. During the past years I learned a thing or two about configuring Linux on a SSD drive. Today I will share those things with you:

1) Buy more than enough RAM

Yep, you should have at least 16GB of RAM. I know you’ll say that you don’t use that, but that is exactly the point. If you would be using it, it would not be enough. And why not? RAM is relatively cheap these days and you don’t want your system to start swapping, since this will wear out your expensive SSD.

2) Use the default partition layout

Contrary to popular belief, the default (guided) partitioning will do. If you partition manually, keep in mind that you do need a swap partition that is bigger than your RAM size.

3) Avoid using swap

You have plenty of RAM, so you could turn off swap or even create a system without swap. I have been running without swap for a year or so and never experienced significant problems (other people say the same). The most annoying side-effect is that the hibernate function is not available, since it uses the swap partition. This is why I recommend to not completely disable swap, but to reduce the swappiness of Linux. This way Linux will use all available RAM before starting to write to your precious SSD, while still allowing you to hibernate. Still, be aware when it comes to using hibernate, because it uses a lot of writes. Run this one-liner to add a permanent swappiness setting:

echo -e "vm.swappiness=0" | sudo tee -a /etc/sysctl.conf

This will only be effective after reboot.

4) Disable access time logging

Remove the access time logging on the file system. Without the “noatime” flag on your file system every read will cause a write, because the file system will update the access time. This is bad for the life-time of your SSD, since it supports a limited number of writes and this is causing significantly more writes. Edit the “fstab” to add the “noatime” flag:

sudo nano /etc/fstab

Now change “errors=remount-ro” to “noatime,errors=remount-ro”. Save the file and reboot.

5) Enable TRIM

TRIM will stop your SSD from slowing down after using it for a while. If you do not enable TRIM the writes may become slower due to the erasing of the deleted blocks. By running this one-liner you can add TRIM as a daily cron job:

echo -e "#\x21/bin/sh\\nfstrim -v /" | sudo tee /etc/cron.daily/trim

You have to make the cron job executable using:

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

It will be run every day to avoid slowing down your writes and you will hardly notice it.

More advice: monitor the expected life-time

Your disk will not run forever. It has an expected life-time and you should probably monitor it using:

sudo smartctl -data -A /dev/sda

This will output something like this:

maurits@nuc:~$ sudo smartctl -data -A /dev/sda
smartctl 5.41 2011-06-09 r3365 [x86_64-linux-3.8.0-26-generic] (local build)
Copyright (C) 2002-11 by Bruce Allen, http://smartmontools.sourceforge.net

=== START OF READ SMART DATA SECTION ===
SMART Attributes Data Structure revision number: 18
Vendor Specific SMART Attributes with Thresholds:
ID# ATTRIBUTE_NAME          FLAG     VALUE WORST THRESH TYPE      UPDATED  WHEN_FAILED RAW_VALUE
1   Raw_Read_Error_Rate     0x0000   006   000   000    Old_age   Offline      -       6
3   Spin_Up_Time            0x0000   100   100   000    Old_age   Offline      -       0
4   Start_Stop_Count        0x0000   100   100   000    Old_age   Offline      -       0
5   Reallocated_Sector_Ct   0x0000   100   100   000    Old_age   Offline      -       0
9   Power_On_Hours          0x0000   100   100   000    Old_age   Offline      -       2592
12  Power_Cycle_Count       0x0000   100   100   000    Old_age   Offline      -       258
232 Available_Reservd_Space 0x0000   100   100   000    Old_age   Offline      -       4914564640
233 Media_Wearout_Indicator 0x0000   100   000   000    Old_age   Offline      -       100

maurits@nuc:~$

At ID# 233 you see the MWI. This is a value starting at 100 and when it reaches values below 10 you should start to worry. Note that different manufacturers may have different names and numbers for this indicator.

Final advice: avoid buying TLC drives

Triple Level Cell (TLC) drives are making their entry in the market. These drives have a dramatically lower expected life-time. The life-time of a drive is measured in Program/Erase (PE) cycles or in Peta Bytes Written (PBW). Check the specs carefully to avoid buying a cheap drive, thinking you made a good deal, while actually buying a model with a lower expected life-time.


21 thoughts on “5 crucial optimizations for SSD usage in Ubuntu Linux”

  1. Update your SSD firmware

    SSD manufactures (Intel, Samsung, SanDisk, Plextor, Corsair, Crucial…) often public new firmwares for their SSD to improve
    improve performance, reliability, and system compatibility, fix technical issues, bugs). Check your SSD’s manufacture often to make sure you have the latest firmware for your SSD. *** Always backup before you do anything ***
    Use EXT4 file system

    Most modern new linux distributions default file system is EXT4. EXT4 gives you good speed and reliable for your SSD, EXT4 also supports TRIM. EXT4 is matured for SSD usage since linux kernel 2.6.28. If you have older linux distributions, you may have an option to choose EXT4 as file system, but newer linux distributions will install with EXT4 as default so you don’t need to worry about.
    Enable TRIM

    TRIM (Trim command let an OS know which SSD blocks are not being used and can be cleared)

    Back up fstab first in case something wrong happen.
    # cp /etc/fstab ~/fstab.bk

    Edit fstab file
    # nano /etc/fstab

    Add discard to your ssd drives or partitions, after ext4
    UUID=bef10b86-494d-41c6-aa46-af72cfba90fd / ext4 discard,errors=remount-ro 0 1
    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

    you can read the full article at http://namhuy.net/1563/how-to-tweak-and-optimize-ssd-for-ubuntu-linux-mint.html

  2. @Bstevens: Thank you for the correction. I corrected the post. I don’t know how “Power/Erase” slipped in there 😉

  3. After creating the trim-cron-job, you still have to make it executable, don’t you?!
    sudo chmod +x /etc/cron.daily/trim

  4. @Stephan: Yes, you are completely right! Post updated, thank you very much.

  5. While I agree on having enough RAM in order to avoiding swapping, I cannot see how you can give absolute figures like 16 GB. The amount you need does depend on the tasks you run on your PC. I have 1 GB of RAM, no swap, and never run out of memeory.

    Trimming and the “discard” option are precious. But be aware that not all SSD devices actually obey the TRIM command. Some simply ignore it.

    Default partitioning is good enough? I disagree. All the partitioning schemes I’ve seen so far have no clue (no way to learn it, actually) about the device’s erase block size, which is usually several megabytes. If you blindly accept the defaults, chances are your partition borders will fall across erase block borders.

  6. Since you already advocated more RAM – Why not use that RAM to further reduce writes to the SSD by mounting /tmp to RAM?! It also has the bonus effect of improving performance.

    Add the following to the end of your FSTAB using the commands described by @Michael Adams above. Then reboot.
    none /tmp tmpfs defaults 0 0

  7. @Richard: Thank you, great advice! Will try it out myself right away..

  8. @Nenad: Yes you may want to check whether or not the upgrade reverted some of it.

Leave a Reply

Your email address will not be published. Required fields are marked *