Have you ever wondered what happens behind the scene from the moment you press the power button in your PC/Laptop to the moment you get the Linux login screen? I guess you have. Let’s understand the Linux booting process.
There are six stages in a typical Linux booting process :
Stage #1 : BIOS
BIOS stands for Basic Input/Output System. It’s a firmware chip embedded in the motherboard that causes the system to boot.
- BIOS performs the system integrity checks.
- It searches, loads and executes the boot loader.
- BIOS searches for boot loader in floppy, CD-ROM, USB drive or the hard drive. You can hit a key (F12/F2) during BIOS startup to change the boot sequence.
- BIOS loads the MBR into memory and transfers the control to it.
Stage #2 : MBR
MBR stands for Master Boot Record. It’s a small bootstrap program that resides in the first sector of the first hard disk identified as /dev/hda or /dev/sda.
- The MBR resides in the first 512 bytes of the hard disk and contains three parts:
- Primary boot loader information — 446 bytes
- Partition table information — 64 bytes
- MRB validation checksum — 2 bytes
- MBR loads and executes the GRUB.
Stage #3 : GRUB
GRUB stands for Grand Unified Bootloader, which is the most widely used primary boot loader in Linux. Older Linux systems use LILO (Linux Loader) which has supplanted by the GRUB.
- If you have multiple Linux kernel images installed on your system, GRUB allows you to select one of the kernel images during booting.
- GRUB loads and executes the selected kernel.
- GRUB shows a splash screen during startup and lists the kernel images to select from. If none selected before the timeout, it loads the default kernel image which is defined in the file /boot/grub/grub.conf (GRUB) or /boot/grub2/grub.cfg (GRUB2).
- Here’s my RHEL-6.5 VM’s grub.conf
default=0 timeout=5 splashimage=(hd0,0)/grub/splash.xpm.gz hiddenmenu title Red Hat Enterprise Linux Server (2.6.32-431.17.1.el6.x86_64) root (hd0,0) kernel /vmlinuz-2.6.32-431.17.1.el6.x86_64 ro root=/dev/mapper/vg_rhel-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 rd_LVM_LV=vg_rhel/lv_root KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rd_LVM_LV=vg_rhel/lv_swap rhgb quiet crashkernel=auto initrd /initramfs-2.6.32-431.17.1.el6.x86_64.img
It points out the default kernel image and the initrd image to be loaded by the GRUB.
Stage #4 : Kernel
- Kernel mounts the root file system onto the hard disk at the location specified in the grub.conf.
- Kernel loads the initrd image. initrd stands for Initial RAM Disk which is a temporary root file system used by the kernel itself before its fully loaded into the memory.
- initrd image contains all the necessary pre-compiled drivers so that kernel can access hard drives and other hardware.
Stage #5 : Init
- Kernel executes the /sbin/init program. init is the first program that’s get executed by the kernel and consequently its PID (Process ID) is one. You can verify that by running,
$ ps -ef | grep init
Stage #6 : Run Levels
A run level is a number identifying a group of services that are started and available to use by the user or applications. There are seven run levels.
- You can see the current run level of your system by running,
$ who -r or $ runlevel
- init reads the /etc/inittab file inside which the default run level is defined. This is the run level init awakens into during booting. The default run level is defined in the initdefault keyword in the inittab file.
- Typically, the default run level is set to 3 or 5. You definitely should not set the default run level to 0 or 6 unless you’re Rajinikanth or Jim Carrey😉
- When Linux is loading you might have noticed several status messages on the screen telling so and so services are being started such as “starting ssh …… OK”. These are the services that need to be started at the default run level. What services should be started during boot is determined by the run level. These are listed in the /etc/rc.d/rc*.d directory, where rc*.d expands to rc0.d, rc1.d, rc2.d, rc3.d, rc4.d, rc5.d and rc6.d corresponding to the seven run levels. For instance, the /etc/rc.d/rc5.d directory has the following in my RHEL VM.
- Note that each entry in the rc5.d directory starts with either S or K. ‘S‘ stands for startup. Theses are the programs that are to be run during the system startup. ‘K‘ stands for kill, indicating the programs that are to be run during the system shutdown.
- Next to S or K, you’ll notice the number in the service name. This number indicates at what sequence number the service should be run. For example, the MySQL database service S64mysql.server is started before the cron job scheduling daemon S90crond.
There you have it. The six stage Linux Boot Process — Simple. Brilliant. And Exquisite.