Saturday, February 20, 2010

Linux SSD Optimization Guide

Solid state drives went from being the new kid on the block to being a stock option in a very short period of time. There has been a lot of speculation concerning SSDs, most of it concentrated on the reliability aspect of them. Although now it is clear that journaling filesystems aren't such a big problem as initially thought to be, you may still want to optimize your computer's operating system and limit the number of writes that the drive receives. Also, some of the optimizations that will be presented in this article only have a performance impact that is related to how the kernel treats the drive and its cache options, rather than trying to divert writes from it.

We will start by tuning the underlying filesystem, then we will subdue misbehaving applications, and after that we will alter the way that the kernel schedules disk writes and the caching method.

Filesystem options
If you are just installing the OS on your new laptop and you are concerned about the lifespan of your SSD, you might as well choose a non-journaling filesystem. You should be aware that journaling ones are more resilient to errors, but if you are using it on a laptop there are few chances of your system suddenly turning off due to an unexpected power failure. Also, you can jar and bump your drive as much as you want, the bits and bytes on the drive will stay right where they are.
Format your partition(s) with the ext2 file system if you think you don't need journaling, otherwise go with its younger incarnations, like ext3 or 4. Whichever you choose, don't forget to enable the noatime option, that will save a few writes by not updating the access time on the files that you use.
You can also apply these two modifications to an existing system, by altering the /etc/fstab file. You can only change from an ext3 filesystem back to ext2, and to do that you just change the corresponding type in the fstab file. Adding noatime option requires you to append it to the field immediately after the partition type. On my computer, the unmodified line looks like this
UUID=57ee6d85-c209-47fc-9ebc-d626dfd99df5  /  ext3  errors=remount-ro 0  1

After applying the changes, it's like this:
UUID=57ee6d85-c209-47fc-9ebc-d626dfd99df5  /  ext2  noatime,errors=remount-ro 0  1

Note that there are no spaces in the mount options field. Also, you can't go from an ext4 back to ext2, since there have been quite a lot of changes in between the two.
If you are an advanced user and you would like to use ext4 without the journal, all you need to create one is a Linux distro with a recent e2fsprogs package and the following command:
mke2fs -t ext4 -O ^has_journal /dev/YOUR_DEVICE

Less Writes
Having cut down on the amount of disk usage that the Linux operating system does just to keep your files organized and accessible, it's time to discipline the applications a little. To do that, we will go back to /etc/fstab, where we will append these lines:
tmpfs    /tmp        tmpfs    defaults    0  0
tmpfs    /var/tmp    tmpfs    defaults    0  0
tmpfs    /var/log    tmpfs    defaults    0  0

They basically move the /tmp folder and the part of /var which holds the system logs (not that important if you aren't running a server) into the ram memory and keeping them away from your SSD.
However, those folders won't be pre-populated as they would have been if they were persistent, so we need to re-create the necessary folders in order to keep various applications happy. Here is a script that takes care of that:
for dir in apparmor apt ConsoleKit cups dist-upgrade fsck gdm installer news ntpstats samba unattended-upgrades ; do
if [ ! -e /var/log/$dir ] ; then
mkdir /var/log/$dir

To make sure that the script will run every time the computer is booted, you need to add it in the /etc/rc.local file, down at the bottom, just above the exit 0 line.
Note that the above script creates the folders required by a system running the GDM desktop manager from GNOME, if you are a KDE fan, add kdm to the for dir in [...] enumeration. The same goes for any errors exposed by dmesg.

Another heavy disk drive used, believe it or not, is your web browser. It makes a cache with elements from the pages you visit, like images and scripts, that can be loaded straight from it on subsequent visits to the website. Although that may sound like a good idea, a fast Internet connection can offset the caching speed boost. So, the best way to see if caching is useful to you or not is to test your browser, both with this function on and off.
I will only show you how to toggle the cache in Mozilla Firefox, but a Google search should allow you to quickly find how to do that for other browsers. It's a pretty straightforward job: type about:config in your browser's address bar, and in the page that opens set the browser.cache.disk.enable variable to false, by double-clicking on it. Job done!

Kernel parameters tuning
Since platter-based hard drives were the most widespread storage option, and they still are, it's only natural for the kernel to be optimized with them in mind.
One of these optimizations is the disk write scheduler, or elevator. The default one used is deadline, but by switching to noop we will get better disk throughput. The way to achieve that depends on the bootloader you are using, but it is basically reduced to passing the elevator=noop parameter to the kernel at boot.
For the original GRUB, adding a parameter to the configuration and making it stick is easy: you edit the /boot/grub/menu.lst file, heading straight for the ## ## Start Default Options ## section. Lower in the file is a line that looks like this: # kopt=vga=normal root=/dev/sda2 ro (note that it might differ significantly on your system, but the # kopt= part is what you are looking for). To those parameters we will add our elevator=noop magic. On my system, I ended up with:
## ## Start Default Options ##
## default kernel options
## default kernel options for automagic boot options
## If you want special options for specifiv kernels use kopt_x_y_z
## where x.y.z is kernel version. Minor versions can be omitted.
## e.g. kopt=root=/dev/hda1 ro
# kopt=vga=normal root=/dev/sda2 ro elevator=noop

After you performed that edit, you have to ask grub to apply the new parameters, and you do that by running update-grub as root.
If you're on a newer system that employs GRUB's ext4 supporting sibling, GRUB2, you need to follow a different path of action. We'll be looking at the /etc/default/grub file, which is a great deal shorter and more clean than its predecessor's configuration. Somewhere in its upper half is the GRUB_CMDLINE_LINUX_DEFAULT= parameter. Add the elevator=noop bit to it, and remember to preserve the quotes. As usual, here's how my setup looks after the modifications:
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash elevator=noop"

Make GRUB2 aware of your modifications by running update-grub2.

One more optimization to apply: the caching method. Regular hard-drives work better with the write-through caching method, but that's not true for SSDs. Solid state drives seem to work better when another type of caching is used, namely write-back. But there is a catch: not all of them support it, but you can easily find out if it works for your drive or not. Run the following command as root: hdparm -W1 /dev/sda, replacing sda with the corresponding path for your setup (if you have more than one drive, run it for all of them, one by one, and note the results). If no error is returned, you can add the command to the /etc/rc.local file, above the exit 0 line, and it will be run each time your computer starts.

That's it. Your SSD should now have a longer lifespan, and your Linux operating system will make the most of the advantages that this kind of drive has to offer.

Original SSD image by Hellisp, licensed under CC-BY-SA 2.5.
Tux logo by Larry Ewing, Simon Budig and Anja Gerwinski.


  1. Well written, thanks!

  2. Hello guys,SSD Drives an good idea,i m using it for long is better than Conventional hard drives, i have a related url so pls visit one time and take decision!!!

  3. thanks, very useful.

  4. Good and clear guide, thanks

  5. What do you think about using relatime in place of noatime, to reduce the risk of breaking applications that use atime?

  6. Wow! Nice To See This! I would be happy to post it with the other SSD Optimization Guides if you are interested in getting some attention with it.


  7. Thanks for the good tips - not sure how I can confirm the performance increase from the TMP being moved

  8. Good info. I'll be checking out the settings on my Ubuntu machine tonight!

  9. Thanks for sharing such good information...

  10. hi, great post. seem that the loop doesn't creates the entire folder list does anybody knows why?
    tried with if and without (without the if it creates apt folder but with it, it doesn't)

  11. Before installing your new SSD make sure to use this guide and optimization tips

  12. Is the 'kopt=...' line truly still '#' commented?

  13. Another issue to consider: TRIM for SSD's

  14. Somebody here posted link to windows tricks for SSD.
    Please remove this links; for those wanting windows crap they can google it.

  15. Hi, Great article about SSD's. I'm been looking for an in depth review of the use of those disks for a lot of purposes, I'm building a nominatim/gazetteer database using postgres to do reverse geocoding. The indexing takes 33 days so far on pretty fast disks. I noticed it uses a lot of swap (which is normal given the size of the .eu dataset). I have it on 2 seperate disks now at the same priority. What is your take on using SSD's for such an application(postgres), and also: Do you think it's a good idea to put 2 SSD's in action to serve as superfast swap memory. This has been on my mind for while now. I would love a reply if you find the time.

  16. This was very helpful, thanks a lot! I have been running Ubuntu on my laptop with with an SSD for over 2 years now, and its as fast as ever with no issues. I just came across your article and added these in, better late than never!

  17. I just want to say that It was very good post, it helped me in finding a good affiliate, System Optimization

  18. Very nice guide. Has been a lot of help.
    Found another one

    Not as complete and easy as this one. slightly different. But with the same features.

  19. I appreciate your post !! it is wonderful !!

  20. Thank you for taking the precious time to write it up... great work!

  21. Interesting post. Ill be sticking around to hear more from you guys. Thanks! Carpet Cleaning adelaide

  22. Thanks for another essential posting. Where else could everyone get that kind of information in their normal complete way of crafting? I have a demonstration incoming week, and I am searching for such information.
    dulux painting sydney

  23. Great post, thanks to share this post with us. I hope its help people a lot to update the Linux OS.
    Website Development Company

  24. SSD manufactures (Intel, Samsung, SanDisk, Plextor, Corsair, Crucial…) often public new firmwares for their SSD to improve
    improve performance, reliability, and system compatibility, fix technical issues, bugs). Check your SSD’s manufacture often to make sure you have the latest firmware for your SSD. *** Always backup before you do anything ***

  25. I found it when I was looking for a different sort of information but I am very interested in the article, It is nice to read such kind of good posts I like your work keep it up!
    Ssd prices

  26. I am not sure the place you are getting your information, however good topic. I needs to spend some time studying more or understanding more. Thank you for wonderful information I was in search of this info for my mission.

    Digital marketing company in Chennai

  27. Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.
    ECommerce Web Design Company in India
    Outsource magento ecommerce services india

  28. Congratulations guys, quality information you have given!!!..Its really useful blog. Thanks for sharing this useful information..Thankyou For shearing This Information With Us.
    Web designer in bangalore
    ECommerce Website developers in bangalore

  29. Very Nice Blog. Really Happy to read this.

  30. काश आपका काम सफल होता और अधिक रोचक लेख होते।

    cần mua chó Poodle

    cách nuôi chó Poodle

    đặc điểm chó Poodle

    Nguồn gốc chó Poodle