Sunday, December 23, 2012

                                  Linux Booting Procedure 


Here I am giving you a detailed view about linux booting procedure. As we know linux booting involves multiple steps and stages . Here it the brief  diagram for better understanding. 






















As per the diagram i will explain each and every process as detail.

1. The initial process is called POST ( Power On Self Test) where the bios will start addressing from a particular location (0xFFFF0). The function of the bios in this stage is to perform hardware check and device initialization . Basically BIOS contain two processes one is for POST and other one will initialize the necessary hardware. To boot the operating system BIOS will search the bootable device where the preference  is defined in the CMOS chip of the system. Bootable devices may be a partition of harddisk, CD-ROM, USB flash drive, a network device etc.

2. Commonly linux is booting from HDD, where the Master Boot Record (MBR) contains the primary boot loader. MBR is the 512 byte size where the first 446 byte contains the boot loader (executable code and error text) , 64 byte contains the partition table details of 4 partitions and next 2 byte is the magic number (0xAA55). The magic number serves as a validation code for MBR. The primary function of the primary boot loader is to identify the secondary boot loader (in stage 2) by looking in the  MBR . It will check the active partition and remaining partitions also will be validated by cross checking with MBR and add the details to RAM for execution.

3. The second boot loader is called as kernel loader and the task of this stage is to load the linux kernel with RAM disk. The boot loader for this stage is called as GRUB ( previously LILO also available). GRUB stands for Grant Unified Boot loader and the major difference between GRUB and LILO is GRUB can recognize the file systems. GRUB can load the linux kernel from ext2 or ext3 file systems and this will be accomplished by making the normal 2 stage boot process in to 3 stage boot process.  Stage 1 (MBR) will load the stage 1.g boot loader which will understand the file system where the linux kernel belongs. For example reiserfs_stage1_5 (for loading Reiser journeling file systems) and e2fs_stage1_5 ( for loading ext2 and ext3 file systems) . In stage 2 GRUB will list the avilable kernel modules and as per the default configuration mentioned in /etc/grub.conf (soft links to /boot/grub/grub.conf) it will load the kernel image and default initrd image to the memory .

                                                                                                          GRUB boot loader
















                                                                                                 


                                                           /etc/grub.conf (/boot/grub/grub.conf)











1. default=0
As default first kernel entry will be get loaded in the system and if you have multiple kernel entries you have to change the default value according to that.
2.timeout=5
While system will boot it will wait 5mins for user selecting the kernel
3.SplashImage
It will load the image while you booting the OS. The default location of the image is mentioned in the line. ( you can change the back ground image to your own by following method
#convert image.png -resize 640x480 -colors 14 splash.xpm
#gzip splash.xpm)
4.hiddenmenu
This will hide the boot menu as default and you can see the boot menu once you press e in grub. ( if  you want to see the boot menu as default you can comment this option)
5.title
This will set the title for kernel lebel as "CentOS (2.6.18-128.el5)
6.root (hd0,0)
This indicates hard drive and partition details
7.kernel
This settings will load the kernel image and mount the root file system in /dev/VolGroup00/LogVol00 and kernel will execute /sbin/init with a process id of 1
8.initrd
Initrd has executable driver modules and it will load the RAM disk.

Kernel 


Kernel is the executable program with compressed stage ( size of 512 KB approximately ) . Kernel will be loaded in RAM disk for execution purpose . This execution will happen trough 3 stages.

a. Assembly routine - Initial stage system will invoke ./arch/i386/boot/head.s and function of this stage is to check basic hardware setup for kernel and it will invoke startup_32 function.

b.Startup_32 - This function will be called from ./arch/i386/boot/compressed/head.c and this function will set up basic environment (stack etc) for kernel booting.

c. decompress_kernel- This function will decompress the kernel and load to the memory and same will be loaded from ./arch/i386/boot/compressed/misc.c

Along with these three functions one more startup_32 function also will be called and this function will do the memory allocation by initializing page tables and enabling memory paging. Once the initrd function is enabled it will act as a RAM disk and allows the kernel to mount the /root file system temporarily also this will allows to detect the file real file systems and network to mount the original file systems for booting process.

Init process 

After the kernel boots init process (/sbin/init)will be started according to the configuration in /etc/inittab

Sample inittab file 


Also you can see this script will call the system initialization script called /etc/rc.d/rc.sysinit which have following functions 

  • run /sbin/initlog - To log the init process 
  • run devfs to manage and run devices 
  • run network scripts /etc/sysconfig/network
  • start graphical boot if configured (RHGB)
  • start console terminals,load keymap,systemfonts - /sbin/mingetty & /sbin/setsysfont
  • mount /proc for starting device controllers 
  • unmount initrd which was mounted in /root for inital booting procedure 
  • mount the /root as RW mode
  • Direct the kernel to load kernel parameters and modules - sysctl,depmod,modprobe
  • set up the clock - /etc/sysconfig/clock
  • perform disk operations as per /sbin/fsck configuration 
  • check mount quatos in non root file systems - fsck,mount,quotacheck,quotaon
  • initialize logical volume - vgscan,/etc/lvmtab
  • activate syslog , write to log files - dmesg 
  • configure sound - sndconfig
  • activate PAM module 
  • activate swapping - swapon 
system will boot as per the run level mentioned in the /etc/initab :initdefault 

Login Process 

The first process which includes in login process in /sbin/mingetty as per the configuration mentioned in  /etc/inittab file (1:2345:respawn:/sbin/mingetty tty1).These lines cause init to spawn mingetty process on run level 2 to 5 . To do this it will use the fork function to make a copy of itself and use the exec function to run the mingetty program .  Once the mingetty is loaded it will execute the login prompt and waits till user enter the user name.If the user enter the user name it will prompt the password and password will be cross checked along with /etc/passwd entry . If the credentials are correct respective shell is logged for the user as per the configuration in /etc/passwd .

I am mentioning the files which involve in login process and its respective roles 

/etc/nologin- This file prevents from user's who are not root from login to the system
/etc/usertty - User to impose special access restriction to user's
/etc/securetty- controls the terminals that the root user can login
.hushlogin - When this file exists in the user home directory , it will prevents the user to check the mail, view the last login and motd message.
/var/log/lastlog- contains the information about the last login details of the user.
/etcpasswd - contains the information about the user UID, password in encripted format and its respective shell details.













2 comments: