Friday, February 01, 2013

Linux SSD setup and tuning

Just a few notes as I move to a new SSD.  Setting it up correctly from the beginning should help drive performance and longevity.

In /etc/rc.local, add
hdparm -W1a0A0 /dev/sda


Partition alignment

This can only be done with a clean system before you install either Linux or Windows. Partition alignment is critical for SSDs as, being memory-based devices, data is written and read in blocks known as pages. When partitions aren't aligned, the block size of filesystem writes isn't aligned to the block size of the SSD, causing extra overhead as data crosses page boundaries.

Aligning partitions is simply a matter of ensuring the first partition starts on a clean 1MB boundary from the start of the disk, ensuring whatever block size the filesystem uses will align with the block size of the SSD (which can also vary). If you create partitions using Windows 7 on an empty drive, it will start partitions at the 1MB boundary automatically.

In Linux, simply run 'fdisk -cu (device)' on the drive you want to partition, press 'n' for new partition, 'p' for primary and enter a start sector of at least 2,048. The general rule is that the starting sector must be divisible by 512, but to cater for all variations of SSD page size and filesystem block size, 2,048 is a good idea (and equates to 1MB).

*Note -- if you use LVM on a bootable partition, grub2 needs to put more code at the beginning of the drive, so you might want to leave even more headroom before the first partition.


For Ubuntu and other distributions using GRUB2, edit the /etc/default/grub file and add 'deadline' to the GRUB_CMDLINE_LINUX_DEFAULT line like so:

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash elevator=deadline"

Then run 'sudo update-grub2'.


Any applications that write excessively to a hard drive are also candidates for moving data. Browsers are a fine example of this -- the browser cache is nice, but it'd work just as well from a spinning platter drive and save your SSD from thousands of writes a day that don't make a huge difference to you.

To move the cache in Firefox, in the browser type 'about:config', right-click anywhere and select New --> String, and add 'browser.cache.disk.parent_directory'. Edit the variable and point it to a directory on a non-SSD drive or, if you don't mind losing the cache between boots and you're using the tweaks above, point it to /tmp for a super-fast memory cache.

Moving the cache in Chrome is a little harder. The directory is hardcoded, but you can use symbolic links to point it to a directory on another drive or to /tmp. You'll find the cache under ~/.cache/chromium. You could also redirect the entire .cache direct
ory, as many programs use this for caching data.


Using tmpfs to reduce drive usage

 tmpfs                /ramdisk             tmpfs nodev,nosuid,noatime,mode=1777,size=100M 0 0 
 tmpfs                /tmp                 tmpfs defaults,noatime,mode=1777   0  0 
 #tmpfs               /var/spool           tmpfs defaults,noatime,mode=1777   0  0 
 tmpfs                /var/tmp             tmpfs defaults,noatime,mode=1777   0  0
 #  .thumbnails files are never deleted and can grow quite large over time.
 tmpfs                /root/.thumbnails    tmpfs defaults,noatime,mode=1777   0  0
 tmpfs                /home/mario/.thumbnails    tmpfs defaults,noatime,mode=1777   0  0

*Note -- I removed the /var/spool mount point because it messes up user cron jobs and mail server services.


This only applies if the swap file is moved to the SSD. The swappiness value affects the activity of the swap file. If you placed the swap file on the SSD you should reduce the swappiness of Linux. There are many good explanations but simply put: Linux tries to anticipate when something might need to be swapped and does it anyway. By reducing swappiness Linux is less likely to do this so swap writes are reduced. Swappiness is a value from 0 to 100, the higher the more swappiness. You can check your current value using:
   cat /proc/sys/vm/swappiness
There are a couple of different ways to change swappiness permanently. I changed this system from it's default of 60 to 1 by adding the following to /etc/sysctl.conf:

No comments: