Booting Process in Linux Operating Systems
Vikrant Thakur
Technical Lead | Embedded Software Engineer | Ex - Marelli , Napino , Lenovo | MSR - IITD
From the moment you press the power button, a behind-the-scenes process begins. This process carefully turns your computer from a powered-down state into a fully functioning system.
·? POST: Initial hardware checks.
·? Bootloader: Loads the kernel and initial RAM disk.
·? Kernel Initialization: Sets up system resources and detects hardware.
·? Initramfs: Prepares the system environment.
·? Mounting Root File System: Mounts the real file system.
·? Systemd/Init: Manages system services and targets.
·? Service Initialization: Starts necessary services.
Power On: The system wakes up.
BIOS/UEFI: BIOS are memory containing the built-in firmware on the motherboard. The BIOS uses the information stored in CMOS chip — another memory module on the motherboard that contains information about the systems hardware configuration . These contain settings related to hardware, such as boot order, system clock, and hardware configurations, are stored. Its job is to perform the POST, a quick check to ensure all your critical hardware components — like the CPU, RAM, and storage drives — are functioning properly.
The BIOS then looks at the sequence of storage devices identified as boot devices , in certain order, in the CMOS chip. Device to boot from — usually the hard drive, SSD, or sometimes a USB or network drive. If everything’s in order, the BIOS proceeds.
BOOTLOADERS (MBR & GRUB):
NOTE: For a device to be bootable, it must have a MASTER BOOT RECORD (MBR), in its first / last sector (SDD ,HDD, depending on manufacturer), which is known as the boot sector. You need to format a disk to add an MBR to its boot sector. SIZE OF MBR = 512 Bytes.( ONE CHUNK)
MBR have details about:
Primary boot loader code(446 Bytes).
Partition table (64 Bytes).
Magic number(2 Bytes).
Primary Boot loader code: This firmware provides boot loader information and location details of actual boot loader firmware on the hard disk. This is helpful for CPU to load second stage of Boot loader.
Partition table: 64 bytes of data which stores information such as what is the start and end address of each partition, size of partition, type of partition(Whether it’s a primary or extended or logical or swap).
Magic Number (32-bit disk signature) : The magic number service as validation check for MBR. If MBR gets corrupted, this magic number is used to retrieve it.
MBR supports up to four primary partitions. MBR is replaced by UEFI (Unified Firmware Interface) in modern machines. Some machine still don’t support UEFI.
Grand Unified Bootloader (GRUB)
The two most common bootloaders for Linux are
The Main generic function of bootloader is to load a image . Its primary function in linux is to load the Linux kernel into memory and start the operating system.
Loading the Kernel:
The bootloader’s main task is to locate and load the Linux kernel from the disk into RAM. The GRUB process consists of three stages, known as Stage 1, Stage 1.5, and Stage 2.
Providing a Boot Menu:
If you have multiple operating systems installed (like different Linux distributions or a dual-boot setup with Windows), the bootloader offers a menu to select which OS you want to boot.
Passing Parameters to the Kernel:
After the bootloader is loaded, the CPU can access the disk and memory and reads from the configuration file stored in /boot/grub/grub.conf on the disk.
It creates a image of a temporary virtual file system, called the INITRD or initial ramdisk and mounts the INITRD image, and load the kernel.
GRUB specifies the partition on which a compressed kernel image and necessary modules and initrd are located.
领英推荐
Bootloaders can pass parameters to the Linux kernel to influence its behaviour during startup. For instance, you can specify kernel options for debugging, or which initial RAM disk (initrd) to use. The file is — /etc/default/grub
Initial RAM Disk (initrd/initramfs):
Bootloaders often load an initial RAM disk image, which is a temporary root file system that the kernel uses during the early stages of booting before the real root file system is mounted.
The initramfs contain scripts (usually in /init) that prepare the system for the transition to the real root file system. It maps the real file system in linux. Linux support variety of the file system process. Linux supports various types of file systems.
Mounting the Real Root File System
Linux File directorie are as follows -
Kernel Initialisation Begins
The kernel mounts it as the root file system. The initramfs contain the necessary tools and drivers to prepare the system for mounting the real root file system.
systemd is the init system and service manager. It is responsible for initializing the system, managing system services, and handling system states. As the first process started by the kernel, systemthe has a process ID (PID) of 1.
Init process looks at the /etc/inittab file to decide the system to boot into the which Linux run level. There are 7 runlevels in total, numbered from 0 to 6. These are:
Runlevel 0: — Brings a system to halt.
Runlevel 1: — Runs LINUX in single-user mode with a Command-line-interface (CLI).
Runlevel 2: — Runs LINUX in multi-user mode with network disabled, and provides CIL.
Runlevel 3: — Runs LINUX in multi-user mode with network enabled, and provides CIL.
Runlevel 4: -Undefined by default to allow a user with necessary permissions to define a custom runlevel by editing the “/etc/inittab” file.
Runlevel 5: — Runs LINUX in multiuser mode with networking enabled, and provides GUI, in addition to CLI.
Runlevel 6: — REBOOT a system.
The init process start executing scripts corresponds to that chosen run level. The script information is stored in different folders in /etc/ folder
/etc/rc0.d/: Scripts for runlevel 0 (shutdown).
/etc/rc1.d/: Scripts for runlevel 1 (single-user mode).
/etc/rc2.d/: Scripts for runlevel 2 (multi-user mode without networking).
/etc/rc3.d/: Scripts for runlevel 3 (multi-user mode with networking).
/etc/rc4.d/: Scripts for runlevel 4 (custom runlevel).
/etc/rc5.d/: Scripts for runlevel 5 (multi-user mode with GUI).
/etc/rc6.d/: Scripts for runlevel 6 (reboot)
/etc/rc.local which are the last commands which runs the normal desktop screen.
System Configuration:
The init process reads configuration files, starts system services, and sets up the user environment. systemctl configures basics services like apache etc. to check the list , use the command systemctl list-units.
Units: Uses unit files to manage services, which are located in directories such as /etc/systemd/system/ and /lib/systemd/system/. These files define how services should be started, stopped, and configured. Our main tool is the systemctl command.
Runlevel/Targets
The system reaches its final state, called a runlevel or target. This is where your system is fully up and running, ready for you to log in and start using it.
Tech lead at Resideo | Not from IIT, IIM.
2 个月More low level detailed info here you will get- https://youtu.be/mTVtytWP9Sk?si=DfFJVhW5M4PfykHp