Wednesday, 8 August 2012

Linux boot process in RHEL

1. Power On Self Test (POST)
The computer power-on self-test tests the computer to make sure it meets the necessary system requirements and that all hardware is working properly before starting the remainder of the boot process. If the computer passes the POST, the computer may have a single beep as the computer starts and the computer will continue to start normally, Passing the control to the BIOS.
 

2. BIOS

  • BIOS stands for Basic Input/Output System
  • Performs some system integrity checks
  • Searches, loads, and executes the boot loader program.
  • It looks for boot loader in floppy, cd-rom, or hard drive. You can press a key (typically F12 of F2, but it depends on your system) during the BIOS startup to change the boot sequence.
  • Once the boot loader program is detected and loaded into the memory, BIOS gives the control to it.
  • So, in simple terms BIOS loads and executes the MBR boot loader.

 

Boot Device
Boot device is the device which contains the bootable code. It can be a hard disk. CD ROM, USB device or a floppy disk. After the execution of BIOS it passes the control to boot device and then to boot loader.

Boot Loader
A computer's central processor can only execute program code found in Read Only Memory (ROM), Random Access Memory (RAM) or an operator's console. Modern operating systems and application program code and data are stored on nonvolatile data storage devices, such as hard disk drives, CD, DVD, flash memory cards, USB flash drive, and floppy disk. When a computer is first powered on, it does not have an operating system in ROM or RAM. The computer must initially execute a small program stored in ROM along with the minimum of data needed to access the nonvolatile devices from which the operating system programs and data are loaded into RAM.
 

3. MBR

  • MBR stands for Master Boot Record.
  • It is located in the 1st sector of the bootable disk. Typically /dev/hda, or /dev/sda
  • MBR is less than 512 bytes in size. This has three components 1) primary boot loader info in 1st 446 bytes 2) partition table info in next 64 bytes 3) mbr validation check in last 2 bytes.
  • It contains information about GRUB (or LILO in old systems).
  • So, in simple terms MBR loads and executes the GRUB boot loader.
 

4. GRUB

  • GRUB stands for Grand Unified Bootloader.
  • If you have multiple kernel images installed on your system, you can choose which one to be executed.
  • 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.
  • GRUB has the knowledge of the filesystem (the older Linux loader LILO didn’t understand filesystem).
  • Grub configuration file is /boot/grub/grub.conf (/etc/grub.conf is a link to this). The following is sample grub.conf of CentOS.
  • #boot=/dev/sda
    default=0
    timeout=5
    splashimage=(hd0,0)/boot/grub/splash.xpm.gz
    hiddenmenu
    title CentOS (2.6.18-194.el5PAE)
              root (hd0,0)
              kernel /boot/vmlinuz-2.6.18-194.el5PAE ro root=LABEL=/
              initrd /boot/initrd-2.6.18-194.el5PAE.img
     
  • As you notice from the above info, it contains kernel and initrd image.
  • So, in simple terms GRUB just loads and executes Kernel and initrd images.
default=0
#default=0 means it'll be booting the default operating system. Usually linux. If you have two operating systems and one is windows. If u want windows to be the default operating system, then u've to give default=1. Its value starts from zero. etc 0,1,2,3 etc.

timeout=5
#timeout defines the time which stage1.5 will give u to switch between operating system options. It is in seconds. After waiting 5 seconds, It'll boot the default OS.

splashimage=(hd0,0)/grub/splash.xpm.gz
#This is for graphical GUI interface for GRUB stage1.5. If you dont want it, you can comment this line.
#The figure of Grub stage1.5 is shown below.

After commenting the splashimage line, grub stage1.5 will look as shown below.


hiddenmenu
#if u give hidden menu option it will hide the other operating systems menu and selection options. Still you can get the menu pressing any key.
#If you dont give this option it will show the list of operating systems and you'll have to select it.


The above picture is grub.conf without hiddenmenu in it. See the operating systems are listed.


The above picture is grub.conf with hiddenmenu in it. See the operating systems are not listed.

title Red Hat Enterprise Linux Server (2.6.18-164.el5)
#this is the title of underlying operating system. You can edit this if u want.
root (hd0,0)
#location of /boot partition.
kernel /vmlinuz-2.6.18-164.el5 ro root=LABEL=/ rhgb quiet
#First it will load the kernel vmlinuz-version in ro=read only format with the label of disk "/"
#rhgb is redhat graphical boot. It gives us a graphical GUI and updates the flow of booting.
#quiet option hides the boot messages before rhgb starts.

initrd /initrd-2.6.18-164.el5.img
#Loading initrd image which gives a basic filesystem for kernel to execute basic commands and load drivers.
GRUB 2nd stage will load the kernel into RAM and pass the control to kernel.
 

5. Kernel

  • Mounts the root file system as specified in the “root=” in grub.conf
  • Kernel executes the /sbin/init program
  • Since init was the 1st program to be executed by Linux Kernel, it has the process id (PID) of 1. Do a ‘ps -ef | grep init’ and check the pid.
  • initrd stands for Initial RAM Disk.
  • initrd is used by kernel as temporary root file system until kernel is booted and the real root file system is mounted. It also contains necessary drivers compiled inside, which helps it to access the hard drive partitions, and other hardware.
Init process (SysV init style only)

"Init is the father of all processes. Its primary role is to create processes from a script stored in the file "/etc/inittab". This file usually has entries which cause init to spawn gettys on each line that users can log in. It also controls autonomous processes required by any particular system. A run level is a software configuration of the system which allows only a selected group of processes to exist. The processes spawned by init for each of these run levels are defined in the /etc/inittab file.

1. It looks at the sysinit script, which "sets the environment path, starts swap, checks the file systems, and takes care of everything the system needs to have done at system initialization." This includes the system and hardware clock, special serial port processes, and the like.
2. Init then looks at the specific runlevel, as specified in that runlevels configuration.
3. Init then sets the source function library for the system. This spells out how to start or kill a program and how to determine the PID of a program.
4. It then searches and starts each applicable process, and it creates a login session for the user.

After it has spawned all of the processes specified, init goes inactive, and waits for one of three events to happen:

1. processes it started to end or die,
2. a power failure signal
3. a request via /sbin/telinit to further change the runlevel.

"/etc/inittab" file
The following is an example for inittab file.

[root@server ~]# cat /etc/inittab
#
# inittab This file describes how the INIT process should set up
# the system in a certain run-level.
# Default runlevel. The runlevels used by RHS are:
# 0 - halt (Do NOT set initdefault to this)
# 1 - Single user mode
# 2 - Multiuser, without NFS (The same as 3, if you do not have networking)
# 3 - Full multiuser mode

# 4 - unused
# 5 - X11
# 6 - reboot (Do NOT set initdefault to this)
id:3:initdefault:
#The above line sets the default runlevel. Here its 3. Thats Full multiuser without Graphics.

# System initialization.
si::sysinit:/etc/rc.d/rc.sysinit
#This line will execute system initialization scripts in all runlevels.

l0:0:wait:/etc/rc.d/rc 0
l1:1:wait:/etc/rc.d/rc 1
l2:2:wait:/etc/rc.d/rc 2
l3:3:wait:/etc/rc.d/rc 3
l4:4:wait:/etc/rc.d/rc 4
l5:5:wait:/etc/rc.d/rc 5
l6:6:wait:/etc/rc.d/rc 6
#The above lines are used for enabling runlevels. If we comment any line, corresponding runlevel will not be active.

# Trap CTRL-ALT-DELETE
ca::ctrlaltdel:/sbin/shutdown -t3 -r now
#The above line enables restarting of system by pressing ctrl+alt+del

# When our UPS tells us power has failed, assume we have a few minutes
# of power left. Schedule a shutdown for 2 minutes from now.
# This does, of course, assume you have powerd installed and your
# UPS connected and working correctly.
pf::powerfail:/sbin/shutdown -f -h +2 "Power Failure; System Shutting Down"

# If power was restored before the shutdown kicked in, cancel it.
pr:12345:powerokwait:/sbin/shutdown -c "Power Restored; Shutdown Cancelled"

# Run gettys in standard runlevels
1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty tty2
3:2345:respawn:/sbin/mingetty tty3
4:2345:respawn:/sbin/mingetty tty4
5:2345:respawn:/sbin/mingetty tty5
6:2345:respawn:/sbin/mingetty tty6
#Above lines are for terminal consoles. The action here is respawn. That is if the process is stopped, It will be started again without any delay.

# Run xdm in runlevel 5
x:5:respawn:/etc/X11/prefdm -nodaemon
#The above line makes GUI available.

There is also a "emergencey" mode. It wont run any init scripts. It'll run only one script that is sulogin. see the image below.



Giving option "emergency" by editing the kernel line in the begining of the system boot.


It'll ask for the password of root user by executing the script "sulogin". It wont run any of other init scripts.

Inittab file passes the control to rc.sysinit script.

"/etc/rc.d/rc.sysinit" file:

The red color "Red Hat" is the starting point of the rc.sysinit script. We can make the booting interactive by pressing "i" button at this point. Normaly we use this function when system is hung by a service or one service is taking a long time to start.



Now here onwards almost everythin is handled by rc.sysinit based on inittab configuration.
All the startup scripts recides in /etc/init.d.

"/etc/rc.d/rcX.d" scripts:

According to the runlevel(X), it'll go to rcX.d and execute all the scripts there. Here in our example default runlevel is 3. So It'll execute the scripts in "/etc/rc.d/rc3.d"

[root@server rc3.d]# ls
K01dnsmasq K24irda K87multipathd S00microcode_ctl S19rpcgssd S85gpm
K01setroubleshoot K30sendmail K88wpa_supplicant S04readahead_early S22messagebus S85httpd K01smartd K35vncserver K89dund S05kudzu S25netfs S90crond K02avahi-dnsconfd K35winbind K89netplugd S06cpuspeed S25pcscd S90xfs
***Output truncated***

You can see a lot of scripts starting with S and K. The scripts starting with K is killing scripts. It'll kill the processes running which are not set to execute .in runlevel X (here 3). The scripts starting with S is starting scripts. It'll start all the processes which are set to execute in runlevel X.

In the above example smartd service is to be killed and Kudzu service is to be started. We can check it by listing chkconfig entries.
[root@server rc3.d]# chkconfig --list smartd
smartd 0:off 1:off 2:on 3:off 4:on 5:on 6:off
[root@server rc3.d]# chkconfig --list kudzu
kudzu 0:off 1:off 2:off 3:on 4:on 5:on 6:off

All the scripts here links to /etc/init.d. see the long listing below.
[root@server rc3.d]# ll K01smartd
lrwxrwxrwx 1 root root 16 May 28 13:31 K01smartd -> ../init.d/smartd
[root@server rc3.d]# ll S05kudzu
lrwxrwxrwx 1 root root 15 May 28 13:31 S05kudzu -> ../init.d/kudzu

"/etc/rc.local" file
In this file we can specify customized scripts which will run at the startup after the scripts in rcX.d(init scripts).

[root@server ~]# cat /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
[root@server ~]#

"/etc/issue" file:
Accordoing to man page "/etc/issue" is a issue - pre-login message and identification file. The file "/etc/issue" is a text file which contains a message or system identification to be printed before the login prompt. Normaly it shows the Redhat release and versions. Kernel version and the architecture of the machine.

[root@server ~]# cat /etc/issue
Red Hat Enterprise Linux Server release 5.4 (Tikanga)
Kernel \r on an \m

"/etc/motd" file:
motd file is Message Of The Day. The contents of /etc/motd are displayed after a successful login but just before it executes the login shell.

No comments:

Post a Comment