Installing Grub boot loader

Grub stands for GRand Unified Bootloader. When a PC starts, processor executes code stored in flash memory on the motherboard commonly known as BIOS (Basic Input Output System). Bios performs power-on self test (POST) which checks hardware. It then finds the device from which to boot the PC and loads its first sector into memory. This sector is 512 bytes in size and is known as MBR (master boot record). Grub’s stage1 which is exactly 512 bytes is installed into MBR. The job of stage1 is to load stage2 (optionally stage 1.5) which displays boot menu and loads the kernel.

Ok, enough theory.

Sometimes, the MBR gets written by another operating system or gets corrupted. In that case, it needs to be reinstalled on the MBR.

The system needs to be booted from rescue CD or boot floppy to install grub.
After booting, type grub and press enter to get grub the prompt like below
 
grub>
Next,we need to tell grub the location of boot parturition (usually /boot) with the following command
 
grub> root (hd0,0)
Filesystem type is ext2fs, partition type 0x83

Here we are setting first hard drive’s first partition as boot location. It is where grub’s stage2 and grub.conf or menu.1st files are found.

Note: For grub drives and partitions starts with 0.

By the way, command completion works at grub prompt so if we type root (h and press tab, grub will either fill
in the remaining part on single partition system or give us possible options. In my case
 
grub> root (hd0,
 Possible partitions are:
   Partition num: 0,  Filesystem type is ext2fs, partition type 0x83
   Partition num: 1,  Filesystem type unknown, partition type 0x8e

Now find where the stage 1 file is located. In multi-boot systems, each installation will have its own stage1 file and the following command returns the location of the first one found.
 
grub> find /boot/grub/stage1
(hd0,0)

One point is worth noting here though. If you are using LVM or separate /boot partition the find command will return “No file found error”. In that case the following command will find the location of stage1
 
grub> find /grub/stage1
(hd0,0)
 
Now to install grub on the MBR of the first harddrive, we issue the following command
 
grub> setup (hd0)
 Checking if "/boot/grub/stage1" exists... no
 Checking if "/grub/stage1" exists... yes
 Checking if "/grub/stage2" exists... yes
 Checking if "/grub/e2fs_stage1_5" exists... yes
 Running "embed /grub/e2fs_stage1_5 (hd0)"...  15 sectors are embedded.
 succeeded
 Running "install /grub/stage1 (hd0) (hd0)1+15 p (hd0,0)/grub/stage2 /grub/grub
.conf"... succeeded
Done.

Type quit to get out of grub prompt and then reboot
grub> quit

2 comments:

  1. from a newbie dumb-dumb - thankyou thankyou thankyou! great post - fixed my problem - now i have to learn more about grub... thankyou for your expertise...

    ReplyDelete
  2. Getting error 15:file not found

    ReplyDelete