GRUB vs GRUB2
Enterprise Linux Distributions are still very heavily geared towards legacy GRUB or the original GRUB boot loader, or at least up to RHEL 6 and SLES 11, and this is reflected in the LPIC-1 101 exam. You will also need to know GRUB 2 as it has started to appear with RHEL 7 and SLES 12. As far as boot manager go both GRUB and GRUB2 will be important in your LInux Admin life as Enterprise distributions are not updated as frequently as domestic Linux distributions. The GRand Unified Boot-loader has been a stalwart for many years and in this tutorial we start off with an earlier boot-loader installed, LILO, so we can install the GRUB boot loader from scratch. We also have to pay particular attention to the way GRUB refers to partitions and the use of the term root within GRUB. To ensure that we pay particular attention to this we will install GRUB onto two machines one with a single file-system and one with /boot partitioned separately.
Identify the Bootable Partition
The first task that we must complete is to identify the GRUB root which is the partition that is marked as bootable in the MBR of the first drive. This can be checked with the output of parted or fdisk.
parted /dev/sda print
From the output here we can see the boot flag is next to partition 1. The size is 5.5Gb so this is likely also to be the root file system for the OS; on other machines we may have the /boot directory partitioned up separately to root. If you view the following output we can see that the same partition 1 is the bootable partition but it is only 214MB so here we have the partition that will mount to the OS root. Now the GRUB root is sda1 and the OS root sda5.
Installing GRUB to a Partition
The stage 1 file of GRUB, will need to be installed into the first sector of the disk, the Master Boor Record (MBR) or in the first sector of the first partition on the first disk, the superblock of the first partition. The MBR is the most usual location and care must be taken if adding GRUB to the superblock. It would be worthwhile backing-up this sector before the operation.
dd if=/dev/sda1 of=/mnt/usb/sda1.superblock count=1 bs=512
To install GRUB natively you will need to boot to a GRUB disk or recovery disk of some kind and run grub
grub grub> root (hd0,0) grub> setup (hd0,0)
Here, on the first line, we run the command grub that opens on interactive shell.
The second line here identify the root of the bootable partition with root (hd0,0); note that GRUB’s root device doesn’t necessarily mean your OS’s root partition. This is partition that shows as bootable in parted or fdisk. Additionally GRUB’s numbering starts at zero; we reference the first disk and the first partition on that disk. From here we can find the stage 1 boot file to add to the superblock.
The third line installs the stage 1 file to the superblock of that partition using the setup command. To install to the drives MBR we could use: setup (hd0).
Installing GRUB to the MBR
Installing GRUB to the MBR is a little easier as we can run this from the OS we can refer to the disk by its GRUB name or OS name hd0 or sda, but not /dev/sda. The output will show you the device mappings from GRUB to OS and this is written to the file /boot/grub/device.map.
grub-install hd0
GRUB configuration /boot/grub/menu.lst
The configuration file for GRUB will need to be created. The file is called , menu.lst (as in menu.list), and should be created in /boot/grub . The file has global settings and boot entries called stanzaz, each menu item releates to a stanza.
Example menu.lst where /dev/sda1 id the boot partition and /dev/sda5 is the root partition
timeout 8 default 0 gfxmenu (hd0,0)/message
title SLES root (hd0,0) kernel /vmlinuz root=/dev/sda5 initrd /initrd
Example menu.lst where /dev/sda1 is the root partition and /boot is not separately mounted.
timeout 8 default 0 gfxmenu (hd0,0)/boot/message
title SLES root (hd0,0) kernel /boot/vmlinuz root=/dev/sda1 initrd /boot/initrd
Note the difference in the two example files. Where the boot partition exists this becomes the GRUB root and the message, kernel and initrd are at the root of that partition. Where a boot partition does not exist separately then the OS root and GRUB root match.
We can reboot to test the operation
Kernel Options
We can supply to the kernel options within the menu.lst; we are already using the root parameter to pass the device to be used as the Operating System root, we can add in other options such as resume which would point to swap space to use for suspend operations such as on laptops. Supplying the run-level number such as 3 will force the system to boot to run-level 3 and ro means that root is mounted read-only initially to allow for file-system checks and then mounted read-write
kernel /boot/vmlinuz root=/dev/sda2 resume=/dev/sda1 ro 3
If we do not want the options to display in the graphical, gfxmenu, then we can make use of the option showopts. Any kernel option placed before this will not be displayed in the graphical boot options.
kernel /boot/vmlinuz root=/dev/sda2 resume=/dev/sda1 ro showopts 3
Here the only options after the showopts is the run-level; in this way all the options are used but only the run-level option is displayed.
If you need to display the kernel options that were used to boot the current OS this can be viewed from the file /proc/cmdline.
cat /proc/cmdline
Interactive GRUB
If we the GRUB menu is not correct we may not be able to boot the system but it is possible to access the GRUB command line so long as access to GRUB succeeded from the stage 1 boot. The system will revert to a text menu and we can choose the option c to select the command line. From here we can enter the root, kernel and initrd values followed by the command boot. When accessing the kernel and initrd files we have TAB completion available to use to we can verify the correct path and file name