Linux Boot

Let us start the Linux booting process with BIOS.

Step 1. BIOS (Basic Input Output System)

POST-power on self test which bios loads  checking for hardware .

Step 2. Stage 1 boot loader (MBR)


4 x 16 bytes = 64 bytes

Step 3. Stage 2 boot loader.

It is called the kernel loader. The main task at this stage is to load the Linux kernel.
Different boot loaders
LILO : Linux Loader.
GRUB : Grand Unified Boot Loader.
We use GRUB, as LILO has some disadvantages. One great thing about GRUB is it has knowledge about the Linux file system. GRUB can load kernel from an ext2 or ext3 file system.
As we mentioned, the boot loader code is executing in different stages because of the size limit of MBR. This stages are different in different GRUB version. These stages are mainly listed as follows:
  1. Stage 1 and Stage 2 : These are two essential stages
  2. Stage 1.5 : An optional stage
Stage 1 is an essential image used to boot up a Linux machine. Usually, this is embedded in an MBR or the boot sector of the partition. The maximum size of stage one image is 512 bytes, because of the MBR size limit.
Stage 1 does not understand file system. It loads stage 2 or stage 1.5 from local disk for further booting process. So it knows Linux file system details.
Stage 2, this is the core image of GRUB. Usually, you can find this stage in a file system (Not necessary). Stage 1.5 is actually a bridge between Stage 1 and Stage 2.  Stage 1.5 will be installed into an area right after the MBR area.
Boot loaders are loading from Stage 2. So you must know some basic details of Linux boot loaders. The common boot loaders in Linux are listed below:
  1. LILO (Linux Loader)
  2. GRUB (Grand Unified Boot Loader)
Nowadays almost all Linux distributions are using GRUB as boot loader. The latest GRUB is GRUB v2.
Both LILO and GRUB can configure as a primary boot loader (on MBR) or the secondary boot loader (on a bootable partition). Both work with supporting OS such as Linux, FreeBSD, NetBSD. They can also work with unsupported OS like Microsoft Windows. That’s a great thing!
Configuration file for LILO is /etc/lilo.conf and some configuration directives are listed below:


Commonly using boot loader is GRUB and its configuration is also listed under the etc folder.  GRUB has two versions. GRUB v1 and the latest GRUB v2. There are a lot of changes in these two versions of GRUB.

GRUB v1 – /etc/grub.conf

This configuration is actually a link to /boot/grub/grub.conf. All kernel details are included in this configuration file.  Sample entries are pasted below:
# grub.conf generated by anaconda
# root (hd0,0)
# kernel /vmlinuz-version ro root=/dev/sda3
# initrd /initrd-[generic-]version.img
title CloudLinux Server (2.6.32-673.26.1.lve1.4.27.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-673.26.1.lve1.4.27.el6.x86_64 ro root=UUID=15f2bf27-2e16-4b6f-bc86-fa74314aa8d5 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet nohz=off
initrd /initramfs-2.6.32-673.26.1.lve1.4.27.el6.x86_64.img

GRUB v2 – /etc/grub2.cfg

This is actually a link to /boot/grub2/grub.cfg. The main difference between GRUB v1 and GRUB v2 is, we can not edit the configuration file for changing the kernel and other settings. However, you can use different grub2-* commands for changing GRUB settings.
A sample configuration is pasted below:
terminal_output console
if [ x$feature_timeout_style = xy ] ; then
set timeout_style=menu
set timeout=5
# Fallback normal timeout code in case the timeout_style feature is
# unavailable.
set timeout=5
BEGIN /etc/grub.d/10_linux ###
menuentry 'CentOS Linux (3.10.0-123.4.2.el7.x86_64) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-123.el7.x86_64-advanced-fe0109f2-6f34-48ae-b51e-1f5fa78305b5' {
set gfxpayload=keep
insmod gzio
insmod part_msdos
insmod ext2
set root='hd0,msdos1'
The line starting from “menuentry” defines Kernels.
Don’t forget, we are at stage 2 of Linux booting process  In stage 2, GRUB is loaded from a known location in boot file system (/boot/grub).
This stage loads other required drives and kernel modules before reading the GRUB configuration file and displaying the boot menu. The familiar GRUB menu is now displayed on the monitor. You can select the kernel from there.

Step 4.  Kernel stage

Here the kernel stage begins. Kernel is in compressed format. We can select the kernel from the GRUB menu. If not selected, the GRUB automatically load the default one in its configuration. We can change the default kernel details from the GRUB configuration.
The kernel you selected is now loaded into the memory. An image file containing the basic root file system with all kernel modules are then loaded into the memory. This image file is located under /boot and it’s known as initramfs.
Initramfs, abbreviated from “initial RAM file system”, is the successor of initrd “initial ramdisk”. This image file contains the initial file system. The GRUB starts the kernel and tells the memory address of this image file. The kernel then mount this image file as a starter memory based root file system.
The kernel then starts to detect the system’s hardware. The root file system on disk takes over from the one in memory. The boot process then starts INIT (SYSTEMD) and the software daemons according to the Sys Admin’s settings. This can be done at next stages.

Step 5. INIT

The kernel, once it is loaded in step 4, it finds init in sbin (/sbin/init) and executes it. [In RHEL/CentOS 7 /sbin/init is linked to ../lib/systemd/systemd]. When init starts, it become the first or parent process on your Linux machine/server.
The first thing init does is reading the initialization file, /etc/inittab. This instructs init to read an initial configuration script for the environment, which sets the path, starts swapping, checks the file systems, and so on. From the /etc/inittab system will find the run level selected and start services by looking in the appropriate rc directory for that run level.


Popular posts from this blog

Docker basic commands (podman)

First week in new Job

Install Cisco AnyConnect on Ubuntu