Moving Linux to an SSD

The other day I needed to move my Linux install to an SSD, but there were a few issues:

  • I was moving to a 240GB SSD from a 1TB HDD
  • I wanted the OS to be installed on the SSD and to make the 1tb drive a /home partition. When installing initially I didn’t think to use a separate /home partition (I usually do, it’s a good idea)
  • I didn’t have enough free space anywhere to make a copy of everything on the 1tb drive (I was able to make a full backup to another machine, but doing so meant that there wasn’t enough free space anywhere to use e.g as an intermediate place for doing repartitioning

Here’s how I did it:

  1. Ensure that /etc/fstab is using uuids rather than device nodes (it was, should be the default these days)
  2. Perform a full backup of the entire system
  3. Reboot into a live environment
  4. Partition and format the SSD using gparted
  5. Mount both the old and new disks
  6. Copy the system over, excluding home directories, with:
    sudo rsync -aXS --exclude=/media/old/home /media/old /media/new

    (Note: I found that using ‘v’ (verbose) in the rsync command slows things down significantly, since there are many small files and most of the time is spent outputting and scrolling text when in verbose mode)

  7. Prepare the old disk as a home partition by moving the system into a subdirectory and then moving the contents of the /home directory into root:
    mkdir /media/old/old_root
    mv /media/old/* /media/old/old_root
    (check for hidden files/folders in root and move them 
    individually with ls -a and mv)
    mv /media/old/old_root/home/* /media/old/
  8. use ‘blkid’ to find the uuid of the new disk
  9. edit fstab, adding a new entry for / using the uuid of the SSD (simply copied the ‘/’ line and changed the uuid), and changing the mount point for the 1tb drive to /home
  10. install grub on the new disk. This requires running from a chroot environment:
    for f in dev sys proc usr; do sudo mount --bind /$f /mnt/new/$f; done
    sudo chroot /media/new
    sudo update-grub2
    sudo grub-install /dev/sdb
  11. Exit chroot (CTRL-D), Reboot, enter BIOS, and change boot priority to boot from the SSD
  12. Save BIOS changes and reboot
  13. You are now running your existing Linux install, with everything (settings, installed programs, data) intact, from an SSD
  14. Now that my system is running from an SSD with my home directory on the 1TB drive, there were certain things in my home directory which I wanted to speed up by putting them on the SSD. To this end, I created an /SSD directory in the root and then moved and symlinked certain things there, so that they would appear in my home directory but really be on the SSD:
    sudo mkdir /SSD
    sudo chown -Rf username /SSD
    mv ~/Work/codebase /SSD/codebase
    ln -s /SSD/codebase ~/Work/codebase
    mv ~/workspace /SSD/workspace
    ln -s /SSD/workspace ~/workspace
    mv ~/VMs /SSD/VMs
    ln -s /SSD/VMs ~/VMs
    (repeat for anything you want to load faster)
  15. Since everything is working, clean up by removing the old_system directory (containing the system files from the 1tb disk), will be located in /home: sudo rm -Rf /home/old_system
  16. This had a huge effect on many things: the system is much much faster, it boots in less than 10 seconds, and loading pages in my codebase is now faster than our production server (TODO: move our server to an SSD machine).

Most of the tutorials for moving linux from one machine to another assume that you’re moving to a bigger disk, or assume that you are using less than 50% of available disk space. In my case, where neither of these was true, I found this method of moving things around on the 1tb disk to be efficient in terms of time and space – moving files around on the same disk is very fast on Linux, since it doesn’t actually need to copy the data, so the ‘mv /media/old/* /media/old/old_system’ and ‘mv /media/old/old_system/home/* /media/old/’ took seconds. The slowest part of this entire procedure was the rsync – copying the OS onto the SSD. Overall I found this process to be fairly simple and painless – I was a little apprehensive about it, but it turned out that moving to a new disk really is quite simple.

I found this page quite helpful when doing this (in particular, installing grub on the new disk).

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>