Introduction
This is the last blog of our Linux blog in which we are going to discuss about the Linux boot process
The Boot Process
- The Linux boot process is the procedure of initializing a system it consists of everything which happens when the computer power is first switched on until the user interface is fully operational
Bios - The First Step
Starting an x86-based(Whose CPU is made with 8086 microprocessor) Linux
number of steps
When the computer is powered on, the BIOS initializes the hardware, including the screen and keyboard, and tests the main memory. This process is also called power-on self-test (POST)
The BIOS software is stored on a ROM chip on the motherboard. After this, the remainder of the boot process is controlled by the operating system
Master Boot Record(MBR) and Boot Loader
After the completion of the POST, the system control passes from the BIOS to the boot loader. The boot loader is usually stored on one of the hard disks in the system, either in the boot sector or the EFI(Extensible Firmware Interface) partition. Thereafter, information on date, time and the most important peripherals are loaded from the CMOS values(after a technology used for the battery-powered memory store which allows the system to keep track of the date and time even when it is powered off)
Several boot loaders exist for Linux; the most common ones are GRUB(Grand unified boot loader), ISOLINUX(For booting from removal device)and DAS U-BOOT(for booting on embedded devices). Most Linux boot loaders provide an interface for choosing the alternative options from booting Linux, and even another operating system that might be installed. When booting Linux, the boot loader is responsible for loading the kernel image and the initial RAM disk or filesystem (which contains some critical files and device drivers needed to start the system) into memory.
The boot loader has two distinct stages
- A system that uses the BIOS/MBR method, the boot loader resides at the first sector of the hard disk, also known as the Master Boot Record. The size of the MBR is only 512 bytes. In this stage boot loader examines the partition table and finds a bootable partition as it finds a bootable partition, it then searches for the second stage boot loader
The second stage boot loader resides under /boot. A splash screen is displayed which shows the operating system, from that os you have to decide the os with which you have to work. After choosing the os, the boot loader loads the kernel of the selected os in the ram and passes control to it. Kernels are almost always compressed, so its first job is to uncompress itself. After this, it will check and analyze the system hardware and initialize any hardware device drivers built into the kernel
Text-Mode Login
Near the end of the boot process, init starts a number of text-mode login prompts. Usually, The default command shell is BASH(Bourne Again Shell)
The Linux kernel
- The boot loader loads both the kernel and an initial RAM-based file system(initramfs) into memory, so it can be used by the kernel. When the kernel is loaded in RAM, it immediately initializes and configures the computer's memory and also configures all the hardware attached to the system. This includes all the processors, io subsystems, storage devices etc.
/sbin/init and Services
-Once the kernel has set up all its hardware and mounted root file system, the kernel runs /sbin/init. this ten starts the other process which gets the system running. Besides starting the system, init is responsible for keeping the system running ad shutting it down cleanly. One of its responsibilities is to act when necessary as a manager of all non-kernel processes; it cleans up after them upon completion, restarts user login services as needed when users log in and out, and does the same for other background system services.
Level of Abstraction
There are three levels of abstraction in Linux OS
Hardware: It is the Physical layer that computes what our system is doing
Kernel: Core component of the Operating system, interacts directly with hardware, provides low services to upper layer components
User Space: The interface we see, for eg shell, or chrome, simply we can say the components with which is every user is familiar to comes under this layer
Privilege level or Protection rings
Kernel operates in kernel mode whereas the user operates in user mode the user cannot access Hardware directly for that they have to communicate with the hardware. Here system calls come in.
System calls
A system call provides the interface between the process and the operating system. it is the way by which a computer program requests a service from the kernel of the os. Different os executes different system calls. In Linux it lets you switch from the unprivileged user mode to the privileged kernel mode
Init Process
There are three init process
System V
Upstart
Systemd
System V
It Starts and stops the process sequentially. to get a quick grasp of what System V is let's take an example, Let's suppose there are two Processes P1 and P2 and P2 is dependent on P1 so P2 should start before P1. This process is not that much efficient because we cannot run two processes parallely. for starting another process the process which is already running in the System should terminate.
Run levels of System v
0-> Shutdown
1-> Single User
2-> Multiple users without networking
3->Mutiple users with network
4->Unused
5->Multiple users using with networking and GUI
6-> Rebooting
To see all these Processes in your Linux os type
ls /etc/
All config file is present in etc and you can find your run config files, i.e rc0.d/, rc1.d/ etc
You can also check the script which is run whenever you turn off the computer by using this command ''' ls /etc/rc0.d''' shut down happens in 0 run level that is why i have written rc0.d after etc
Upstart
- It used events and job model and respond to any event as soon as it happens. Jobs is the action performed and events is the message received from another process. The upstart init conf file is present in etc/init/ you can check the scripts by running
How does it work?
When the system starts it looks for the run level in which the system is currently in and completes the events which is needed to be completed in that run level. The jobs and events are nested and trigger again and again which keep the system running
ls /etc/init
Systemd
- It is the most emerging for init it is being adapted and it Uses goals to get your system up and running and goals. this system is kind of flexible and robust. It does not do all the processes sequentially to get the system started
How it works
First loads up all the config files all these config files are present in two different locations i.e ls /etc/systemd/
and ls /lib/systemd/system
This must contain default.target which contains these targets which are to be run by the computer to keep the system running Like upstart works on the basis of runlevels, likewise Systemd runs on targets.
1-Poweroff.target-> Shutdown
2- Rescue.target -> Single user
3- Multi-user.target -> Multiuser with networking
4- Graphical.target -> Multiuser with networking and gui
5- Reboot.target -> Reboot
Sudo shutdown -h now
It will shut down the system change the state of the system