Stages in the Linux Boot Process: What Happens When You Press the Power Button
Have you ever wondered what happens when you press the power button on your Linux system? What exactly happens between the time you hit the power button and the time you see the login prompt? In this blog post, we will take a look at the different stages of the typical Linux booting process. We will examine what takes place during each stage, and discuss some of the most important things that happen during startup. So if you’re interested in learning more about how your Linux system starts up, keep reading!
What is Linux?
Linux is a Unix-like computer operating system. It was first released in 1991, and since then, it has become one of the most popular operating systems in the world. Linux is open-source software, which means that its source code is freely available to anyone who wants to download it. This also means that Linux is very customizable, and there are many different versions of Linux available, each with its own unique features and capabilities.
What do you mean by booting?
When we talk about booting a Linux system, we are referring to the process of starting up the operating system. This includes loading the kernel and all of the necessary modules, as well as initializing various services and setting up the graphical environment.
What happens when you press the Power Button?
When you press the power button on a Linux system, there are six stages that take place in order to bring the system up and running. We will go over each stage in detail below.
Stage One: BIOS Initialization
The first stage of the booting process is called BIOS initialization. The full form of Bios is Basic Input Output System. This is where all of the initial hardware configurations take place. The BIOS reads the boot sector of the hard drive at MBR, and then it looks for a Bootloader. If it finds one, it will load the Bootloader and hand over the control to it.
Functions of BIOS
POST – Power On Self Test
When we turn on the PC, the BIOS will perform a test on the computer called Power On Self Test. This test will go through all the hardware, check their functionality, and make sure they are properly functional.
In most computers, Bios is stored in a Bios Chip of the motherboard which is nonvolatile. This means the data will not be erased even if the power supply is turned off. But what about the settings of BIOS? Bios settings are stored in CMOS Chip which is volatile. This includes BIOS Password, data and time, and all other saved BIOS data. But since it is volatile, we have to make sure that the CMOS chip gets enough voltage even after the system is powered off. This is achieved using a coin cell which is placed in the motherboard. This coin cell provides enough voltage to keep the CMOS Chip functional even if the System is powered Off.
To erase bios settings, all you need to do is simply remove and replace the battery that is powering the CMOS chip.
The BIOS first looks for, loads, and executes the boot loader program from the Master Boot Record (MBR). When the boot loader is found, it’s loaded into memory, and control is transferred to it by the BIOS.
What about UEFI?
UEFI or Unified Extensible Firmware Interface can be considered as the next version of BIOS. UEFI is the next level of BIOS that will fire up when the system starts to boot up. Most modern computers use UEFI instead of BIOS. But why?
In addition to all of the regular features that you’d expect in BIOS, UEFI also includes a number of advanced settings. Secure Boot verifies the operating system’s validity, ensuring that no malware has tampered with the boot procedure.
The Secure Boot prevents any digitally unsigned driver from being installed and allows no malicious software, such as rootkits, to run. A rootkit is a collection of malicious software that gives hackers administrator access to the computer system.
The UEFI is a more modern method that supports greater hard drives, quick boots, enhanced security features, and conveniently supports a graphical user interface with mouse cursors.
Stage Two: The BootLoader
The bootloader is basically a program that will load the Kernal of the operating system to the memory. The bootloader is probably the first thing you will see when you are starting up the computer. This is the part where we select the OS image using Keyboard for the computer to load. When considering Linux there are two types of Bootloaders – GRUB and LILO.
Functions of Bootloader
The boot loader loads Kernel and initrd or initial ramdisk which is a temporary file system that contains the information to load the real OS, loads necessary modules to mount the actual file system, and starts the kernel. In short, the bootloader loads the kernel to the memory.
You can also interrupt the boot process and interact with the bootloader and change you load the operating system. We can also pass different boot options to the operating system.
What is Initial RAM Disk or initrd?
Initial RAM Disk or initrd is a temporary File System that’s loaded into memory when the system boots. This will contain helpers that will detect and load the hardware, set up the drivers, and help the actual file system mounted. If Root filesystem is stored in LVM volume, the initrd will contain the Kernel Image to mount the LVM Volume as Root File System.
Once Initrd mounts the actual file system, its job is done and the operating system starts to load from the real file system.
The Linux Kernel, Initrd and other configurations are stored in /boot/. A kernel is typically named vmlinux or vmlinuz. If the kernel is compressed and the name ends in z.
Stage 3 – Loading the Kernel
The kernel is a core program of an operating system. The kernel handles the rest of the startup process, handles input, output requests from other programs, Manages memory and hardware peripherals. Initrd is used by the Kernel to mount temporary file system.
The Kernel is loaded in 2 stages – In the first stage, the compressed Kernel file is loaded and decompressed and in the second stage, the main Kernel Startup process is initiated. Once that much is done, the Kernel will execute the init program under /sbin/init.
init program is the first program to be executed when a Linux computer is booted. It gets the process ID 1. In most modern OS, initd is replaced by SystemD which has so much more (..and we will get back to init and systemd in a minute)
Functions of Linux Kernel
Every computer needs hardware and software to function. There is a layer of software that acts as an intermediary between the hardware and all other software that needs to use that hardware and that intermediary is Kernel.
Linux Kernel has complete control over your system when your Linux OS is running. Whenever you need to start a process, it is the Kernel that does all the necessary stuff in the background. If that process needs some memory, it will be the Kernel that allocates that memory.
Kernel Ring Buffer
When you are logging into the system, you should be able to see so many messages flying by. That is the bootloader, initrd and Kernel doing its job. The Kernel Ring Buffer contains all the messages related to the Linux Kernel. Ring buffer is a data structure that always stays the same size. That means, once the Ring Buffer is filled, the old messages will be discarded.
To see the contents of the Kernel Ring Buffer, use dmesg command. All the messages are stored in the file /var/log/dmesg file.
In short, Kernel loads the file system and starts the initial program.
Stage 4 – Run Level / Targets
Init program checks the inittab file to decide the initial RunLevel, which goes when decided will start the corresponding programs and run necessary scripts.
Linux uses something call Run Level to determine what are processes are needed to be run when the OS boots Up. There are mainly 7 run levels and for each run level, there are a set of processes that will be started.
Run Levels varies distro to distro but basically, there are mainly 7 Run Levels
7 Runlevels in Linux
Runlevel 0 | Shuts down the system |
Runlevel 1 | Single-user mode |
Runlevel 2 | Multi-user mode without networking |
Runlevel 3 | Multi-user mode with networking |
Runlevel 4 | User-definable |
Runlevel 5 | Multi-user mode with networking |
Runlevel 6 | Reboots the system to restart it |
Run Level and Init Program
In old operating systems and in some new ones, the Run Levels are described and initiated by init process or init program. All the configurations was stored in /etc/init. We can simply edit /etc/init file to change the run levels. But the init program is now being phased out.
InitD vs SystemD
In most of todays operating system, the init program is replaced by systemd. Systemd is most currently adapted and used replacement. Instead of runlevels, systemd uses a concept called targets. They are roughly equivalent to run levels.
To see all available Targets, you can check /lib/systemd/system. Here also, we can easily set the desired Target/run level in the system by using the command – systemctl isolate TARGET. For example – systemctl isolate graphical.target
Run Level Programs
Depending on the Run Level /Target the system is running, it will run a series of scripts. Obviously, each run level’s start scripts will be different in terms of what they accomplish. The rc directories contain the start scripts for each run level, which may be found in certain files.
How do we find it? At /etc/rc.d directory there will be either a set of files named rc.0, rc.1, rc.2, rc.3, rc.4, rc.5 and rc.6, or a set of directories named rc0.d, rc1.d, rc2.d, rc3.d, rc4.d, rc5.d and rc6.d.
For example, run level 1 will have its start script either in file /etc/rc.d/rc.1 or any files in the directory /etc/rc.d/rc1.d.
Why is Linux so famous?
Linux is famous for a few different reasons. First, it’s very stable and reliable. Linux systems rarely crash, and they are much less likely to be infected by viruses or malware than Windows systems.
Second, Linux is very customizable. There are many different versions of Linux available, each with its own unique features and capabilities. This means that you can find a version of Linux that fits your specific needs and preferences.
Third, Linux is free and open-source software. This means that you can download its source code for free, and you are free to modify it however you want.
Fourth, Linux is very secure. It is much less likely to be infected by viruses or malware than Windows systems, and it is also more secure against hacker attacks.
So those are some of the reasons why Linux is so famous. It’s stable, reliable, customizable, free, and secure – a perfect combination for anyone looking for an alternative to Windows.
Now that we know a little bit about Linux, let’s take a look at the six stages of the Linux booting process.