Friday 12 September 2014

Linux Boot-Process

Linux Boot Process
  • Power on
  • Power will go through SMPS to main board.
  • On main Board BIOS chip / ROM will wake up first
  • Then BIOS will loaded in memory. It is also called as boot strapping.
  • Now BIOS will take H/W inventory. Like keyboard, monitor, Display card etc.
  • Bios will go to CMOS for bootable device list and sequence
  • Bios has only target to load Kernel in RAM
  • Bios will now load first sector of bootable device i.e. HDD in RAM. Also known as MBR
- MBR : Master boot Record
    • Cylinder 0, Head 0 and Sector 1.
    • In fdisk -l we have seen size of sector is 512 bytes. So MBR is of 512 bytes
    • MBR is devided in 3 parts : Boot Sector ( BS ), Partition Table ( PT ) and Magic number.
0x228
<=========x=================== 512 bytes ========================>
| |
<-----446 bytes ---->| <--PT -64 bytes->| < Magic no 2 bytes--->
BS | PT | Magic #
| |
<============================ 512 bytes ========================>
M B R
    • Boot sector is of 446 bytes which actual contains boot loader of all OS loaded in Hard Disk. On 0x228 byte of BS there is a tiny set of binaries which will help Bios to redirect to Linux loader
    • Partition Table contains information about partition like primary, extended, size, format etc. It size 64 bytes.
    • Magic number is 2 bytes which is like a signal that all is OK. This is also called as MBR signature
  • Continue to step 8.......... Bios come to MBR where 1st stage boot loader resides which has the target to load 2nd stage boot loader i.e. GRUB in older version of linux LILO. 1st stage boot loader has the information about GRUB.
  • BIOS can understand binary. Grub configuration is also in MBR. BIOS will take CHS number of splash image from GRUB configuration and load in in RAM.
  • BIOS will also read default kernel and time out from GRUB configuration. It will then display GRUB message on default selected kernel till time out seconds.
  • After time out Bios will then read CHS number of kernel i.e. vmluniz-2.6.18..... . Now Bios will pull the kernel in Ram from specified CHS and hand over all the controls to Kernel i.e. vmluniz-2.6.18.....
  • Now Kernel is king of all and he is going to call Process. Kernel makes another call through Bios for initrd image. Now initrd .img will extrract all drivers to load real
What is initrd : initrd maens Initial Ram Disk to mount a temporary file system in Ram
    • To see what is in initrd mount initrd.img to one directory
# cp /boot/initrd-2.4.20-8.img /tmp - Let's play with a safe copy
# cd /tmp
# mv initrd-2.4.20-8.img initrd-2.4.20-8.img.gz --> gunzip need extn
# gunzip initrd-2.4.20-8.img.gz
# mount -t sysfs -o loop initrd-2.4.20-8.img /win
# cd /win
# ls
  • Initrd contains driver of ext3, jdb etc
  • Kernel will now mount “ / “ with the help of ext driver. Now he can access hard disk partition
  • Kernel call its 1st process /sbin/init



Init Tab
# Author:       Miquel van Smoorenburg, 
#               Modified for RHS Linux by Marc Ewing and Donnie Barnes

id:3: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

# Things to run in every runlevel.  This line is only in Red Hat 7.X Used to flush disk buffers.
ud::once:/sbin/update

# Trap CTRL-ALT-DELETE
ca::ctrlaltdel:/sbin/shutdown -t3 -r now

# When our UPS tells us power has failed, schedule a shutdown for 2 minutes from now.
pf::powerfail:/sbin/shutdown -f -h +2 "Power Failure; System Shutting Down"

# If power was restored before the shutdown kicked in, cancel it.
pr:12345:powerokwait:/sbin/shutdown -c "Power Restored; Shutdown Canceled"

# 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

# Run xdm in runlevel 5
x:5:respawn:/etc/X11/prefdm -nodaemon

Init is the 1st process called by Kernel
Init works as per /etc/inittab file which is specified above. In redhat linux it seems like this
First it will check run level In the above example it is 3 i.e. command line full user mode with networking
Then it will call /etc/rc.sysinit in a different sub-shell.
rc.sysinit will
    • Run initlog
    • Sets minimal Path
    • Check for network configuration and sets hostname as per file
    • It calls /etc/init.d/functions this script will call mainly 17 functios
      1. daemon - Start a program
      2. killproc - Stop a program
      3. pidofproc - Find the process ID of a program
      4. status - Gets the status of a process (running, dead, locked)
      5. echo_success - Echo OK to the screen in proper colors.
      6. echo_failure - Echo FAILED to the screen in proper colors.
      7. echo_passed - Echo PASSED to the screen in proper colors.
      8. success - Log to the system logger that something succeeded and echo_success if bootup is verbose
      9. failure - Log that something failed and echo_failure if verbose bootup.
      10. passed - Log that something passed and echo_passed if verbose bootup.
      11. action - Run some action, log output to the system logger.
      12. confirm - Confirm whether we want to run this service.
    • /sbin/loglevel will sets the log level
0 - The system is unusable
1 - Action must be taken immediately
2 - Critical condition
3 - Error condition
4 - Warning condition
5 - Normal but significant condition
6 – Informational
6 - Debug message
    • Configures Kernel Parameter
    • Welcome Message “Welcome To CentOS...”
    • Map the KEYTABLES
    • Raid Setup
    • Mounts filesystem from /etc/fstab
    • Check mouting options fro fsck and dumping options
    • Checks quotas, sets quota parameter
    • Mounts all file system like except NFS and /proc which is already mounted
    • Re configures system files like keynoard, network
    • Start up swap
    • Initialised serial ports
    • Run dmesg and dumps to /var/log/message
  • As specified in runlevel id it is 3 so /etc/rc script will run all the script from /etc/rc3.d in a different sub-shell
    • See the correct run level
    • checks previous run level
    • Checks run level in and see whether rc<run-level>.d exist or not
    • /etc/rc script will kill all services which start with letter 'k' and starts all scripts which starts with 's'
  • /etc/rc.d/rc.local script will run after starting of all services. Even we can also add scripts/commands to run in start up
  • Then As per inittab ctrl-alt-tab trap will get set
  • Now ups consideration will get set
  • 6 min-getties will come out in Ram if runlevel 3 is there and if run level 5 is there then it will run UI scripts from /etc/X11/prefdm
  • A getty is is a program that opens a tty port, prompts for a login name,
and runs the /bin/login command. It is normally invoked by init.
  • The mingetty daemon is used to listen for virtual consoles
(like the 6 that run by default with your keyboard and monitor)
and cannot be used to handle serial lines
  • The first mingetty [awake one] loads device driver /dev/tty1
      • execs /etc/issue
      • mingetty then executes the /bin/login process
and is then put to sleep state by init
      • /bin/login displays login. You login!
      • /bin/login execs /usr/bin/passwd which challenges for passwd from
user which then does authentication and authorization using PAM
      • init then puts the login daemon into sleep state, wakes up
mingetty which now takes over
      • mingetty checks for .hushlogin in $HOME/$USER
      • If $HOME/.hushlogin DOES NOT EXISTS
then it does the foll :
      • execs lastlog* -u $USER using /var/log/lastlog
      • execs /etc/motd
      • checks users mail, if any, in /var/spool/mail/$USER
      • init then wakes /bin/login process
      • mingetty then goes into zombie state and is killed by init
      • login then loads /bin/bash as a monitored child process
  • /bin/bash takes over and login goes into sleep state
  • Execs /etc/profile Sets system-wide ENV variables Global Profile
  • exec /etc/inputrc Sets keyb mappings [See stty -a]
Show Terminal characteristics
  • stty -echo --> Terminal Characteristics
  • eg /etc/inputrc See man bash - /bell-style
============
set bell-style [audible] [none] [visible]
set disable-completion [off] on
  • exec /etc/profile.d/*.sh [13 shell scripts are executed]
1. colorls.sh
2. glib2.sh
3. gnome-ssh-askpass.sh
4. krb5.sh
5. lam.sh
6. lang.sh
7. less.sh
8. mc.sh
9. pvm.sh
10. qt.sh
11. vim.sh
12. which-2.sh
13. xpvm.sh
  • Execs /etc/bashrc - Sets system-wide [Global] aliases/shell functions
sets the users and root's final global umask
  • execs user's personal .bash_profile
  • execs .bashrc Run after every command you execute
  • .bash_history
  • .bash_logout
  • Finally, at last, the comforting shell login prompt bash !!