Skip to main content

A COMPLETE WALK THROUGH LINUX BOOTING PROCESS

Many of us are using Linux, but how many have ever wondered what exactly happens when a system is powered on, how Linux kernel is loaded, how a login screen prompts? Let's have a deep walk though Linux booting process. After reading this, one would have understanding not only on Linux booting process, but also on BIOS, UEFI firmware, MBR & GPT partitions, bootloaders like GRUB, Linux kernel etc. Let's get started . . . .
In order to boot Linux, a Linux capable bootloader such as syslinux or GRUB is needed. They need to be installed in Master Boot Record (MBR) or in GUID Partition Table (GPT). Boot loader loads the kernel and initial ramdisk before actual boot process is initialized. This process is different for different firmwares such as BIOS and UEFI.
When the power is on, Power On Self Test (POST) begins which ensures hardwares are ready. After it, BIOS or UEFI firmware is loaded which initializes required hardwares for booting.

WHAT IS BIOS?
BIOS (Basic Input Output System) is a firmware which normally resides in a independent flash storage or ROM in the motherboard. After POST operation, POST code gets flushed from RAM but BIOS runtime remains and searches for devices (CD-ROM, USB, partitions on HDD or even devices on the network), which are both active and bootable in the order defined in CMOS settings. It understands partition table only. So it loads stage-1 bootloader i.e. MBR, a 512 byte sector located in the first sector on the disk (sector 1 of cylinder 0, head 0), whose job is to load stage-2 bootloader such as GRUB, Linux Loader (LILO), syslinux etc. Then MBR takes control from BIOS and loads next disk sector which looks for actual bootloader in the partition, which then loads the OS.

WHAT IS UEFI?
Unlike BIOS, which supports MBR boot code, UEFI is not limited to 446 byte code limitation as it recognizes both partition table and the filesystems. UEFI firmware supports both MBR and GPT though it does not use MBR whether it exists or not, instead it uses EFI partition to determine which UEFI application to be executed, from which disk and partition. If EFISTUB is enabled by default, the application could be the kernel itself else it could be other shell scripts or boot manager or even the boot entry could be a disk.
Vendor can store its files under EFI_FILE_SYSTEM/EFI/VENDOR_NAME/ folder and the boot programs can be launched using UEFI shell.
In my case EFI_FILE_SYSTEM = /boot/efi and VENDOR_NAME = ubuntu.
So I can find my boot programs in /boot/efi/EFI/ubuntu/ folder. Normally EFI file system is formatted as FAT32. In my case EFI file system is installed in /dev/sda1. So I can check informations by typing [fdisk -l /dev/sda1], which would result:

root@ubuntu:# fdisk -l /dev/sda1
Disk /dev/sda1: 513 MiB, 537919488 bytes, 1050624 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos

UEFI also verifies authenticity of EFI binary if secure boot is enabled.
After bootloader is loaded, based on configuration it loads the kernel with required kernel parameters and optional initial RAM disk (initramfs/initrd), a temporary root file system. Bootloader passes control to the kernel, which then gets decompressed and initialized.  If initramfs is used, it gets unpacked, which becomes the initial root filesystem. The kernel then executes /init or main.c program which is the main function for the Linux kernel, as the first user-space process. So init always gets process id as One. Then high level system initialization starts.
After kernel is booted, the real root is  mounted which replaces the initial temporary root filesystem. /sbin/init gets executed replacing /init process. Few Linux distros use systemd as default init. 'init' calls getty once for each virtual terminal. It initializes each TTY which would ask for username and password. Once credentials are entered, they are checked against passwd and shadow file by getty. passwd and shadow files can be found under /etc/ directory. Then it calls login, which initiates session for the user and provides a shell (which ever is configured in passwd file for the user).
If graphical user interface is desired then display manager can be configured to avoid getting login prompt on tty. The login program sets up environment variables and begins a session for the user. Once shell is started it would run bashrc for that user else if start-X is configured at login then startx or xinit would be called.

This is how we get our workspace after turning the system on.
It seems a hell lot of processes are executed, but these happens incredibly fast, that login screen prompts with in few seconds. Hopefully I have not missed any intermediate processes. If you find any, feel free to add in comment section, I will update my post accordingly.

Thanks
Ayush

Comments

Popular posts from this blog

How to install Arch Linux?

What is arch linux? Arch Linux is a GNU/Linux distribution, which aims at providing the latest stable version of most of the softwares by following a rolling release model. It defines simplicity without adding unnecessary modifications. It is designed to boot on-line, so that you will get the latest version of Arch in any way. If you have already arch installed on your system then you can always upgrade it by using [pacman -Syu] command. When you boot arch Linux from USB it will automatically start DHCPCD and drop a ZSH shell . you can use nano, VIM to modify any file. Before installation: - Verify the boot mode [ ls /sys/firmware/efi/efivars ] If the directory does not exist then your system might be booted in BIOS or CSM mode, else it is UEFI. - Connect to Internet: If Arch did not get any IP address do run [ dhcpcd ] manually. - for wireless connectivity:- [ iw dev ] to get wireless interface name. [ iw dev INTERFACE link ] to get the status of the interface. [ iw dev

What is Master Boot Record (MBR) ?

Master Boot Record is the information stored in the first sector (cylinder 0, head 0) of a disk, which contains primary bootloader, which is a 512 byte image. It contains both program and a partition table. 446 bytes out of 512 bytes contains executable codes and error messages text. Next 64 bytes are partition table. Each table of 16 bytes. That is the reason why in MBR labled disks, maximum 4 partitions can be created (16 * 4 = 64 bytes). Last 2 bytes is for validation check for MBR. The job of the primary boot loader is to find stage-2 or secondary bootloader from the partition table. Then it loads boot record from the device into RAM. MBR content can be extracted by running dd command as a root user. [ dd if=/dev/sda of=/tmp/mbr bs=512 count=1 ] where /dev/sda is our HDD, /tmp/mbr is the destination file where MBR will be extracted to. As MBR is of size 512 byte,  512 block size is mentioned. As it is in binary format, to view this file in ASCII format enter [ od -xa /tmp/mbr

GNU/Linux

WHAT IS LINUX? People often consider Linux as a complete operating system, but technically it is an operating system kernel, which is the part originally developed by Linus Torvalds. WHAT IS GNU/LINUX? Linux is a kernel which makes system hardware usable by providing an interface to the operating system, but only cosidering linux is of no use. We need an operating system with bunch of applications which would use the hardware resources by the help of kernel to get our jobs done. Richard Stallman's free software foundation had created many softwares but what they were missing was the kernel, which Torvald had developed. So they merged to get a complete operational OS, which is known as GNU/Linux today. GNU/Linux is licenced under GNU General Public License(GPL) which allows distribution and sale of possibly modified and unmodified versions of Linux but requires that all those copies be released under the same license and be accompanied by the complete correspoding source co