Linux Booting Process
When you press the power on button and wait for few seconds, you get prompted for user login. Ever wondered how this happens?
Today’s plan is to clear the magic behind the process of getting this login prompt after power on i.e. the booting process. I’ll be discussing this in the context of a Linux based system, other OS(Operating System) boot up might slightly vary.
The Linux booting process can be summed up in six main steps as follows.
- BIOS (Basic Input Output System)
The main purpose of the BIOS is to perform system integrity checks, find the boot loader program and pass the control to that program. BIOS is an OS independent, special piece if firmware which runs from ROM(Read Only Memory).
The system integrity check performed by BIOS is called POST(Power On Self Test). This is a very brief test on CPU, memory and storage devices to verify that the system is in a boot-able state.
Then, it will check in the boot priority order to find a boot-able device. It will check in Disk drive, SD card reader, CD/DVD ROM, hard drive according to the boot priority that is configured.
Once a boot-able device is located, it will search for the Master Boot Record, load it and hand over the control to it.
2. MBR (Master Boot Record)
The master boot record is located in the first sector of boot-able disk( in my case it’s ‘dev/sda’ — these device files change based on the controllers used)
The master boot record is less than 512 bytes in size and contains three main portions — Primary boot loader, partition table and MBR validation check.
The MBR validation check contains information about GRUB(Grand Unified Boot loader).
3. GRUB
GRUB is responsible for starting the operating system and has knowledge of the file system. Older Linux systems used another boot loader named LILO(Linux Loader).
GRUB configuration file (/boot/grub/grub.conf), contains information about kernel and the Init rd(Initial RAM Disk) image. Init rd contains necessary modules/drivers to load actual OS file system.
$ ls -F /boot/ — lists Linux kernel images, init rd image and other necessary information
If multiple kernel images are present, GRUB shows a splash screen and prompts to select. If not selected, GRUB loads the default kernel according to configuration.
4. KERNEL
Kernel mounts the root file system as specified in the grub.conf and starts ‘/sbin/init’ process. Once the root file system is loaded, kernel dismounts the temporary file system and loads the real file system.
$ ps -ef | grep init — can check the ‘/sbin’process
Kernel is responsible for getting the hardware running.
You can view the messages related to Linux kernel using the Kernel ring buffer. Kernel ring buffer is a data structure that is always the same size and when it’s full, old messages are discarded to give place for latest messages. This content gets stored in ‘/var/log/dmesg’
$dmesg — prints content of kernel ring buffer
$ uname -mrs — shows the kernel image that’s being loaded
$ dpkg — list | grep linux-image — lists all available kernel images
5. INIT
In older systems, kernel looks at ‘/etc/inittab’ file to check the run level. Following are available run levels.
0 — halt
1 — single user mode
2 — multi user mode without NFS
3 — Full multi user mode
4 — unused
5 — X11 (run level 3 + display manager)
6 — reboot
Now, ‘systemd’ has taken over ‘inittab’ and systemd has targets which are roughly equivalent to run levels.
When inittab is used, to change the default run level it can be added to ‘initdefault’. When systemd is used, ‘/etc/init/rc-sysinit.conf’ contains the default run level information.
6. RUN LEVELS
Depending on run level(target), kernel starts executing the programs from one of the directories below.
$ ls /lib/systemd/system — lists available targets
Run level targets here are simlinks
Run level 0 — /etc/rc.d/rc0.d/ or /etc/rc0.d
Run level 1— /etc/rc.d/rc1.d/ or /etc/rc1.d
Run level 2— /etc/rc.d/rc2.d/ or /etc/rc2.d
Run level 3— /etc/rc.d/rc3.d/ or /etc/rc3.d
Run level 4— /etc/rc.d/rc4.d/ or /etc/rc4.d
Run level 5— /etc/rc.d/rc5.d/ or /etc/rc5.d
Run level 6— /etc/rc.d/rc6.d/ or /etc/rc6.d
If you check inside these directories, they contain programs starting with ‘S’ or ‘K’. S stands for startup scripts and K stands for kill scripts. The succeeding numbers in the program name denote the order of programs.
Based on the target/run level, the OS will be loaded and you will be prompted for login. :)
References
https://www.youtube.com/watch?v=RgLMBXg5b9I
https://www.liquidweb.com/kb/linux-runlevels-explained/
https://www.youtube.com/watch?v=ZtVpz5VWjAs
https://www.youtube.com/watch?v=RdbyPwo4W2E