How Kernel boot up ??
How the Linux Kernel Boots?
Hey tech lovers. This time back with one of the interesting topics of Linux, how Operating System start? The reason behind writing this blog is curiosity within me pulls down to know what's going under the hood when OS starts. After knowing can stop myself from sharing with you all. I believe that an individual can not learn everything but by sharing things it becomes easier for the learner to understand things properly within a short span of time. Always try to share your knowledge. It enhances your knowledge as well as saves time for others.
Let's begin with the topic:
What happens after pressing the Power ON button on the computer?
As the user powers ON the machine, the Power supply goes to the motherboard. The motherboard tries to start the CPU. After the CPU starts, first of all, it clears old data from registers. After that CPU goes to its memory location(0xffff0000→ address sector on the hard disk) to see if any instruction is present or not. By the way, that address is the home or location of BIOS. Basically, the CPU executes the BIOS program. And that's how BIOS starts.
Note:
Basically, BIOS or UEFI represents firmware. The general difference between them is that BIOS is traditional and UEFI is modern one. Your system may have either of them.
To check which one is present in your system, run efibootmgr command.
BIOS(basic input/output system) performs POST (Power On Self Test) checks
After BIOS starts, it performs various checks or tests on many components of hardware such as CPU tests, RAM tests, etc. The whole testing process is known as POST(Power On Self Test). Basically, the main task of BIOS is to ensure that the computer hardware is functioning correctly or not. If POST fails, the computer may not be usable and so the boot process does not continue. After successfully completing of POST operation. The next job of BIOS is to search the boot sector from the attached disks. And do you know, what the boot sector contains? It's a Boot record popularly known as a Master Boot Record (MBR). Simply, BIOS loads MBR instructions code to RAM, executes it, and handles over control to the MBR.
What MBR does do?
The first 512-byte sector is on the hard drive along with the partition table. The total amount of space allocated for the actual MBR is 446 bytes and the remaining are assigned to the Partition table.
Because the boot record must be so small, it is also not very smart and does not understand filesystem structures. Therefore the sole purpose of MBR is to locate and load GRUB. In order to accomplish this, GRUB must be located in the space between MBR and the first partition on the drive. After loading GRUB into RAM, MBR handles control over the GRUB program.
At sector 0 ---> MBR is located of size 512 bytes
Between sector 0 and 2048 ---> GRUB is located of size which is of size 1MiB(2048*512 bytes = 1024KiB = 1MiB)
From 2049 sectors onwards ---> Partitioning of disk starts
Note: Hard disk is divided into sectors. The sector is the smallest unit of Hard disk. 1 Sector = 512 byte
GRUB(Grand Unified Boot Loader)
What is the use of GRUB?
To know the answers you have to know what is booting first of all.
In technical terms, booting means copying the kernel image from a hard disk to memory and afterward executing it. So, copying the kernel image from disk to memory is performed by the GRUB program. Along with the kernel image GRUB also helps to pass some parameters. This is where GRUB comes into use.
But the biggest problem for GRUB is how to search the kernel image in the disk. Listen carefully don't get confused. I know you are thinking that searching any file in Linux is very easy, yes 100% right no doubt of that, but it is when your OS is in a running state(i.e. Kernel is running + necessary drivers and programs are running).
But currently, only GRUB is running, your system has not yet started. What it means is, that neither the kernel is running nor the required drivers and necessary programs are running. So, here searching any file is like an impossible task. Then, how does GRUB search kernel images from disk ??
Let’s see how GRUB solves these problems.
GRUB has to find the answer to 2 questions:-
- What is Kernel and its parameters?
- How to find or search it on the hard disk?
To load the Kernel image into memory GRUB has to search the Kernel image along with parameters from the disk. To access the hard disk and file system for searching Kernel images. GRUB takes the help of BIOS or UEFI. Btw, disk allows BIOS or UEFI to access hardware storage via Logical Block Addressing(LBA). Are you able to memorize how BIOS searched for MBR from disks? LBA is a universal, simple way to access data from any disk. One more thing, most Boot Loader Program, GRUB, can read partition table( index page that maps files to their corresponding address on the hard disk) which is built-in support for read-only access to the file system. Therefore, GRUB solves the problem of searching kernel images from the disk. Once it gets the Kernel image(i.e. **.iso** file) and its parameter it loads to memory along with that it also loads initramfs .
Below, BOOT_IMAGE refers to the kernel image as well as its parameter
BOOT_IMAGE=/boot/vmlinuz-4.15-generic root=UUID=179hfy4955 ro quiet splash vt.handoff=1
kernel image ---> /boot/vmlinuz-4.15-generic
Parameters ---> root, ro(read-only), vt.handoff,
initramfs is an archive containing the kernel modules for the hardware required during the boot time, initialization scripts, drivers, and more. These are modules that connect with hardware as well as software. Maybe this question arises in your mind as to why the Kernel image is not directly attached to its modules. Why these modules are kept separately in the form of `initramfs`. Let's contradict our point, so let's say all kernel modules are present inside the Kernel image. Basically, what will happen, is as the CPU starts executing the Kernel image then along with that all modules will also start executing whether it is needed by the Kernel or not. This leads to the bottleneck of RAM or an overflow of RAM. As a result, memory may crash. So, that's why Kernel modules are separated and only those modules are taken from `initramfs` which are required by the Kernel at that particular time. For example, we all know that there is a module or driver for a printer that is used when the printer is attached. But during the boot time, the printer's driver module is not required. But if it is too executed then it's total waste of your RAM and CPU. To know more about initramfs refer.
Once the GRUB loads the kernel image and executes/starts it. The control is transferred from GRUB to Kernel and from here Kernel work starts.
High-level idea regarding the function of the kernel:
- Kernel initialization
- CPU inspection
- Memory inspection
- Device bus discovery
- Device discovery
- Auxiliary kernel subsystem setup
- Root filesystem mount
- User space start(Systemd)
During the initialization process, Kernel must mount a root file system before starting init or systemd. We don't need to go into detail about it. Finally, the kernel's last job is to start Systemd and also hand over control to Systemd. This is the end of the boot process. At this point, the Linux kernel and systemd are running but unable to perform any productive tasks for the end user because nothing else is running.
Systemd is a system as well as a service manager in Linux. It is the mother of all processes and programs. From here all your services and user space programs are managed as well as controlled by it. In the next blog will know about Systemd in detail.
flowchart:-
References
https://opensource.com/article/17/2/linux-boot-and-startup
https://learning.oreilly.com/library/view/hands-on-booting-learn/9781484258903/
https://learning.oreilly.com/library/view/how-linux-works/9781098128913/
Thanks for reading it. Hope you liked it and gained some knowledge. Tried my best to make you under the topic step by step. Thank you guys. Will see you in the next blog.
Comments
Post a Comment