Inspirational Quotes and Speeches

Monday, February 28, 2011

Linux Boot process

Chapter 3: Linux Boot Process

Boot process is specific to the machine's architecture . We are going to discuss the boot process details for intel based PC architecture.

The entire boot process can be divided into various different components , primarily:

  • BIOS
  • Boot Loader
  • Bootstrap
  • Kernel
  • Init

3.1 BIOS:

Whenever a machine is powered on , the 1st program to run is BIOS. BIOS is a simple an assembly language program . Primary jobs of BIOS are:

  • POST : Performs the Power On Self Test, or POST , to test the peripherals if they are functioning properly at hardware level.
  • CPU Initialization : BIOS checks the clock speed of CPU and will put the CPU on realmode. At the time of power on , cpu will be in real mode. In real mode cpu works on direct physical addresses only.
  • Memory Probing : BIOS checks for machine's available primary memory(RAM).
  • Basic Devices Initialization : BIOS initializes only those devices which are required to carry out the boot process. For example boot process requires basic I/O devices . So BIOS initializes standard input device and standard output device. Apart from basic I/O it also initializessome controllers like PCI controller, IDE controller. IDE controller are needed because without IDE controllers floppy disks / hard disks cant be read or written.
  • Creating Memory Map - BIOS creates real mode memory map or real mode address space. This memory map is always architecture specific. It expects the kernel image to be loaded directly at those addresses. That is why we build the kernel image ( bzImage ) according to the machine's architecture. The bzImage carries real mode , architecture specific addresses so that it can find those direct addresses and can be loaded there.
  • Loading MBR - The last job of BIOS is to find the boot device and to jump to its sector ‘0’ ( boot sector ). BIOS has got access to only sector '0' of a boot device. Whatever BIOS finds in sector ‘0’ , it loads that in real mode address space.

3.2 Boot Loader:

The first sector of a boot device is called as sector '0' or the Master Boot Record (MBR) . BIOS can read only sector ‘0’ ,it cant read any other sector. The size of the MBR is only 512 bytes. By default, MBR contains MS-DOS bootable files and the disk partition table. For loading the linux , you need to use a special program called bootloader in place of this default. Boot loader is a program that loads the operating system. Bootloader even helps you to boot more than one one operating systems on your machine.

For linux, there are two bootloaders available, GRUB and LILO. GRUB (Grand Unified Bootloader) is the default and commonly used boot loader. GRUB is capable of booting DOS, Windows,Linux and BSD operating systems.

BIOS jumps to the Master Boot Record and loads the bootloader from there. Infact loading of bootloader is done in various steps , but we dont want to go into details of it . Also, you should remember that this bootloader program too is allocated the real mode address space or memory.

After bootloader is loaded into memory , it looks into /boot directory and loads the kernel image. Note here, that it is bootloader that expects all the files and kernel image needed for booting under /boot directory. This is the reason why kernel image and all boot related files are kept under /boot directory.

Again, remember , the addresses allocated in kernel image also are real-mode addresses , the direct physical addresses.

Once bootloader loads kernel image to memory , the control is passed to kernel image .

3.3 Bootstrap:

In the previous chapter , we saw that bootstrap is basically assembly code that is added to bzImage .

When bzImge is loaded , the code of bootstrap starts executing. The tasks of bootstrap are,

  • It forces cpu to go into protected mode. In protected mode we have 4 GB of virtual address space or virtual addressing enabled.
  • It enables the MMU (Memory Management Unit) . MMU has got address translation ability , mapping between virtual addresses and physical addresses. CPU contains MMU instruction set.
  • It does these jobs and then uncompresses this image. You can see “Decompressing Linux….” Messages on console at this point.
  • It calls “start_kernel” function after that. start_kernel is similar to main function. kernel image starts running and initializes all the devices , schedulers, console etc. . You can see lot of text flying on the screen related to this. kernel boots up from here.

3.4 Kernel :

  • Kernel initialize CPU and performs the “Bogo MIPS” calculation. Though BIOS has already intialized CPU but kernel does not depend upon BIOS initialization. It will re-initialize the CPU again. It will create one structure and stores all the CPU details in it. After that it performs “Bogo MIPS” calculation. Using "Bogo MIPS" calculation kernel estimates the cpu speed. At bootup time, yoou can see the message " Bogo MIPS is ....." .
  • Kernel re-initializes parallel ports, serial ports and all the on-board controllers like USB, IDE, SCSI, Serial, parallel, timers, RTC etc. These startup drivers are also called BSP drivers or Board Support Drivers.
  • Interrupts will be enabled. All the data structures required to hold the interrupts including interrupt descriptor table will be setup , i.e. which device is using which interrupt, IRQ details will be filled.

  • File system required to read from this disk, /boot , /root , /etc , /home etc. is initialized. File system must be loaded now so that can now start using disk.
  • After this loader will be started. Loader starts a process that is registered in the PCB with PID ‘0’ . Name of this process is swapper.

  • “swapper” will start a thread called “init” thread. “init” is a kernel thread. “init” kernel thread will look into /bin and look for a program whose name is “init”. This kernel thread is told for where to look for init process via boot parameter in bootloader configuration file. If kernel thread doesn’t find init there , it becomes a reason for kernel panic and the booting stops there. This may also happen if file system is not initialized . “init” thread looks into /bin and starts executing init process. init’s parent is “init” thread. /bin/init is registered as PID 1 and this is the first user space process.

From here starts second part of booting , so let's have a recap of all previous steps.

Fig. 3.1: linux boot process

3.5 Init:

Once init starts , “swapper” goes into the sleep state, into an idle loop and rest of the bootup process is taken up by init process. Init reads ‘/etc/inittab’ configuration file and starts initializing of all the services needed. At this stage of booting , you can see lot of OK messages on console. Here is the inittab file looks like,

# vi /etc/inittab

# inittab is only used by upstart for the default runlevel.
#
# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# System initialization is started by /etc/event.d/rcS
#
# Individual runlevels are started by /etc/event.d/rc[0-6]
#
# Ctrl-Alt-Delete is handled by /etc/event.d/control-alt-delete
#
# Terminal gettys (tty[1-6]) are handled by /etc/event.d/tty[1-6] and
# /etc/event.d/serial
#
# For information on how to write upstart event handlers, or how
# upstart works, see init(8), initctl(8), and events(5).
#
# Default runlevel. The runlevels used 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:5:initdefault:


# System initialization.
si::sysinit:/etc/rc.d/rc.sysinit

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


# 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


Fig. 3.2: /etc/inittab file

Let's see this inittab file line by line . For now leave the line , id:5:initdefault , we will take it later. Next Line in inittab is ,

Sysinit : /etc/rc.d/rc.sysinit

It tells the init process to run a system initialization script at location /etc/rc.d/rc.sysinit. That script initializes all the mandatory services needed for an operating system e,g, File system validation and mounting , setting up the enviornment etc.

The next set of lines in inittab corresponds to executing /etc/rc.d/rc scripts. These are called rc scripts. You can see these rc scripts are passed arguments ( 0, 1, 2, 3, 4, 5, 6 ) . The arguments passed to the rc script is called init run-levels . Now just go back and see the very first line in inittab script ( id:5:initdefault ) . This line sets the default run-level to 5. After initializing the essential services , init does run-level specific intialilzation.

These run-levels allow you to run your machine in any of the different modes available. Different possible modes are,

  • Single user mode
  • Multiuser mode
  • Full multiuser with networking mode
  • GUI mode

Each of this mode is denoted by a run–level ,

  • Run-level 1 is Single user mode
  • Run-level 2 is Multiuser mode
  • Run-level 3 is Full multiuser with networking mode
  • Run-level 4 is GUI mode

You are seeing the default run-level is 5 ( id:5:initdefault ) . So by default init process brings up machine in level 5 , so control will jump to level 5 of piece of code given below.

l5:5:wait:/etc/rc.d/rc 5

It directs init to run script /etc/rc.d/rc with argument 5. If you give 5 as an argument to rc, it will go into rc5.d folder. If you give 3 as an argument to rc, it will go into rc3.d folder. If you give 2 as an argument to rc, it will go into rc2.d folder and so on . Now look into rc/rc5.d directory, you will find some scripts. Some scripts are starting with K and some are starting with S. scripts starting with S are start scripts while scripts starting with K are kill scripts . When system enters into a run-level , all the start scripts are executed. When system exits from a run-level, all kill scripts are executed.

Each start script is for one service , e.g. S11auditd is for initializing audit daemon, S08iptablesis for initializing iptables etc. So, each start script is initializing one service and that service is specific to run-level. You can also start services at boot time by adding startup and kill scripts here.

After intialization of mandatory and run-level specific services , init comes to the last section of inittab , i.e. ' Run gettys in standard runlevels ' . It Initializes terminals and runs mingetty application. Mingetty starts a program which validates the user and you get a login screen. If validation passes , then you will get shell . That's where your booting process is complete !!!