GRUB

From DikapediaV2
Revision as of 00:18, 27 August 2024 by Ardika Sulistija (talk | contribs) (Created page with " GRUB -- Grand Unified Bootloader; executes the Kernel. * GRUB goes through two stages. * After the two stages, the kernel and initrd is loaded into memory. * Config files: ** <b>GRUB 1</b> (old vers) <b>/etc/grub.conf</b> -> <b>/boot/grub/grub.conf</b> ** <b>GRUB 2</b> (New Grub) <b>/etc/grub2.cfg</b> -> <b>/boot/grub2/grub.cfg</b> *** GRUB 2 reads its configuration from the <b>/boot/grub2/grub.cfg</b> file on traditional <b>BIOS-based</b> machines and from the <b>/boot...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

GRUB -- Grand Unified Bootloader; executes the Kernel.

  • GRUB goes through two stages.
  • After the two stages, the kernel and initrd is loaded into memory.
  • Config files:
    • GRUB 1 (old vers) /etc/grub.conf -> /boot/grub/grub.conf
    • GRUB 2 (New Grub) /etc/grub2.cfg -> /boot/grub2/grub.cfg
      • GRUB 2 reads its configuration from the /boot/grub2/grub.cfg file on traditional BIOS-based machines and from the /boot/efi/EFI/redhat/grub.cfg file on UEFI machines. This file contains menu information.

The GRUB 2 configuration file, grub.cfg, is generated during installation, or by running the /usr/sbin/grub2-mkconfig utility, and is automatically updated by grubby each time a new kernel is installed. When regenerated manually using grub2-mkconfig, the file is generated according to the template files located in /etc/grub.d/, and custom settings in the /etc/default/grub file. Edits of grub.cfg will be lost any time grub2-mkconfig is used to regenerate the file, so care must be taken to reflect any manual changes in /etc/default/grub as well.

See: https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/system_administrators_guide/ch-working_with_the_grub_2_boot_loader

Making Persistent Changes to a GRUB 2 Menu Using the grubby Tool


https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/system_administrators_guide/ch-working_with_the_grub_2_boot_loader#sec-Making_Persistent_Changes_to_a_GRUB_2_Menu_Using_the_grubby_Tool

Listing the Default Kernel

To find out the file name of the default kernel, enter a command as follows:

~]# grubby --default-kernel
/boot/vmlinuz-3.10.0-229.4.2.el7.x86_64

To find out the index number of the default kernel, enter a command as follows:

~]# grubby --default-index
0

Changing the Default Boot Entry

To make a persistent change in the kernel designated as the default kernel, use the grubby command as follows:

~]# grubby --set-default /boot/vmlinuz-3.10.0-229.4.2.el7.x86_64

Viewing the GRUB 2 Menu Entry for a Kernel

To list all the kernel menu entries, enter a command as follows:

~]$ grubby --info=ALL

On UEFI systems, all grubby commands must be entered as root.

To view the GRUB 2 menu entry for a specific kernel, enter a command as follows:

~]$ grubby --info /boot/vmlinuz-3.10.0-229.4.2.el7.x86_64
index=0
kernel=/boot/vmlinuz-3.10.0-229.4.2.el7.x86_64
args="ro rd.lvm.lv=rhel/root crashkernel=auto rd.lvm.lv=rhel/swap vconsole.font=latarcyrheb-sun16  vconsole.keymap=us rhgb quiet LANG=en_US.UTF-8"
root=/dev/mapper/rhel-root
initrd=/boot/initramfs-3.10.0-229.4.2.el7.x86_64.img
title=Red Hat Enterprise Linux Server (3.10.0-229.4.2.el7.x86_64) 7.0 (Maipo)

Try tab completion to see the available kernels within the /boot/ directory.

What you see in grubby is the same as /boot/grub2/grub.cfg or /etc/grub2, as grubby operates directly on those.


If the above doesn't change the default (like Neshon's case) try to use --set-default-index=<val> to change default kernel: https://www.golinuxcloud.com/grubby-command-examples/#12_Set_default_kernel_using_index


MY FAVORITE WAY

Got this from xxxx64681 - Uninstalled the kernel version that was 0, and reinstalled kernel updates, chose the new 0 by setting the default, and ran mkconfig command and rebooted.

Search for kernel versions in grub (couple ways):

# awk -F\' '/menuentry / {print $2}' /boot/grub2/grub.cfg
CentOS Linux (3.10.0-1160.24.1.el7.x86_64) 7 (Core)
CentOS Linux (3.10.0-1127.19.1.el7.x86_64) 7 (Core)
CentOS Linux (3.10.0-1127.13.1.el7.x86_64) 7 (Core)
CentOS Linux (3.10.0-514.21.2.el7.x86_64) 7 (Core)
CentOS Linux (0-rescue-8e384ff5a5d64280bb8b6750dfeafcaa) 7 (Core)

$ sudo awk -F\' '$1=="menuentry " {print i++ " : " $2}' /boot/grub2/grub.cfg
0 : Amazon Linux (4.14.219-161.340.amzn2.x86_64) 2
1 : Amazon Linux (4.14.181-140.257.amzn2.x86_64) 2
2 : Amazon Linux (4.14.177-139.254.amzn2.x86_64) 2
To change:
sudo yum remove 3.10.0-1062.12.1.el7.x86_64
sudo yum install kernel
sudo yum update kernel
grub2-set-default 0
grub2-mkconfig -o /boot/grub2/grub.cfg
sudo reboot


How To's



How to roll back the kernel version

https://aws.amazon.com/premiumsupport/knowledge-center/ec2-linux-kernel-panic-unable-mount/

"VFS: Cannot open root device XXX or unknown-block(0,0)
Please append a correct "root=" boot option; here are the available partitions:
Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0)"

Use EC2 Rescue instance to rollback to earlier kernel version.

Steps where /dev/xvdf2 is root FS: (OLD WAY)

mount /dev/xvdf2 /mnt
mount -o bind /proc /mnt/proc && mount -o bind /dev /mnt/dev && mount -o bind /sys /mnt/sys
chroot /mnt
vi /etc/default/grub (Change to GRUB_DEFAULT=1)
grub2-mkconfig -o /boot/grub2/grub.cfg (grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg for EFI)
exit
umount /mnt/{proc,sys,dev} && umount /mnt

If the above doesn't work try the link below:

How do I revert to a known stable kernel after an update prevents my Amazon EC2 instance from rebooting successfully?>

https://aws.amazon.com/premiumsupport/knowledge-center/revert-stable-kernel-ec2-reboot/

How to install an old kernel version
$ sudo yum -y install kernel-4.14.209-160.339.amzn2.x86_64

# and then run this...sometimes? Didn't have to do this on AL2 though.
dracut -f -v /boot/initrd-4.12.14-95.51-default.img 4.12.14-95.51-default
dracut -f -v /boot/initramfs-2.6.32-696.10.3.el6.x86_64.img 2.6.32-696.10.3.el6.x86_64 
$ sudo reboot


Another Example of installing kernel from rpm.pbone.net RHEL6

https://linoxide.com/install-and-update-redhat-kernel-rpm/

1. Installed the exact same kernel as customers on RHEL6 (2.6.32-754.35.1): 1A. http://rpm.pbone.net/resultsb_dist_79_size_30318452_name_kernel-firmware-2.6.32-754.35.1.el6.noarch.rpm.html

# wget ftp://ftp.pbone.net/mirror/vault.centos.org/6.10/updates/x86_64/Packages/kernel-firmware-2.6.32-754.35.1.el6.noarch.rpm
# rpm -i kernel-firmware-2.6.32-754.35.1.el6.noarch.rpm

1B. http://rpm.pbone.net/info_idpl_73482398_distro_centos6_com_kernel-devel-2.6.32-754.35.1.el6.x86_64.rpm.html

# wget ftp://ftp.pbone.net/mirror/vault.centos.org/6.10/updates/x86_64/Packages/kernel-devel-2.6.32-754.35.1.el6.x86_64.rpm
# rpm -i kernel-devel-2.6.32-754.35.1.el6.x86_64.rpm

1C. http://rpm.pbone.net/resultsb_dist_79_size_33985172_name_kernel-2.6.32-754.35.1.el6.x86_64.rpm.html

# wget ftp://ftp.pbone.net/mirror/vault.centos.org/6.10/updates/x86_64/Packages/kernel-2.6.32-754.35.1.el6.x86_64.rpm
# rpm -i kernel-2.6.32-754.35.1.el6.x86_64.rpm

1D. Run dracut:

dracut -v -f /boot/initramfs-2.6.32-754.35.1.el6.x86_64.img 2.6.32-754.35.1.el6.x86_64

1E. Verified grub, default is 0, and is pointing to the kernel 2.6.32-754.35:

cat /etc/grub.conf

5. Reboot and confirm the kernel version is correct

# uname -r
2.6.32-754.35.1.el6.x86_64


How to search and download a kernel / kernel-devel package that is not available in the repositories by using RPM

1) Use http://rpm.pbone.net/ to search for the package. Then click on the package you want based on your OS.

2) Right click on the package and copy link.

2) Use wget to download the package:

# wget ftp://ftp.pbone.net/mirror/vault.centos.org/6.8/updates/x86_64/Packages/kernel-devel-2.6.32-642.11.1.el6.x86_64.rpm
--2021-04-29 23:20:30--  ftp://ftp.pbone.net/mirror/vault.centos.org/6.8/updates/x86_64/Packages/kernel-devel-2.6.32-642.11.1.el6.x86_64.rpm
           => “kernel-devel-2.6.32-642.11.1.el6.x86_64.rpm”
Resolving ftp.pbone.net... 93.179.225.212
Connecting to ftp.pbone.net|93.179.225.212|:21... connected.
Logging in as anonymous ... Logged in!
==> SYST ... done.    ==> PWD ... done.
==> TYPE I ... done.  ==> CWD (1) /mirror/vault.centos.org/6.8/updates/x86_64/Packages ... done.
==> SIZE kernel-devel-2.6.32-642.11.1.el6.x86_64.rpm ... 11105876
==> PASV ... done.    ==> RETR kernel-devel-2.6.32-642.11.1.el6.x86_64.rpm ... done.
Length: 11105876 (11M) (unauthoritative) 

100%[==================================================================================================>] 11,105,876  4.32M/s   in 2.5s    

2021-04-29 23:20:34 (4.32 MB/s) - “kernel-devel-2.6.32-642.11.1.el6.x86_64.rpm” saved [11105876]

4) Check to see if it downloaded:

# ls -al kern*
-rw-r--r--. 1 root root 33393432 Apr 29 23:13 kernel-devel-2.6.32-642.11.1.el6.x86_64.rpm

5) Use rpm to install the package:

# rpm -i kernel-devel-2.6.32-642.11.1.el6.x86_64.rpm

6) Check to see if it was installed:

# rpm -qa | grep kernel
kernel-devel-2.6.32-642.11.1.el6.x86_64

7) To uninstall:

# rpm -e kernel-devel-2.6.32-642.11.1.el6.x86_64
# rpm -qa | grep kernel

How to Change the Default Ubuntu Kernel on AWS

https://meetrix.io/blog/aws/changing-default-ubuntu-kernel.html

(azure to aws using CE) case


How to check if machine is running BIOS or UEFI


[ -d /sys/firmware/efi ] && echo UEFI || echo BIOS
[ -d /sys/firmware/efi ] && echo "UEFI Boot Detected" || echo "Legacy BIOS

Add notes about grub2-install (/boot/grub2) vs grub-install (/boot/grub)


Look at my case


Kernel paramaters


https://linux.die.net/man/8/dracut
https://mirrors.edge.kernel.org/pub/linux/utils/boot/dracut/dracut.html#dracutcmdline7
https://man7.org/linux/man-pages/man8/systemd-udevd.service.8.html
https://www.kernel.org/doc/html/v4.14/admin-guide/kernel-parameters.html
https://www.theburningofrome.com/contributing/what-is-rhgb-quiet-in-grub-conf/

/etc/default/grub is the main (master) configuration file. It is used by the grub2-mkconfig tool, which is used by anaconda when creating grub. cfg during the installation process, and can be used in the event of a system failure, for example if the boot loader configurations need to be recreated.

These are some that I have encountered. Some are common, some are not:

  • rd.lvm=0
    • disable LVM detection
  • rd.lvm.vg=<volume group name>
    • only activate all logical volumes in the the volume groups with the given name. rd.lvm.vg can be specified multiple times on the kernel command line.
  • rd.lvm.lv=<volume group name>/<logical volume name>
    • only activate the logical volumes with the given name. rd.lvm.lv can be specified multiple times on the kernel command line.
  • rd.lvm.conf=0
    • remove any /etc/lvm/lvm.conf, which may exist in the initramfs
  • rd_NO_LVMCONF
    • remove any /etc/lvm/lvm.conf, which may exist in the initramfs
  • biosdevname=0
    • boolean, turn off biosdevname network interface renaming
  • modprobe.blacklist
    • For RHEL-7 the kernel command line parameter modprobe.blacklist=<module name> can be used to blacklist the module for the initramfs as well as the real root, without the need to create a modprobe.d configuration file and regeneration of the initramfs.
  • net.ifnames
    • Network interfaces are renamed to give them predictable names when possible. It is enabled by default; specifying 0 disables it.
  • nomodeset
    • The nomodeset parameter instructs the kernel to not load video drivers and use BIOS modes instead until X is loaded. Note: Many open source drivers have removed support for non-kernel mode setting.
  • rdblacklist=<drivername>
    • do not load kernel module <drivername> This parameter can be specified multiple times.
  • consoleblank= [KNL]
    • The console blank (screen saver) timeout in seconds. Defaults to 10*60 = 10mins. A value of 0 disables the blank timer.
  • rhgb
    • redhat graphical boot – This is a GUI mode booting screen with most of the information hidden while the user sees a rotating activity icon spining and brief information as to what the computer is doing.
  • quiet
    • quiet = hides the majority of boot messages before rhgb starts.
  • rd.break
    • The rd.break parameter interrupts the boot process before the control is passed over to the kernel. At this point, when you run the passwd command to do the password reset, the associated shadow file (/etc/shadow) is modified with an incorrect SELinux context. The touch /.autorelabel command creates a hidden file named .autorelabel under the root directory. On the next boot, the SELinux subsystem will detect this file, and then relabel all of the files on that system with the correct SELinux contexts. On large disks, this process can take a good amount of time.
    • This is the equivalent of when the system boots into emergency mode/dracut prompt due to some boot failure.
    • In this scenario, you may see an empty fstab file (i.e. etc/fstab.empty)
  • init=/bin/bash
    • This is a feature, and is used for system maintainance: it allows a sysadmin to recover a system from messed-up initialization files like /etc/fstab or change a forgotten password. In Unix-like systems, init is the first process to be run, and the ultimate ancestor of all processes ever run. It's responsible for running all the init scripts. So when you do init=/bin/bash, you're telling the Linux kernel to run /bin/bash as init, rather than the system init.


Resetting Root Password using rd.break


You will have to reset the password manually by interrupting grub by using the kernel parameter rd.break.

1. Boot the system and interrupt the booting process when you're at the GRUB menu by pressing 'e'.
2. Add the kernel parameter rd.break to the Linux kernel line.
3. Press 'ctrl+x' to boot.
4. You will enter the emergency mode prompt (or preboot screen, per Joe) to authenticate to unlock the drive.
5. Then run the following commands:

# mount -o remount,rw /sysroot
# chroot /sysroot
# passwd 
# touch /.autorelabel
# exit
# logout

6. A couple of minutes and once done, the system will reboot upon which you can log in as the root user with the new password.


Reference: [+] https://www.tecmint.com/reset-forgotten-root-password-in-rhel-8/ [+] https://learn.redhat.com/t5/Platform-Linux/Unable-to-reset-the-root-password-when-disabling-SELinux/td-p/21082 [+] https://unix.stackexchange.com/questions/509798/what-does-touch-autorelabel-do-when-we-reset-the-root-password-in-red-hat-en/509801#509801


Using init=/bin/bash when system is booting to Emergency Mode


This method works on a workstation.

You can use this method if a user has misconfigured an /etc/fstab entry.

  1. On boot/grub menu, select "e" to edit the kernel boot line. At the end of the line, add "init=/bin/bash". Ctrl-X to continue the boot process.
  2. After the above was done, you end up at a shell prompt.
  3. The root filesystem is read-only, so we had to remount it with rw permissions by running: "mount -o remount,rw /"
  4. Edit /etc/fstab as needed.
  5. Reboot system