Thursday, January 26, 2006

Dual booting Linux with just a single partition

Dual booting into two different Linux systems when they are installed on different partitions is easy, however since I wanted to switch from Debian to Ubuntu, without losing the Debian install, and there wasn't place on the harddisk to create yet another partition I had to search for another way. The solution was actually quite easy, however it required a tiny bit of C programming. It works like this, first you take your old Linux install and move all its content in / to CHROOT_TARGET, doing that with a live-cd is probally the easiest, then you take the Linux installation disk of the new system you want to install and install it onto the same partition however without formating the filesystem. Once the new system is installed and booting fine you have to install a /sbin/init wraper to get the old system back to live, it looks like this:

#include
#include
#include

#define ROOT_DEVICE "/dev/hde1"
#define ROOT_FILESYSTEM "reiserfs"
#define CHROOT_TARGET "/debian"

int main(int argc, char** argv)
{
mount(ROOT_DEVICE, "/", ROOT_FILESYSTEM, MS_REMOUNT, "") ;
chroot(CHROOT_TARGET);
mount(ROOT_DEVICE, "/", ROOT_FILESYSTEM, MS_REMOUNT | MS_RDONLY, "");
return 0;
}


Compile it via:
gcc -static chrootinit.c -o chrootinit -Wall


and copy it to /sbin/chrootinit. Once that wrapper is in place and the given ROOT_DEVICE, ROOT_FILESYSTEM, CHROOT_TARGET variables are adjusted to your needs its time to modify the boot loader configuration, todo that all you need is to add a init=/sbin/chrootinit parameter and adjust the kernel location:

title           Debian GNU/Linux, kernel 2.6.15-1-k7 (chrootinit)
root (hd1,0)
kernel /debian/boot/vmlinuz-2.6.15-1-k7 root=/dev/hde1 init=/chrootinit
initrd /debian/boot/initrd.img-2.6.15-1-k7
Once done with it you might also want to remove the root filesystem entry from /debian/etc/fstab to avoid some error messages at boot. The old system should now be bootable again, in the case of Debian it even works without any noticable error messages, other distributions might varry here.

No comments: