Radu Cotescu's professional blog

g33k w17h pa45510n

Migrating Your Ubuntu Machine to a SSD Drive

SSD drives have finally become affordable and more reliable than they used to be. Partly this was sure helped by the price boost of the traditional hard-disks manufactured in Thailand (I think everybody still reminds the floods from the summer of 2011). The competition helped the development of innovative solutions and after 4 years since their mass-consumption breakthrough they have managed to become a standard on ultrabooks and premium laptops.

Because the price / GB of the classic mechanical hard-disk was so low (as low as US$0.05 per GB for 3.5 inch drives and US$0.10 per GB for 2.5 inch drives), producers started to offer larger drives for the same money. Unfortunately SSD technologies cannot and probably will not evolve the same way due to memory degradations once the chip size decreases. In particular this led to higher prices for the same disk size, making SSDs more attractive for boot / OS drives instead of plain storage ones.

Migrating a current OS installation usually has to take into consideration that the SSD destination disk might not be able to fully accommodate the previously stored data. The following paragraphs will describe what solutions can be employed in order to successfully migrate an Ubuntu installation (or any other Linux distribution) to a SSD drive.

How To

Depending on the available storage space of the SSD, there are two available solutions:

  1. the destination drive is just as big as the source drive, or larger – ddrescue

  2. the destination drive is smaller than the source drive – rsync, gparted, boot-repair

Tools needed for both solutions:

  1. an Ubuntu live CD / live USB stick from which to boot and perform the migration;

  2. at least two SATA drives, one for the source and one for the destination disk;

  3. at least two SATA data cables and two SATA power cables if you’re using a desktop machine to perform the migration, or a SATA to USB adaptor if you’re using a laptop;

  4. a Philips screwdriver to mount / un-mount the hard drives and possibly the 3.5” adaptor.

It’s time to connect the hard drives to your machine.

1. Same size or larger SSD

If the SSD you have installed on your machine is the same size or larger than your current mechanical drive it means that you’re rich. Furthermore, the work you have to do to migrate your system to the new drive does not require too much effort. ddrescue is a great tool for restoring information from failing drives but it can also be used to perform bitwise clones.

To start migrating your system from one drive to the other, perform the following steps:

  1. boot your machine using the live CD / USB stick;

  2. enable the “universe” repositories on Ubuntu and install gddrescue:

     sudo apt-get update && sudo apt-get install gddrescue
    
  3. use gddrescue to clone your drive:

     sudo ddrescue -v --force /dev/sda /dev/sdb
    

    where /dev/sda is your source drive and /dev/sdb is your destination drive

  4. have patience; depending on your disk size and your machine’s performance this might take a while.

At the end you should be able to seamlessly replace the old spinning drive with the SSD and just enjoy the snappiness of your system.

2. Smaller SSD - the usual compromise

Trying to clone a larger disk to a smaller one using ddrescue is not trivial, even if the total used storage space would be less than the SSD’s storage capacity. In this case three software tools of the *nix Swiss Army knife have to be used:

  1. gparted – for setting out the partitioning layout;

  2. rsync – for selectively synchronising the needed data (you can easily decide what to exclude using patterns);

  3. boot-repair – to fix your Grub2 setup.

Partitioning with gparted

Boot your system using your live Ubuntu image. Using gparted (it is included on the live image) create the desired partitions on your SSD. Currently only 2 Linux file systems properly support SSDs (making use of native TRIM): btrfs and ext4. Out of these two, only ext4 is considered mature enough to be used on production-ready systems, due to its mass-adoption in the Linux world.

In order to align your partitions with the drive’s sectors (for optimal performance and longevity) make sure to leave the first MiB of storage unallocated and to align your partitions to MiB, not cylinders. This is fairly intuitive with gparted.

Remember to add the boot flag to the partition where your /boot folder will be stored.

Syncing with rsync

Once your SSD drive is partitioned, mount the source and destination partitions. This can easily be done from Nautilus, the default file browser from Ubuntu. Just open the home folder and on the left sidebar you can see the unmounted file systems. Click on them to let the system mount them automatically. I strongly recommend to mount them two by two (1 source, 1 destination) in order to avoid any complications. Using rsync selectively sync your partitions:

    sudo rsync -a -v --exclude "excluded_folder_name_or_pattern" /media/<source_uuid>/ /media/<destination_uuid>

Restoring grub

Unfortunately rsync will not be able to handle grub, thus you will not be able to boot your system. Restoring grub is very straight-forward. Again you will need the live CD / live USB stick to boot up your machine. After Ubuntu is up and running from the installation medium, you just need to install and use Boot-Repair. A very good guide is available here.

Specific mount options for SSD

Because of the way SSD drives work, some mount options need to be specified in your /etc/fstab file:

  • discard – this enables TRIM;
  • noatime – this disables pointless writes to disk to store the last accessed time for a read file

Your fstab file should look more or less like this:

1
2
3
4
5
6
7
8
9
10
# /etc/fstab: static file system information.
#
# Use 'blkid -o value -s UUID' to print the universally unique identifier
# for a device; this may be used with UUID= as a more robust way to name
# devices that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
proc            /proc           proc    nodev,noexec,nosuid 0       0
UUID=0765430a-83ae-4530-b1b1-a976cd72ce16 /               ext4    discard,noatime,errors=remount-ro             0 1
UUID=263bba00-32f4-4bea-966b-ab7ae1fc3e8b /home           ext4    discard,noatime,errors=remount-ro,user_xattr  0 2

Conclusions

Migrating from your slow mechanical hard drive was easy. I am pretty sure that if you have not used a SSD drive until now you will definitely question yourself why it took you so long to switch - I know I do. Sometimes we fail to remember that the hard-disk is still the slowest component in a computing system. A SSD might be the solution to speed up your machine instead of buying the newest and coolest CPU and it’s definitely cheaper as long as you can accommodate with less storage space for the drive that should store your OS and applications (and maybe your media workspace).

How To, Linux, Ubuntu

« Why awk is better than cut Switching to Mac from Ubuntu »

Comments