SSD tips and tweaks

Hard drive

Endangered technic. photo by Patrick Lindenberg

SSD work fine out of the box and most optimisations aren’t really necessary, but I think these are useful if you want to increase the life-time of your brand new SSD.

Swap on SSD

If you have enough RAM that you don’t need swap (and you don’t need to hibernate instead of suspend) it might be good idea to disable swap completely. To do this, we can just comment out the line for swap partition in /etc/fstab'(example on my machine):

UUID=a495548e-1d7e-464b-8072-bdc43d07d3a5 none swap sw 0 0

Just add “#” in the beginning of the line:

#UUID=a495548e-1d7e-464b-8072-bdc43d07d3a5 none swap sw 0 0

If you don’t have too much RAM or you need to hibernate your machine, better action is just to reduce the swappiness. This way the swap is used only when necessary. Add this line to your /etc/sysctl.conf:

vm.swappiness=1

Root directory on SSD

This is where you will notice the most benefit over spinning disk. This is where you can reduce writes as well as on root directory you probably won’t need access time logging (some applications might need it on home directory) which would just create a lot of unneeded writes. Let’s edit out /etc/fstab again and add ‘noatime’ flag to line for our root directory:

UUID=bbc3a677-386a-40cb-b89c-fdf2ceb97cab / ext4 noatime,errors=remount-ro 0 1

Deadline I/O scheduler

By default Linux uses the CFQ I/O scheduler which is desirable for spinning disks, but it isn’t optimal for SSDs. For SSDs I would recommend using the Deadline scheduler. First find out which disk on your machine is SSD:

$ grep . /sys/block/sd?/queue/rotational
/sys/block/sda/queue/rotational:1
/sys/block/sdb/queue/rotational:1
/sys/block/sdc/queue/rotational:1
/sys/block/sdd/queue/rotational:0

Look at the number at the end of the line. This shows the /dev/sdd is SSD on my machine as “rotational is false”. Find out the current scheduler on this disk:

$ cat /sys/block/sdd/queue/scheduler
noop deadline [cfq]

Oh okay! Let’s set it to deadline by adding this line to /etc/sysfs.conf

block/sdd/queue/scheduler = deadline

It should apply on next reboot. To set the scheduler without rebooting (this change will reset unless you set the scheduler in /etc/sysfs.conf):

# echo deadline > /sys/block/$YOURDRIVE/queue/scheduler

Now it should look like this:

$ cat /sys/block/sdd/queue/scheduler 
noop [deadline] cfq

TRIM

There are several ways to do TRIM on Linux, but some of them (discard option fstab) might damage your SSD if it has buggy firmware. Using fstrim should generally be safe on all SSDs. To set fstrim to TRIM your SSD regularly first save this script:

#!/bin/sh
fstrim -v /

to /etc/cron.daily/trim or /etc/cron.weekly/trim depending whether you want TRIM done daily or weekly. On my machine it is done weekly, but if it isn’t often enough for you – do it daily.