Linux booting process
The Linux booting process is mainly divided into 5 steps.
1.BIOS / UEFI
SYSTEMD [For new version distro ] or
INITD [For Old less than CentOS 6 ]
BIOS : This performs POST (power on self test) and then based on the boot order mentioned on CMOS, it selects the appropriate boot disk to execute the MBR. So, basically it executes the POST and hands over the task to MBR.
MBR : MBR actually sits on the 1st sector (or famously known as zero sector) of the boot drive.
This is also named as Stage-1 boot loader.
MBR hands over the control to a different programme called GRUB.
GRUB : [Grand Unified Boot Loader] – This the default boot loader of Linux
GRUB main purpose is just to Load the Kernel into MEMORY
Kernel : Once the Kernel is loaded into the Memory, Kernel initialises itself.
Then it loads the initrd image(Initial Ram Disk)
Eventually, this initrd.img contains the temporary root file system and other essential Kernel Modules, which are used to load the actual (/) root file system.
Once the root file system is loaded into the memory, the kernel goes to the
background and gives the control to Systemd
Systemd: Here kernel actually forks the system demon and executes the process known as initd or systemd [This is the first process runs on the Linux upon booting, so it gets the pid as 1 ]
Explanation of Linux boot process and system initialization in more details:
Bios/UEFI initialization :
Once we press the power button on our computer, it initialises its own firmware and gets the CPU running.
Importantly, one of the CPU processors(multi-core) is chosen as a boot-strap processor[BSP], the remaining processors are called application processor.
BSP remains active during the boot process and the AP remains halted.
the BSP executes the BIOS code, The BIOS code actually executes the POST.
Once the POST is completed, the BIOS next mission is to boot an OS[Operating system]
So, it starts to locate the booting device,
In this case, the booting device can be a Hard Disk, CD, Flash drive, or a network device.
At last, the CMOS contains the priority of the booting device, which can be changed by the user by loading the BIOS interface.
Hard Disk is always the default boot device.
So, BIOS reads the Zero sector of the disk, which is 1st 512 bytes of the disk.
The contents on that first 512 bytes, loads into the Memory.
The Zero sector of the disk. First 512 Bytes of the disk.
It contains the code that is known as Stage-1 boot loader.
|512 Bytes = 440 bytes of Code + 4 Bytes of Disk Signature + 2 Bytes of Nulls + Four 16 bytes(64 Bytes) of partition table + 2 Bytes of MBR signature.|
To see the 440 bytes of code into the MBR, execute the command –> dd if=/dev/xvda bs=440 count=1 | hexdump -C
To See the last 2 bytes of MBR Signature --> dd if=/dev/xvda bs=512 count=1 | hexdump -C | tail -c 2 | hexdump -C
Remember, the 440 bytes of code residing on the MBR actually cannot load the entire kernel, because it does not have the intelligence to learn about the file system etc.
Then, why do we care about this 440 bytes of code ?
Because this is used to load the actual boot loader.
The Boot Loader :
A boot loader is the first software programme that runs when a computer starts.
Linux, LILO, GRUB-Legacy and GRUB2 are the bootloaders.
Windows NTLDR (New Technology Loader), Loadlin(DOS)
For Linux boot loader GRUB2, there are 3 stages.
Stage1 – 446 bytes of boot.img resides on MBR –> This is used to load the Stage 1.5 Bootloader
Stage1.5 – 30 KB of code, known as core.img. [512 B of MBR+30KB of Core.img+1st partition+2nd partition = 1st disk]
This is generally used to store the file system drivers and loadable kernel modules(LKM)
This enables the stage 1.5 to load stage 2 bootloader.
Stage2 – Let’s identify what files belong to GRUB stage 2.
If you navigate to a Linux terminal on /boot
You would see the files that is required to boot the OS
- vmlinuz –> Compressed bootable Kernel Image
- initrd.img or initramfs.img –> This is the compressed virtual replica of the root file system.
- config –> File contains the configuration parameters for the Linux kernel
- system.map –> Symbol table used by the kernel
Moreover, Grub displays the list of all OS and expects the user to select the OS that it should load.
Now, where the flashing screen comes from.
It comes from /boot/Grub/grub.cfg –> This file contents are shown on the terminal while the machine boots.
If by any reason, you need to do some changes to this file, its better to change on the file under cat /etc/default/grub
After making any changes, execute either update-grub or grub2-mkconfig
Now, once the system boots and we select the kernel that needs to boot. And that leads us to the next stage.
Kernel Initialization :
- Here the Vmlinuz is uncompressed and loaded into the Memory,
- Kernel initializes itself after it loads
- It accessed the keyboard and other peripherals
- Moreover, the Kernel is loaded into the memory, now it needs to perform its 2nd task to mount the Linux root (/) file system.
- But, to mount the root file system, it needs to access its Kernel modules, which are there under /lib/modules, which is not possible unless the root file system itself is mounted.
- To get the solution, the image file called /boot/initrd.img uncompressed and loads
- Meanwhile this initrd.img file contains the directories that mirrors the real file system .
- Also, the contents of initrd.img file is a temporary root file system that helps the kernel to mount the root file system and load all kernel modules.
- Subsequently, Kernel executes a command pivot_root which alters the temporary file system (initrd) from the memory and establish the permanent root file system (/)
- Finally, Kernel initialises the scheduler with PID=0
- At this time, forks and executes /sbin/init or /bin/systemd program [ They are soft link to /lib/systemd/systemd)
Systemd or init process: Based on the system run level the process takes further.
Few notes, that may be helpful.
As a result, In older Linux distro(Ubuntu 14, Debian 6, RHEL6 and CentOS 6, they use Init) They call as Runlevels
In this case the newer distro (Ubuntu 15.04, Debian 7, RHEL 7 & CentoS7, they use Systemd) they call it as systemd or Targets
Advantage of systemd is,
1. It performs Fast Booting
2. It gives the login prompt
3. Gets displayed very soon.
There are 7 Runlevels or targets
|Init Run Levels (Old Distro)||Systemd or Targets (New Distro)|
|0= Poweroff |
1 = Rescue
2 = Multiuser (W/O Network )
3 = Multi User (NW)
4 = Unused
5 = Graphical
6 = Reboot
1 = Rescue Target 2 = Multiuser (W/O Network ).Target
3 = Multi User (NW).target
4 = Unused.Target
5 = Graphical.Target 6 = Reboot.Target