CentOS / RHEL 7 : GRUB2 configuration file /boot/grub2/grub.cfg explained

The GRUB2 configuration file /boot/grub2/grub.cfg

– Starting RHEL 7 GRUB 2 is the bootloader. The GRUB 2 configuration file is /boot/grub2/grub.cfg.

– Do not edit this file directly. Use the grub2-mkconfig command to generate grub.cfg. This command uses the template scripts in /etc/grub.d and menu-configuration settings taken from /etc/default/grub when generating grub.cfg.

– The /etc/grub2.cfg file is a symbolic link to /boot/grub2/grub.cfg.

 

Below is a sample GRUB2 configuration file.

..... (skiped for breivity)
### BEGIN /etc/grub.d/10_linux ###
menuentry 'CentOS Linux (3.10.0-514.el7.x86_64) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-514.el7.x86_64-advanced-10bac86a-a9be-478c-b63f-46d3ca061e03' {
	load_video
	set gfxpayload=keep
	insmod gzio
	insmod part_msdos
	insmod xfs
	set root='hd0,msdos1'
	if [ x$feature_platform_search_hint = xy ]; then
	  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1'  a52207e4-01fd-4f12-98b7-681d56f21dc5
	else
	  search --no-floppy --fs-uuid --set=root a52207e4-01fd-4f12-98b7-681d56f21dc5
	fi
	linux16 /vmlinuz-3.10.0-514.el7.x86_64 root=/dev/mapper/cl-root ro crashkernel=auto rd.lvm.lv=cl/root rd.lvm.lv=cl/swap rhgb quiet LANG=en_US.UTF-8
	initrd16 /initramfs-3.10.0-514.el7.x86_64.img
}
menuentry 'CentOS Linux (0-rescue-9cdb9ab3246a4b3f9c0849ecd597f25e) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-0-rescue-9cdb9ab3246a4b3f9c0849ecd597f25e-advanced-10bac86a-a9be-478c-b63f-46d3ca061e03' {
	load_video
	insmod gzio
	insmod part_msdos
	insmod xfs
	set root='hd0,msdos1'
	if [ x$feature_platform_search_hint = xy ]; then
	  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1'  a52207e4-01fd-4f12-98b7-681d56f21dc5
	else
	  search --no-floppy --fs-uuid --set=root a52207e4-01fd-4f12-98b7-681d56f21dc5
	fi
	linux16 /vmlinuz-0-rescue-9cdb9ab3246a4b3f9c0849ecd597f25e root=/dev/mapper/cl-root ro crashkernel=auto rd.lvm.lv=cl/root rd.lvm.lv=cl/swap rhgb quiet
	initrd16 /initramfs-0-rescue-9cdb9ab3246a4b3f9c0849ecd597f25e.img
}
.......

 

The /etc/default/grub File

 

GRUB 2 menu-configuration settings are taken from /etc/default/grub when generating grub.cfg. The following lists the contents of the /etc/default/grub file:

# 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_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=cl/root rd.lvm.lv=cl/swap rhgb quiet"
GRUB_DISABLE_RECOVERY="true" 

 

If changes are made to any of these parameters, you need to run grub2-mkconfig to re-generate the /boot/grub2/grub.cfg file. For example:

 # grub2-mkconfig –o /boot/grub2/grub.cfg

 

Values are described as follows:

GRUB_TIMEOUT: The time in seconds after the menu is displayed to boot the default entry, unless a key is pressed. The default is 5. Set to 0 to boot immediately without displaying the menu, or to -1 to wait indefinitely.

GRUB_DISTRIBUTOR: Set by distributors of GRUB and is used to generate more informative menu entry titles. The example evaluates to CentOS Linux Server.

GRUB_DEFAULT : The default menu entry to boot. A value of 0 boots the first menuentry. A value of 1 boots the second menuentry. A value of saved instructs GRUB 2 to load the last successfully loaded operating system.

 

A GRUB_DEFAULT value of saved also allows you to use the grub2-set-default and grub2-reboot commands to specify the default entry. These two commands are described as follows:

grub2-set-default: Sets the default entry for all subsequent reboots

grub2-reboot: Sets the default entry for the next reboot only

 

For example, with GRUB_DEFAULT=saved, the following command sets the default entry for all subsequent reboots to the second menuentry:

# grub2-set-default 1 

 

GRUB_DISABLE_SUBMENU: By default, the grub2-mkconfig command generates a top-level menu entry for the kernel with highest version number and puts all other found kernels or alternative menu entries for recovery mode in a submenu. Setting GRUB_DISABLE_SUBMENU=true disables this.

GRUB_TERMINAL_OUTPUT: The terminal output device. When specifying multiple devices, separate the valid terminal output names with spaces.

GRUB_CMDLINE_LINUX: Kernel boot parameters. More information on kernel boot parameters is provided in the next slide.

GRUB_DISABLE_RECOVERY: By default, two menu entries are generated for each Linux kernel: one default entry and one entry for recovery mode. Setting GRUB_DISABLE_RECOVERY=“true” disables this.

 

Explaining menuentry

 

The GRUB 2 configuration file, /boot/grub2/grub.cfg, contains menuentry stanzas, which represent an installed Linux kernel. Each stanza begins with the menuentry keyword with options. Each menuentry is also a single boot menu entry in the GRUB 2 menu. The associated block of code is enclosed in curly brackets, { }.

 

The stanza includes a linux16 directive followed by the path to the kernel and an initrd16 directive followed by the path to the initramfs image. The linux16 directive specifies the kernel version number to be booted as well as kernel boot parameters. A separate /boot partition was created; therefore, the path to the kernel (as well as to the initramfs image) are relative to /boot.

 

The initrd16 directive must point to the location of the initramfs file corresponding to the same kernel version. In other words, the kernel as given on the linux16 /vmlinuz-[kernel_version] line must match the version number of the initramfs image given on the initrd16 /initramfs-[kernel_version].img line of each stanza.

 

Was this answer helpful? 0 Users Found This Useful (0 Votes)