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-install /dev/sda
update-initramfs -c -k all

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.

No comments: