This article will focus on different stages of boot process in Linux OS. There are 6 distinct stages of boot process, each explained in brief as below. There’s also a note on runlevels which represents the state of the system after boot, which are usually managed by systemd.
Embedded Linux boot process similar to standard Linux to much extent, but due to the variations in hadware and the board specifics like multi-stage bootloaders, it becomes somewhat different in bootloader section and pre-dominant use of u-Boot bootloader in embedded systems also needs to be addressed. To find out specifics of Embedded Linux Boot Process see this article.
1. BIOS (Basic Input/Output System)
- BIOS is a program that controls computer’s hardware from the time the computer is started until the main operating system takes over.
- BIOS first performs some integrity checks, i.e. power on self test (POST) of the HDD or SSD.
- BIOS acts as an intermediary between the CPU and the input and output devices. This eliminates the need for the operating system to be aware of the hardware addresses of the input and output devices.
- When there’s a change in device details, only the BIOS Configuration needs to be updated. (accomplished by pressing a specified key F12 or F2 as soon as the computer begins to start up).
- BIOS program, written in the assembly language of the CPU used, is stored on Electrically Erasable Programmable ROM (EEPROM) or flash memory.
- BIOS searches for, loads, and executes the boot loader program.
- BIOS loads and executes the Master Boot Record (MBR) boot loader.
- BIOSs permit the user to select the order in which the system searches for bootable media.
- MBR is usually on HDD or SSD, but sometimes on a USB stick or CD-ROM such as with a live installation of Linux.
- Once the boot loader is loaded into memory and the BIOS gives control of the system to it.
2. Master Boot Record (MBR)
- MBR is responsible for loading and executing the GRUB boot loader.
- MBR is located in the 1st sector of the bootable disk, which is typically
/dev/hda
, or/dev/sda
. - MBR is less than 512 bytes in size. This has three components 1) primary boot loader info in 1st 446 bytes 2) partition table info in next 64 bytes 3) mbr validation check in last 2 bytes.
3. GNU GRand Unified Bootloader (GRUB)
- GRUB is the typical boot loader for most modern Linux systems.
- GRUB Splash Screen is the first to appear on screen. If you have multiple kernel images installed, you can use your keyboard to select the one you want your system to boot with.
- GRUB configuration file is usually at
/boot/grub/grub.conf
or/etc/grub.conf
.
4. Kernel
- The Kernel that was selected by GRUB first mounts the root file system that’s specified in the
grub.conf
file. - Then Kernel executes the
/sbin/init
program, which is *always the first program to be executed. You can confirm this with its *process id (PID), which should always be 1. - The kernel then establishes a temporary root file system using Initial RAM Disk (initrd) until the real file system is mounted.
5. Init
- With Init stage, system executes runlevel programs. System looks for an init file
/etc/inittab
to decide the Linux run level.
6. Runlevel Programs
- A runlevel is a mode of operation in the computer operating systems that implements Unix System V-style initialization.
- Conventionally, seven runlevels exist, numbered from zero to six.
- Only one runlevel is executed on startup; run levels are not executed one after another.
- A runlevel defines the state of the machine after boot.
- By default most of the LINUX based system boots to runlevel 3 or runlevel 5.
- Depending on your default init level setting, which are usually managed by systemd, the system will execute the start scripts for each run level are different performing different tasks. These start scripts corresponding to each run level can be found in special files present under rc sub directories.
- At
/etc/rc.d
directory there will be either a set of files namedrc.0
,rc.1
,rc.2
,rc.3
,rc.4
,rc.5
andrc.6
, or a set of directories namedrc0.d
,rc1.d
,rc2.d
,rc3.d
,rc4.d
,rc5.d
andrc6.d
. - In these directories, you’ll find programs that start with either an “S” or “K” for startup and kill, respectively. Startup programs are executed during system startup, and kill programs during shutdown.
- There are numbers right next to S and K in the program names. Those are the sequence number in which the programs should be started or killed.
root@PTL011669:# cd /etc/rc0.d/ root@PTL011669:rc0.d# ls K01alsa-utils K01avahi-daemon K01docker K01kerneloops K01mdadm-waitidle K01rpcbind K01spice-vdagent K01apache2 K01bluetooth K01etc-setserial K01lighttpd K01mosquitto K01rsyslog K01unattended-upgrades K01apache-htcacheclean K01cgroupfs-mount K01gdm3 K01lvm2-lvmetad K01networking K01saned K01uuidd K01atop K01chrony K01hddtemp K01lvm2-lvmpolld K01plymouth K01setserial K01virtualbox K01atopacct K01cups-browsed K01irqbalance K01mdadm K01postfix K01speech-dispatcher root@PTL011669:rc0.d#
- At
- Changing runlevel:
- init is the program responsible for altering the run level which can be called using telinit command.
- Need for changing the runlevel:
- There can be a situation when you may find trouble in logging in in case you don’t remember the password or because of the corrupted
/etc/passwd
file, in this case the problem can be solved by booting into a single user mode i.e runlevel 1. - You can easily halt the system by changing the runlevel to 0 by using telinit 0.
- There can be a situation when you may find trouble in logging in in case you don’t remember the password or because of the corrupted
- Standard runlevels:
ID | Name | Description |
---|---|---|
0 | Off | Turns off the device. |
1 | Single User mode | Mode for administrative tasks. |
2 | Multi-user mode | Does not configure network interfaces and does not export networks services. |
3 | Multi-user mode with networking | Starts the system normally. |
4 | Not used/user-definable | For special purposes. |
5 | Full mode | Same as runlevel 3 + display manager. |
6 | Reboot | Reboots the device. |