Tuesday, February 05, 2013

Moving Linux to a new hard drive

Move your files

This assumes that you've already partitioned and formatted all partitions on the new drive. Remember to mark the boot partition as bootable.

Mount the new hard drive and sync all files:

sudo -s
mkdir -p /mnt/newdrive
mount /dev/sdb1 /mnt/newdrive
rsync -avx / /mnt/newdrive/
# repeat for other mountpoints as appropriate

Change the UUID

Now you'll need to edit all config files that use the hard drive's UUID (like /mnt/newdrive/etc/fstab) and change all UUID entries to match the new drive.  In vim, this handy command will give you a headstart:

:r !ls -l /dev/disk/by-uuid

Install bootloader

For the next step I like to physically install the drive and boot up from a live Linux CD.  Once you're in, run:

sudo -s
mkdir -p /mnt/newdrive
mount /dev/sdb1 /mnt/newdrive
chroot /mnt/newdrive /bin/bash
grub-mkdevicemap
grub-install /dev/sda
update-grub2
update-initramfs -c -k all
exit

Wrap it up, stick a bow on it

Cross your fingers and reboot!  If it doesn't come up, check your partitions, making sure the correct one is marked bootable, and verify that your fstab entries are all correct.  Remember that Ubuntu-based systems might hang for over 2 minutes on boot if networking isn't available, so be patient.

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


From http://apcmag.com/how-to-maximise-ssd-performance-with-linux.htm

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.

Scheduler

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'.

Applications

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
Applications
ory, as many programs use this for caching data.


From http://wiki.freeswitch.org/wiki/SSD_Tuning_for_Linux

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.

Swappiness

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:
   vm.swappiness=1
   vm.vfs_cache_pressure=50