GRUB – GRand Unified Bootloader plays crucial role in proper and stable work of a Linux system. Thanks to its the Linux is able to load the system in different modes but also enables us to recover the OS if something went wrong or the system got crashed.
The GRUB is a Linux bootloader, the program that is usually installed between Master Boot Record of a hard drive and the first partition. Thanks to GRUB we may boot the system with different kernels, in different target modes or if we have more operational systems GRUB enables us choosing between them. GRUB configuration can be changed in the fly during booting, from the menu we may press ‘E‘ to temporarily edit the file or press ‘C‘ to open GRUB command prompt.
If we want to add any parameters to the kernel via GRUB, we have to add them to line that starts with ‘linux16‘. If we are going to boot system into specified mode we add at the end of the line ‘systemd.unit=name.target‘. When there is a problem with X Window we may run linux in ‘multi-user.target’ or ‘rescue.target’.
GRUB config file is placed in /boot/grub2/grub.cfg and consists of a ‘header‘ section and ‘menuentries‘ that number depends on number of kernels installed on the system. Each menuentry block containes two lines ‘linux16‘ and ‘initrd16‘. They specify the path of the kernel and RAM disk filesystem that is loaded during the boot process. This GRUB config file is exactly the same that appears during booting when you press ‘E‘.
If we want to make any changes regarding booting process, we have to use grub2-mkconfig tool. We have to generate the new version of the file /boot/grub2/grub.cfg based on file /etc/default/grub and scripts in /etc/grub.d/. After making changes in /etc/default/grub file we generate new grub.cfg file.
grub2-mkconfig -o /boot/grub2/grub.cfg
It makes no sense doing changes directly to file /boot/grub2/grub.cfg because this file is automatically generated and any change we made will be lost during the booting.
Here is the file /etc/default/grub that we should edit
GRUB_TIMEOUT – the time before GRUB will proceed to loading the default OS
GRUB_DEFAULT – default kernel, ‘saved‘ instructs GRUB to check at the saved_entry variable in the file /boot/grub2/grubenv .
GRUB_TERMINAL_OUTPUT – specifies the option for the kernel. ‘rd.lvm.lv‘ – the name of the logical volume, where root filesystem and swap partition are placed. ‘vconsole.font‘ and ‘vconsole.keymap‘ – default fonts and keyboard. ‘Crashkernel‘ is used to reserve memory for kdump (captures a kernel core dump if the system crashed). ‘Rhgb quiet‘ hides boot messages end enable graphical boot.
If we want to update GRUB we have to run ‘grub2-install‘ command. We may also specify the hard drive if we have more drives- ‘grub2-install /dev/sda‘. If we’ve generated new grub file with grub2-mkconfig tool then we don’t have to run any other command.
If there is an error in GRUB, it may happens that system will be unbootable. Then GRUB will go into CLI automatically, we may also get into CLI by pressing ‘C’ key. By pressing TAB key we get the list with commands that we may use in given moment, similar to ‘?’ in Cisco. In below example I was searching the ‘grub’ file in order to find /boot partition with ‘grub.cfg’ file. As you see the syntax of commands is different than standar command line, also partitions are named differently because haven’t been mounted yet.
If the grub2-mkconfig doesn’t work might be necessary reinstallation of grub2-tools. We are doing this in this way, firstly we remove old files , than we reinstall grub2-tools and generate new grub.cfg file
GRUB Rescue Mode
Booting in troubleshooting mode requires access to DVD with system or to the system via network. Everything boils down to mounting filesystem to /mnt/sysimage and making its a root partition, then we may work and make any changes on discovered partitions.
Root Password Recovery (CentOS)
In case of forgotten password, the procedure of recovery is really simple and consists of a couple of steps.
1. On the screen with menu of systems press ‘E’
2. At the end of the line ‘linux16’ add ‘rd.break‘
3. Press CTRL+X to boot the system
4. the ‘rd.break’ interrupts booting before root filesystem is mounted. Because filesystem is not mounted we may access to its by ‘ls /sysroot‘
5. Remount the root /sysroot filesystem as read-write and change the root directory to /sysroot
mount -o remount,rw /sysroot
6. Now, change the password
7.Because SELinux hasn’t been run, the passwd command doesn’t preserve the context of /etc/passwd file. To ensure that the /etc/passwd file is labeled with the correct SELinux context, instruct Linux to relabel all files at the next boot with the command:
8. Now type 2 times ‘exit’ to leave chroot jail and reboot the system