An SSD (solid-state drive) is one of the best investments you can get to speed up your system. If you already have a quad core processor and four or more gigabytes of RAM, this is the next step for upgrading your system for maximum performance. Your system can only run at the fastest speed of it’s slowest component, and for most computers these days that’s the speed that the hard drive can read and write data. An SSD can really crank this up. How much improvement can you get? Well, my system went from a boot up time of 116 seconds to just eight seconds. So without further ado, here’s how to get the best bang for your buck tuning Linux for SSD performance and long life.

First off, don’t throw out your old HD! Or if you don’t have one already, you should get one for file storage; external or internal, it really doesn’t matter. You’ll need it because (1) SSDs work best in conjunction with a regular platter-based hard drive for various technical reasons and (2) for file storage. The regular hard drive will be used for downloads and similar file operations where you’ll do a lot of file writes but they don’t need to be fast, as well as for storing large files that don’t actually need fast read access. Videos, for example, can easily be read off a regular hard drive in time to play the actual footage, so there’s no advantage to storing them on your SSD. Same reasoning applies to eBooks, music, etc – pretty much multimedia of any kind. Plus SSDs are expensive compared to regular hard drives – it’s unlikely you’ll be able to afford a 2TB SSD to store your multimedia on regardless!

Backing up an existing system or installation of a new one is beyond the scope of this article, so I’m assuming you either already know how to do this or have found a good tutorial elsewhere.


Before Installing: Partitioning the SSD

First and foremost, you’ll need to partition your new SSD. There are plenty of partitioning schemes out there, so choose whatever works best for you – one giant partition, separating out boot/root/home/etc, whatever.

NOTE: If you don’t already, you should have the drive set to AHCI mode in your BIOS settings in order for the OS to be able to use TRIM automatically.

Most modern installers are bright enough to correctly partition the SSD for you; if you’re doing a fresh install, you can skip this.

However if you’re going to be writing from a back up, you’ll need to do this manually and partition alignment is important. Since SSDs are memory-based devices, data is written and read in blocks known as pages. When partitions aren’t aligned the block size of filesystem writes isn’t aligned to the block size of the SSD, causing extra overhead as data crosses page boundaries.

All this means is that the partition needs to start at the 1MB boundary from the start of the disk and ensuring whatever block size the filesystem uses will align with the block size of the SSD. Assuming you’ve got the SSD attached to an existing system or have booted from a live CD, you can this via fdisk:

$ sudo fdisk -c -u <device>

(where <device> is your SSD – eg, /dev/sda)

Then enter ‘n’ for new partition, ‘p’ for primary and enter a start sector of at least 2048 sectors. The general rule is that the starting sector must be divisible by 512; however, to cover all SSD page size and filesystem block sizes 1024 kB (ie, 1MB, or 2048 sectors) is a good idea. If you’re creating multiple partitions, be careful that your offsets and partition sizes are all multiples of 1 MB.

If you’re using a GUI tool like GParted, it should automatically adjust this for you. To be sure, you can check and manually specify the amount of free space preceding a partition in the create/resize partition dialogs to 1 MB:

Note that the “Align to:” option is set to “MiB”.
Also, I accidently entered “2” instead of “1” for the “Free space preceding” value 😛

Before Installing: Partitioning the HD

The HD is a much simpler affair. Allocate a partition for swap (more than 2 GB is likely a waste of space), and the rest can be used for a single partition for storing multimedia files and documents. If you plan to mostly store videos and the like you might want to consider using the XFS filesystem, which is optimized for large files (ie, bigger than 10MB), or having one partition for smaller files and one for large ones. This is really only a concern if you’re streaming media, though.

After Installing: Tuning Your System

Eliminate Filesystem Journal Access Time Writes

The most commonly used filesystem these days is ext4, which is a journaling file system and supports the SSD TRIM function.

Journaling means it writes the access and modification times for every file and directory every time they are touched. This will degrade your SSD over time and so reduce it’s life, and it’s a small performance hit as well. However, it’s easy to turn access time journaling off.

You’ll also need to turn on TRIM, so that Linux can properly manage overwriting data on the drive.

Using your favourite text editor as root, open /etc/fstab and change the options for all partitions on your SSD by adding noatime,nodiratime,discard (and removing relatime if it’s present).

In this example fstab boot, root, home, opt and usr are all on separate partitions on the SSD. boot is on a partition with the ext2 filesystem, which doesn’t support TRIM or full journaling, and var is on a regular HD.

# <file system> <mount point>   <type>  <options>       <dump>  <pass>
proc            /proc           proc    nodev,noexec,nosuid 0       0
UUID=cab79669-bd6a-496e-86f3-3858085afd6e /               ext4    noatime,nodiratime,discard,errors=remount-ro 0       1
/dev/sda1       /boot           ext2    noatime         0       2
UUID=57515e35-d0f3-45de-ac6a-0d2760378113 /home           ext4    defaults                           0       2
UUID=7423b14b-d542-41be-bc05-e164ee3e81dc /opt            ext4    noatime,nodiratime,discard         0       2
UUID=2ea67cda-3a87-41e2-b079-d5fe09554295 /usr            ext4    noatime,nodiratime,discard         0       2
UUID=e29dcf95-25c6-452c-9028-7acca0ba212a /var            ext4    noatime,nodiratime,discard         0       2

Note: your UUIDs will be different from the example ones. If you don’t know what they are, you can find out using the commandsudo blkid“.

It’s important to note that discard can result in performance issues when deleting a large number of small files.  As a result, some prefer to use a cron job to regularly run TRIM.  If you’re finding yourself suffering from these issues, as root open /etc/cron.daily/trim using your favourite text editor and add the following:

echo "*** $(date -R) ***" >> $LOG
fstrim -v / >> $LOG
fstrim -v /home >> $LOG
fstrim -v /opt >> $LOG
fstrim -v /usr >> $LOG
fstrim -v /var >> $LOG

You’ll need to have an fstrim entry for each ext4 partition mounted on the SSD (ie, one for every line in /etc/fstab that would have had the discard option).

Change the I/O Scheduler

The I/O scheduler optimizes disk operations for speed. The Linux default scheduler is CFQ (Completely Fair Queuing), which is optimized to work around the rotational latencies of regular hard drives. Since SSDs don’t have that bottleneck the deadline scheduler, which prioritizes reads over writes, works better. There are several ways to do this, but the easiest assumes you are using GRUB2 for your bootloader. As root, open /etc/default/grub in your favourite editor and the option”elevator=deadline” to the value for “GRUB_CMDLINE_LINUX_DEFAULT“.

This example is from an Ubuntu PC:

# If you change this file, run 'update-grub' afterwards to update
# /boot/grub/grub.cfg.

GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash elevator=deadline"

Then update GRUB2:

$ sudo update-grub2

Minimize Swap

Swap (or the “pagefile”, in Microsoft parlance) is hard drive space that is used to store information moved from RAM if it hasn’t been used for a while or if RAM is full and the currently active process needs more memory. The system will also move information to the swap file from RAM if it hasn’t been accessed for a while, in order to preemptively free up RAM for more active processes.

In order to keep information in RAM, you can set the swappiness to zero. At zero, swap will only be used when RAM is completely full. Since we’re keeping our swap file on the slow HD in order to spare wear and tear on our super slick fast SSD, we want to access it as little as possible.

NOTE:  This is only a good tweak if you have enough RAM that you never page out memory to the swap file; otherwise you may crash your machine.  If you don’t have a large amount of RAM (usually 4GB or more is enough), then set your swappiness to 10 to minimize swapping.

NOTE:  Some systems/apps may still rely on swap, and act oddly if it’s set to zero.  If you start getting strange issues, try setting your swappiness to minimize swapping.

To change your swappiness, open /etc/rc.local in your favourite text editor as root and add “echo 0 > /proc/sys/vm/swappiness” on a line before “exit 0“:

echo 0 > /proc/sys/vm/swappiness
exit 0

Move tmp Directories to RAM

Temporary system files also involve a lot of writes, and so shouldn’t be located on the SSD if it can be helped. Instead, they can be kept in memory – super fast access there! Of course, if you have less than 4 GB of RAM this won’t really be a good option. If that’s the case, keep them on the SSD as putting them on your HD would hugely slow down your system.

Some recommend putting your log files in RAM as well, which I think is a terrible idea as they will disappear after ever reboot. You generally only need them rarely for troubleshooting, but when you need them you need them badly. So despite the numerous writes they require, I recommend keeping them around on your SSD (putting them on your HD will slow down your system, much like swap).

As root, open /etc/fstab in your favourite editor and add these lines:

# <file system> <mount point>   <type>  <options>       <dump>  <pass>
tmpfs   /tmp          tmpfs   defaults,noatime,nodiratime,mode=1777   0       0
tmpfs   /var/spool    tmpfs   defaults,noatime,nodiratime,mode=1777   0       0
tmpfs   /var/tmp      tmpfs   defaults,noatime,nodiratime,mode=1777   0       0

You’ll also want to move your browser’s cache to RAM as well.

For Firefox, go to about:config and either edit or add the string entry “browser.cache.disk.parent_directory” with the value “/tmp“.

For Chrome, the cache directory is hard coded. However, you can symlink it to “/tmp” instead:

$ ln -s ~/.cache/chromium /tmp

Since many applications use “~/.cache“, you might want to symlink that whole directory. However, if you regularly edit video or large images you might run out of RAM very quickly as the caches fill up. A more conservative strategy would be to check the various cache sizes after a month and symlink them individually as appropriate.

Ready to Rock!

That’s it! Reboot, and you’re on your way with a lean, mean high performance Linux machine!

Technical Note: Why You Should Use Both an HD and an SSD

An SSD works best for files that are often read (ie, opening up a document or video, or starting a program) but not written (ie, saving or downloading a file) for two reasons:

  1. Reads are generally faster than writes (ie, saving a file) for SSDs.
  2. Writes will wear out your SSD much faster than reads compared to a regular hard drive (over a long period of time, anyway).

So basically you don’t want to download stuff onto your SSD, or use it for system temporary files or browser caches. Your old magnetic platter HD is ideal for low demand downloads and such like where disk latency is unlikely to be greater than your actual down bandwidth. And assuming you have a modern system with 6GB+ of RAM you can use RAM for temp and cache files to keep your system blazing fast without wear and tear on your SSD. 

Related Posts
Installing Webalizer and Publishing Analytics to Your Website
For Linux users hosting their own websites using Apache, the log files provide a wealth of analytics information about bandwidth usage, visitor locales, referrers & keywords, etc.  However, they're not ...
Ubuntu with GNOME has been a popular desktop Linux distro for a few years now.  However, despite a great deal of excellent work by Canonical, there are still annoyances that ...
GNOME has always been my go-to window manager on Linux, usually on Ubuntu.  However, it does have a few annoying quirks and deficiencies.  One of the most aggravating ones is ...
Recently I needed to install Asterisk with FreePBX on an existing Ubuntu 10.04 LTS (Lucid Lynx) 64 bit server.  Since Asterisk is really targeted at CentOS this was a bit ...
While Ubuntu does many things well out of the box, being able to recognize Android devices connected by USB is not one of them.  Instead the devices have to be ...
Installing Webalizer and Publishing Analytics to Your Website
Linux Tip: Permanently Disable NumPad Mouse Keys
Disable Numpad Mouse Keys on Linux+GNOME Permanently
Installing Asterisk 1.8 and FreePBX 2.9.0 on Ubuntu
Mounting your Android’s SD Card on Ubuntu