Sunday 8 December 2013

Linux Boot Process ; How Computer Boots Up ??

Linux Boot Process ; How Computer Boots Up ??

The PC boot process is a 3-stage boot process that begins with the BIOS executing a short program that is stored in the Master Boot Record (MBR) of the first physical drive. Since this stage 1 boot loader needs to fit in the MBR, it is limited to 512 bytes and is normally written in assembly language.
  There are a number of boot loaders that can load Linux.

     ● GRUB and LILO are the most commonly use  ones on X86 hardware.
       
     ● EFI is used on the Intel® Itanium® family.



Step by Step & Detailed Linux Boot Process

First Power On the System (PC). After this BIOS will take place.

BIOS :-

Once System start, first step BIOS (Basic Input Output System) will do POST (Power On Self Test). The POST will check all hardware connected to system working correctly or not. Once POST completes, it flushes from the memory, but the BIOS runtime services remain and it searches for devices for bootable disk, that order of preference defined in the complementary metal oxide semiconductor (CMOS) settings. A boot device can be a floppy disk, a CD-ROM, a partition on a hard disk, a device on the network, or even a USB flash memory stick.

Once BIOS detects the bootable device then it executes the MBR (Master Boot Recorder).
1st Stage of the Boot Loader.
MBR is 1st sector (512 Bytes) of the 1st bootable device. In 512 bytes, 3 parts are there :

i. 1st 446 bytes has primary boot loader information.
ii. Next 64 bytes for Partitions (16+16+16+16) for this reason only we are able to make 4 Primary Partitions.
iii. Next 2 bytes for validation check of MBR. (Magic Number)

Now the MBR Loads in to RAM.
First Stage of the boot loader loads it-self in to memory, & finds the second stage boot loader, this is done by looking through the active partition table. When it finds an active partition, it scans the remaining partitions in the table to ensure that they are all inactive. After this verification the active partition’s boot record is reads from the RAM, and it will execute it.
In simple MBR loads the GRUB (Grand Unified Boot loader) from 1st sector of the 1st bootable partition(HDD).

GRUB :-
GRUB has the knowledge of the file system, but older Linux loader LILO didn’t understand filesystem.
The stage 2 boot loader presents a boot menu to the user based on /boot/grub/grub.conf or menu.lst. This contains a boot script. GRUB displays a splash screen, waits for few seconds, if you don’t enter anything, it loads the default kernel image as specified in the grub configuration file.
Here the sample of grub.conf file.
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS Linux (2.6.32-220.2.1.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-220.2.1.el6.x86_64 ro root=UUID=92a892bc-b9cd-45a4-be09-4c2df953b2f1 rd_NO_LUKS rd_NO_LVM rd_NO_MD rd_NO_DM LANG=en_US.UTF-8 SYSFONT=latarcyrheb-sun16 KEYBOARDTYPE=pc KEYTABLE=us crashkernel=auto rhgb quiet
initrd /initramfs-2.6.32-220.2.1.el6.x86_64.img
———————————————————————————————
hd0,0 : In this hd0 means the first Hard Disk,& “,0 Means 1st partition. Your Kernel & Initrd presented in the first partition of the first HDD.
In second line will see kernel /vmlinuz.x.x.x.. Instead of /boot/vmlinuz.x.x.x because hd0,0 is boot partition, For third line is also same.

KERNEL :-
Once kernel take place to load,
Mounts the root file system as specified in the “root=” in grub.conf file as read only, to avoid the problem with fsck, compressed kernel image. Typically this is a zImage (compressed image, less than 512KB) or a bzImage (big compressed image, greater than 512KB), that has been previously compressed with zlib. At the head of this kernel image is a routine that does some minimal amount of hardware setup and then decompresses the kernel contained within the kernel image and places it into high memory.
When the kernel is loaded, it immediately initializes and configures the computer’s memory and configures the various hardware attached to the system, including all processors, I/O subsystems, and storage devices. It then looks for the compressed initrd image in a predetermined location in memory, decompresses it, mounts it, and loads all necessary drivers. Next, it initializes virtual devices related to the file system, such as LVM or software RAID before unmounting the initrd disk image and freeing up all the memory the disk image once occupied.
During the boot of the kernel, the initial-RAM disk (initrd) that was loaded into memory by the stage 2 boot loader is copied into RAM and mounted. This initrd serves as a temporary root file system in RAM and allows the kernel to fully boot without having to mount any physical disks. Since the necessary modules needed to interface with peripherals can be part of the initrd, the kernel can be very small, but still support a large number of possible hardware configurations. After the kernel is booted, the root file system is pivoted (via pivot_root) where the initrd root file system is unmounted and the real root file system is mounted.

INIT :-
After the kernel is booted and initialized, the kernel starts the first user-space application.
In a Linux system, the first application started is commonly /sbin/init. INIT is the 1st Process of the linux system, It’s PSID is 1.
First, it runs the/etc/rc.d/rc.sysinit script, which sets the environment path, starts swap, checks the file systems, and executes all other steps required for system initialization. For example, most systems use a clock, so on them rc.sysinit reads the /etc/sysconfig/clock configuration file to initialize the hardware clock.
The init command then runs the /etc/inittab script to decide which run level os has to come up.

   Most Linux distributions use the following standard run levels 0-6.
  
   0. System halt. (Do NOT set initdefault to this)
   1. Single-user mode. S and s may also be used here.
   2. Multi-user mode with no remote networking.
   3. Multi-user mode with networking. This is the standard
      operating mode of a non-GUI based system. All
      networking daemons are running.
   4. Unused.
   5. Multi-user mode with a GUI. This mode has most of the
      same daemons as run level 3 plus it is running the X
      Window System with a display and window manager.
   6. Reboot. (Do NOT set initdefault to this)

INIT identifies the default run level from /etc/inittab , After this the init command sets the source function library, /etc/rc.d/init.d/functions, for the system, which configures how to start, kill, and determine the PID of a program.
The init program starts all of the background processes by looking in the appropriate rc directory for the runlevel specified as default in /etc/inittab. The rc directories are numbered to corresponds to the runlevel they represent. For instance, /etc/rc.d/rc5.d/ is the directory for runlevel 5.
In /etc/rc.d/rc5.d/ directory what are all files starting with “K” those responsible for Kill the process when system is rebooting or shouting down. And Starting “S” those responsible for Start the process.
After the system is finished booting, it is possible to log in as root and execute these same scripts to start and stop services. For instance, the command/etc/rc.d/init.d/httpd stop stops the Apache HTTP Server.
Each of the symbolic links are numbered to dictate start order. The order in which the services are started or stopped can be altered by changing this number. The lower the number, the earlier it is started. Those symbolic links with the same number are started alphabetically.
One of the last things the init program executes is the /etc/rc.d/rc.local file. This file is useful for system customization.
After the init command has progressed through the appropriate rc directory for the runlevel, the /etc/inittab script forks an /sbin/mingetty process for each virtual console (login prompt) allocated to the runlevel. Runlevels 2 through 5 has all six virtual consoles, while runlevel 1 (single user mode) has one and runlevels 0 and 6 have none. The /sbin/mingetty process opens communication pathways to tty devices, sets their modes, prints the login prompt, accepts the user’s username and password and initiates the login process.
In runlevel 5, the /etc/inittab runs a script called /etc/X11/prefdm. The prefdm script executes the preferred X display manager — gdm, kdm, or xdm, depending on the contents of the /etc/sysconfig/desktop file.
Once finished, the system is operating on runlevel 5 and displaying a login screen.
****************** END of the Boot Process *****************