Home > Uncategorized > Grub2 Chainloading

Grub2 Chainloading

Recently, I installed Debian Squeeze as my main Operating System (OS). Usually I have multiboot configuration in this laptop with 2 GNU / Linux distributions and Windows XP. This means I have to configure my bootloader to load the 3 systems.

The GNU / Linux I installed usually one as main OS while the other for distrohopping. But, I started to get tired to wipe the for-distrohopping partition every time I install a new distro. So then I wipe the partition (32 GB) into four 8 GB partitions so instead of trying a distro one by one, I opted to just install them all. LoL.

So, the number of OS to load increases to six. And the big problem is my Master Boot Record (MBR) which hold the data for the Basic Input Output System (BIOS) to boot OS is controlled by my main OS which is Debian 6.0 aka Squeeze and it uses Grub2 bootloader aka Grub-PC which is relatively new bootloader in GNU / Linux ecosystem.

The configuration file for Grub2 is located in /boot/grub/grub.cfg. You can think that this is the equivalent of /boot/grub/menu.lst in Grub legacy. If you open this file you’ll see at the top of it a warning message like this:

 # DO NOT EDIT THIS FILE # # It is automatically generated by grub-mkconfig using templates # from /etc/grub.d and settings from /etc/default/grub 

So, the grub.cfg is generated using grub-mkconfig command. When there was just three OS in it I took a simple route to mount all of my partitions and run:

 grub-mkconfig > grub.txt rm /boot/grub/grub.cfg mv grub.txt /boot/grub/grub.cfg 

From the snippet code above you’ll know that I run grub-mkconfig and put the generated configuration to grub.txt file, remove the original grub.cfg and move the newly generated grub.cfg to replace it. This way the Grub2 will probe any OS from any mounted partitions and if possible create an entry in Grub2 for that OS into Grub2 bootlader entries. The script for probing OSes is residing in /etc/grub.d/30_os-prober.

The problem arise when openSUSE 11.3 in one of the partition is having a kernel upgrade. The Grub2 configuration is outdated so it can’t boot the openSUSE and it needs to be updated to follow openSUSE kernel upgrade. It’s not a big problem when there are just three OS in my system. But, there are six now and updating the grub.cfg every time I have kernel upgrade is a waste of time and can be painful.

To solve this issue, people usually do a chainload. It is a method that ‘transfer’ the OS load from the main bootlader of your system that holding the MBR to the bootloader of the OS in another partition. I always do this too when my MBR was controlled by LILO (Slackware) and Grub legacy (Debian 5.0 aka Lenny) but the Grub2 configuration is pretty much confusing. So, I began crawling in the cloud looking for answer that mostly explain the complexity of Grub2 until I find this: http://techpatterns.com/forums/about1544.html

That’s quite simple enough. So, I tried it like this:

Disable OS Prober

 chmod a-x  /etc/grub.d/30_os-prober 

Add entries of another OS in /etc/grub.d/40_custom

 vim /etc/grub.d/40_custom 

so it will be like this:

 #!/bin/sh exec tail -n +3 $0 # This file provides an easy way to add custom menu entries.  Simply type the # menu entries you want to add after this comment.  Be careful not to change # the 'exec tail' line above.

menuentry "ubuntu" { insmod ext2 set root=(hd0,7) chainloader +1 }

menuentry "openSUSE" { insmod ext2 set root=(hd0,8) chainloader +1 }

menuentry "Windows" { insmod ntfs set root=(hd0,2) chainloader +1 }

Then update the grub configuration


There. It worked well. Kernel upgrade is no more a pain for extra work in bootloading. And I can let the default bootlader of any OS to be the bootloader of their own.

This article assume you have installed other OS bootloader in its own root partition. So, when you boot your main OS it will pass the boot to the bootloader in the root partition of your other-OS.

Categories: Uncategorized
  1. March 6, 2011 at 9:08 am

    awesome. nice infoo gan

  2. A.F.M.
    March 24, 2013 at 6:28 pm

    Thank you! You saved my day (and more!). Graditude!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: