Linux Boot Process - Machine Power Up/Reset to Login Prompt

Linux Boot Process - Machine Power Up/Reset to Login Prompt

Before we work with any of the operating systems or any server based works, we should first need to understand the background of what Linux consists of and what the procedure, flow and process is when you spin up a linux server.

Linux distributions are one of the most used servers out there when we are talking about cloud, microservices, deployments, containers, orchestration and other DevOps related terminologies.

Here, I am going to explain in detail about the Linux booting process.

After reading this post, you can expect to get a transparent idea about the Linux booting process. 

From powering up/reset to login prompt, we will mainly divide the Linux booting process into 6 areas.

Stage-1: BIOS

Stage-2: MBR

Stage-3: GRUB

Stage-4: Kernel

Stage-5: Init

Stage-6: Runlevel Programs

 

1. BIOS:

The first stage of the Linux boot process is BIOS which is the short form of Basic Input/Output System which performs some system integrity checks over hard drive. This initializes the physical hardware and makes sure if all hardwares are good and this is called a Power-on-self test (POST). This stage first searches, then loads and executes the boot loader program which is mostly from the MBR. Boot loader can be considered either from a hard drive, floppy drive, or a cd-rom drive. Depending on your system you can press a F12 or F2 key during the BIOS startup to change the boot sequence. BIOS gives the control to it, once the boot loader program is detected and loaded into the memory. To summarize, we can say that the BIOS loads and executes the MBR boot loader.

 

2. MBR:

Master Boot Record is the second stage of the boot process and it is shortly referred as MBR. Once the BIOS is initialized, it searches for the boot block to boot up the system. Depending on the partitions made on your disk, it will look for MBR for the boot process. MBR is typically located at /dev/hda, or /dev/sda. It is less than 512 bytes in size. 

This has three components

1) primary boot loader info - 446 bytes

2) Partition Table info - 64 bytes

3) MBR validation check - 2 bytes

MBR contains information about GRUB (or LILO - (Last In Last Out) in old systems).

To summarize, we can say that the MBR loads and executes the GRUB boot loader.

 

3. GRUB:

GRUB is the short form for Grand Unified Bootloader. We can say that GRUB is a splash screen which is displayed during the boot process of the linux machine. Let's say we have multiple kernel images installed on our system, then this splash screen will show the list of kernels to choose from which is to be executed. This screen usually waits for a few seconds for your response and if you don’t enter anything, it will load the default kernel image as mentioned in the GRUB config file. 

The grub configuration file is located in this location “/boot/grub/grub.conf” (/etc/grub.conf is a link to this). This has the knowledge of the filesystem and this defines your kernel images.

This grub.conf file contains the kernel and initrd image.

Boot parameters in grub.conf

initrd - The location of the initial RAM disk is specified here.

BOOT_IMAGE - Location of the kernel image (example: /dev/sda)

root - Root filesystem location where the kernel searches inside this location to find init. Device names such as /dev/sda1 or /dev/hda1 are often represented by it's UUID.

ro - This parameter mounts the filesystem as read-only mode.

quiet - This is used to not display messages that are going on in the background during boot.

splash - To show the splash screen during the boot process.

So, to summarize, we can say that GRUB loads and executes Kernel and initrd images.

 

4. Kernel:

When the Kernel is executed, it mounts the root file system as specified in the “root” in grub.conf

Before diving in, we should know the differences between initrd and initramfs. We know that the Kernel manages the system hardware but during the bootup, not all drivers are available. In a temporary filesystem, all the essential modules needed for the Kernel for the rest of the hardware are present. Initrd (Initial ram disk) was used in the older versions of Linux for this where the kernel would mount the initrd and get the necessary drivers which are compiled inside for the bootup to access the hard drive partitions and other hardwares and when everything was loaded, it would then replace the initrd with the actual root filesystem. In current days, initramfs is being used as the temporary root filesystem which is built into the kernel itself which loads all the required drivers for the real root filesystem and thus locating initrd file is not necessary.

Now the Kernel executes the /sbin/init program

The init program has a Process ID (PID) of 1 as its the first executed program by Linux Kernel. The Process ID (PID) can be checked by using the command ‘ps -ef | grep init’ in the terminal.

To run fsck safely, initially the root partition is mounted in read-only mode. The root filesystem changes to read-write mode after it remounts. After this the kernel locates the init program and executes it.

5. Init:

Coming to Init, to decide the Linux run level it first looks at the /etc/inittab file. We will discuss runlevels in the next section but just for your understanding, below are the available run levels.

0 – halt

1 – Single user mode

2 – Multiuser, without NFS

3 – Full multiuser mode (CLI)

4 – unused

5 – X11 or GUI

6 – reboot

The default initlevel is identified from /etc/inittab and this is used to load all appropriate programs.

To identify the default run level, execute ‘grep initdefault /etc/inittab’ on your system 

Preferred default run level setting would be either 3 or 5. Try to avoid setting the default run level to 0 or 6 as this might halt or reboot the process. 

 

6. Runlevel programs:

During the Linux system boot up, we do see a number of services getting started and these are the runlevel programs which are executed from the run level directory as defined.

The system will execute the programs from one of the following directories depending on the default init level setting.

- Run level 0 – /etc/rc.d/rc0.d/

- Run level 1 – /etc/rc.d/rc1.d/

- Run level 2 – /etc/rc.d/rc2.d/

- Run level 3 – /etc/rc.d/rc3.d/

- Run level 4 – /etc/rc.d/rc4.d/

- Run level 5 – /etc/rc.d/rc5.d/

- Run level 6 – /etc/rc.d/rc6.d/

Programs that start with S (startup) and K (kill) under the /etc/rc.d/rc*.d/ directories refer to programs that start with S are used during startup and the programs that start with K are used during shutdown.

The numbers right next to S and K in the program names are the sequence numbers in which the programs should be started or killed.


We will discuss more on Linux operating systems, commands, networking and administration in the future posts. Follow up DevOpsPal on social media for the latest updates and please share this content to your fellow members who are into DevOps practices and Software Development. Also comment below and share your views on this post and if you think that there is more to this, then feel free to mention it

Recent Updates

Recent updates on DevOpsPal

Social

Follow us of social for latest updates.

DevOps Hub

Links to get started.