Saturday, September 3, 2011

Optimizing Ubuntu 11.04 for SSD

I recently upgraded my Dell XPS and installed a Crucial m4 64Gb SSD. In the occasion of starting with a fresh new disk I also installed Ubuntu 11.04. Being nervous about having too many write actions on the disk (even though that shouldn't be a problem with today's SSDs) I decided to make some changes to the configuration to reduce the number of disk actions.

WARNING: Be careful when making these changes as they can result in stopping your machine from booting!

DISCLAIMER: I will NOT be responsible for any damages or an inaccessible machine! You do this at your own risk, no guarantee is given.


1. Create /tmp/ in a memory RAM-disk.

The /tmp/ directory on any linux installation is primarily used for - yes - temporary files. Programs frequently create and delete files they need for temporary caching, communicating between sub-processes and other various reasons. It is safe to move /tmp/ to ram since the OS will clean the /tmp/ directory upon boot anyway - we will save ourselves a bit of time during boot.

To make this change edit the file /etc/fstab as a root user:


john@doe:$> sudo emacs /etc/fstab


Add the following line without touching any other part of the file:


tmpfs /tmp tmpfs nodev,noexec,nosuid 0 0


Make sure there are the white spaces between the different words.


2. Mount volumes on SSD using the noatime option.

By default Ubuntu writes the last accessed time attribute to files. This means that for every file access Ubuntu writes to disk (read and write!). The noatime option forces Ubuntu to no longer write the last read/write access time to the file. Edit your fstab file by replacing relatime with noatime in fstab:


john@doe:$> sudo emacs /etc/fstab


N.B. You may find out that some applications like mail agents may no longer see that files have changed; in that case use relatime: it will force Ubuntu to write the last access time only during writes to the files and no longer during read.


3. Move Firefox cache to RAM

Firefox puts its cache in your home directory by default. By moving this cache to /tmp/ you can speed up Firefox and reduce disk writes.

Enter 'about:config' in the Firefox address bar (without the quotes). Right click and create a new string with value 'browser.cache.disk.parent_directory'. Set the value to '/tmp'.


4. Move Chromium cache to RAM

Chromium has the same problem - it's cache is in your home directory. This means a lot of reads/writes to your disk during surfing. Unfortunately it is not as easy as with Firefox to move the cache to a different directory. In the following steps I will outline how I solved this on my machine. At this point I assume /tmp is in memory and not on disk (see the first change).

Create a directory in /tmp/ for chromium's cache
(as a normal user)

john@doe:$> mkdir -p /tmp/chromium


Make a symbolic link from the users home dir to /tmp/chromium (check man ln to see the syntax and use of the command)
(as a normal user)


john@doe:$> rm ~/.cache/chromium/Default
john@doe:$> ln -s /tmp/chromium ~/.cache/chromium/Default


Now we are going to prepare the machine:

1) create the directory /media/cache (once)
2) on startup (boot) copy the cache in /media/cache to /tmp/
3) on shutdown copy the cache from /tmp/chromium to /media/cache/


john@doe:$> mkdir -p /media/cache


Now we are going to create a file in /etc/rc6.d that will be executed upon shutdown of the system. It is called K02chromium. The first letter K stands for Kill. The two digits that follow are used for determining the order in which the scripts in that directory are executed during shutdown. The higher number gives a higher priority. The text after that is free and can be anything.

Do the following as root (sudo -i)


john@doe:$> sudo -i

root@doe:$> emacs /etc/rc6.d/K02chromium


Insert the following snippet:

#!/bin/bash

# use rsync to synchronize the temp dir with the backup dir (/media/cache)
# options: a all, r recursive, P progress, o keep owner
# --delete removes any files from the destination that are no longer there
rsync -arPo --delete /tmp/chromium /media/cache/


As root make the file executable:

root@doe:$> chmod +x K02chromium


Modify the file /etc/rc.local. This file is executed upon boot. Insert the following lines before 'exit 0':


# To make sure on startup the cache is copied to /tmp add to /etc/rc.local the following
# use rsync to synchronize the backup dir (physical) with the temp dir (also physical - do not use link)

rsync -arPo --delete /media/cache/chromium /tmp/


Save the file and your done!