Linux is a popular operating system that runs on a variety of devices, from desktops and laptops to servers and embedded systems. But how does Linux start up and load the operating system? In this post, we will explore the six stages of the Linux booting process, from the moment you turn on your device to the moment you see the login prompt.
1. BIOS
BIOS stands for Basic Input/Output System, and it is the first program that runs when you power on your device. BIOS is stored in a ROM chip on the motherboard, and it is responsible for initializing the hardware components and peripherals, such as the keyboard, display, disk drives, and serial communications. BIOS also performs a power-on self-test (POST) to check the integrity and functionality of the hardware.
After the POST, BIOS looks for a bootable device, such as a hard drive, a USB drive, or a CD-ROM, depending on the boot order configured in the BIOS settings. BIOS then reads the first sector of the boot device, which is called the Master Boot Record (MBR), and loads it into memory. The MBR contains the boot loader program, which is the next stage of the booting process.
2. MBR
MBR stands for Master Boot Record, and it is the first 512 bytes of the boot device. The MBR contains three parts: a boot code, a partition table, and a boot signature. The boot code is a small program that locates and loads the boot loader, such as GRUB or LILO. The partition table contains information about the partitions on the disk, such as their size, type, and location. The boot signature is a 2-byte value that identifies the MBR as a valid boot sector.
The boot code in the MBR executes and scans the partition table for an active partition, which is the partition that contains the boot loader. The boot code then reads the first sector of the active partition, which is called the boot sector, and loads it into memory. The boot sector contains the second stage of the boot loader, which is the next stage of the booting process.
3. GRUB
GRUB stands for GNU GRand Unified Bootloader, and it is the most common boot loader for Linux systems. GRUB consists of two stages: the first stage is stored in the boot sector, and the second stage is stored in a file system. The first stage of GRUB loads the second stage, which is a larger program that can access multiple file systems and display a graphical menu. The menu allows the user to select a kernel image to boot, or to edit the boot options. If the user does not select anything, GRUB loads the default kernel image after a timeout.
GRUB can also chain-load other boot loaders, such as Windows Boot Manager, by loading their boot sector instead of a kernel image. GRUB can also load an initial RAM disk (initrd) image, which is a temporary file system that contains drivers and modules needed by the kernel. GRUB then passes control to the kernel, which is the next stage of the booting process.
4. Kernel
The kernel is the core of the Linux operating system, and it is responsible for managing the system resources, such as memory, CPU, disk, network, and devices. The kernel also provides an interface for user programs to access the system services, such as system calls, signals, and interrupts. The kernel is stored in a file, usually named vmlinuz, in the /boot directory.
When GRUB loads the kernel, it decompresses it and copies it into memory. The kernel then initializes itself and the hardware devices, using the information passed by GRUB and the initrd image. The kernel also sets up the virtual memory, the scheduler, the network stack, and the security framework. The kernel then mounts the root file system, which is the main file system that contains the operating system files. The kernel then executes the first user program, which is /sbin/init, and passes control to it. This is the next stage of the booting process.
5. Systemd / init
Systemd and init are programs that initialize and manage the user space, which is the environment where user programs run. Systemd and init are also called init systems, and they are responsible for starting and stopping services, such as network, logging, graphical interface, and user login. Systemd and init are also called process 1, because they have the process ID of 1, and they are the parent of all other processes.
Systemd and init are configured by files that specify the dependencies and order of the services. Systemd uses unit files, which are located in /etc/systemd/system or /usr/lib/systemd/system directories. Init uses init scripts, which are located in /etc/init.d or /etc/rc.d directories. Systemd and init also use runlevels or targets, which are modes of operation that define the set of services to run. For example, runlevel 3 or multi-user.target is the mode for text-based login, and runlevel 5 or graphical.target is the mode for graphical login.
Systemd and init start the services according to the runlevel or target specified by the kernel or the user. Systemd and init also monitor the services and restart them if they fail. Systemd and init also allow the user to change the runlevel or target, and to start or stop services manually. Systemd and init then start the last service, which is the login prompt, which is the final stage of the booting process.
6. Runlevels [0-6]
Runlevels are modes of operation that define the set of services to run on a Linux system. There are seven standard runlevels, numbered from 0 to 6, and each runlevel has a different purpose and functionality. The runlevels are:
- 0: Halt. This runlevel shuts down the system and turns off the power.
- 1: Single-user mode. This runlevel allows the user to perform maintenance tasks, such as repairing the file system or changing the password. Only the root user can log in, and no network or graphical interface is available.
- 2: Multi-user mode without network. This runlevel allows multiple users to log in, but no network or graphical interface is available. This runlevel is rarely used, and it is similar to runlevel 3.
- 3: Multi-user mode with network. This runlevel allows multiple users to log in, and the network is available. However, no graphical interface is available, and the user can only use the text-based console.
- 4: User-defined. This runlevel is not used by default, and it can be customized by the user or the system administrator.
- 5: Multi-user mode with network and graphical interface. This runlevel allows multiple users to log in, and the network and the graphical interface are available. This is the default runlevel for most desktop Linux systems, and it provides the full functionality of the system.
- 6: Reboot. This runlevel restarts the system and boots into the default runlevel.
The user can change the runlevel by using the init or systemctl command, or by editing the /etc/inittab or /etc/systemd/system/default.target file. The user can also see the current runlevel by using the runlevel or systemctl get-default command.
Conclusion
In this post, we have learned about the six stages of the Linux booting process, from BIOS to runlevels. We have seen how each stage performs a specific task and passes control to the next stage, until the system is fully operational and ready for the user. We have also learned about the configuration files and commands that control the booting process and the services. We hope that this post has given you a better understanding of how Linux boots and works.
Comments
Post a Comment