Tuesday, September 15, 2015

Booting Process

 * Booting Process of  a Linux System has been divided basically into 3 parts.

A. BIOS 
B. Bootloader
C. Kernel

A. BIOS (Basic Input Output System)

- The first program run when we power on the system.
- This program run from the ROM.
- Bios is a OS independent program. that is loaded into the ROM.

•This is the first thing which loads once you power on your machine.

•When you press the power button of the machine, CPU looks out into ROM for further instruction.

•The ROM contains JUMP function in the form of instrucion which tells the CPU to bring up the BIOS

•BIOS determines all the list of bootable devices available in the system.

•Prompts to select bootable device which can be Hard Disk, CD/DVD-ROM, Floppy Drive, USB Flash Memory Stick etc (optional)

•Operating System tries to boot from Hard Disk where the MBR contains primary boot loader.

BIOS performs number of tasks.

like - POST - power -on self-test

- POST check the storage device, keyboard, mouse etc. connect to the system and OS loaded device, its also check the display card connected to the system, checks just basic things that helps a system to boot.

- POST does not check the USB connected to your system or Sound card connected or not. Its just check the basic things.

- Boot an OS from : a storage device, Proceed through the list until it finds  a MBR, (Note: BIOS Search in order all storage devices [Removal devices, Hard drive, CD-ROM Drive, Network bootable devices] until it finds the MBR file. MBR - Master Boot Record. When it finds the MBR, system stop searching the storage device. Because in MBR has the all information is given about:

- The storage device loaded with OS,

- The space given to the drive,

- OS information

- MBR - does not have lot of space it is just the first sector of the drive.

- MBR is less than 512 bytes in size. This has three components 1. primary boot loader info in 1st 446 bytes  2. Partition table info in next 64 bytes 3. MBR validation check in last 2 bytes.

- It contains information about GRUB or LILO, So, in simple terms MBR loads and executes the GRUB boot loader.

- But when BIOS did not find any MBR file then system stop working and gives eroor  massage that system did not fined the OS.

- Once the boot loader program(GRUB/LILO) is detected and loaded into the memory, BIOS gives the control to it.

- So, in simple terms BIOS loads and executes the MBR boot loader.

B. Bootloader
*************
After the MBR, bootloader comes in action.

- bootloader can be reside in MBR or in any device because MBR does not have enough space.

MBR runs the bootloader

- bootloader loads the OS
- Bootloader  boot the OS that can be set automatic or prompt for the  option for boot from deferment   devices and OS installed in the system.

 The very common bootloader now days is :

- LILO (Linux Loader)

- GRUB (Grand Unified Bootloader)

Bootloader may create an "initrd image"

- That is Initial ramdisk

- A RAM-based  file  system.

- initrd file contain the basic information of drivers, software, & files that helps to run or load with    the OS while booting.

- Boot loader loads the kernel

- from /boot/ directory that is /boot/vmlinuz.

- All kernel files are located in to the /boot/ directory.

Ex.
[root@server2 ~]# cd /boot/

[root@server2 boot]# ls

config-2.6.18-164.el5         symvers-2.6.18-164.el5.gz
config-2.6.18-400.1.1.el5      symvers-2.6.18-400.1.1.el5.gz
grub                                          System.map-2.6.18-164.el5
initrd-2.6.18-164.el5.img       System.map-2.6.18-400.1.1.el5
initrd-2.6.18-400.1.1.el5.img   vmlinuz-2.6.18-164.el5
lost+found                     vmlinuz-2.6.18-400.1.1.el5
message

[root@server2 boot]#

- The kernel may be a compressed file .

- Gets the hardware running

- linuxrc (linux run command)

- linuxrc is a program that is started in the start-up stage of the kernel prior to the actual boot process.
This allows you to boot a small modularized kernel and to load the few drivers that are really needed as modules. linuxrc assists in loading relevant drivers manually. However, the automatic hardware detection performed by YaST is usually quite reliable. The use of linuxrc is not limited to the installation; you can also use it as a boot tool for installed system and even for an independent RAM
disk–based rescue system. Refer to Section

- initrd ramdisk is dismounted.

- Now its use the real disk.

- File system is mounted.

- Kernel runs init.

 C. GRUB

- GRUB stands for Grand Unified Bootloader

- If you have multiple kernel images installed on your system, you can choose which one to be executed.

- GRUB displays a splash screen, waits for few seconds, if you don't enter anything, it loads the default kernel image as specified in the grub configuration file.

- GRUB has the knowledge of the file system (the older Linux loader LILO didn't understand file system)

- Grub configuration file is /boot/grub/grub.conf (/etc/grub.conf is a link to this). The following is sample grub.conf of CentOS.

 This loads the kernel in 3 stages

GRUB stage 1: 

•The primary boot loader takes up less than 512 bytes of disk space in the MBR - too small a space to contain the instructions necessary to load a complex operating system.

•Instead the primary boot loader performs the function of loading either the stage 1.5 or stage 2 boot loader.

GRUB Stage 1.5: 

•Stage 1 can load the stage 2 directly, but it is normally set up to load the stage 1.5.

•This can happen when the /boot partition is situated beyond the 1024 cylinder head of the hard drive.

•GRUB Stage 1.5 is located in the first 30 KB of Hard Disk immediately after MBR and before the -first partition.

•This space is utilized to store file system drivers and modules.
•This enabled stage 1.5 to load stage 2 to load from any known location on the file system i.e. /boot/grub

GRUB Stage 2:

•This is responsible for loading kernel from /boot/grub/grub.conf and any other modules needed

•Loads a GUI interface i.e. splash image located at /grub/splash.xpm.gz with list of available kernels where you can manually select the kernel or else after the default timeout value the selected kernel will boot The original file is /etc/grub.conf of which you can observe a symlink file at /boot/grub/grub.conf

Sample /boot/grub/grub.conf

default=0
 timeout=5
 splashimage=(hd0,0)/grub/splash.xpm.gz
 hiddenmenu
 title Red Hat Enterprise Linux Server (2.6.18-194.26.1.el5)
         root (hd0,0)
         kernel /vmlinuz-2.6.18-194.26.1.el5 ro root=/dev/VolGroup00/root clocksource=acpi_pm divisor=10
         initrd /initrd-2.6.18-194.26.1.el5.img
 title Red Hat Enterprise Linux Server (2.6.18-194.11.4.el5)
         root (hd0,0)
         kernel /vmlinuz-2.6.18-194.11.4.el5 ro root=/dev/VolGroup00/root clocksource=acpi_pm divisor=10
         initrd /initrd-2.6.18-194.11.4.el5.img
 title Red Hat Enterprise Linux Server (2.6.18-194.11.3.el5)
         root (hd0,0)
         kernel /vmlinuz-2.6.18-194.11.3.el5 ro root=/dev/VolGroup00/root clocksource=acpi_pm divisor=10
         initrd /initrd-2.6.18-194.11.3.el5.img

- In the above info, it contains kernel and initrd image.

- So, in simple terms GRUB just loads and executes Kernel and initrd images.

Kernel

This can be considered the heart of operating system responsible for handling all system processes.

 Kernel is loaded in the following stages:

1.Kernel as soon as it is loaded configures hardware and memory allocated to the system.

2.Next it uncompresses the initrd image (compressed using zlib into  zImage or bzImage formats) and mounts it and loads all the necessary drivers.

3.Loading and unloading of kernel modules is done with the help of programs like insmod, and rmmod present in the initrd image.

4.Looks out for hard disk types be it a LVM or RAID.

5.Unmounts initrd image and frees up all the memory occupied by the disk image.

6.Then kernel mounts the root partition as specified in grub.conf as read-only.

7.Next it runs the init process

 Init Process

•Executes the system to boot into the run level as specified in /etc/inittab Sample output defining the default boot runlevel inside /etc/inittab

# Default runlevel. The runlevels used by RHS 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:

 As per above O/P system will boot into runlevel 5

 You can check current runlevel details of your system using below command on the terminal

# who -r
          run-level 3  Jan 28 23:29                   last=S

•Next as per the fstab entry file system's integrity is checked and root partition is re-mounted as read-write (earlier it was mounted as read-only).

Runlevel scripts

A no. of runlevel scripts are defined inside /etc/rc.d/rcx.d

Runlevel  Directory

 0 /etc/rc.d/rc0.d
 1 /etc/rc.d/rc1.d
 2 /etc/rc.d/rc2.d
 3 /etc/rc.d/rc3.d
 4 /etc/rc.d/rc4.d
 5 /etc/rc.d/rc5.d
 6 /etc/rc.d/rc6.d

•Based on the selected runlevel, the init process then executes startup scripts located in subdirectories of the /etc/rc.d directory.

•Scripts used for runlevels 0 to 6 are located in subdirectories /etc/rc.d/rc0.d through /etc/rc.d/rc6.d, respectively.

• What are the s and k scripts in the etc rcx.d directories

•Lastly, init runs whatever it finds in /etc/rc.d/rc.local (regardless of run level). rc.local is rather special in that it is executed every time that you change run levels.

NOTE: rc.local is not used in all the distros as for example Debian.

Next if everything goes fine you should be able to see the Login Screen on your system.

-------------------------------------------------------------------------------------------------------------------------
Key points to remember.

> Exactly where is the Kernel files in Linux ?

> Kernel files means the bootable files for linux

> Manley the bootable files are located in "/boot" directory

[root@server2 ~]# cd /boot/

Q. What is "grub" ?

> GRUB  is a bootloader directory, where all the bootables files are located that supports to load the OS.

> The main file is for the bootloader in grub is "grub.conf",

> grub.conf file mainly  define these bellow thins

1. What will be the hold time while booting.
2. From which device system should boot first.
3. Which OS will boot first in duel boot system.
4. From which run-level system will boot first.

[root@server2 boot]# cd grub/

[root@server2 grub]# ls

device.map     grub.conf         minix_stage1_5     stage2
e2fs_stage1_5  iso9660_stage1_5  reiserfs_stage1_5  ufs2_stage1_5
fat_stage1_5   jfs_stage1_5      splash.xpm.gz      vstafs_stage1_5
ffs_stage1_5   menu.lst          stage1             xfs_stage1_5

> Menu.lst file is the link file of grub.conf file

[root@server2 grub]# ls -al

lrwxrwxrwx 1 root root     11 Jan 10 16:49 menu.lst -> ./grub.conf

[root@server2 grub]# cat grub.conf

# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/sda2
#          initrd /initrd-version.img
#boot=/dev/sda
default=0 [ The first OS will boot, here we can change the number(1,2,3) according to number of installed OS ]

timeout=5 [ Time in second, will wait the system to boot, we can change the no. of seconds according to our help]

splashimage=(hd0,0)/grub/splash.xpm.gz [ The image location of the OS while booting. OR  Specifies the location of the splash screen image to be used when GRUB boots. ]

hiddenmenu [  ]

password --md5 $1$PrBdE$.oozjdVt0GiZZ897mZmBH1 [ To set the  password for single user mode ]

title CentOS (2.6.18-400.1.1.el5) [The first OS installed]
        root (hd0,0)
        kernel /vmlinuz-2.6.18-400.1.1.el5 ro root=LABEL=/ rhgb quiet
        initrd /initrd-2.6.18-400.1.1.el5.img

title CentOS (2.6.18-164.el5) [The Second OS installed or latest Updates of the installed OS]
        root (hd0,0)
        kernel /vmlinuz-2.6.18-164.el5 ro root=LABEL=/ rhgb quiet
        initrd /initrd-2.6.18-164.el5.img

[root@server2 grub]#

----------------------------------------------------------------------------------------------------------------------

IMPORTANT FILES

1. /etc/fstabe - File System table

This file is used by linux Operating Systems to mount partitions on boot. It needs this because it can not guess what the partitions are and what to mount them to.

2. /etc/issue - Pre-login message

This file contain the message for users before login

3. /etc/motd - Message of the Day : Post-login message

This file contain the message for users after login.

4. /etc/rc.local - Run a command when boots up

This is the last file which is executed during boot, Any program or command to be executed during startup should be placed in this file.

5. /etc/passwd -

Stores essential information, which is required during login, user account information, User ID, Group ID, Home directory, shell etc.

6. /etc/shadow -

This file stores actual password in encrypted format for user's account with additional properties related to user password.

7. /etc/group -

This file stores information or defines the user groups. It defines the groups to which users belong.

8./etc/gshadow -

This file contains the shadowed information for group accounts.

9. /etc/skell -

/etc/skell directory is the directory used by useradd to create the default setting in a new user's home directory. /etc/skell to ensure that all new users on our system get the same initial settings.

[root@server3 skel]# ls -al

total 56

drwxr-xr-x  3 root root  4096 May 21 02:51 .
drwxr-xr-x 95 root root 12288 May 21 04:07 ..
-rw-r--r--  1 root root    33 Sep 25  2014 .bash_logout
-rw-r--r--  1 root root   176 Sep 25  2014 .bash_profile
-rw-r--r--  1 root root   124 Sep 25  2014 .bashrc
drwxr-xr-x  4 root root  4096 Oct 29  2013 .mozilla
[root@server3 skel]#

10. /etc/login.defs -

The /etc/login.defs  file defines the site-specific configuration for the shadow password. This file is a readable text file, each line of the file describing one  configuration parameter.The lines consist of a configuration name and  value, separated by whitespace.

11. /etc/default/useradd -

When a user is created, different properties related to user are implemented, and these default user properties are stored in different files. User properties like, where user's home directory is to be created, what should be the user's shell, etc are taken from a file "user/default/useradd".

When useradd command executed, it takes some of user property values form a file called /user/default/useradd from where it gets the following values

[root@server3 ~]# cat /etc/default/useradd

# useradd defaults file
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes

[root@server3 ~]#

No comments:

Post a Comment