How to migrate to a larger hard disk in Linux

Last updated 23 November 2010

There are a few guides out there on how to migrate from one disk to another with Linux, but most are out-of-date or incomplete - they don't include details on grub, or they don't mention /etc/fstab or the kernel boot options.

This is the complete list of steps you need to follow in order to migrate from one hard disk to another with a single reboot and no messing with the console or boot loader. I've made the following assumptions:

So here's what you need to do (all commands run as the root user, of course):

  1. Create a partition on /dev/sdb: fdisk /dev/sdb and follow the instructions (probably as simple as n p 1 return return w)
  2. Format the partition on /dev/sdb1: mkfs -t ext3 /dev/sdb1
  3. Run tune2fs -l /dev/sdb1 and note down the "Filesystem UUID" - for reference mine is 3222fc62-295e-4665-a3dc-e4960a337316
  4. mount /dev/sdb1 /mnt
  5. cd / && find . -xdev -print0 | cpio -pa0V /mnt
  6. mount --bind /dev /mnt/dev
  7. mount --bind /sys /mnt/sys
  8. mount --bind /proc /mnt/proc
  9. chroot /mnt
  10. Edit /etc/fstab and find the device for the mountpoint "/". If the device begins with UUID=xxx change it to the new UUID. If the device is LABEL=nnn change it to use the UUID. If the device is /dev/sda1 then there's no need to change it. For reference the line in my setup is
    UUID=3222fc62-295e-4665-a3dc-e4960a337316  / ext3 relatime,errors=remount-ro 0 1

The next steps depend on whether you're running GRUB legacy or GRUB2. For GRUB legacy:

  1. Edit /boot/grub/menu.1st and change the UUID in the same way on the kernel line for each boot option. If the "root" option to the kernel reads root=/dev/sda1 there's no need to change it. For reference the line in my setup is
    kernel /boot/vmlinuz-2.6.24-26-virtual root=UUID=3222fc62-295e-4665-a3dc-e4960a337316 quiet
  2. Run /usr/sbin/grub-install --recheck /dev/sdb
  3. Shutdown. Remove the old hard drive, move the new hard drive so it shows up in the same position in the BIOS, and reboot

For GRUB2

  1. Run update-grub (this is on Ubuntu 9.04 or later - for other systems you might need to run grub-mkconfig -o /boot/grub/grub.cfg).
  2. Shutdown. Remove the old hard drive, move the new hard drive so it shows up in the same position in the BIOS, and reboot

mike at bfo dot co dot uk, 23 Nov 2010