www.leniwiec.org

Two days ago I replaced my hard drive in laptop with a new solid state drive, and switched to the new Ubuntu 14.04LTS release. As everyone knows, frequent write operations slowly kill that kind of drives. Below I would like to show you a few tips how to tune your ubuntu to be more ssd-friendly, and enjoy the long life of your expensive drives. ;)

  1. Turn off swapping
    Nowadays RAM memory is cheap, 8 or 16Gb of installed chips are not any kind of luxury. Even buying more chips and installing them is not any sort of budget-ruining operation, so basically everyone can afford it and turn off disc swaping, which generates tons of write operations on it.
    Tu turn off swaping, or drastically limit it, we need to change a system parameter which determines how high system load should be to enable disk swaping. The smaller this number is, the more load is needed to start swaping. You can check your current setting by typing in your console something like this:

    cat /proc/sys/vm/swappiness

    To change this setting permanently, you need to type at the end of /etc/sysctl.conf file:

    vm.swappiness = 1
    vm.vfs_cache_pressure=50

    If you change the value in the first line to 0, you will turn off swaping completely (not only limit it as in the example above).

  2. Turning off access time stamp(information that is written every time you access a file on your drive).
    In order to do this, you need to add a noatime parameter to each mounted drive in /etc/fstab file. It should look similar to this example below:

    UUID=86a5fc7a-efb8-41a7-b208-23827fc8fe4f / ext4 noatime,errors=remount-ro 0 1

  3. Turn on TRIM
    Every SSD drive should be regularly cleaned (trimmed). If a disc is not trimmed, in the long run it starts to work slowlier and slowlier… To do so, we will use the fstrim command. In order to trim your disc manually, you need to type in your console:

    fstrim -v /

    which causes trim for the main file system. If you have your disc partitioned to more than one partition, you have to run the trim command for each of them. In my case, I have two partitions – one mounted as root / and second one as /home, it looks like this:

    fstrim -v /
    fstrim -v /home

    I would recommend adding this command into /etc/rc.local file, so it will run each time you turn on your computer.
    If your computer is always on (e.g. server), you can add these commands to a cron-job. In order to do so, you will need to edit the /etc/crontab file, and add lines similar to:

    10 12 * * * root fstrim -v / && fstrim -v /home

    These lines mean that we will run fstrim command for / and /home, every day at 12:10 am.
    On my laptop I assigned this command to the cronjob, because I want to boot my system faster, and 12:10 am is a time, when I almost always have it turned on. So trim will be proceeded every day ;)

  4. Do not use hibernation on your system
    Yes, hibernation causes tons and tons of write operations onto your drive, so be aware of it.
    For me, it was not a big deal, because my laptop is now booting in something around 3 or 4 seconds, so it is totally acceptable for me not to hibernate it. ;)

  5. Do not run defragmentation process on your SSD drive! This will kill your drive!
  6. Move your temporary files (/tmp) and system logs (/var/log) to a virtual file system stored in RAM.
    We can create a virtual drive in the operational memory and mount it at boot time into /tmp and /var/log. By doing this, we take some write load off of the ssd drive. The one and only failure of this solution is that we will lost all system logs every time we reboot our machine. But hey! Who cares about logs on workspace, right ? ;)

    What we need to do is to add the lines below into the /etc/fstab file:

    tmpfs /var/log tmpfs defaults,noatime 0 0
    tmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0

    Next, to restore directory structure at every boot, we need to add to our /etc/rc.local the following code (at the end of the file):

    for dir in apparmor apt cups dist-upgrade fsck gdm installer samba unattended-upgrades ;
    do
    if [ ! -e /var/log/$dir ] ; then
    mkdir /var/log/$dir
    fi
    done

  7. Limit FireFox cache writes
    To do so, you need in your FireFox go to Edit->Preferences->Advanced then Tab network and set Cached Web Content to 0.

There are lots of information on the Internet about tuning your system to use the ssd drives, but most of them are simply going too far (level obsession;). I think that doing a few simple tweaks in your system, which will eliminate the main sources of heavy write-operations, is enough to be able to happily enjoy the long life of your SSD drive. ;-)

7 COMMENTS
Piotr
25 June 2014
ad

Z /var/log trzeba uważać, bo po jakimś czasie się okazuje, że jakaś usługa nie startuje, a człowiek zapomina, że /var/log (także /var/spool, jak niektórzy proponują) jest w tmpfs.
Miałem tak z jedną usługą, nie pamiętam, czy było to couchdb, mysql czy apache.
Gdyby skrypty startowe sprawdzały i tworzyły te katalogi, byłoby ok, niestety nie wszystkie to robią.
Ja wróciłem do /var/log na dysku.

leniwiec
25 June 2014
ad

@PIOTR na desktopie, a o desktopach mowa, nie ma to większego znaczenia. Poważnego serwera, mimo wszystko bym na ssd chyba jednak nie stawiał ;)

TyGrys
26 January 2015
ad

Cześć Piotrze :) jestem początkującym “linuksiarzem” czy miałbyś jakieś porady dotyczące konfiguracji systemu jeśli w laptopie mam zainstalowany dysk SSD i HDD w miejscu cd-rom? czy mogę wtedy partycję swap i home utworzyć na HDD?
W takiej sytuacji SSD 64 GB chyba w zupełności wystarczy?

Pozdrawiam

leniwiec
29 January 2015
ad

TyGrys: To co piszesz jest rozsądne. Najlepiej na HDD wsadzić /home, /var i /tmp a całą resztę czyli / na SSD, wtedy te 64Gb to nawet zbyt wiele :)

29 April 2015
Tomasz Root
1 January 2017
ad

Prócz włączenie TRIM, reszta jest bezcelowa. Przykładem są serwery na dyskach SSD, gdzie gwarancje sięgają ~10 lat. Jestem w posiadaniu dysku na PCIe x4 z gwarancją na 10 lat.

leniwiec
1 January 2017
ad

@Tomasz Root, zgadza się, z tym że dwa lata temu, tanie dyski z segmentu konsumenckiego były po prostu słabe, i te wszystkie zabiegi miały większy/mniejszy sens.

Post comment