What is MBR and Howto reinstall GRUB and Howto use GRUB as a Main Boot Manager


Release V1.1. 24 July 2002

Problem: I can not boot in Linux (it is possible to boot in Windows) or I would like GRUB controls all my boot partitions.

Assumption: for easy the explanation and for supporting the wide range of PC configurations, I need to make some assumptions. By following the instructions carefully you will be able to "convert" this instruction reflecting to you own PC configuration.

You have 2 harddrivers HDA & HDB, each with 2 partitions. You have a boot manager called "Boot commander" (NB: better use the freeware Xosl from www.xosl.org, a very good product), which controls both windows-boot and Linux-grub-boot.

HDA: partition 1 (hd0,0) is for win98, and partition 2 (hd0,1) is for games. (hd0,0 means driver 1 partition 1) and so on.

HDB: partition 1 (hd1,0) is for / (Linux root mount-point: which include /boot, /etc, /usr, /var, /home, /proc…) and partition 2 (hd1,1) is for swap (Linux swap).

On every harddriver (for PC) there are a MBR (Master boot record) record (a record is just a file with informations inside) on sector 1 (cylinder=0 & head=0) and 4 boot records (meaning 4 primary partitions). One sector fills 512 bytes, inside the MBR are there an executable code and four entry ŕ 16 bytes for each of the primary partition. The entry contains the informations about a partition (boot active or not, size in sector, start/end sector, file system type). By looking at the entry the executable code knowns which one of the partion is "active" (meaning boot active), it then jumps to that partition and execute the first sector on that partition (not confuse with the first sector on a hard drive, it is the one and only MBR) there. That first sector on a primary partion called a "Boot record" sector. So 4 primary partitions means that you could have 4 different operative systems on a disk. The first "boot record sector" starts at head=1, sector=1 and cylinder=0. Since DOS only supports one primary partition (yes even there are four partitions there. Actually Dos supports two, the second one is the extended partition) you need to have some tools (just like BootMagic/pfdisk.exe or fdisk in Linux or a new OS) to partition the disk and then use BootMagic or Grub/LILO to manager/control (for example which one of the partitions you want to make active) them.

It is possible to modify/change/add the "executable code" in the MBR. It is what the boot-manager-programmers do, they can put some (the space is extremely limit) of their code there, and then use it to trigger the other code.

Below is a screen dump of a portion of a MBR record. At the position 1BEh you can find the property of the first primary partition (the yellow colour; 80 means boot active; 01 01 00 stands for Head Sector Cylinder at the start point for the first primary partition. It means the executable code from the MBR will execute the code at the postion 010100 at boot). Notice the last two bytes 55AA is the signature for a MBR/boot record.

Preparation:

1) You need to know the partition infos about your disks. Use "fips.exe -t" from the Redhat-CD1/dosutils-directory. Be carefull and read the readme.txt before go further. When you have it,quit it quickly with CTRL+C. You can also use dos fdisk.exe, but the output is not so clear. Please write the information about the partitions down.



The above image tells me that on the primary partion 1 there is a DOS (system 0Bh), on the second is a extended dos (system 0Fh) and on the third there is a linux swap (system 83h).

2) We need to make a diskette to boot into the grub shell. From there we are able to repair the boot sector or to "upgrade" Grub to the "main" boot manager.

From an another Linux machine (your friend or at work) make a GRUB boot shell menu on a floppy disk.

a) cd /usr/share/grub/i386-redhat
b) dd if=stage1 of=/dev/fd0 bs=512 count=1
c) dd if=stage2 of=/dev/fd0 bs=512 seek=1

The dd copies the boot procedure to the floppy. The first file "stage1" fills the first sector on the floppy disk. The file stage2 written just after the first 512 bytes (seek=1).

3) Now you are ready to boot your PC, after a very short time, the boot menu will appear. I like the grub shell very much, because you can quicky do/test many things from there without waiting for booooting from a floppy. To get help please press the TAB-key.

Now you have 2 choices, choice a) is to repair the linux-grub-boot, choice b) is to repair grub AND to install the grub-boot into the MBR and take control for all other boot systems too.

Choice a)

Inform the grub shell where it can find your /boot directory. From my assumptions above it will be at (hd1,0), the second harddriver, partition 1.

Type:
root (hd1,0)

Now tell grub to install boot information on (hd1,0) (according to my assumptions the linux-grub-boot was laid on the second driver partition 1).
setup (hd1,0)

Grub will write "stage1"-file into the first sector on (hd1,0) , it will then "trigger" the other boot files at directory /boot.

If everything successes the word "success" will appear after a short time. Please reboot your PC.

Voilà.

Choice b) (install grub into MBR)

Inform the shell where it can find your /boot directory. From my assumptions above it will be at (hd1,0), the second harddriver, partition 1.

Type:
root (hd1,0)

Now tell grub to install boot information into the MBR (it will then overwrite the boot manager "Boot Commander", which I mention above), which means the first sector (not the boot record sector; remember there are 1 MBR sector and 4 boot record sectors on each harddrive) on harddriver1 (hd0).

setup (hd0)

Grub writes "stage1"-file into the first sector on (hd0). From there it takes control for all other partitions too.

If everything successes the word "success" will appear after a short time. Please reboot your PC.

Voilà, GRUB now becomes your main boot manager for your multi OS system.

If you are aware you will discove that the two above methods does not "touch" your boot record sector at anytime, only at the MBR record and at the boot record where the /boot directory lies. Usually your Windows stays on c:\, which means the first boot sector ("some sector" behind the MBR record sector). It is a good thing that Grub does thing this way, you can always "go back" where you start. I had seen many Windows/DOS boot manager applications which "move" my orginal boot records and modify the four partition entry in the MBR record. And when I do an uninstall, it can not find out how to "restore" thing back as before (meaning one or more partitions completely disapear. Of course if you are smart you can avoid this situation). It is risky to do thing this way if the programmers do not calculate/test for all the combinations which could occur.

Troubleshooting:

If it does not succeeded please try again, reread my assumptions and change it correctly into your enviroment. Be careful with the hardriver number and partition number.

Some Grub options:

*) From the console you can use the command grub-install, it is just a shell script, which use the same functions as the grub shell.

*) Boot in single mode (when forget root password):
Press e at the promt and press e again at the kernel-line. Write S at the end of that line (please take a look at the next image). Press then b (boot).

*) To list the grub.conf at the grub-prompt, type:
cat (hd1,0)/boot/grub/grub.conf
(use TAB for completion, very useful)

Beyond is the output.

The grub.conf might look like this for RH7.2:
title Red Hat 7.2 2002 (2.4.7-10)
timeout=20
root (hd1,0)
kernel /vmlinuz-2.4.7-10 ro root=/dev/hdb1
initrd /initrd-2.4.7-10.img

NB: hd1,0 = /dev/hdb1

*) To boot Linux without a grub configuration file: Use TAB-key for completion. Type:
root (hd1,0)
kernel /boot/vmlinuz-2.4.7-10 root=/dev/hdb1
initrd /boot/initrd-2.4.7-10.img
boot

If your Linux-boot is completely out of tune, use rawrite.exe (Redhat CD1 /dosutils) to make a setup/boot diskette in DOS, and then use "linux rescue" to access to the root prompt. Copy all the files at /usr/share/grub/i386-redhat to /boot/grub. And make a grub.conf similar (except the kernel number) to this one above. Control that the files at the directory /boot are intact. Boot the PC and goto step 2.

 

Good luck

Copenhagen June 2002, Tuan Nguyen