Search

Ubuntu - Tweaks for SSD drive

Contents[Hide]

dropcap-ubuntu-ssd

Nowadays, SSD disks are more and more common, especially for laptops, as they are by nature shock proof.

Even if they don't offer the same price per Gb than standard hard disk, they provide a fantastic performance boost and are more and more affordable.

But, even if it gives a huge boost, a SSD disk is not behaving like a normal hard disk with rotating platters. Their main characteristics are :

  • their very fast read and write speed
  • their neutrality to random access
  • their limited write cycles

This article explains how to tweak a Ubuntu computer to take the full benefit of a SSD drive and to preserve it at the maximum against unneeded write accesses.

This has been tested on Ubuntu Precise 12.04 AMD64, but it should be applicable to any debian derived flavour and even to any Linux distribution for most of it.

Some of these tweaks have been adapted from a very good ArchLinux article.

1. Adjust Drive Mount Parameters

Ubuntu use the relatime flag for updating file metadata when files are accessed. As these updates involve some write accesses and have almost no interest on a desktop, we can replace it with noatime parameter. With this parameter, access time to files won't be tracked at all.

Linux supports TRIM, which is important for maintaining the performance of an SSD over time as files are added, deleted and changed and lets the SSD know which blocks can be safely cleared. No distributions currently enable it by default, but it's simple to do by adding the discard flag to the SSD mount options. This is applicable only for Ext4 filesystems (not Ext2 or Ext3).

To apply these tweaks, the only thing to do it to edit /etc/fstab and to add them as option to the SSD drive.

# sudo gedit /etc/fstab

/etc/fstab

# / was on /dev/sda1 during installation
UUID=2c7d95eb-6dcc-4db8-9d2b-9aca7c3b05f1    /     ext4      discard,noatime,errors=remount-ro    0    1

2. Temporary Directories as tmpfs

If your system is having enought memory (2 Gb +), you should mount some temporary directories as a tmpfs, a ramdisk which dynamically expands and shrinks as needed.

This will reduce unnecessary writes to the SSD. A typical directories /tmp. All temporary files on your system sould be created there.

If you want to go further, you can also add :

  • /var/log : if you don't mind losing log files between boots, you can also mount this directory as a tmpfs.
    Considering that a many applications are quite verbose in their logs, it will avoid a lot of write operations to your SSD.
    If you are using services like Apache, you may have to recreate some file structure used by Apache at every boot.
  • /var/spool : this spool directory may also be candidate.
    If you are using anacron, you will have to recreate an anacron subdirectory at every boot in the directory as anacron uses this sub-directory (thanks to Ioannis).

To do so, add these lines at the end of /etc/fstab :

# sudo gedit /etc/fstab

/etc/fstab

...

# SSD tweak : temporary directories as tmpfs
tmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0

# SSD tweak : extra steps (read ahead)
tmpfs /var/log tmpfs defaults,noatime,mode=0755 0 0
tmpfs /var/spool tmpfs defaults,noatime,mode=1777 0 0

3. Change Default Scheduler

The scheduler helps organise reads and writes operations to disks in the I/O queue to maximise performance.

The default scheduler in the Linux kernel is cfq (Completely Fair Queuing). It has been designed to deal with the rotational latencies of spinning platter drives.

Some other schedulers are available : noop and deadline. The first one noop is basically no scheduler at all, it's a basic FIFO (First In, First Out) queue. In comparison, deadline does some sorting to guarantee read requests take priority over write, which is useful if you want to guarantee read responsiveness under heavy writes.

So, it becomes obvious that for SSD, we should consider switching from cfq to deadline.

The most elegant and future proof way to do this is to write a udev rule. That rule will detect the type of drive mounted (rotational or not) and apply a scheduler according to that type.

Create and edit a file in /etc/udev/rules.d named 60-schedulers.rules.

# sudo gedit /etc/udev/rules.d/60-schedulers.rules

/etc/udev/rules.d/60-schedulers.rules

# Handle the scheduler choice according to the type of disk detected

# system default : set cfq scheduler for rotating disks
ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="1", ATTR{queue/scheduler}="cfq"

# SSD specific : set deadline scheduler for non-rotating disks
ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="0", ATTR{queue/scheduler}="deadline"

After reboot, if your SSD is the main drive, you should get something like this :

# cat /sys/block/sda/queue/scheduler
noop [deadline] cfq

4. Tune Swapping Behaviour

By default Ubuntu creates a swap partition according to your memory size.

Note that most modern desktops with more than 2 Gb of memory will very rarely use swap. The notable exception is systems which make use of the hibernate feature.

The following is recommended tweak for SSDs using a swap partition that will reduce the "swappiness" of the system thus avoiding writes to swap.

The value we will setup is the percentage of free memory before the system will start to use swap. We will set it to 0 so that system fills up full memory before swapping.

# sudo sh -c "echo 0 > /proc/sys/vm/swappiness"

To make it persistent across reboot, we need to modify /etc/sysctl.conf to add that key :

# sudo gedit /etc/sysctl.conf

/etc/sysctl.conf

...
# minimize swap use to a minimum
vm.swappiness=0

5. Browsers

5.1. Firefox - Disable Anti-phishing security

The anti-phishing features of Firefox generates a big amount of data and may cause it to become slow to start or exit, and might also affect the browsing speed since Firefox apparently contacts google for every http request.

The problem is that Firefox maintains a sqlite database that might grow quite big, and reading and writing to this database begins to take some time when it grows bigger.

If you feel that you do not need Firefox to tell you which sites may be suspect you can disable this feature by turning off the following options under Edit / Preferences / Security :

  • Block reported attack sites
  • Block reported web forgeries

It can also be done under about:config page by setting these booleans to false :

  • browser.safebrowsing.enabled
  • browser.safebrowsing.malware.enabled

After closing Firefox, you need to delete all files beginning with urlclassifier in your profile folder ~/.mozilla/firefox/xxxxxxxx.default where xxxxxxxx is some combination of letters and numbers. Some of these files might be recreated by Firefox, but they won't grow any larger than their initial size.

Here is the command line to delete these files :

# rm -i ~/.mozilla/firefox/*.default/urlclassifier*

5.2. Place Browser Cache into RAM

Most modern browsers are heavily using cache to speedup navigation. Even if it is quite efficient from the user's side, it is quite heavy on the disk load side.

If you hold quite a lot of RAM, it can be a good option to place your browser's cache in ramdisk at boot, to use the ramdisk during navigation and to replace the cache on the disk during shutdown.

As a result, your navigation speed will be drastically inceased and your SSD disk writes will be limited to a strict minimum.

This is exactly what does a small tool called Profile-sync-daemon originally developped for ArchLinux by graysky.

It handles many different types of browsers (firefox, Chrome, Opera, ...).

As graysky has ported it to Ubuntu and as a PPA is now available, it is really easy to install it :

# sudo add-apt-repository ppa:graysky/utils
# sudo apt-get update
# sudo apt-get install profile-sync-daemon
...
--------------------------------------------------------------------------
Define which users will make use of the sync in /etc/psd.conf
Read the manpage before use or see the wiki page
https://wiki.archlinux.org/index.php/Profile-sync-daemon

ALWAYS backup your profiles data before using utils like psd!

--------------------------------------------------------------------------

Next step is to configure it thru /etc/psd.conf.

As a minimum, you should declare your login as candidate for cache buffering. The configuration file is quite self-explanatory.

# sudo gedit /etc/psd.conf

/etc/psd.conf

...
USERS="yourlogin"
...

Now that configuration is done, you can check what browser profile will be managed by profile-sync-daemon :

# profile-sync-daemon parse
Profile-sync-daemon v5.19

Psd will manage the following per /etc/psd.conf settings:

browser/psname: chromium/chromium
owner/group: nicolas/nicolas
sync target: /home/nicolas/.config/chromium
tmpfs dir: /run/shm/nicolas-chromium
profile size: 7.9M

browser/psname: firefox/firefox
owner/group: nicolas/nicolas
sync target: /home/nicolas/.mozilla/firefox/gqn6g8v0.default
tmpfs dir: /run/shm/nicolas-firefox-gqn6g8v0.default
profile size: 212M

Everything is ok, Firefox is in the list.

So we can now start the psd service. According to your profile size, this can take some time ...

# sudo service psd start

Your Firefox cache is now managed as a ramdisk.

It is located under /run/shm/yourlogin-firefox-xxxxxxxx.default.

6. If you get some System Freeze

In case your system is showing some type of freeze, it may come from the fact that your SSD disk is not compatible with Linux NCQ (SATA Native Command Queueing), which is enabled by default.

This should not be the case with modern SSD, but it happens with some old generation or no-name ones.

To disable NCQ,you need to add the kernel parameter libata.force=noncq.

# sudo nano /etc/default/grub

/etc/default/grub

...
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash libata.force=noncq"
...

Once modified, propagate it to grub database :

# sudo update-grub

After next reboot, these freezes should be part of the past.

 

Your computer should now be more responsive and your SSD life expectancy should have increased quite a bit.

Hope it helps.

Signature Technoblog

This article is published "as is", without any warranty that it will work for your specific need.
If you think this article needs some complement, or simply if you think it saved you lots of time & trouble,
just let me know at This email address is being protected from spambots. You need JavaScript enabled to view it.. Cheers !

icon linux icon debian icon apache icon mysql icon php icon piwik icon googleplus