Monday, May 4, 2009

Linux Boot Process

There process of booting a linux system consists of various stages. The boot process is same in almost all the linux variants. I have tried my level best to explain it in a simple way, so that a beginner can understand the process easily.

Here we go...

When a system is switched on the processor looks at the end of system memory for the Basic Input/Output System or BIOS program and runs it. The BIOS is made up of two parts: the POST code and runtime services. POST(Power On Self Test) checks the system hardware and performs local device initialization. Then BIOS runtime services check for a valid boot device. If no configuration changes are made in BIOS it will first check floppy drive then harddisk drive and then on removable disk drives if any. If no boot device is found it will sends an interrupt and boot process will be terminated.

The boot device contains primary bootloader in the first 512-byte sector of the disk. This segment is called the Boot Sector. Sometimes it is also called Master Boot Record(MBR). Once a valid boot device is found, primary bootloader is loaded into RAM and BIOS passes control to it.

Stage 1

The primary boot loader that resides in the MBR is a 512-byte image containing both program code and a small partition table. The first 446 bytes are the primary boot loader, which contains both executable code and error message text. The next sixty-four bytes are the partition table, which contains a record for each of four partitions (sixteen bytes each). The MBR ends with two bytes that are defined as the magic number (0xAA55). The magic number serves as a validation check of the MBR.

The job of the primary boot loader is to find and load the secondary boot loader (stage 2) by looking through the partition table for an active partition. When it finds an active partition, it scans the remaining partitions in the table to ensure that they're all inactive. When this is verified, the active partition's boot record (secondary boot loader) is read from the device into RAM and executed.

Stage 2

The secondary boot loader is also called the kernel loader. The task at this stage is to load the Linux kernel and optional initial RAM disk.

The second stage boot loader has two functions:

1. Display the list of available kernels upon request, defined in bootloader configuration file.
(ie, for LILO /etc/lilo.conf and for Grub /etc/grub.conf)

2. Consult with filesystem to load the default kernel image and initrd image into memory.

With the images ready, the stage 2 boot loader passes the control to kernel image.

Stage 3

From here the Kernel stage begins.

The kernel image is a compressed image typically a zImage or bzImage.

The bzImage is decompressed by the C function decompress_kernel (located in ./arch/i386/boot/compressed/misc.c). When the kernel is decompressed into memory, it immediately initializes and configures memory paging and configures the various hardware attached to the system, including all processors, I/O subsystems, and storage devices.

Then it loads the initial RAM disk(initrd) and loads all the necessary drivers. 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. At this point, there are no user applications that allow meaningful input to the system, not much can be done with the system. To set up the user environment, the kernel executes the /sbin/init program.

Init

After the kernel is booted and initialized, the kernel starts the first user-space application. The /sbin/init program (also called init) coordinates the rest of the boot process and configures the environment for the user.


"I will not say I failed 1000 times, I will say that I discovered there are 1000 ways that can cause failure" -- Thomas Edison

ref:
http://www.ibm.com/developerworks/linux/library/l-linuxboot/
http://www.linuxtopia.org/online_books/centos_linux_guides/centos_linux_reference_guide/s1-boot-init-shutdown-process.html

No comments: