23 July 2006

Moving root partition in linux

After several frustrated atempts to move my root partition to another hard drive I finally found a way to do so.

The error was simply the initrd of the old root which was pointing to the wrong partition!

Here's what I had in the begining:

-> Root partition in /dev/hdb1 (when I first installed linux I still had windows in /dev/hda1)
-> Swap partition in /dev/hdb2, pretty simple, huh?
-> Space available in /dev/hda

I wanted to move my whole installation to the newer-faster-cooler-nicer-stabler-hard-drive (aka hda1) without the need of a reinstall, and I was told that the only thing I needed was to create a partition in hda1, copy (cp -ax if I'm not mistaken) everything from / to the new place, update the /etc/fstab and /boot/grub/menu.lst and finally reinstall grub.

But it wasn't that easy, I had to learn a lot of grub, initrd, kernel, filesystems, partitions and how to rescue the system from stupidity to (at last) do it the propper way (at least for me!)

Here's what I did:

(let's consider I had a empty /dev/hda, which I didn't but it'd be safer)

-> Booted with the Gparted LiveCD
-> Created a XFS partition in /dev/hda (/dev/hda1)
-> Created a SWAP partition in /dev/hda (/dev/hda4)
-> Marked hda1 with the bootable flag
-> Copied everything from /dev/hdb1 to /dev/hda1 (through grsync which is included in the Gparted LiveCD), that was very nice, the interface is very straightforward and is much simpler to solve any error that with those odd-looking cp options mentioned above.
-> Booted with Knoppix (so that I wouldn't mess with any of the roots I had installed)
-> Installed GRUB:
-> #grub
-> root(hd0,0)
-> setup(hd0)
[so it installs in the MBR instead of the first sector of the drive (a BadThing(tm) for XFS)]
-> Mounted the new partition
-> #mount /dev/hda1 /mnt/hda1
-> Fixed the new /etc/fstab
-> #vim /mnt/hda1/etc/fstab
Changed the /dev/hdb1 for /dev/hda1 and /dev/hdb2 for /dev/hda4
[IMPORTANT] if you have for some reason any strange parameter for mounting the root partition (it was EXT3, thererefore I had "errors=remount-ro" in the options, and that simply doesn't work for XFS) please check in man mount the options for your filesystem.
My /etc/fstab is now like this:

# /etc/fstab: static file system information.
#
#
proc /proc proc defaults 0 0
/dev/hda1 / xfs defaults 0 1
/dev/hda4 none swap sw 0 0
sysfs /sys sysfs defaults 0 0



OK, so far it took a while to copy everything, but it's pretty simple, the next point is where I lost a lot of time on.

I simply tried to rearange the /boot/grub/menu.lst so that it'd boot the kernel in the right partitition, but it would only boot it from /dev/hdb1, I didn'tknow why!! After all, the fstab was right...

I then asked the right questions to google and I was enlighted with the answer: The INITRD has hardcoded the root partition link.

So I tried several ways to rebuild a initrd (using yaird, mkinitrd, mkinitramfs) and NONE of them would work for me. So I was told to simply chroot into the new partition and install another kernel...

Let's continue then.

-> Chroot'd to the new partition:
-> #chroot /mnt/hda1
-> Mounted the proc and sys partitions:
-> #mount proc -t proc /proc
-> #mount sys -t sysfs /sys
-> Installed another kernel:
(if you still have the linux-image-2.X.X.X.deb in your apt cache dir, you can simply #dpkg-reconfigure linux-image-2.X.X.X and it will rebuild your initrd. I didn't had, and decided to upgrade my kernel anyway, so I did the following)
-> # apt-get install linux-image-2.6.17-1-k7 linux-headers-2.6.17-1-k7 linux-source-2.6.17
-> After the download (circa 60MB) the installer will create a new initrd (and load the root information from your new /etc/fstab), and mess your /boot/grub/menu.lst so I recomend you fix it:
-> # vim /boot/grub/menu.lst
-> make sure you have a option similar to this:

title Debian GNU/Linux, kernel 2.6.17-1-k7
root (hd0,0)
kernel /boot/vmlinuz-2.6.17-1-k7 root=/dev/hda1 ra vga=791
initrd /boot/initrd.img-2.6.17-1-k7
savedefault
boot

-> So, now simply umount everything and reboot
-> #umount /proc
-> #umount /sys
-> #exit
-> #reboot

-> And boot to your new kernel and partition.

It should work right now :)