Friday, November 23, 2007

PC Boot Init Process in Linux

1. BIOS: The Basic Input/Output System is the lowest level interface between the computer and peripherals. The BIOS performs integrity checks on memory and seeks instructions on the Master Boor Record (MBR) on the floppy drive or hard drive.
2. The MBR points to the boot loader (GRUB or LILO: Linux boot loader).
3. Boot loader (GRUB or LILO) will then ask for the OS label which will identify which kernel to run and where it is located (hard drive and partition specified). The installation process requires to creation/identification of partitions and where to install the OS. GRUB/LILO is also configured during this process. The boot loader then loads the Linux operating system.
4. The first thing the kernel does is to execute init program. Init is the root/parent of all processes executing on Linux.
5. The first processes that init starts is a script /etc/rc.d/rc.sysinit
6. Based on the appropriate run-level, scripts are executed to start various processes to run the system and make it functional.

The Linux Init Processes:

The init process is the last step in the boot procedure and identified by process id "1". Init is responsible for starting system processes as defined in the /etc/inittab file. Init typically will start multiple instances of "getty" which waits for console logins which spawn one's user shell process. Upon shutdown, init controls the sequence and processes for shutdown. The init process is never shut down. It is a user process and not a kernel system process although it does run as root.

System Processes:
Process ID 0 is the Scheduler
Process ID 1 is the init process
Process ID 2 is the kflushd
Process ID 3 is the kupdate
Process ID 4 is the kpiod
Process ID 5 is the kswapd
Process ID 6 is the mdrecoveryd



Run /sbin/initlog
Run devfs to generate/manage system devices
Run network scripts: /etc/sysconfig/network
Start graphical boot (If so configured): rhgb
Start console terminals, load keymap, system fonts and print console greeting: mingetty, setsysfonts The various virtual console sessions can be viewed with the key-stroke: ctrl-alt-F1 through F6. F7 is reserved for the GUI screen invoked in run level 5.
Mount /proc and start device controllers.
Done with boot configuration for root drive. (initrd) Unmount root drive.
Re-mount root file system as read/write
Direct kernel to load kernel parameters and modules: sysctl, depmod, modprobe
Set up clock: /etc/sysconfig/clock
Perform disk operations based on fsck configuration
Check/mount/check/enable quotas non-root file systems: fsck, mount, quotacheck, quotaon
Initialize logical volume management: vgscan, /etc/lvmtab
Activate syslog, write to log files: dmesg
Configure sound: sndconfig
Activate PAM
Activate swapping: swapon


Run Levels:
LINUX has six states of operation of which "0" is the shutdown state and "3" and above are fully operational with all essential processes running for user interaction. Upon system boot the LINUX system /sbin/init program starts other processes by performing the following:

Init will bring up the machine by starting processes as defined in the /etc/inittab file.
The computer will be booted to the runlevel as defined by the initdefault directive in the /etc/inittab file.

Runlevel "5" will boot the system into GUI mode using XDM and X-Windows. Booting to runlevel "3" (often called console mode) is often used by servers which do not need a graphical user interface. If booted to init level "3" one can promote the run level with the command
[root prompt]#

· The inittab file will allow you to capture key sequences (ctrl-alt-del), start dial in internet connections etc.

One of these process started by init is /sbin/rc. This script runs a series of scripts in the directories /etc/rc.d/rc0.d/, /etc/rc.d/rc1.d/, /etc/rc.d/rc2.d/, etc

Scripts in these directories are executed for each boot state of operation until it becomes fully operational. Scripts beginning with S denote startup scripts while scripts beginning with K denote shutdown (kill) scripts. Numbers follow these letters to denote the order of execution. (lowest to highest)

One may switch init levels by issuing the init command with the appropriate runlevel. Use the command "init #" where # is one of s,S,0,1,3,5,6. The command telinit does the same.
The scripts for a given run level are run during boot and shutdown. The scripts are found in the directory /etc/rc.d/rc#.d/ where the symbol # represents the run level. i.e. the run level "3" will run all the scripts in the directory /etc/rc.d/rc3.d/ which start with the letter "S" during system boot. This starts the background processes required by the system. During shutdown all scripts in the directory which begin with the letter "K" will be executed. This system provides an orderly way to bring the system to different states for production and maintenance modes.

List state and run level of all services which can be started by init: chkconfig --list or service --status-all grep running

Run Level Commands


Shutdown:
init 0

shutdown -h now

-a: Use file /etc/shutdown.allow
-c: Cancel scheduled shutdown.

halt -p

-p: Turn power off after shutdown.
poweroff

Reboot: init 6
shutdown -r now
reboot

Enter single user mode: init 1