Note: This is an RHCSA 7 exam objective.
Presentation of GRUB2
GRUB2 is the new Linux bootloader. GRUB2 stands for GRand Unified Bootloader version 2.
As GRUB was not maintained for some time and lacked some critical features like GPT management needed to handle disks bigger than 2.4TB, it was decided to start a new version from scratch with modularity in mind.
GRUB2 provides the following new features:
- ability to boot on various file systems (xfs, ext4, ntfs, hfs+, raid, etc),
- gzip files decompression on the fly,
- management of all disk geometries,
- support for GPT (GUID Partition Tables) and MBR (Master Boot Record),
- portability with different architectures (BIOS, EFI, Coreboot, etc),
- ability to load modules at execution time.
GRUB2 Organization
The GRUB2 configuration is spread over several files:
- /boot/grub2/grub.cfg: this file contains the final GRUB2 configuration (do not edit it directly!),
- /etc/grub2.cfg: this is a symbolic link to the /boot/grub2/grub.cfg file,
- /etc/default/grub: this file contains the list of the GRUB2 variables (the values of the environment variables can be edited),
- /etc/sysconfig/grub: this is a symbolic link to the /etc/default/grub file,
- /etc/grub.d: this directory contains all the individual files internally used by GRUB2.
This tutorial will only explore knowledge required for the RHCSA exam.
Refer to the Additional Resources section for more details.
Basic Management
To get the details about the current active kernel, type:
# grub2-editenv list saved_entry=CentOS Linux (3.10.0-229.11.1.el7.x86_64) 7 (Core)
Note: This information is stored in the /boot/grub2/grubenv file.
To get the list of the kernels displayed at boot time, type:
# grep ^menuentry /boot/grub2/grub.cfg menuentry 'CentOS Linux (3.10.0-229.20.1.el7.x86_64) 7 (Core)' ... menuentry 'CentOS Linux (3.10.0-229.14.1.el7.x86_64) 7 (Core)' ... menuentry 'CentOS Linux 7 (Core), with Linux 0-rescue-f19b719117b44bf3a3fb777bd4127' ...caf
To permanently define the kernel to execute at boot time (here 0 for the first entry), type:
# grub2-set-default 0
To display the GRUB2 variables, type:
# cat /etc/default/grub GRUB_TIMEOUT=5 GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)" GRUB_DEFAULT=saved GRUB_DISABLE_SUBMENU=true GRUB_TERMINAL="serial console" GRUB_SERIAL_COMMAND="serial --speed=115200" GRUB_CMDLINE_LINUX="rd.lvm.lv=rhel/swap crashkernel=auto rd.lvm.lv=rhel/root console=ttyS0,115200" GRUB_DISABLE_RECOVERY="true"
Where
- GRUB_TIMEOUT defines the boot waiting delay (here 5 seconds),
- GRUB_DISTRIBUTOR contains the distribution name (here CentOS Linux),
- GRUB_DEFAULT specifies the default menu entry; it can be a number, an entry name or the string saved which means the entry saved during the last reboot or the execution of the grub2-set-default command,
- GRUB_DISABLE_SUBMENU allows (false) or not (true) the display of a submenu (see below),
- GRUB_TERMINAL defines the terminal input & output device (here console and serial),
- GRUB_SERIAL_COMMAND configures the serial port,
- GRUB_CMDLINE_LINUX specifies the command-line arguments added to the menu entries for the Linux kernel,
- GRUB_DISABLE_RECOVERY defines if all entries can be selected in recovery mode through a separate line (false) or only the default entry (true).
If you want to change the content of any variables in the previous file, you will need to type:
# grub2-mkconfig -o /boot/grub2/grub.cfg
Note: This is the main command to memorize for the exam. You can also replace /boot/grub2/grub.cfg with /etc/grub2.cfg
To better understand some of the environment variables, here are the standard display with GRUB_DISABLE_RECOVERY=”true” and GRUB_DISABLE_SUBMENU=true:
CentOS Linux 7 (Core), with Linux 3.10.0-229.20.1.el7.x86_64 CentOS Linux 7 (Core), with Linux 3.10.0-229.14.1.el7.x86_64 CentOS Linux 7 (Core), with Linux 0-rescue-f19b719117b44bf3a3fb777bd4127> Use the ^ and v keys to change the selection. Press 'e' to edit the selected item, or 'c' for a command prompt.
If GRUB_DISABLE_RECOVERY is set to “false”, here is the new display:
CentOS Linux 7 (Core), with Linux 3.10.0-229.20.1.el7.x86_64 CentOS Linux 7 (Core), with Linux 3.10.0-229.20.1.el7.x86_64 (recovery m> CentOS Linux 7 (Core), with Linux 3.10.0-229.14.1.el7.x86_64 CentOS Linux 7 (Core), with Linux 3.10.0-229.14.1.el7.x86_64 (recovery m> CentOS Linux 7 (Core), with Linux 0-rescue-f19b719117b44bf3a3fb777bd4127> CentOS Linux 7 (Core), with Linux 0-rescue-f19b719117b44bf3a3fb777bd4127> Use the ^ and v keys to change the selection. Press 'e' to edit the selected item, or 'c' for a command prompt.
Each kernel line gets an associated line in recovery mode.
If GRUB_DISABLE_RECOVERY is now set to “true” (like in the initial standard display) and GRUB_DISABLE_SUBMENU is set to false, here is the new display:
CentOS Linux 7 (Core) Advanced options for CentOS Linux 7 (Core) Use the ^ and v keys to change the selection. Press 'e' to edit the selected item, or 'c' for a command prompt.
If the first entry is selected (“CentOS Linux 7 (Core)”), the system boots. If the second option is chosen, the standard menu is shown with an additional line at the bottom to go back to the first menu with the Esc key:
CentOS Linux 7 (Core), with Linux 3.10.0-229.20.1.el7.x86_64 CentOS Linux 7 (Core), with Linux 3.10.0-229.14.1.el7.x86_64 CentOS Linux 7 (Core), with Linux 0-rescue-f19b719117b44bf3a3fb777bd4127> Use the ^ and v keys to change the selection. Press 'e' to edit the selected item, or 'c' for a command prompt. Press Escape to return to the previous menu.
Additional Resources
To better explore GRUB2, you can:
- read Brendan Swigart’s article,
- loot at a GRUB2 CentOS article,
- read the GRUB2 ArchLinux wiki,
- explore the GRUB2 configuration manual,
- read a Tecmint article about configuring and troubleshooting GRUB2,
- read a LinuxSecrets tutorial about changing boot order with GRUB2,
- read this article describing the RHEL 7 booting process.
Hi CertDepot,
So in the exam if there is a question like modify the grub timeout to 10 we just edit the /etc/default/grub file and modify the GRUB_TIMEOUT=10, then run
# grub2-mkconfig -o /boot/grub2/grub.cfg ?
Yes, I think so.
Hi Certdepot, do you have a procedure in rhel 7 to create another boot device, like for example I want sdb1 to be an alternate boot device. I need to know as well how to configure it in grub so upon bootup it will boot from the preferred drive.
I haven’t got any answer for you. I think you should ask your question on the ServerFault website.
Sorry.
Alternate boot device… let’s think logically here for a bit: GRUB2 needs to know where to find the kernel and its initram image in order to boot the kernel, right? So:
– know how to tell GRUB2 where to look for a kernel and its accompanying initramfs.
– learn the syntax of addressing boot-capable devices when configuring GRUB2.
– force GRUB2 into autodiscovering the new kernel and autoediting appropriate config files.
Can you please provide all I have to do to update a linux kernel from a given url? (ex: http://server1.host1.example.com/rhcsa. )
yum install http://server1.host1.example.com/rhcsa/kernelxxx.rpm should be enough.
Ok, I have mastered the grub2-mkconfig -0 /boot/grub2/grub.cfg command..
I edit the file /etc/default/grub, but I can login to higher version of kernel..which is easy..but why not the lower?
I have been mastered the grubby command as well.
Hi CertDepot,
I mistakenly run grub2-mkconfig -o /etc/default/grub
Is there any way to regenerate the grub file?
Edit the /etc/default/grub file, copy the content of this file shown in the tutorial:
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR=”$(sed ‘s, release .*$,,g’ /etc/system-release)”
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL=”serial console”
GRUB_SERIAL_COMMAND=”serial –speed=115200″
GRUB_CMDLINE_LINUX=”rd.lvm.lv=rhel/swap crashkernel=auto rd.lvm.lv=rhel/root console=ttyS0,115200″
GRUB_DISABLE_RECOVERY=”true”
Thanks for your reply but is there any way to re-generate through command or reinstalling package? I was wondering if this happened in live exam what will we do.
You can always reset the machine (as the last resort probably).
In an exam environment or even production machine this is something to do with caution. You don’t want the machine to be unbootable.
With grub version 2, most updated OS use this version, there is a method to scan for kernels. Stick with manually editing the relevant file. Usually yum installs the kernel to the grub. lookup grub-probe. I would suggest testing this several times.