An Insight into the Linux Booting Process


Published on

This presentation gives you an insight how a linux system boots. Booting is more then just turning ON your system and waiting it to start.

  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

An Insight into the Linux Booting Process

  1. 1. INSIGHT INTO“LINUX BOOTING PROCESS” By, Hardeep Singh Bhurji 1
  2. 2. BOOTINGIn the early days, bootstrapping a computer meantfeeding a paper tape containing a boot program ormanually loading a boot program using the front paneladdress/data/control switches. Todays computers areequipped with facilities to simplify the boot process, butthat doesnt necessarily make it simple. 2
  4. 4. BIOSWhen a system is first booted, or is reset, the processor executes code at a well-known location. In a personal computer (PC), this location is in the basic input/outputsystem (BIOS), which is stored in flash memory on the motherboard.The central processing unit (CPU) in an embedded system invokes the reset vector tostart a program at a known address in flash/ROM.The computer begins the boot sequence by resetting the CPU. This sets severalregisters to fixed values and then executes the code found at a specific address. Thisaddress is mapped in hardware to a ROM chip that contains the BIOS.Your BIOS will usually provide you with a way to tell it about the different hardwarethat is attached. These settings are stored in the CMOSThe BIOS provides a number of routines and services that are mostly useless to theLinux kernel. A large part of the BIOS exists to support legacy systems like DOS.There is an obvious advantage here to eliminating this excess code and theLinuxBIOS project shows just how much advantage there is 4
  5. 5. There are basically four things the BIOS does at this point. The Power On Self Test, or POST routine runs to find certain hardware and to test that the hardware is working at a basic level. Hardware devices are initialized. PCI devices need to be given IRQs, input/output addresses and a table of these devices is then displayed on-screen. The BIOS must search for an operating system on the various media that is supported. This usually occurs by searching for a boot sector on the devices that are specified in the CMOS. Once a valid boot sector has been found, it is copied into RAM and then executed. 5
  6. 6. BOOT SECTORSStage 1 Bootloader:A hard disk can have a boot sector in the first sector of the drive (which is knownas the Main Boot Record) or in the first sector of a primary partition.The primary boot loader that resides in the MBR is a 512-byte image containingboth program code and a small partition table .The first 446 bytes are the primaryboot loader, which contains both executable code and error message text. The nextsixty-four bytes are the partition table, which contains a record for each of fourpartitions (sixteen bytes each). The MBR ends with two bytes that are defined asthe magic number (0xAA55). The magic number serves as a validation check ofthe MBR. 6
  7. 7. 7
  8. 8. STAGE 2 BOOTLOADERThe secondary, or second-stage, boot loader could be more aptly called the kernel loader.The task at this stage is to load the Linux kernel and optional initial RAM disk.The first- and second-stage boot loaders combined are called Linux Loader (LILO)LILO is too large to fit into a single sector, so it is loaded in stages. Its progress can betracked by the letters it prints on the screen. The first stage loader is what is found in theboot sector. It simply prints out an "L" and then loads the second stage loader. Once thesecond stage loader has been loaded, LILO prints an "I" and then executes the secondstage. The second stage loader prints an "L" and then attempts to read a map file fromdisk. This map file tells LILO what operating systems are available. The map file iscreated when the lilo command is executed at the command prompt or when one installsLinux. If the map file loads properly and is not corrupt, LILO prints out an "O", showingthat it has successfully loaded.Disadvantage : When LILO is told to boot a Linux kernel it reads it directly from thedisk. It knows the kernels location because of information found in the map file. BecauseLILO requires this map file to point to a hard-coded location on the hard drive, as soon asone moves the kernel or the disk geometry changes, LILO needs to be run to generate anew map file. 8
  9. 9. GRUB BOOT LOADERThe GRand Unified Bootloader, otherwise known as GRUB, was written to addressthe need for a standard method of booting an operating system. This standard isknown as the "Multiboot Specification" and GRUB is capable of booting acompatible kernel.Instead of using raw sectors on the disk, as LILO does, GRUB can load a Linuxkernel from an ext2 or ext3 file system.GRUB also boots in stages due to its size. Stage 1 is what is present in the main bootrecord and merely loads the next stage. Stage 1.5 is also quite small, but is able tounderstand filesystems. Depending what filesystem contains the Stage 2 loader, a differentStage 1.5 is used. 9
  10. 10. GRUB BOOT LOADERStage 2 is the meat of the loader and contains many features and options that can beexplored. Because GRUB understands partitions and filesystems, it can load a kernelthat it wasnt told about previously. No map file is necessary and GRUB does not needto be re-run after installing a new kernel.Examples include reiserfs_stage1_5 (to load from a Reiser journaling file system) ore2fs_stage1_5 (to load from an ext2 or ext3 file system). When the stage 1.5 bootloader is loaded and running, the stage 2 boot loader can be loaded.With stage 2 loaded, GRUB can, upon request, display a list of available kernels(defined in /etc/grub.conf, with soft links from /etc/grub/menu.lst and /etc/grub.conf).You can select a kernel and even amend it with additional kernel parameters.Optionally, you can use a command-line shell for greater manual control over theboot process. 10
  11. 11. KERNELMany Linux distributions are shipping default kernels that support as muchhardware as possible. In the past this has meant that the kernel image would behuge, containing support for as many bootable devices as possible. They wereunable to make the kernel completely modular because sometimes thesemodules would be needed to mount the filesystem that contained the moduleThe solution was to have the kernel load an initial RAM disk image, alsoknown as an initrd image, that contained many of the modules that would beneeded to boot. Most of these modules will be SCSI controller drivers andsupport for various filesystems.At this point, the BIOS has selected the boot device and its boot sector hasbeen loaded. The boot manager nows loads the kernel image and possibly aninitial RAM disk image. Once loaded into RAM, the kernel is executed and thesetup code runs. 11
  12. 12. KERNELThe kernel must initialize any devices the system has. Even devices that have beeninitialized by the BIOS must be reinitialized. This provides portability and robustness byensuring that each system has been initialized in a similar fashion, independent of theBIOS.The next step that is performed by the setup code is switching the CPU from Real Modeto Protected Mode. Once in Protected Mode, privilege levels are assigned to runningprocesses. This allows the operating system to be protected from normal user programs.The setup code now loads the compressed kernel and calls the decompress_kernel()function. It is at this point that you will see the "Uncompressing Linux..." messageprinted on the screen.The kernel will now begin printing a large number of messages on the screen as itinitializes the scheduler, irqs, console, hardware, etc. This is done in the start_kernel()function and nearly every kernel component is initialized by this function.The kernel_thread() function is called next to start init. The kernel goes into an idleloop and becomes an idle thread with process ID 0. 12
  13. 13. KERNELWhen the bzImage (for an i386 image) is invoked, you begin at./arch/i386/boot/head.S in the start assembly routine . This routine does somebasic hardware setup and invokes the startup_32 routine in./arch/i386/boot/compressed/head.S. This routine sets up a basic environment(stack, etc.) and clears the Block Started by Symbol (BSS). The kernel is thendecompressed through a call to a C function called decompress_kernel (locatedin ./arch/i386/boot/compressed/misc.c). When the kernel is decompressed intomemory, it is called. This is yet another startup_32 function, but this function isin ./arch/i386/kernel/head.S.In the new startup_32 function (also called the swapper or process 0), the pagetables are initialized and memory paging is enabled. The type of CPU is detectedalong with any optional floating-point unit (FPU) and stored away for later use.The start_kernel function is then invoked (init/main.c). 13
  14. 14. 14
  15. 15. During the boot of the kernel, the initial-RAM disk (initrd) that was loadedinto 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 thekernel to fully boot without having to mount any physical disks.Since the necessary modules needed to interface with peripherals can be partof the initrd, the kernel can be very small, but still support a large number ofpossible 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 ismounted. 15
  16. 16. INITThe first program that is run under the kernel is init. This program is alwaysprocess 1. The Linux kernel can be told which program to use as init bypassing the "init=" boot parameter. If this parameter is not specified, thenthe kernel will try to execute /sbin/init, /etc/init, /bin/init or /bin/sh inthat order. If none of these exist, then the kernel will panic.There are alternatives to using the init included with your system. You canactually place any executable in place of init. If one was building anembedded system, they could replace init with a program written in C thatwould run faster and could be streamlined for the system. The script basedstartup used by most systems is much easier to use, though, because of howeasy it is to make changes. 16
  17. 17. INITMost systems use System V init. This system starts by running the init program. Theinit program reads its configuration from the inittab file that is located in the etcdirectory. This file is where runlevels come into existence. By editing this file, onecould make their system use only three runlevels or could add some if they wished.The main settings that are necessary are the specifying of the default runlevel. Thereis also a line specifying a script to run before all the others, like setting up localnetand mounting filesystems.Many of the entries are also specified with respawn. This parameter tells init tocontinually execute this program whenever the previous one exits. When one logs out,init respawns the getty process to allow another login. 17
  18. 18. RUN LEVEL SCRIPTSLinux generally has eight runlevels. Runlevel 0,1,6 and S are reserved forspecific functions.No scripts are run when entering runlevel S. It is for this reason that runlevel Sis not meant to be entered directly with an already running system.At this point init is running and a default runlevel has been specified. The initialsysinit script has been run which has done things like setup networking, set thetime, check filesystems and then mount them. A swap file will be mounted atthis point as well. init now calls the script for the default runlevel.This script and pretty much all the other boot scripts, are located in /etc/init.d.Each runlevel is given a directory to hold scripts for that runlevel and thesedirectories are labeled rc0.d, rc1.d, rc2.d up to rc6.d. But having a copy ofevery script in each runlevel would be an administrative nightmare formaintaining, so these directories contain symbolic links that point to the realscripts. The real scripts are located in the /etc/init.d directory. 18
  19. 19. FINISHING UPOnce init finishes with the startup scripts, it runs a getty process on eachterminal specified in inittab. Getty is what you see when you are logging in.Once you enter your username, the getty process runs login, which in turnasks for your password. 19
  20. 20. THANKS 20