Hack and / - Migrate to a New Hard Drive

Storage needs always seem to grow, but when you are ready to upgrade to a new hard drive, how do you transfer all those files? Read on for a tried-and-true method to migrate all your old files to a new drive.
5. Update fstab

What you do during this step will vary a bit, depending on how you set up your partitions. If you moved your partition layout around, you need to edit the /etc/fstab file on your new root partition so that it reflects the new drives you have set up.

Traditionally, this has been a simple step for me, because I try to order the partitions the same and generally don't have to touch fstab, but on this last migration, I had to add an extra step due to Ubuntu's use of UUIDs to reference partitions. A lot of modern distributions don't refer to partitions by their device name. Instead, a unique identifier called the UUID is assigned to each partition. If you see UUID=longstringofhex in your /etc/fstab, this means you.

Now, you have two choices here. The first choice is to change all these UUID lines to reference the actual device. This will work, and is less prone to typos that will make the system not boot, but you will lose the advantage of UUIDs. The other method is to reference the UUIDs for your new partitions and put them in place of the old UUIDs. You can find the list of disk-to-UUID mappings under /dev/disk/by-uuid:

greenfly@minimus:~$ ls -l /dev/disk/by-uuid/
total 0
lrwxrwxrwx 1 root root 10 2008-04-06 16:00 
 ↪634719fd-a6da-4fee-8646-0d485d7681db -> ../../sda2
lrwxrwxrwx 1 root root 10 2008-04-06 16:00 
 ↪665d7008-fde9-4055-8af9-483697acb005 -> ../../sda1
lrwxrwxrwx 1 root root 10 2008-04-06 16:00 
 ↪cf3892fd-e3d8-446f-8552-4c633be9c382 -> ../../sda3

Of course, you always could choose a hybrid of the two approaches, and set the hard device names in the fstab for the first boot, and then once you have confirmed the system boots, you then can update fstab with UUIDs.

6. Update GRUB

As with fstab, if you changed your partition layout, you need to update your GRUB configuration under /boot/grub/menu.lst (or on some systems, in /boot/grub/grub.conf) to reflect your changes. Also, GRUB can reference drives by UUID, so if you see references to UUID in the GRUB configuration file, be sure to update it to reflect the new values. Once the file has been updated, chroot into your new root partition's mountpoint and then run grub-install:

chroot /mnt/sdb1 /usr/sbin/grub-install --recheck /dev/sdb

Change /mnt/sdb1 and /dev/sdb to reflect your new mounted root partition and its disk device, respectively. If the chrooted grub-install doesn't work, you typically can use your rescue disk (or single user) grub-install with the --root-directory option:

/usr/sbin/grub-install --recheck --root-directory /mnt/sdb1 /dev/sdb

7. (Optionally) Update the Initial Ramdisk

After I used my new system for some time, I noticed it wouldn't resume correctly from hibernation. It seemed like each time the swap partition would get corrupted. After some troubleshooting, I found that the root cause was a hard-coded resume device based on UUID that is put in the initial ramdisk for the machine. You may or may not run into this issue, depending on your Linux distribution, as each distribution manages its initrd differently. But, here is the fix for my Ubuntu system. I was able to find the offending reference to the old UUID in /etc/initramfs-tools/conf.d/resume. All I needed to do was update that file on the new drive to point to the new UUID for my swap partition, then run update-initramfs from the new system, and reboot.

Kyle Rankin is a Senior Systems Administrator in the San Francisco Bay Area and the author of a number of books, including Knoppix Hacks and Ubuntu Hacks for O'Reilly Media. He is currently the president of the North Bay Linux Users' Group.


Kyle Rankin is VP of engineering operations at Final, Inc., the author of many books including Linux Hardening in Hostile Networks, DevOps Troubleshooting and The Official Ubuntu Server Book, and a columnist for Linux Journal. Follow him @kylerankin


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

More complete guide

Anonymous's picture

There's a more complete guide at http://bremford.org/tips/MigrateDiskInUbuntu.html which uses the CPIO approach but also includes details on how to configure grub.

Didn't work for me first time

Benny's picture

My "new" drive (external USB) was mounted read-only. This led to a cryptic stream of messages from CPIO complaining that the file being copied was not found on the destination drive.
Remounted as RW and all is good :-)

Kyle's Disk Migration

Theo Schmidt's picture

This is a super method which I've used many times with good success, although the correct GRUB update remains a challenge.