Grub Installation

GNU GRUB is a bootloader (can also be spelled boot loader) capable of loading a variety of free and proprietary operating systems. GRUB will work well with Linux, DOS, Windows, or BSD. GRUB stands for GRand Unified Bootloader.

Installation of GRUB

GRUB can be installed to a variety of devices, before we install anything; we need to know where the files are. If installed, GRUB menu is located on the root partition under:

# vi /boot/grub/menu.lst

The GRUB files can be found in the image of your operating system (usually a CD), under:

/usr/lib/grub/i386-pc

You should also be aware of the basic Linux commands regarding the hard disks and partitions:

# sudo vi /etc/fstab                OR
# sudo gedit /etc/fstab

Some useful commands that one might need when dealing with boot, hard drives and partitions: Display the partition table:

# fdisk -l
Display the mounted partitions:
# cat /etc/fstab

To install GRUB in MBR, you will need to boot from external media (floppy, live Linux CD). Then, once you reach the GRUB prompt, execute the following commands:

Find the GRUB root device:

# grub (press enter key)
grub> find /boot/grub/stage1

GRUB will search for all available Stages 1 and present them. If there is more than one operating system image present (e.g. SUSE, Kubuntu, Mandriva), it will have more than one stage1 available.

grub>  root (hd0,1)
grub> setup (hd0)
grub> grub-install hd0
grub> quit
#

GRUB on a floppy disk

To create GRUB on a floppy disk, we must copy the Stage 1 and Stage 2 files from the image directory to the first and second sector of the floppy disk. We’ll use the dd command, which copies information sector by sector. Here’s the set of commands that you need to execute (taken from the Manual):

# cd /usr/lib/grub/i386-pc
# dd if=stage1 of=/dev/fd0 bs=512 count=1
# dd if=stage2 of=/dev/fd0 bs=512 seek=1

Common problems

GRUB got deleted; how to restore? This will often happen if you install Windows after Linux. Windows assumes it’s the only operating system in the world and does not try to live with existing information present in the MBR; it overwrites it.

The easy way: Use Super Grub Disk.

The hard way Boot from floppy disk or CD (any Linux live CD should do). Get to the grub command line.

grub > find /boot/grub/stage1 (optional) root (hdX,Y)
grub> setup (hd0)
grub> quit

Windows is installed on a non-first hard disk (Swapping)

GRUB cannot directly boot Microsoft operating systems. And sometimes, even chain loading may not work, especially if Windows is not installed on the first hard disk.

You need to perform a virtual swap between hard disks. Like this:

grub> map (hd0) (hd1)
grub> map (hd1) (hd0)

After you add these two lines, you should be able to boot into Windows (or DOS, for that matter). There is more than one Windows operating system installed on one hard disk (Hide/Unhide)

Again, Windows can cause problems if there’s more than one instance present on a hard disk, especially if installed on primary partitions. When you use the chain loader command, the control is transferred to the Windows boot loader, but which one? The problem is easily solved by hiding and unhiding partitions. If GRUB hides a partition, it will not be visible by Windows and prevent a possible confusion. Vice versa, if you unhide a partition, Windows will detect it and be able to boot from it, without getting confused.

Example

Let’s say we have Windows installed on the first and the second partition of your hard disk. We wish to boot the first instance of Windows. Then, we need to execute the following set of commands to make it all work:

grub>  unhide (hd0,0)
grub> hide (hd0,1)
grub> root no verify (hd0,0)
grub> chain loader +1
grub> make active

You resized a partition; GRUB is gone:

This is an interesting case. It can happen after you use a partitioning or imaging software to make changes to the Partition Table. Usually, this will happen when you make such a change in Windows. Linux will not be informed of the change, because Windows is blind to anything.

The solution is that most likely the filesystem is damaged and needs to be repaired.

Boot from a live CD and execute the following commands, assuming you know where Linux partitions are. Check the filesystem:

# fsck.ext2 /dev/hdXY (hda2, hdb3, etc.).

Create the ext2/3 filesystem parameters again.

# tune2fs –j  /dev/hdXY

Now mount the root filesystem on /mnt/sysimage and run grub:

#  mount -t ext2  /dev/hdXY  /mnt/sysimage
#  cd /mnt/sysimage/sbin
# grub        (press enter key )
grub > grub-install hd0
grub> quit

Subscribe For More Content