# How do I optimize the OS for SSDs?

What steps should be taken before/during/after installation of Ubuntu on a Solid State Drive to optimize performance and ensure maximum durability of the drive?

-

I have successfully used several different techniques to improve the way Ubuntu uses the storage device, whether that be solid state or traditional drive.

For SSD's you are looking to minimise the number of times the drive is written too, as reads should not add wear to the drive.

1) Manage the swap file

If you do not hibernate your computer and you have ample RAM memory to run all your applications, then in theory you do not need a swap partition.

If you have a mix of SSD and hard drives, place your swap partition on the hard drives only.

2) No Writes for Read Timestamps (suitable for SSD's and hard drives)

Mounting your partitions with the options noatime and nodiratime will stop timestamp writes when you read files and folders. These timestamp writes are not generally required unless you use a local mail server client such as mutt.

Edit your /etc/fstab configuration file (carefully - take a backup to be sure as breaking your fstab configuration can prevent you system from working):

cp /etc/fstab ~/fstab-backup
gksudo gedit /etc/fstab


Edit the mounting options for your partitions by adding the text noatime and nodiratime to the lines defining your root (/) and other partitions if you have them (/home) - Note: if you have a /home partition, start with that just changing that partition if you are concerned about breaking something

# / was on /dev/sda2 during installation
UUID=587e0dc5-2db1-4cd9-9792-a5459a7bcfd2 /               ext4    noatime,nodiratime,errors=remount-ro 0       1

# /home was on /dev/sda3 during installation
UUID=2c919dc4-24de-474f-8da0-14c7e1240ab8 /home           ext4    noatime,nodiratime,defaults        0       2


You will need to reboot your machine before these changes take effect

3) Minimising writes from the OS and applications

Assuming that you are not running a mission critical product server, most people do not look at logs should something go wrong (especially as serious errors are rare for most Ubuntu users). Therefore you can configure Ubuntu so all logs get written to RAM memory rather than the SSD.

Note: only make the following changes when you have installed all software you are going to use (especially things like Apache web server), otherwise you may experience some issues with missing directories in /var/log

For background to this approach, see prolonging the life of your flash drive on ubuntu-eee.com

Open /etc/fstab with an editor (assuming you have backed up the /etc/fstab file)

gksudo gedit /etc/fstab


Add the following lines at the end of the fstab file and save:

# Uncomment these after all server based applications installed - eg. apache
#tmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0
#tmpfs /var/tmp tmpfs defaults,noatime,mode=1777 0 0
#tmpfs /var/log tmpfs defaults,noatime,mode=0755 0 0
#tmpfs /var/log/apt tmpfs defaults,noatime 0 0
# none /var/cache unionfs dirs=/tmp:/var/cache=ro 0 0


You will need to reboot your machine before these changes take effect

-
Nice find, JR0cket. I used to contribute to Ubuntu-eee, before it became EasyPeasy. Ramvi was a gentleman. wiki.geteasypeasy.com/… –  Scaine Feb 3 '11 at 22:18
About the last bit for logs and stuff, the tmpfs lines are commented, so why would adding those lines make any difference? Do we need to add it uncommented? –  Oxwivi Sep 1 '11 at 17:50
I can understand if this is meant to improve speed, but most of what you wrote seems intended to improve SSD life. Isn't it the case that with modern SSDs these improvements are pointless? And at the expense of more RAM usage! (for example, see the link given in this other answer) –  Chan-Ho Suh Apr 23 '12 at 23:30
There's no need for 2). relatime does the job of preventing writes very well and it's been active by default since kernel 2.6.30. –  Mihai Capotă Nov 21 '12 at 14:02
Just to add to @MihaiCapotă's comment there is a Server Fault answer with more details on why noatime is not needed. –  Cas Jan 8 '13 at 12:20

### SSD Life

Generally I wouldn't bother - the worries about SSD life are overblown. You can read this detailed article about why you really shouldn't worry. In short the circuitry inside modern SSDs manages wear-levelling for you, and they know how to do it far better than you.

In the article is a calculation of the life of an SSD that is receiving writes at a continuous rate of 80M/s. The life is 51 years. That is based on 2007 technology - SSD life will be longer now. And you almost certainly don't write to your SSD at 80M/s 24 hours a day.

### SSD Performance

However performance degradation over time can be a problem, and TRIM is the solution. There are two options

• manual TRIM

You have to enable automatic TRIM yourself . (Basically you add the discard option to your mount options, provided you are using ext4.) I have found a blog post reporting that the discard option slows down your system when deleting files.

You can occasionally do it manually (or in a cron job) using fstrim. If you just have one partition then all you need to do is:

sudo fstrim /


Note that fstrim is only available in 11.10 and newer. For older systems you will need the wiper.sh script. I found the script at /usr/share/doc/hdparm/contrib/wiper.sh.gz on my system.

If you're wondering, the problem that TRIM solves, as described by Wikipedia, is:

SSDs store data in flash memory cells that are grouped into pages, with the pages (typically 4 kB each) grouped together into blocks (typically 128 pages per block, totaling 512 kB). NAND flash memory cells can only be directly written to when they are empty. If they are considered to contain data, the contents first need to be erased before a write operation can be performed reliably. In SSDs, a write operation can be done on the page-level, but due to hardware limitations, erase commands always affect entire blocks. As a result, writing data to SSD media is very fast as long as empty pages can be used, but slows down considerably once previously written pages need to be overwritten. Since an erase of the cells in the page is needed before it can be written again, but only entire blocks can be erased, an overwrite will initiate a read-erase-modify-write cycle: the contents of the entire block have to be stored in cache before it is effectively erased on the flash medium, then the overwritten page is modified in the cache so the cached block is up to date, and only then is the entire block (with updated page) written to the flash medium. This phenomenon is known as write amplification.

-
I wish I could vote multiple times. THIS would be one of those answers. That link has solved a worry I have had for a long time. Many thanks Hamish. –  Luis Alvarado Jun 6 '11 at 14:48
Interesting article from storagesearch.com. I wish it would give a date! So does the above answer mean that SSD owners don't need to bother with suggestions in the first answer, with the exception of TRIM? I don't have much need for file access times, but with 2G of memory, having a swap partition might still be useful when running some photo editing software along with several other memory-intensive programs like Chrome. –  lsiden Dec 17 '12 at 2:00
@lsiden: the article does mention "Later:- in May 2008" part way through. And you are correct that you shouldn't worry about all the other stuff in the first article. Just enable TRIM and enjoy the speed :) –  Hamish Downer Dec 19 '12 at 21:41
The article refers to enterprise grade SSDs : he uses 2 millions cycles of write endurance. Consumer SSDs have about 3000-5000 cycles. A 128 GB SSD with 3000 cycles gives 57 days of continuous 80MB/s writing. –  LeartS Feb 15 '14 at 14:56

There are several points:

Alignment:

What is often pointed out is the right alignment of the partition. This should be equal to the block size of the SSD. Play safe and make your partitions aligned to MiB boundaries. Note that you can't do this with the Ubuntu installer's partition tool (which uses MB not MiB), but you can boot the live CD, use Gparted (which uses MiB), then click Install to use the partitions you set up.

The right scheduler:

A important point is the scheduler wich should be noop. You can set this scheduler via kernelparameter elevator=noop or via a entry echo noop > /sys/block/sda/queue/scheduler in you rc.local.

Mountflags:

I would recommend noatime and discard

Tmpfs

To put tmp on a ramdisk can increase the life time of the ssd. To use this put the following line in you fstab: none /tmp tmpfs defaults 0 0

Generally if you want to dive deeper into this topic I would recommend this excellent wiki-article.

-

Fast tuning course for your SSD on Ubuntu:

filesystem

Arch wiki mentions few preferable options for SSD file system - one of them is unstable, others are ext* ones. I assume ext4 is one of the best picks.
Note: In case of ext4 you may want to use discard mount option.

fstab

# <file system> <mount point> <type> <options>                                  <dump>  <pass>
proc            /proc         proc   nodev,noexec,nosuid                        0       0
tmpfs           /tmp          tmpfs  nodev,nosuid,noatime,mode=1777             0       0
/dev/sda1       /             ext4   defaults,noatime,discard,errors=remount-ro 0       1
/dev/sda2       /home         ext4   defaults,noatime,discard,user_xattr        0       2


Few important things here are:

• For systems with >=2 gigs of memory, locating /tmp in the RAM is desirable.
• No swap partition. Nowadays it's needed only for hibernation, since modern machines has pretty big amount of RAM.
• noatime and discard options. Info is here.

scheduler

Consider switching from the default scheduler, which under most Linux distro's is cfq (completely fair queuing), to the noop or deadline scheduler for an SSD. Using the noop scheduler, for example, simply processes requests in the order they are received, without giving any consideration to where the data physically resides on the disk. This option is thought to be advantageous for SSDs since seek times are identical for all sectors on the SSD.

# SSD performance tuning
echo noop > /sys/block/sda/queue/scheduler


info

-
Without swap, and with /tmp in RAM, it's very easy to get to an out of memory situation, as a lot of programs use /tmp as a storage space (for example Brasero for storing DVD images). –  arrange Dec 9 '11 at 19:19
Not really. tmpfs by default is 10% of RAM. The size can be adjusted using size option though. –  Andrejs Cainikovs Dec 11 '11 at 21:54
Brasero is a bad example, it shouldn't do that in the first place as Lennart Poettering explains in his blog 0pointer.de/blog/projects/tmp.html –  LiveWireBT May 12 '12 at 11:56

4) enable automatic TRIM

If your SSD supports it, you should also enable automatic TRIM (as described here)

-

Ok "long story short":

1. Yes. It is like a normal hdd. Here is a good overview.
2. Some special extras, that I will cover.
3. Quite good. I use it with a server.

Format as ext4 during install, and create a small swap ~1 GB. After install edit fstab with sudo gedit /etc/fstab and add the following line

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


This will create a ramdrive for your temp files, which will lower the ageing. Also add noatime,nodiratime,discard to your ext4 line after defaults. This will also lower wear, and enable TRIM function. Save and reboot.

-
Thanks a lot. I am confused because I have always used dual boot Linux and it will be first time when I will be installing Linux on a fresh SSD, so the process and storage both are new to me. but it seems that it is same as installing Ubuntu on HDD, right? –  Varun Apr 23 '12 at 22:33
It is the same. At install you might be asked to create the partition table, but that is one click. –  gajdipajti Apr 23 '12 at 22:36
Another question, Do I need to make any change to BIOS for my Dell laptop. I am going to install SSD and Ubuntu today? –  Varun Apr 24 '12 at 13:15
No, modern bios autodetect the type hard disks. –  gajdipajti Apr 24 '12 at 15:51
Thanks a lot, very helpful, will let you know what happens tomorrow :) –  Varun Apr 24 '12 at 18:34

I would not add this line to your fstab, var/tmp folder is meant to survive reboots, and that could cause issues for you.

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


When I configure new system I leave all the tmp folder commented out this way if anything happens I can check the logs and stuff. Then once I have the main system setup I will un-comment them, but I never add the above line, here is what I use:

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


And if I am having any issues with my system I comment those out to be able to check everything even after a reboot or forced reboot after a lockup.

Also you don't need nodiratime, noatime option takes care of both by itself.

As for TRIM, if your hw/sw support it, it's a must, I do not use discard in fstab. I create a daily cron, because my PC is always on, by doing this:

gksu gedit /etc/cron.daily/trim


Then add this to file and save (If you dont have a seperate /home partition on ssd, or have other parts that are on ssd you should get the idea how to modify this:

#!/bin/sh
LOG=/var/log/trim.log
echo "*** $(date -R) ***" >>$LOG
fstrim -v / >> $LOG fstrim -v /home >>$LOG


Then make the file executable by:

sudo chmod +x /etc/cron.daily/trim


I also edit my rc.local like so:

gksu gedit /etc/rc.local


Add this above "exit 0" and below the last #:

# Modification for SSD
# you may want to add more folders to be checked/created to this list
for dir in apparmor apt ConsoleKit cups dist-upgrade fsck gdm installer news ntpstats samba speech-dispatcher unattended-upgrades; do
if [ ! -e /var/log/$dir ] ; then mkdir /var/log/$dir
fi
done


You can copy and paste from here so there is no errors, to the layman it looks out of whack, it is not.

I have also read that leaving 10% of your ssd drive unformatted can help extend life, that remains to be seen. I have not read that much into this so I can't vouch if it makes sense to do so.

This is the best all around guide, he did not skim around Google for a day and then come up with a guide, you should check it out HERE

-

There are some good info How to tweak and optimize SSD for Ubuntu, Linux Mint from http://namhuy.net/1563/how-to-tweak-and-optimize-ssd-for-ubuntu-linux-mint.html you might be interested in

To install preload on Ubuntu, Linux Mint or debian based distributions

# apt-get update && apt-get install preload


To change swappiness setting:

\$ su -
# nano /etc/sysctl.conf


And add this line into sysctl.conf file.

vm.swappiness = 10

-

UPS is required.
In the file /etc/sysctl.conf
vm.dirty_writeback_centisecs = 15000
vm.swappiness = 10

Find the configuration file daemon, usually /etc/syslog.conf или /etc/rsyslog.d/ and all the paths of the form /var/log/ change by writing a minus sign ("-") in front of ways.
Before
mail.err /var/log/mail.err
After
mail.err -/var/log/mail.err

use FS btrfs and use the option "-o ssd"

-

TRIM allows an operating system to inform an SSD which blocks of data are no longer considered in use and can be wiped internally. Trimming enables the SSD to handle garbage collection overhead, which would otherwise significantly slow down future write operations to the involved blocks, in advance.1

In Ubuntu 14.04 a new feature has been added to the util-linux package that regularly trims SSDs automatically, but only Intel and Samsung SSDs have TRIM enabled by default, because some cheap SSDs can even brick themselves when running TRIM.2 The contents of /etc/cron.weekly/fstrim in Ubuntu 14.04:

#!/bin/sh
# call fstrim-all to trim all mounted file systems which support it
set -e

# This only runs on Intel and Samsung SSDs by default, as some SSDs with faulty
# firmware may encounter data loss problems when running fstrim under high I/O
# --no-model-check option here to disable the vendor check and run fstrim on
# all SSD drives.
exec fstrim-all

-

I suggest to place only those things which are read at boot time on the SSD any maybe applications which require much time to load.Data and logs and other uncritical things I would locate on a normal HDD.Also you could setup your ubuntu to only load a big initramfs from SSD at boot time and not write back changes to ssd.This has the benefit, that changes to this partition are not persistent which is sth like a protection for your boot system.Therefor you would need much more RAM of course.

I would e.g. place the partitions /, /etc, /usr, /boot, /lib 32/64 on SSD while sth like

/opt, /bin, /sbin, /root, /home and even swap (increase RAM!!!) on HDD

Wikipedia says:

The Linux kernel supports the TRIM function starting with version 2.6.33. The ext4 file system is supported when mounted using the "discard" parameter. The most recent disk utilities (and therefore installation software that make use of them) also apply proper partition alignment.

For backups there are many ways, simplest of which is (r)sync plus cron job.

-
Do you know what each of the locations are used for? All mentioned locations except for /root, /home and swap should be put on the SSD for speed because it's mostly read-only. For speed benefit, put /var on the SSD too. –  Lekensteyn Dec 9 '11 at 17:44
Depends on what you intend to speed up and how big your SSD is. 120 GB should be enough, okay, but with smaller SSD you reach borders easily. –  Michael K Dec 9 '11 at 17:47
The / partition for Kubuntu Oneiric takes 4.5GB on my SSD. 20GB is sufficient for / minus /home. The efforts for tweaking the filesystem layout, spreading it over several partitions is not worth it. –  Lekensteyn Dec 9 '11 at 18:15

It is clear that /var/tmp should not be in tmpfs since by definition it's content needs to be preserved beyond reboots:

-

How to tweak and optimize SSD for Ubuntu, Linux Mint

Enable TRIM

TRIM (Trim command let an OS know which SSD blocks are not being used and can be cleared)

Back up fstab first in case something wrong happen.

# cp /etc/fstab ~/fstab.bk


Edit fstab file

# nano /etc/fstab


UUID=bef10b86-494d-41c6-aa46-af72cfba90fd / ext4 discard,errors=remount-ro 0 1


noatime and nodiratime are mount options in linux for linux file system. noatime disables atime updates on file system, and nodiratime will disables atime updates on directory system. By adding noatime and nodiratime will greatly reduce ssd load means performance gains.

Edit fstab file

# nano /etc/fstab


UUID=bef10b86-494d-41c6-aa46-af72cfba90fd / ext4 discard,noatime,nodiratime,errors=remount-ro 0 1