Moving Your Linux Root Partition

*[This article is somewhat obsolete because we can use UIDs to identify disks.]*

I had a pretty fast IDE drive, but finally got a much faster SATA drive. I partitioned it with a large /home, and a smaller empty partition. The intention was to load the latest, new version of Ubuntu onto the empty partition on the SATA drive (and mounted as root), and retain the /home partition already on the SATA drive. Unfortunately, Ubuntu’s installer would not install onto the empty partition without formatting both partitions. The installer will upgrade a system in-place; it’ll reformat a new system; but it won’t install into an empty partition, leaving the other partitions intact.

So, I had to move the root partition from the old IDE drive, to the new SATA drive. Specifically, the original setup was:

/dev/hdc3 /
/dev/hdc1 /home

After I got the SATA drive, it became:

/dev/hdc3 /
/dev/sda3 /home

My goal was:

/dev/sda1 /
/dev/sda3 /home/

This involves copying some files, and juggling the boot sequence a little bit, so that we creep toward the final goal.

So I had to copy hdc3 to sda1. Easier said than done, because the / contains special directories like /dev /sys and /boot.

Solution: Mount the new root under /mnt/newroot. Use cp -a to copy any non-special directories. Make the special directories (dev, sys, proc, home). Use ln -s to make the special symlinks for initrd* and vmlinuz*

Then, go into /boot/grub, and edit the menu.lst and add a configuration to boot from this new partition. Place it after the first menu item. It will look something like this:

title       Ubuntu, kernel 2.6.15-28-amd64-generic on sda1
root        (hd1,0)
kernel      (hd1,0)/boot/vmlinuz-2.6.15-28-amd64-generic root=/dev/hdc3 ro quiet splash
initrd      (hd1,0)/boot/initrd.img-2.6.15-28-amd64-generic
savedefault
boot

(hd1,0) refers to /dev/sda1 in grub notation.

Also, you will probably have to alter device.map to look like this:

(hd0)   /dev/hdc
(hd1)   /dev/sda

That just means all references to hd0 really map to hdc, and hd1 maps to sda.

This mapping compensates for the fact that bios boot order affects these values.

Once all this is done, run grub-install to put the bootloader on the disk:

sudo grub-install /dev/hdc

You’ll probably specify /dev/hda instead of hdc.

Reboot, and try to load the kernel from the new partition. If it works, you know you can boot this copy of the kernel, and use the same old root file system. The next goal is to mount the new root file system. So edit the menu.lst to mount the sda1 partition as root:

title       Ubuntu, kernel 2.6.15-28-amd64-generic on sda1
root        (hd1,0)
kernel      (hd1,0)/boot/vmlinuz-2.6.15-28-amd64-generic root=/dev/sda1 ro quiet splash
initrd      (hd1,0)/boot/initrd.img-2.6.15-28-amd64-generic
savedefault
boot

Then do sudo grub-install /dev/hdc.

Before rebooting, mount /dev/sda1 to someplace like /mnt/sda1. Edit /mnt/sda1/etc/fstab.

fstab will contain the old mount points. Edit them so that /mnt/sda1 mounts on /.

Reboot. On this reboot, it should still load the MBR from hdc. Go into the grub menu, and pick the customized configuration.

If it boots, and then mounts /dev/sda1 as /, good. You can take it to the next step, by booting directly from sda.

Go into /boot/grub. Note that this is on /mnt/sda1, so the files will be the old versions (that you copied from the orignal disk to the new disk, up near the first step above).

First edit device.map so everything’s flipped around, and hd0 is /dev/sda.

(hd0)   /dev/sda
(hd1)   /dev/hdc

Edit menu.lst, changing references to the old root (hdc3) to the new root (sda1).

Make sure grub references to the root are (hd0,0).

Run sudo grub-install /dev/sda

(At this point, I’m not going to bother with setting up an alternate boot on the old partition.)

Before rebooting, make sure you have some kind of bootable rescue disk. An ubuntu live cd is adequate.

Reboot.

Press DEL to get into the BIOS setup menu. Once in, disable the IDE hard drive, but leave the SATA drive enabled. This will prevent the old IDE drive from booting.

Let it boot, and go into the grub menu. You should see that the customized boot config, above, is absent. That’s because you’re not booting into the new partition, which lacks it.

Pick the first kernel, and boot. If it works, you’re home free. You can now shut down and remove the old IDE drive. (And then, later, install the latest Ubuntu.)