Friday, 11 August 2017

Boot process of linux

I know you always get amazed, and curious to know how your linux box starts, So lets have a quick look at it.

So, How does a Linux Box boot :

The computer either powered on or reset, the BIOS gets executed.

BIOS: Basic Input Output System. Its is a set of firmware and software, traditionally stored in a EEPROM (Electrically Erasable Programmable Read Only Memory) chip. That EEPROM is CMOS (Complementary Metal Oxide Semiconductor) which requires a battery to keep the settings saved.
The BIOS performs two tasks:

1. POST (Power on Self-Test): BIOS detects and initialize the connected hardware on various ports and PCI. It also checks whether the hardware is working from its basic level.
2. Run Controller: After POST, BIOS checks for the boot sectors in respective media according to boot priority saved in BIOS itself.

Boot Sector: A 512 byte starting sector of Hard disk, Floppy or CD/DVD which contains boot information.
A hard disk contains a boot sector called MBR (Master/main boot record) which is 512 Byte starting sector which holds boot loader,
Where 446 Bytes are used to contain a boot loader, next 64 Bytes are used to contain partition (primary) information and next 2 Bytes contains magic numbers. Magic number checks the validation status of MBR.
By default DOS MBR is used in a hard disk which can be overwrite with Linux Boot Loader like LILO & GRUB.
GRUB is new boot loader which overcomes the limitations of LILO, so here I am going to explain GRUB.

GRUB: (Grand Unified Boot Loader) an intelligent boot loader which supports chain boot loading (A responsibility of also booting another OS'es like Windows, DOS, FreeBSD etc.).Being large in size, GRUB boots in stages :

Stage 1: It is presented into MBR and loads the next stage boot loader. It refers partition table for an active partition and after finding active partition it checks other partitions to ensure that whether they are inactive.
Stage 1.5: The stage 1.5 contains drivers & modules to understand a file system. When stage 1.5 boot loader is loaded, it is able to load the stage 2 boot loader located at ext2 or ext3 file system partition as it is able to read a file system.
Stage 2: The task at this stage is to loads the Linux kernel & initial RAM disk. With stage 2 loaded GRUB display a list of available kernels (defined in /boot/grub/grub.conf).
User can select a kernel and also some additional kernel parameters can be used there. User can use a command line shell for greater manual control over the boot process.
Kernel: The loaded kernel is not much executable kernel. It is a compressed kernel image. It gets control from stage 2 boot loader. Typically it is zImage (compress image less than 512KB) or a bzImage (big compressed image more than 512KB). The kernel is now decompressed through a call to a C function called decompress kernel.
During the boot of kernel, the initial RAM disk which was loaded by stage 2 boot loader is copied into RAM and mounted. This provides temporary root file system in RAM and allow kernel to fully boot without having mount to any physical disk. Initrd also provides the needed modules to access a list of hardware interface. After the kernel is booted, the root file system is pivoted (The process of overcoming by initrd to root file system) and initrd root file system in unmounted and the real root is mounted now.
Init : After kernel is booted and initialized, the kernel starts the first process called init ('/sbin/init', '/etc/init', '/bin/init' or '/bin/sh). The init program reads it's configuration from the 'inittab' file that is located in the 'etc' directory.This file is where run levels come into existence. A runlevel is a software configuration that allows only a selected group of processes to exist. Red Hat Linux Runlevels are:

1 - Single User Mode
2 - Multi User mode
3 - Full Multi User Mode 4-NotUsed
A inittab file overview:
1.   | id:3:initdefault:
2.   | si::sysinit:/etc/init.d/rcS
3.   | l0:0:wait:/etc/init.d/rc 0
4.   | l1 : S1:wait:/etc/init.d/rc 1
5.   | l2:2:wait:/etc/init.d/rc 2
6.   | l3:3:wait:/etc/init.d/rc 3
7.   | l4:4:wait:/etc/init.d/rc 4
8.   | l5:5:wait:/etc/init.d/rc 5
9.   | l6:6:wait:/etc/init.d/rc 6
10. | ca:12345:ctrlaltdel:/sbin/shutdown -h now
11. | su:S016:respawn:/sbin/sulogin
12. | 1:2345:respawn:/sbin/agetty tty1 9600
13. | 2:2345:respawn:/sbin/agetty tty2 9600
14. | 3:2345:respawn:/sbin/agetty tty3 9600
15. | 4:2345:respawn:/sbin/agetty tty4 9600
16. | #5:2345:respawn:/sbin/agetty tty5 9600
17. | #6:2345:respawn:/sbin/agetty tty6 9600

Here in file:
The first line tells the default runlevel, in which mode the system will boot. 3 stands for full Multi User Mode.

The Second line tells about the scripts that are needed to be run for each runlevel. There is also a line specifying a script to run before all the others, like setting up localnet and mounting filesystems.

The Line 3rd to 9th describe the scripts associated to 0 to 6 runlevels. When the system boots, default runlevel associated scripts runs.

10th Line describes Alt+ctrl+del action. 11th line describes switching user script.
Many of the entries are also specified with 'respawn'. This parameter tells init to continually execute this program whenever the previous one exits. When one logs out, init respawns the getty process to allow another login.

The directories /etc/init.d/rc (0-6) contains services scripts. The service which gets automatically start on system boot is named as starting from 'S' and which is disabled by default or by user is named as starting from 'K'.Immediately following the identifier is a two digit number that indicates the order it is to be executed. The scripts are actually run in alphabetical order, so the lower numbered scripts are run first.

Finishing Up: Once init finishes with the startup scripts, it runs a getty process on each terminal specified in inittab. Getty is what you see when you are logging in.
Once you enter your username, the getty process runs login, which in turn asks for your password.

No comments:

Post a Comment