Saturday, October 3, 2009

Setup grub2 to load from Windows XP bootloader (ntldr)

Brief Background:

Very recently I came across the information that grub2 would be used as default starting from UBUNTU 9.10. I found that grub2 has a lot of added features when compared to grub-legacy. So I decided to give it a try. But I did not want to install grub2 to the MBR on my system. I also had grub-legacy installed in a boot partition and I do not want to wipe out the boot record of this partition as well.


The basic aim is:

1) To have a single boot partition with both grub-legacy and grub2 installed inside it

2) Have grub2 installed to a sub-directory under the boot directory so that the installed files from grub-legacy and grub2 are kept separate

3) Boot from ntldr both grub-legacy and grub2

I searched through the internet by I couldn't find any useful information on how to achieve this. Hence I decided to write this procedure on my own. It worked wonderfully for me and I hope that it would be useful to some others as well.

Machine Configuration:

I have the following hard disk configuration (got using fdisk -l)

Device Boot Start End Blocks Id System
/dev/sda1 * 1 3824 30716248+ 7 HPFS/NTFS
/dev/sda2 3825 20022 130110435 f W95 Ext'd (LBA)
/dev/sda5 3825 8923 40957686 7 HPFS/NTFS
/dev/sda6 8924 12747 30716248+ b W95 FAT32
/dev/sda7 12748 12938 1534176 83 Linux
/dev/sda8 12939 14154 9767488+ 83 Linux
/dev/sda9 14155 15391 9936171 83 Linux
/dev/sda10 15392 16677 10329763+ 83 Linux
/dev/sda11 16678 19856 25535286 8e Linux LVM
/dev/sda12 19857 20022 1333363+ 82 Linux swap / Solaris


The UUID details are as follows (got using blkid command)

/dev/sda1: UUID="B2B82DD2B82D963F" TYPE="ntfs"
/dev/sda6: LABEL="NEW VOLUME" UUID="7CA3-82D0" TYPE="vfat"
/dev/sda5: UUID="761067301066F711" TYPE="ntfs"
/dev/sda8: UUID="7d1c620c-c474-4fca-8531-37b961fa274d" TYPE="ext3" SEC_TYPE="ext2"
/dev/sda7: LABEL="/boot1" UUID="cdf388bb-0283-4d5b-9a3e-9dd629d4e630" TYPE="ext3" SEC_TYPE="ext2"

/dev/sda9: UUID="35933fee-1aec-4c15-b74f-ea5518c770ac" TYPE="ext3" SEC_TYPE="ext2"
/dev/sda10: UUID="cc76db39-a5d9-43d8-8b1b-d4d5072b8eed" SEC_TYPE="ext2" TYPE="ext3"
/dev/sda11: UUID="B5KCEe-Mw1p-feJ0-xceN-Zjd4-j3FL-LtGuu2" TYPE="lvm2pv"
/dev/sda12: TYPE="swap" UUID="74df223f-b4a3-4a57-a2dc-df2a643552db"
/dev/mapper/VolGroup00-LogVol00: LABEL="Fedora-11-x86_64" UUID="15a1912e-73bb-45ab-916e-35ecab795a2c" TYPE="ext4"

/dev/mapper/VolGroup00-LogVol01: UUID="def5e55f-96d6-4c33-afa0-45b4758fba99" TYPE="ext3" SEC_TYPE="ext2"

/dev/sdb1: UUID="7AE4834AE4830799" LABEL="FreeAgent Drive" TYPE="ntfs"


As it could be seen I am using /dev/sda7 as the boot partition. I have installed grub-legacy in this boot partition.

Software Prerequisites:

1) Grub2 ( Grub 1.97 beta3 downloaded from ftp://alpha.gnu.org/gnu/grub/)
2) All pre-requisites for compiling grub (like ruby etc.,)
3) loadbin-0.1-src-2008-03-01.zip & grubinst-1.1-src-2008-01-01.zip from http://download.gna.org/grubutil/
4) gimp or imagemagik to edit the splash screen
5) VirtualBox 3.06 (optional)

Disclaimer:
This is process which worked for me. There is no guarantee that it would work for others as well. Use it at your own risk. It is good to have a backup of your important data before you attempt this setup

Step by Step Setup:

I have made this entire setup using a Linux box running Linux Mint 7. The steps should remain the same even if it is done within windows

1) Download grub1.97 beta3 as shown above and unzip it to a local folder e.g. /home/srinivasan/Downloads/grub-1.97~beta3

2) To compile grub 1.97 beta3 use the following steps

$ cd /home/srinivasan/Downloads/grub-1.97~beta3
$ ./autogen.sh
$ ./configure --prefix=$PWD/grub2 --enable-efiemu --enable-grub-emu --enable-grub-emu-usb --enable-grub-pe2elf --enable-grub-mkfont

$ make
$ make install

Please note that the last step is not done with root login. The installation would happen to a directory named grub2 within the current directory. This will create a folder structure like the one shown below under the grub2 directory

bin
etc
include
lib
sbin
share

All the command required for our installation and image creation are under the grub2/bin and grub2/sbin directory

3) Similarly compile loadbin and grubinst packages as well using the instructions provided in the respective packages.

Installing Grub2 without overwriting the MBR:
This is the most trickiest part of the whole process and my require some trial and error.

4) Create a directory grub2 under the /boot (in the boot partition) using the command

$ sudo mkdir -p /boot/grub2/boot/grub
$ sudo chmod -R 777 /boot/grub2 ( to avoid any permission problems - You can change it later)

5) Create the device map under /boot/grub2/boot/grub directory using
$ cd /home/srinivasan/Downloads/grub-1.97~beta3/grub2/sbin
$ sudo ./grub-mkdevicemap --no-floppy --device-map=/boot/grub2/boot/grub/device.map

6) Do an install of grub2 under the /boot/grub2 folder using the command

sudo ./grub-install --debug --modules="biosdisk ata ext2 part_msdos msdospart normal ls configfile help fshelp fs_uuid fs_file ntfscomp ntfs fat sh tar lvm sleep test true read reboot multiboot minicmd minix memdisk lua" --root-directory=/boot/grub2 --grub-setup=/bin/true --grub-mkdevicemap=/boot/grub2/boot/grub/device.map '(hd0,7)'

I am using (hd0,7) since my boot partition is /dev/sda7. This could be fine tuned as per the individual needs.

This command would copy all the mod files to /boot/grub2/boot/grub directory. Further a file called core.img is also created in the same directory. All the modules specified with the --modules option would be embedded into the core.img file. If you want to add additional modules to the core.img you can add them to the modules option as well.

Embedding the modules inside the core.img would increase its size, but initially it is better to have the modules embedded it for debugging purpose. Sometimes the disk numbering is not correct. In my case eventhough I have only one internal hard disk and one external hard disk the disk numbering in grub2 is shown as (ATA6). I was able to debug this only using the embedded image

7) Create image loadable by ntldr using loadbin and grubinst binaries. I my case I have compiled the loadbin binaries under /home/srinivasan/devel/loadbin-0.1-src-2008-03-01. My grubinst package is under the directory /home/srinivasan/devel/grubinst-1.1-src-2008-01-01

$ cd /home/srinivasan/devel/loadbin-0.1-src-2008-03-01
$ ./mkimage -d /dev/sda7 ldgrub2.bin /boot/grub2/boot/grub/core.img grub2.bin
$ mv grub2.bin /home/srinivasan/devel/grubinst-1.1-src-2008-01-01/
$ cd /home/srinivasan/devel/grubinst-1.1-src-2008-01-01
$ ./grubinst -o -b=grub2.bin grub2.mbr

8) Copy the files grub2.mbr and grub2.bin to c:\

9) edit c:\boot.ini and add the following line

c:\grub2.mbr="GRUB2 Bootloader"

10) Create a default grub.cfg and place it in the folder /boot/grub2/boot/grub

11) Now reboot the machine and you should be able to see the "Grub2 Bootloader" menu

12) Select the "Grub2 Bootloader" option and you should enter either the grub2 menu if everything is fine, else it will fall back into grub2 shell.

13) If you are in the grub2 shell then it means grub is unable to find your configuration file. This may most probably be due to hard disk numbering. To check this type ls -l command in the grub shell this will show the disk configuration.

14) In case you need to make changes then you should recreate the core.img and rerun step 7 and step 8

15) To recreate the core.img alone use the following command


sudo /home/srinivasan/Downloads/grub-1.97~beta3/grub2/bin/grub-mkimage --output=/boot/grub2/boot/grub/core.img --prefix='(ata6,7)/boot/grub2/boot/grub' part_msdos ext2 ntfscomp ntfs fat raid ata biosdisk msdospart normal ls help configfile fshelp probe fs_uuid fs_file sh tar lvm sleep test true read search chain cmp cpio datehook date datetime drivemap dm_nv echo elf extcmd font gfxterm gzio loopback linux16 linux boot cat vga vga_text video videotest tga halt reboot multiboot minicmd minix memdisk lua

IMPORTANT: Please note the prefix option. It shows (ata6,7) . This is the id got for the boot partition by executing "ls -l" command in the grub2 shell.


If everything is working well then all additional modules can be removed and the core.img recreated.