GRUB is a very common bootloader on GNU+Linux systems. Many people multi boot their computers with Windows, different flavours of GNU+Linux and other operating systems, and some times GRUB gets destroyed/removed accidentally. In those case there is no other way except reinstalling GRUB. Also when making a bootable USB device GRUB is useful. We will talk about how to install GRUB in a partition with the help of the grub-install script which comes with the GRUB package, and also how to manually install GRUB using grub shell.
You either need to install grub with the grub-install command or with the grub shell. Both the processes are described below. After you have done with installing, you would need to make the partition bootable which you want to boot. A configuration file is optional and should be included as you wish.
Note: all the instructions below is to be used with GRUB version 0 and 1, and is not applicable to GRUB2
- Boot a GNU+Linux OS (LiveCD or Persistent install), or launch a rescue prompt, from where you can run GRUB.
Lets say you are going to install GRUB in /dev/sdXy . Replace the ‘X’ and the ‘y’ per your configuration, and mount that partition in some mount point. Below "/mnt/tmp" is the mount point.
mkdir /mnt/tmp mount /dev/sdXy /mnt/tmp
Now invoke the grub-install command as shown below
grub-install --no-floppy --root-directory="/mnt/tmp" /dev/sdX
The first option --no-floppy will not probe for floppy drives. The second option --root-directory says to copy the GRUB files in the path "/mnt/tmp" that is, in the mount point of the device where you want to install GRUB. At last /dev/sdX tells the device where GRUB is to be installed. You can also use the corresponding BIOS devce in place of /dev/sdX. This will install GRUB in the MBR of the disk /dev/sdX.
For example to install GRUB in my usb drive which is mounted in “/media/disk” and the USB device is /dev/sdb1 . The grub-install command is
grub-install --no-floppy --root-directory="/media/disk" /dev/sdb
Instead if you want to install GRUB in the first sector of a partition say /dev/sdXy then you should just replace the device name with /dev/sdXy instead of /dev/sdX . For example if i want to install GRUB in the first sector of the partition /dev/sda5 then i should execute
grub-install --no-floppy --root-directory="/mnt/tmp" /dev/sda5
After the installation is complete a similar message as below is displayed.
Probing devices to guess BIOS drives. This may take a long time. Installation finished. No error reported. This is the contents of the device map /media/disk/boot/grub/device.map. Check if this is correct or not. If any of the lines is incorrect, fix it and re-run the script `grub-install'. (hd0) /dev/sda (hd1) /dev/sdb
It tells to check the device.map file data. Check if the BIOS drive and device file mapping is correct. If it is correct then it is all successful. Else you need to fix the mapping and rerun the script, and probably if the mapping showed is wrong, some other partition’s boot sector which was wrongly mapped might be wiped out. Check the device.map section.
grub-install says that “/dev/sdX does not have any corresponding BIOS drive.”
To solve this add an option --recheck to the above existing command. So it becomes
grub-install --no-floppy --recheck --root-directory="/media/disk" /dev/sdb
GRUB maps each storage device connected to the corresponding GRUB BIOS drive, and stores the mapping in "/boot/grub/device.map” file. grub-install reads the device map, that is the device file to BIOS drive mapping, from the boot/grub/device.map of the partition which is being used as root. If this file is not present then the system is scanned and a new one is made, else if it is there then the data inside it is used. The --recheck flag forces it to scan again even the file is present and update the file. This will populate the file with the updated devices in the map. Although the manual discourages the usage of this option, because GRUB only guesses the mapping of the BIOS devices and device files, and which might get wrong, but it works in almost all normal conditions. Or you can make the BIOS and device mappings manually with the device.map file.
Install with grub Shell
To install GRUB from the grub shell follow the instructions below.
Mount the partition where you would keep the GRUB files, and copy the GRUB files, the stage1, stage1.5 and the stage2 files into “boot/grub” directory of that partition. The grub files could be found in the “/boot/grub” of the currently booted OS, or rescue console.
mkdir /mnt/tmp mount /dev/sdXy /mnt/tmp mkdir -p /mnt/tmp/boot/grub cp /boot/grub/*stage* /mnt/tmp/boot/grub
Create an empty file, name it say “mypart” in the root of the partition. This will help us locate this partition uniquely from the other partitions which might have GRUB.
Then launch the grub shell by executing
Now get the corresponding BIOS drive of the drive /dev/sdX, by finding the “mypart” file, invoking the find GRUB command. It will print the corresponding BIOS drive of the partition where this file exists. Lets say it is (hd?,?). Then set (hd?,?) as root, and apply setup command to set GRUB. And finally enter quit to exit, from GRUB shell.
find /mypart root (hd?,?) setup (hd?) quit
In the above grub command sequence enter setup (hd?) to install GRUB in MBR.
To install GRUB in the first sector of the partition enter setup (hd?,?).
You can also find any other files to get the BIOS drive, such as you can find for the GRUB files itself. Remember the path of the files to found shoud be relative to the root of the partition in which you are finding, and not relative to the current root. Remember to include a leading slash.
A sample output is shown below:
grub> find /myusb find /media.repo (hd1,0) grub> root (hd1,0) root (hd1,0) Filesystem type is ext2fs, partition type 0x83 grub> setup (hd1) setup (hd1) Checking if "/boot/grub/stage1" exists... yes Checking if "/boot/grub/stage2" exists... yes Checking if "/boot/grub/e2fs_stage1_5" exists... yes Running "embed /boot/grub/e2fs_stage1_5 (hd1)"... 23 sectors are embedded. succeeded Running "install /boot/grub/stage1 (hd1) (hd1)1+23 p (hd1,0)/boot/grub/stage2 /boot/grub/grub.conf"... succeeded Done.
And thus you can install GRUB from the grub shell
Setting Partition Boot Flag
To be able to boot from the storage device you need to set the boot flag of the partition where the boot directory exists. For example if you are installing GRUB in HDD, then you need to set the boot flag of the partition which contains the boot directory. If you are making a bootable usb then you need to set the boot flag of the USB drive partition which contains the boot directory. The boot flag can be set using fdisk or parted. The methods are shown below:
Unmount the disk partition, and make the USB partition bootable with ‘parted‘
# umount /dev/sdXy # parted /dev/sdX set y boot on
For example if your USB drive partition be /dev/sdb1 (containing the boot dir). Then the above commands would be:
# umount /dev/sdb1 # parted /dev/sdb set 1 boot on
The process of setting the bootflag with ‘fdisk‘ is as below:
# fdisk /dev/sdXy
Enter ‘p‘ in the ‘fdisk‘ prompt. If you see “*” beside the disk partition under the boot column then the partition is already bootable, simply enter ‘q‘ to quit. Else to make it bootable: Enter ‘a‘ then, when asked, enter the partition number and press enter (if your drive’s partition is /dev/sdb1 then enter ‘1‘ as the partition number). Enter ‘p‘ again to check and then ‘w‘ to write the changes.
Setting config file
After GRUB has been installed a configuration file grub.conf or menu.lst is needed to load any operating system. To make GRUB to pick up the file needs to be inside "/boot/grub/". "/boot/grub/grub.conf" has a below basic syntax outline.
# Lines starting with hash are comments # general format: # default=0 ##The 0th entry is the default to boot (optional) # timeout=5 ##Wait 5 seconds before automatically booting the default (optional) # splashimage=/path/to/image.xpm.gz ##A background image (optional) # # title Entry Title ##The title of the entry # root (hd0,2) ##The BIOS device where the kernel and initrd is present # kernel /path/to/kernel.bin kernel_options ##The path to kernel and the kernel options # initrd /path/to/initrd.img ##The path to the initial ramdisk file # A sample is below default=0 timeout=5 splashimage=(hd0,2)/boot/grub/splash.xpm.gz title Fedora (22.214.171.124-170.2.35.fc10.i686) root (hd0,2) kernel /boot/vmlinuz-126.96.36.199-170.2.35.fc10.i686 ro root=UUID=18c316f8-e63b-4d46-b8f6-746a19e03ef1 rhgb quiet vga=0x365 initrd /boot/initrd-188.8.131.52-170.2.35.fc10.i686.img # To chainload another partition title Other OSes root (hd0,0) chainloader +1 # This will load the first sector of the root partition and boot from it
For more on GRUB configuration file check out info grub and read the “Configuration” section.
To set kernel parameters are OS specific. To set them properly you need to consult the OS manual.This completes the installation of GRUB.
GRUB maps each storage device connected to the corresponding GRUB BIOS drive, and stores the mapping in "/boot/grub/device.map” file. GRUB BIOS devices starts from (hd0,0), this represents the first partition of the first disk and corresponds to /dev/sda1. (hd1,2) means the third partition of the second disk (count starts from 0), which corresponds to /dev/sdb3, and so on. Similarly to represent the first disk as a whole (hd0) is used which is /dev/sda and to represent the third disk (hd2) is used which is /dev/sdc.
The device map file is in the below format
# This is a comment line starts with hash '#' # General format: BIOS_DRIVE DEVICE_NAME (hd0) /dev/sda (hd1) /dev/sdb (hd2) /dev/sdc
You can edit, or create or edit and add correct device mappings in such a device.map file manually, in "boot/grub" of the partition where you want to install grub and invoke grub-install. Like in the above example when installing GRUB in /dev/sdb mounted on "/media/disk", you need to edit the file "/media/disk/boot/grub/device.map" and add a correct mapping: (hd1) /dev/sdb
And GRUB is installed.
GRUB in MBR vs First sector of partition
Installing GRUB in a partition instead of MBR is useful when you have multiple OS in different partitions, and each has its own GRUB or Syslinux or other bootloader installation. One of those installations would reside in the MBR. The other OSes could be simply booted by handing over the control to the bootloader residing in the partition of the other OS. This is known as chainloading.
Please check put the grub manpages and info mages and the grub official site for more info. http://www.gnu.org/software/grub/manual/html_node/index.html
03.03.11 : Section added : “Setting Device Boot Flag” . Typo fixed
13.04.11 : Restructure of sections. one paragraph added at beginning. No content change