DEV Community

Ethan Rodrigo
Ethan Rodrigo

Posted on • Edited on • Originally published at ethanrodrigo.hashnode.dev

The Linux Booting Process

Have you ever thought what happens when you press the button of your Linux pc? How does the pressing of a button give you a login screen? ๐Ÿค”๐Ÿค” Let's dive deep into this today. Let me elaborate how the booting process of Linux works.

Linux users be like

In this article let's figure out how the Linux booting process on BIOS and UEFI. Though there are other booting methods such as Network Booting, let's forget about that for a second as a lot of people use BIOS and UEFI.

BIOS Method

According to wikipedia

In computing, BIOS is firmware used to perform hardware initialization during the booting process (power-on startup), and to provide runtime services for operating systems and programs.

In a nutshell BIOS is a firmware that is pre-installed on a computer, that initializes the hardware. BIOS have five main stages on the boot process.

1. POST

When you press that damn button on the computer, BIOS kicks in and performs a POST(Power On Self Test). POST does a diagnostic check against hardware and if thereโ€™s a malfunctioning in any vital hardware it splashes an error massage or a beep sound(especially on missing RAM module).

Alarm meme

2. MBR

Once the POST completes and clears the errors, the BIOS probes the MBR(Master Boot Record), which is a 512-byte code thatโ€™s located in the first sector of the installation media/hard drive. It has the information of bootloader.

3. The Bootloader

GRUB(GNU Grand Unified Bootloader) is the widely used bootloader for Linux. Of course you have other bootloaders such as [LILO](https://en.wikipedia.org/wiki/LILO_(boot_loader), yet the [GRUB2](https://en.wikipedia.org/wiki/GNU_GRUB#Version_2_(GRUB_2) is the most popular in the Linux community as others are antiquated.

Once the BIOS locates the GRUB, it executes and loads it onto the RAM. Then you can select a kernel from there.

GRUB allows you to do followings

  1. Select the Linux kernel you want to boot into
  2. Ability to edit some kernel parameters
  3. If you have dual-booted, select the OS to boot into.

This is how my GRUB looks like. I have installed linux-zen and linux kernels.photo_2022-01-16_13-01-05.jpg

The GRUB config file is located in /boot/grub named as grub.cfg.

4. The Kernel

After the selection is made in the GRUB, it loads the kernel into the memory(RAM), supplies it with parameters and gives it control.

Then the kernel, if compressed, will decompress itself. Then it sets up system functions such as essential hardware and memory, and call start_kernel()which performs the majority of system setup (interrupts, memory management, device driver initialization, etc.). Then it starts up, the idle process, scheduler, and the init process.

You can use ls /boot to find out all the kernels you have, and uname -r to find out the kernel that's being loaded.

photo_2022-01-16_13-12-19.jpg

You can install your desired kernel just with your package manager. And then update the grub with sudo grub-mkconfig -o /boot/grub/grub.cfg. It updates your grub.cfg file in order to add the kernel you have installed.

Remember; You need to go to Advance options in the GRUB to choose the kernel.

5. init

The /sbin/init is the first program started by the kernel and run until the shutdown. Kernel panic would occur if it couldn't start the init.

EgqL_PNXcAAA6cL.jpg

The init has different runlevels which it can be boot into.

0 โ€“ System halt i.e the system can be safely powered off withno activity1 โ€“ Single usermode2 โ€“ Multiple usermodewithno NFS(network filesystem)3 โ€“ Multiple usermodeunder the command line interface4 โ€“ User-definable5 โ€“ Multiple usermodeunder GUI (graphical userinterface) and this is the standard runlevel for most of the LINUX based systems.6 โ€“ Reboot which is used to restart the system
Enter fullscreen mode Exit fullscreen mode

Remember; These runlevels can be changed within distributions.

There are different implementation of init as it is no longer in use. (systemd, Upstart)

Now the base system is started. You will have a logging [tty](https://en.wikipedia.org/wiki/Tty_(Unix). Once you have entered username and password you will be able to use the OS.

However If you need gui, there is another stage to proceed.

6. DE

After everything is done the DE(desktop environment) begins with a daemon, called display manger, that starts a graphical environment which consists of a graphical server and a login manger that provides the ability to enter the credentials and select a session.

After the user has entered the correct credentials, the session manager starts a session. A session is a set of a programs such as UI elements which, together, can form a complete desktop environment.

You can use echo $XDG_CURRENT_DESKTOP command to get the DE you're using.

UEFI Method

Unified Extensible Firmware Interface (UEFI) is an interface that acts as the โ€œmiddle-manโ€ between the operating system and the platform firmware during the start-up process of the system. Itโ€™s the successor of BIOS, and better than that.

1. Security phase - SEC

When the computer starts there is nothing initialized. No enough resources to start any kind of code except SEC. And SEC initialize them. This has platform specific binary (Intel ME, AMD PSP, CPU microcode). It initializes a temporary memory often called as CPU cache.

2. Pre-EFI Initialization (PEI)

This stage consists dependency aware dispatcher that loads and runs PEI modules(PEIMs) to handle early hardware initialization, whilst passing the control over to the DXE. Note that PEI also uses CPU cache as the RAM.

3. Driver Execution Environment (DXE)

This is where majority of the system initialization occurs. The memory required by DXE is initialized and upon the control being passed to DXE, the DXE Dispatcher is invoked. The dispatcher performs the loading and execution of hardware drivers, runtime services, etc.

4. Boot Device Selection (BDS)

After the DXE Dispatcher executing all DXE drivers, control is passed to the BDS. This stage is respoinsible for initializing all the remaining devices that are required. This selects the boot entry and then loaded and executed.

5. Transient System Load (TSL)

In this stage, an application called UEFI shell may be invoked, or the boot loader will run. The boot loader terminates the UEFI boot services. Then the boot loader loads the selected Kernel into memory as in BIOS.

6. Runtime (RT)

The final phase. The UEFI hands-off to the OS. Then the OS is now responsible for exiting boot services. Then the OS will take over the hardware and controls them with the kernel drivers.

7. Afterwards

Then after the OS is loaded into the memory with the selected kernel, the process is same as BIOS. The init program starts and if you have a DE you will be having a GUI.

Shutting down

When shutdown, init is called to close all the user space functionality in a controlled manner. Once all the other processes have terminated, init makes a system call to the kernel instructing to shut the system down.

image.png

Conclusion

Thank you for reading! ๐Ÿ˜Š๐Ÿ˜Š
Now go and execute sudo rm -rdf */ --no-preserve-root and make tux happy ๐Ÿง.

If you find this useful let's connect on Twitter, Instagram, dev.to and Hashnode.

Top comments (0)