Linux Interview Q&A #58 - Explain Linux Boot Process ? #linux #viral

technotes of Hemant
16 Feb 202307:48

Summary

TLDRThis video script offers an insightful guide to the Linux boot process, essential for troubleshooting and system repair. It begins with the server power-up, followed by the BIOS's POST hardware check and bootable device search. The MBR's role in loading the bootloader, GRUB, is explained, detailing its stages and configuration. The script then delves into the kernel's responsibilities, including initializing hardware, loading drivers, and preparing the root file system. Finally, it touches on systemd's role in managing system services and reaching the operational state, providing a comprehensive overview of Linux's startup sequence.

Takeaways

  • 🔌 The Linux boot process begins with powering up the server, which triggers the BIOS to perform POST (Power-On Self-Test) to check hardware integrity.
  • 📂 BIOS, located in ROM, scans for bootable devices and looks for the MBR (Master Boot Record), the first sector of a bootable disk, to locate the bootloader.
  • 🔍 The MBR consists of a small program (446 bytes), a partition table (64 bytes), and a magic number (2 bytes) for error detection.
  • 🛠️ The bootloader, historically Lilo and now commonly GRUB or GRUB2, is responsible for loading the operating system.
  • 🔁 GRUB has multiple stages: stage 1 is in the MBR, stage 1.5 acts as an intermediary for filesystem interpretation, and stage 2 is the main GRUB image.
  • 📝 The bootloader reads configuration from files like '/boot/grub/grub.conf' or '/boot/grub2/grub.cfg' to determine how to load the kernel and initrd images.
  • 💾 The kernel initializes hardware, locates and decompresses the initrd image, loads necessary drivers, and initializes virtual devices like LVM or RAID.
  • 🌐 Once the kernel has set up the system, it creates a root device, mounts the root partition, and starts the first process (pid 1), which is now systemd in modern systems.
  • 🔄 Systemd, known as the 'mother of all processes,' manages the boot process by mounting filesystems, starting services, and bringing the system to a productive state.
  • 🔖 The boot process concludes with systemd reading '/etc/systemd/system/default.target' to determine the system's target state, such as graphical for desktops.

Q & A

  • What is the first step in the Linux boot process?

    -The first step in the Linux boot process is powering up the server, after which the hardware transfers control to the BIOS.

  • What are the two important tasks performed by BIOS during the boot process?

    -BIOS performs a Power-On Self-Test (POST) to check the health of all hardware components and then scans for bootable devices.

  • What is the significance of the Master Boot Record (MBR) in the boot process?

    -The MBR is the first sector of a bootable storage device, containing a small program that points to a bootloader, a partition table, and a magic number for error detection.

  • What is the role of the bootloader such as GRUB or GRUB2 in the Linux boot process?

    -The bootloader, like GRUB or GRUB2, is responsible for loading the operating system by reading configuration settings and identifying the kernel and initrd images.

  • What are the different stages of GRUB and what do they do?

    -GRUB has stages 1, 1.5, and 2. Stage 1 is in the MBR, stage 1.5 is an intermediate stage for file system interpretation, and stage 2 is the main GRUB image that loads the operating system.

  • What does the kernel do after it is loaded during the boot process?

    -Once the kernel is loaded, it initializes and configures the computer's memory and hardware, decompresses itself, locates and loads the initrd image, and initializes drivers for file systems like LVM or RAID.

  • What is the purpose of the initrd image in the boot process?

    -The initrd image contains drivers and system information needed during the boot process before the main system's root file system is available.

  • How does the kernel prepare the system for the final boot steps?

    -The kernel creates a root device, mounts the root partition in read-only mode, freezes unused memory, and starts the first process (pid 1), which is managed by init or systemd in modern systems.

  • What is the role of systemd in the boot process of a Linux system?

    -Systemd is responsible for managing the boot process by starting services, mounting file systems, and bringing the Linux host to a state where productive work can be done.

  • How does systemd determine the system's boot target?

    -Systemd reads the /etc/systemd/system/default.target file to identify the default target, which could be a graphical target for desktop workstations, equivalent to run level 5 in older systems.

Outlines

00:00

🖥️ Linux Boot Process Overview

The paragraph discusses the Linux boot process, starting from powering up the server to the initial steps of the boot sequence. It explains how the control is transferred from the hardware to the BIOS, which is located in the ROM. BIOS performs a POST (Power-On Self-Test) to check the health of all hardware components and then scans for bootable devices. It looks for the boot sector or boot loader, which is typically found in the MBR (Master Boot Record) of the first sector of a bootable device. The MBR consists of the initial 446 bytes of low-level code that points to a boot loader like GRUB or GRUB2, followed by a 64-byte partition table, and ending with a magic number for error detection. The paragraph also covers the evolution of bootloaders from LILO to GRUB and GRUB2, explaining that GRUB has multiple stages, with stage 1 being in the MBR and stage 1.5 and 2 handling file system interpretation and loading the operating system respectively.

05:05

🔧 Kernel and System Initialization

This paragraph delves into the role of the kernel in the boot process once the bootloader has transferred control. It describes how the kernel initializes and configures the computer's memory and hardware, uncompresses itself, and looks for the initrd (initial RAM disk) image. The kernel then loads necessary drivers, initializes virtual devices related to the file system such as LVM or RAID, and unmounts the initrd to free up memory. The kernel proceeds to create a root device, mounts the root partition in read-only mode, and freezes unused memory. The final step involves starting the first process (pid 1), which in modern systems is handled by systemd, replacing the traditional init process. Systemd is responsible for managing various aspects of the running Linux system, including mounting file systems, starting services, and devices. It reads from /etc/fstab to mount devices and identifies the default target from /etc/systemd/system/default.target, which determines the system's boot target, such as a graphical target for desktops.

Mindmap

Keywords

💡BIOS

BIOS, or Basic Input/Output System, is a firmware that initializes and tests computer hardware components during the booting process before starting the operating system. In the script, BIOS is mentioned as the first step after powering up the server, where it performs POST (Power-On Self-Test) to check the health of hardware components and then scans for bootable devices. It is crucial for the boot process as it ensures that the hardware is functioning correctly before loading the operating system.

💡POST

POST, or Power-On Self-Test, is an initialization routine that runs when a computer is powered on. It is part of the BIOS and checks the computer's hardware to ensure everything is functioning correctly. In the context of the video, POST is highlighted as an important task performed by BIOS to ensure that no faulty hardware prevents the system from booting up, and it can alert the user with beep sounds if an issue is detected.

💡Boot Sector

The Boot Sector is the first sector of a data storage device, such as a hard drive or a bootable USB drive, that contains the bootloader code. It is crucial for the boot process as it is where the BIOS looks to find the bootloader that will load the operating system. In the script, the boot sector is described as the starting point for the BIOS to locate the bootloader, which is essential for initiating the boot process.

💡MBR

MBR stands for Master Boot Record, which is the first sector of a storage device like a hard disk. It contains the boot sector, a partition table, and a signature. The MBR is significant in the boot process as it is where the BIOS looks to find the bootloader. The script explains that the MBR is read by the BIOS to load the bootloader, which then proceeds to load the operating system.

💡Bootloader

A bootloader is a program that loads the operating system into the computer's memory when the system is started. It is stored in the boot sector of a storage device. In the script, bootloaders like GRUB and GRUB2 are mentioned as essential components that facilitate the loading of the operating system after the BIOS has checked the hardware and located the boot sector.

💡GRUB

GRUB, or Grand Unified Bootloader, is an example of a bootloader used in Linux systems. It is mentioned in the script as the modern replacement for the older Lilo bootloader. GRUB is responsible for loading the operating system by reading the MBR and locating the necessary files to initiate the boot process. The script explains that GRUB has multiple stages, with stage 1 being part of the MBR and stage 2 being the main GRUB image that reads configuration files and loads the kernel and initrd images.

💡Kernel

The Kernel is the core component of an operating system that has complete control over the system's resources and manages the communication between the hardware and software. In the script, the kernel is described as the next major component to load after the bootloader. It initializes and configures the hardware, loads drivers, and sets up the system for the userspace processes to run.

💡Initrd

Initrd stands for Initial RAM Disk, which is a temporary root file system used during the Linux boot process. It contains drivers and other files needed to mount the real root file system. The script explains that the kernel looks for the initrd image, decompresses it, and loads the necessary drivers before it can access the actual root file system.

💡Systemd

Systemd is an initialization system and service manager for Linux operating systems, which has largely replaced the older SysVinit system. It is mentioned in the script as the first process with PID 1 that starts after the kernel has set up the system. Systemd is responsible for managing system processes, mounting file systems, and bringing the system to a state where it can perform productive work.

💡Boot Process

The boot process refers to the steps a computer takes to start up from a powered-off state to a fully operational state where it can run applications. The script provides a detailed walkthrough of the boot process in Linux, from powering on the server and the role of BIOS, through the stages of the bootloader, to the kernel's initialization and the final steps managed by systemd.

Highlights

Understanding the Linux boot process is crucial for troubleshooting and fixing the operating system.

The boot process begins when the server is powered up and control is transferred to BIOS.

BIOS performs POST to check the health of all hardware components.

BIOS scans for bootable devices and looks for the boot sector to provide the bootloader.

MBR is the first sector of a bootable media, containing a small low-level code pointing to a bootloader.

GRUB or GRUB2 is commonly used as the bootloader in modern systems.

Bootloader stages include stage 1 in MBR, stage 1.5 for filesystem interpretation, and stage 2 for the main GRUB image.

GRUB configuration settings are found in /boot/grub/grub.conf or /boot/grub2/grub.cfg.

The kernel initializes and configures computer memory and hardware during the boot process.

The kernel looks for the initrd image, decompresses it, and loads required drivers.

Initrd is used for initializing drivers for filesystems like LVM or RAID.

The kernel mounts the root partition in read-only mode after initializing drivers.

Systemd is the first process started with pid 1, responsible for bringing the Linux host up.

Systemd manages mounting file systems, starting services, and devices attached to the Linux system.

Systemd starts by mounting devices defined in /etc/fstab and identifying the default target to boot into.

The boot process is completed by systemd starting the required dependencies and reaching the specified target.

Transcripts

play00:02

Hello Everyone.  

play00:04

This is one of the interesting question which you  will find in most of the interviews. If you leave  

play00:09

apart interviews one need to understand the boot  process if they want to troubleshoot the Linux  

play00:16

or in bad you have to fix the operating system,  you should undertand exactly how linux boots.  

play00:25

So, rather than lot of text with theory we would  try to cover them with help of some diagramatic  

play00:32

steps. Now, very first step start with your server  and you Power up the server, then the step of  

play00:41

event that happen is your hardware basically  tranfer the control to a program called BIOS.  

play00:47

Now, this bios resides in the ROM, so it has to  perform two important task. The first one is POST,  

play00:54

where it will perform the check of, the health  check of all the hardware component attached  

play00:59

to your server, so that in case any of the  hardware component is bad, it will stop system  

play01:04

from starting. And you will notice a beep of  sounds. Once the hardware are good then, it will  

play01:12

start scanning the list of bootable devices. Your system might be attached with lot of devices  

play01:18

like floppy drive, optical drives, or usb or  anything else. SO it would start looking for the  

play01:24

bootable devices. And it will start looking for  the first sector or you would say a BOOT SECTOR,  

play01:31

which can provide you the boot loader. So, once  it can find the boot sector or the boot loader,  

play01:38

then it will read the MBR on that disk or  whatever device it is, and load the program.  

play01:46

Now, if you don't know what MBR is, it's  basically the first sector or the first 512 bytes,  

play01:52

usually it finds in the first sector of  your bootable media. If it's a hard disk,  

play01:58

it's path is something as /dev/hda or maybe  something like /dev/sda. Now, it comprises of  

play02:05

first 446 bytes, which is a small low level code,  which basically points to a boot loader, whatever  

play02:11

it is GRUB GRUB2. or anything else, or on a disk  it can point to another hard disk as well.  

play02:20

Now the next 64 bytes, these are the partition  table and rest 2 are the magic number, these  

play02:28

are sometime used for the error detection or even  for the sanity of the media which you are using.  

play02:34

Now if we speak about bootloader previously  there was Lilo so we would not discuss much  

play02:39

on the Lilo but nowadays it's grub  or the recent one have the grub2  

play02:45

so its major rule is just to load the operating  system. It's a program basically written in the  

play02:51

MBR and /boot partition and basically or  hard disk and it loads operating system.  

play02:58

So this code in the past basically used to fit  in the first four four six bytes of your MBR,  

play03:03

but since the size is increasing the complexity of  the operating system is increasing and sometimes  

play03:10

we have to boot almost any operating system  this size is increasing with size with time.  

play03:16

Now BOOT again, the grub is not a single program  it has several stages so the stage 1, basically  

play03:25

it's the code in the first this is the main code  which is found in the first 512 bytes of MBR  

play03:32

so the size is limited so usually it points to  the next stage of grub that can be 1.5 or 2.  

play03:39

Now this 1.5 stage is the intermediate  stage basically it allows us the file  

play03:45

system to be interpreted properly  then you will find the in stage 2.  

play03:52

So it's a main grub image and it's usually decided  on the file system in the slash boot partition  

play03:57

or add the slash boot grub whatever path it  is it usually reads the boot grub2/grub.comnf  

play04:02

or the old systems bootcrubgrub.com for the  configuration settings that how it will load  

play04:08

the kernel or something like that. And also it will try to identify from  

play04:15

the slash boot or grub.conf will try to  find the kernel and the initrd images.  

play04:20

Now what kernel is and what will  perform later let's see further.  

play04:34

Now when grub transfer the control  to the kernel and kernel is booting,  

play04:38

we can see a lot of messages and it can be easily  suppressible through a RHGB quiet. So, when a  

play04:45

kernel is loaded it immediately initializes and  configure the computer memory and various Hardware  

play04:51

attached to the system. That includes something  like processors, I/O systems, storage devices.  

play05:00

Then it has to uncompress itself so it  start looking for the initrd image in a  

play05:04

predetermined location in the memory, now  decompresses it mount it and load all the  

play05:10

required drivers for the next steps. Then it  would initialize a virtual device related to  

play05:16

your file system so your storage whatever file  system you have carried over storage like lvm,  

play05:20

raid. Because once initRd is removed or unmounted  it cannot be accessed, so it basically initializes  

play05:28

all these drivers and then free up all the  memory that disk image has occupied.  

play05:33

Now, once these steps are done  it create a root(/) device  

play05:38

it mounts your root partition in a read-only  mode and then freeze all the unused memory.  

play05:42

so everything is done a root is there but still  there's no other steps remained with kernel too.  

play05:50

So the last step what it will perform is start you  a process that start with the pid1 in the previous  

play05:59

sysvinit systems that process is called init but  for the now the new systems that is a systemd.  

play06:06

Now systemd,  

play06:14

if you say it's a mother of all the processes  and is responsible for bringing the Linux host  

play06:21

up to a state in which the productive work can  be done. Some of its function which are more  

play06:27

extensive than the old init program are to manage  many aspects of a running Linux host, includes  

play06:32

mounting file system starting the services and the  devices attached to a particular linux system.  

play06:40

so now what we'll do for firstly it will  start mounting the devices defined in the  

play06:48

/etc/fstab. So any swap files or an  additional partition it start mounting.  

play06:54

Now it will start having the access of  /etc/ so let's start reading the file  

play07:02

/etc/systemd/system/default.target to identify  in which default Target I have to boot.  

play07:08

Now once the default default is nothing just  a symbolic link to the true target file,  

play07:15

so for a desktop workstation it would  be like a graphical Target which was  

play07:20

equivalent to run level 5 in the old  systems. so once identify the Target  

play07:26

in which it has to boot it start looking  for the file for the required dependencies  

play07:32

and once all the dependencies are up, your system  will start putting into the required Target. that  

play07:37

is in short a small version how the complete  boot process happens anything else you have  

play07:43

or any questions you have always feel free  to put in the comment section thank you bye.

Rate This

5.0 / 5 (0 votes)

関連タグ
Linux BootBIOSGRUBKernelinitrdSystemdHardware CheckBootloaderMBRFile System
英語で要約が必要ですか?