In this blog post we are going to talk about all that happens right from booting of your linux system till a bash prompt appears on your screen. So tighten your seat belts as this might be a long journey altogether 🙂
It all starts from the UEFI/BIOS, which is a firmware interface that enables your computer to understand the hardware. Its sole purpose is find a bootable disk, it might be a CD/ROM, a USB pen drive or may be even your hard drive.
Next, if you wish to boot a linux distribution then all you require is a boot loader. The task of a boot loader is to load the operating system, which means it is going to basically initiate the kernel.
To get initiated, the kernel requires drivers to be present. In order to get these drivers, there is initramfs. The initramfs is a initial ram file system which has been compiled to contain the root file system as well as some drivers that helps kernel in the booting process.
Once we have the drivers loaded, we are now ready to load the init . Init is the very first process your kernel is going to look for. The init on the previous linux systems was an process with the name /sbin/init but now in the newer distributions it has been re-directed to the systemd.
Systemd is basically the modern system manager, it is responsible to start “everything” 🙂
That was a brief introduction to the boot process, lets dive deep now into each of the aspects mentioned above :
UEFI or BIOS ?
BIOS and UEFI are nothing but interfaces that were developed to provide a platform that resides between the hardware/firmware and operating system.
BIOS is an older version of the interface, with the increase in development of hardware there was an emerging need to upgrade the interface and that is when UEFI came into the picture.
Booting from BIOS or UEFI disks:
On a BIOS system, the Master Boot Record(MBR) is read from disk and the stage1 boot loader is activated. The stage1 boot loader is only 446 bytes and its task is to load the larger stage2 boot loader that resides in the first MB of the disk. The first MB of the disk is the meta-data area. So, everything that is needed to load the kernel is completely present in this first MB of the disk.
This is how the MBR looks:
This is stored in the hexa-decimal format, so i can’t quite explain it to you :-p but there are some interesting facts to this. If you see on the lower right-hand side of the screen, you will be able to see something like ” GRUB .. Geom.Hard Disk.Read. Error”, this signifies that you shall enter a rescue shell mood if there are any errors while initiating the grub.
Another interesting thing we can observe is the highlighted ’83’ which signifies the code number for Linux partition type. So you can also identify the partitions from the MBR. Also, 55aa is kinda the magic code which signifies the completion of the MBR. One important thing to note here is that, logical partitions are not stored in the MRB so we do not find the ‘8e’ code in the block.
Enough about BIOS, lets check out some facts about UEFI too!
On a UEFI system, a GUID PArtition Table(GPT) is always used, although a MBR is maintained for the backward compatibility. The GPT partition table contains an EFI System Partition(ESP) which contains a directory with name EFI. In this directory, each boot loader has its own identifier and corresponding sub-directory. The amazing thing is that, the boot loader itself has the .efi extension and resides in these subdirectories along with its supporting files.
From BIOS/UEFI to the boot loader:
The boot loader is responsible for loading the OS kernel. Different boot loaders are available(such as GRUB, SYSLINUX, LILO, etc) but currently GRUB 2 is the most commonly used boot loader.
- Also, it provides other options like:
- Select a kernel from the boot manual.
- Pass kernel parameters
- Support other operating systems such as windows.
Now that we understand a little more, lets look into the boot process closely:
You can find the GRUB2 in your /boot/grub2/ directory:
Out of the above files shown, the most interesting content is present in the grub.cfg file. The grub.cfg file contains almost everything that the grub should execute. The grub.cfg file is a key configuration file of your system, you will know that as soon as you try to open that file it tells you not to update this file.
Quite interesting! isn’t it?
Lets check out some of the important content of the grub.cfg:
Above shown is the function load video, in this function you shall be able to see the insmod. You must have probably heard of insmod before, that pretty much what we do in a kernel environment. Different modules for the video card are present here, so depending on your specific video hardware, chances are that the right configurations will be loaded.
What is /etc/grub.d/10_linux?
Let us now understand, what exactly is in 10_linux block? The /etc/grub.d/10_linux is the input file which is providing the system which all the drivers that are needed like:
- load_video: video loader must be loaded.
- gzio: which helps in reading the initramfs.
- part_msdos: which provides the system with MBR partition table.
- ext2: it allows grub to read the contents of the /boot directory.
The above highlighted line set root=’hd0,msdos1′ indicates the first partition on the hard drive. We normally call the sd1 partition to indicate the first partition but given that sda is the kernel name and since the kernel has not been loaded yet, we cannot refer the partition as sda.
The above highlighted lines of shell scripts are basically the parameters that are passed to the kernel while the booting process. You shall find similar kind of code if you press e while your system boots.
If you scroll towards the end of the file(grub.cfg), you shall notice grub.d being mentioned many time. So what is grub.d? It is nothing but simply a file that reads all the kernels that are installed in the /boot directory and will provide menu entries. Menu entries are visible while the booting process.
The interface for the administrators is present in /etc/defaults/grub:
Some of the interesting statements are:
GRUB_TIMEOUT=5: this specifies how much time the grub should wait before it automatically selects the option for booting.
GRUB_CMDLINE_LINUX=: specifies the command line options, these are the options that are passed to the kernel that is loading.
We can remove rhgb which is the Red Hat graphical boot and quite makes the user see nothing while the system is booting, from the options. If we want the changes to be persistent then, we can use grub2 commands like grub2-mkconfig and so on.
So the reason why we need the initramfs is that, the linux kernel is modular, it is not compiled with all the possible disk drivers inside.
If you are using some proprietary drivers to access the hardware in your machine, which is required for booting, then the proprietary drivers cannot be in the kernel as they are proprietary and that is why it makes sense writing them to an initramfs.
Last is the systemd, i guess systemd is a huge topic itself. Lets talk about it in my next blog 🙂
Hope you enjoyed reading, happy hacking 🙂