Wednesday, 16 March 2016

Managing grub2 with CentOS

By default CentOS uses the grub2 bootloader.

There are several main configuration files:

/etc/default/grub # Sets the general characteristics of the bootloader e.g. timeouts, menu options and so on.

/etc/grub.d/ # This directory contains various configuration files:

00_header: Used to detect and set various settings like timeouts, load modules into the kernel and so on.

00_tuned: Used to apply any settings with Tuned (tuned-adm) - this allows you to tweak the kernel for better performance for specific server roles.

01_users: Script used to check if any users for grub have been defined.

10_linux: Used to create the linux entries in the bootloader.

20_linux_xen: Used to create the linux (running under Xen) entries in the bootloader.

20_ppc_terminfo: Script to check the terminal size when running the bootloader.

30_os-prober:  This script is used to detect operating systems on hard drives.

40_custom: Used to add your own entries to grub - e.g. a custom kernel.

/boot/grub2/grub.cfg # This is the file main configuration file that is generated and read by the grub bootloader. In CentOS 7 this file should not be manually changed as changes will be overwritten!


grub2-mkconfig - This commands compiles information from the above configuration files and generates the grub2.cfg file e.g.:

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

grub2-install - Allows you to install grub to the MBR or a partition e.g.:

grub2-install /dev/sda

Entry Breakdown

Below I have extracted an entry from the grub bootloader on my local system that I will break down:

menuentry 'CentOS Linux (4.5.0-1.el7.elrepo.x86_64) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-327.el7.x86_6$
        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'  93af92d3-387a-4ab8-8d5f-83f8c36785b1
          search --no-floppy --fs-uuid --set=root 93af92d3-387a-4ab8-8d5f-83f8c36785b1
        linux16 /vmlinuz-4.5.0-1.el7.elrepo.x86_64 root=/dev/mapper/centos-root ro crashkernel=auto rhgb quiet LANG=en_GB.UTF-8
        initrd16 /initramfs-4.5.0-1.el7.elrepo.x86_64.img
--class: This variable is purely used for theme purposes (e.g. displaying an image / logo of the OS when you hover over the boot entry and can safely be removed.

--unrestricted: This command simply allows all users to boot the menu item - and is needed (even if there are no users defined!)

load_video: This command loads the video drivers.

set gfxpayload: This defines the resolution - although if set to 'keep' it will preserve the value set by gfxmode. If it is set to 'text' it will simply boot into normal text mode.

insmod gzio: Load the gzip module

insmod part_msdos: The msdos module is required to read the msdos (MBR) table.

insmod xfs: The xfs module is used to boot are filesystem for the OS.

set root: The first instance defines where the MBR is stored.

linux16 /vmlinuz-4.5.0-1.el7.elrepo.x86_64 root=/dev/mapper/centos-root ro crashkernel=auto The first part - linux16 ... instructs the kernel to be loaded with a 16bit boot protocol (rather than grub's default - 32bit) this is usually done as some BIOS functions do not support the 32bit boot protocol. It proceeds to define the kernel version, the root path of the OS (i.e. '/') and a fallback kernel. rhgb quiet LANG=en_GB.UTF-8 initrd16 /initramfs-4.5.0-1.el7.elrepo.x86_64.img: This portion defines the initramfs image that will be loaded - this is a stripped down OS that is loaded into RAM initially that allows you to then boot from your OS.

* Note: LVM is used by default in CentOS 7 - this is why we see references to '/dev/mapper/xxxxx' rather than something like Debian or Ubuntu that might simply refer to the partitions as /dev/sda1 etc. *


Post a Comment