Technotes

Technotes for future me

Grub

Fix grub

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

Split Grub entries

When grub only show the entries below:
Red Hat Enterprise Linux Server
Advanced options for Red Hat Enterprise Linux Server

It can be restored to regular menu by moving the submenu entry below the last menuentry.

Rebuilding the initramfs (RHEL 6, 7)

If you are in a kernel version different to the initrd you are building (including if you are in Rescue Mode) you must specify the full kernel version, including architecture:

dracut -f /boot/initramfs-2.6.32-220.7.1.el6.x86_64.img 2.6.32-220.7.1.el6.x86_64

Checking initramfs (RHEL 7)

In RHEL 7, if the initramfs for a kernel is missing, be certain that the /etc/grub2.cfg and /boot/grub2/grub.cfg includes the reference to the newly installed initramfs, or is rebuilt to include this reference.

grep initrd /etc/grub2.cfg
    initrd16 /initramfs-3.10.0-514.6.1.el7.x86_64.img
    initrd16 /initramfs-3.10.0-514.el7.x86_64.img
    initrd16 /initramfs-0-rescue-29579289bce743ebbf3d42aa22ebd5fe.img

grep "menuentry " /boot/grub2/grub.cfg
menuentry 'Red Hat Enterprise Linux Server (3.10.0-514.6.1.el7.x86_64) ...'
menuentry 'Red Hat Enterprise Linux Server (3.10.0-514.el7.x86_64) ... '
menuentry 'Red Hat Enterprise Linux Server (0-rescue-29579289bce743ebbf3d42aa22ebd5fe) ...'

To rebuild the /boot/grub2/grub.cfg file:

On BIOS-based machines, issue the following command as root:

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

On UEFI-based machines, issue the following command as root:

grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg

Source: https://access.redhat.com/solutions/1958

Set default boot kernel

The boot configuration when using GRUB 2 is in the /boot/grub2/grub.cfg file. You can also refer to it by the /etc/grub2.cfg file which is a symbolic link. To force a system to always use a particular menu entry and to set default boot kernel, use the menu entry name as the key to the GRUB_DEFAULT directive in the /etc/default/grub file.

To list the available menu entries, run the following command as root:

awk -F\' '$1=="menuentry " {print $2}' /etc/grub2.cfg
CentOS Linux (3.10.0-957.1.3.el7.x86_64) 7 (Core)       <<==== Entry 0
CentOS Linux (3.10.0-693.el7.x86_64) 7 (Core)           <<==== Entry 1
CentOS Linux (0-rescue-4c3582b97843401fbc3c8e68c07e553c) 7 (Core)

GRUB 2 supports using a numeric value as the key for the saved_entry directive to change the default order in which the kernel or operating systems are loaded. To specify which kernel or operating system should be loaded first i.e. to set default boot kernel, pass its number to the grub2-set-default command which sets the default boot menu entry for all subsequent boots.

Note:

grub2-set-default command only works for GRUB configuration files created with GRUB_DEFAULT=saved in /etc/default/grub.

grep GRUB_DEFAULT /etc/default/grub
GRUB_DEFAULT=saved

For example to set default boot kernel let us use numeric value. Now as per our /etc/grub2.cfg we have two menuentry for kernel

awk -F\' '$1=="menuentry " {print $2}' /etc/grub2.cfg
CentOS Linux (3.10.0-957.1.3.el7.x86_64) 7 (Core)       <<==== Entry 0
CentOS Linux (3.10.0-693.el7.x86_64) 7 (Core)           <<==== Entry 1
CentOS Linux (0-rescue-4c3582b97843401fbc3c8e68c07e553c) 7 (Core)

where “entry 0” is loaded currently (3.10.0-957.1.3.el7.x86_64).

Let us change it to old or previous kernel version (3.10.0-693.el7.x86_64)

grub2-set-default 1

Validate the changes

grep saved /boot/grub2/grubenv
saved_entry=1

Next rebuild your GRUB2 configuration

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

Now after rebooting the node, we see the old kernel has successfully loaded

uname -r
3.10.0-693.el7.x86_64

Source: https://www.golinuxcloud.com/set-default-boot-kernel-version-old-previous-rhel-linux/

Last updated on 31 Jan 2021
Published on 11 Dec 2019
Edit on GitHub