Linux Bootup

Kernel

The kernel creates a process with process id 0 which eventually gets converted into the swapper process. The kernel then optionally runs the initrd (which stands for init RAM disk) which is a temporary filsystem in the the RAM for kernel unil the filesystem on the hard-disk in initialised.
The process 0 would then invoke the init process (usually located in /sbin/init)

Init

All the processes are invoked directy or indirectly by the init process (with the exceptio of pager process.) init process has the process id of 1. The init when started will take care of the following things:
  1. Checks the filesystem integrity
  2. Starts vital programs and determines the run level from /etc/inittab file. The inittab file is a configuration file and contains entries where each entry is a line of the following format
    id:runlevels:action:process
    If the system's current runlevel is equal to the runlevel in the entry then the action or process in the entry would be executed.

    Following is the description of the each field:
    id a string identifier which is used to identify the entry. The id should be unqiue for all the entries.
    runlevels This field specifies for which run-levels this field is to be considered for. Currently there are 7 runlevels defined (from 0-6) and multiple runlevels can be defined in this field e.g. a value of 125 in this field means that the entry is significant for run evels 1,2, and 5.
    action This field specifies an action which needs to be taken if this entry is significant. What this means that if the urrent run-level of the system is same as the runlevel specified in the runlevel field for this entry then the action needs to be taken. Following values are allowed for the action field:
    initdefault Specifies the runlevel which should be entered after system boot. This actions determines specifies the current runlevel of the system . If no entry with this action exists, init will ask for a runlevel on the console. The process field is ignored with this action.
    respawn Execute the process mentioned in process field and restart it whenever it terminates. However, init does not need to wait for the termination of the process.
    wait Execute the process mentioned in the process field and wait for its termination before proceeding.
    once Execute the process in the process field once when the specified runlevel is entered
    boot The process mentioned in the process field will be executed during system boot. The runlevels field is ignored. init does not need to wait for the process to terminate.
    bootwait Same as "boot" above, but init waits for the process to terminate.
    off This does nothing.
    ondemand This process will be executed whenever the specified ondemand runlevel is called.
    sysinit The process specified in the process field will be executed during system boot. It will be executed before any boot or bootwait entries. The runlevels field is ignored.
    powerwait The process specified in the process field will be executed when init receives the SIGPWR signal. Init will wait for the process to finish before continuing.
    powerfail Same as powerwait but init does not wait for the process to complete.
    powerokwait The process specified in the process field will be executed when init receives the SIGPWR signal provided there is a file called "/etc/powerstatus" containing the word "OK". This means that the power has come back again.
    ctrlaltdel The process specified in the process field is executed when init receives the SIGINT signal. This means someone on the system console has pressed the "CTRL-ALT-DEL" key combination.
    kbrequest The process specified in the process field will be executed when init receives a signal from the keyboard handler that a special key combination was pressed on the console keyboard.
    process Specifies the process to be executed. If the process starts with the '+' character, init will not do utmp and wtmp accounting for that process. This is needed for gettys that insist on doing their own utmp/wtmp housekeeping (a historic bug).

    Determining system's runlevel

    Typically the first line in the inittab file would be with initdefault action in order to set the runlevel for the system. It would normally look something lke the following id:5:initdefault:
    The runlevel choosen with the inidefault will cause the system to boot with different capabilities. Following are the general capabilities configured with different runlevels.
    0 Halt : Causes the system to shutdown.
    1 Single user mode. Mainly used for administrative tasks.
    2 Multi user mode without networking.
    3 Multi user mode with networking.
    4 Not used.
    5 Multi user mode with networking and GUI support.
    6 Reboot. Causes the system to reboot.
  3. The linux file system is typically setup with bunch of directories for each runlevel and there are couple of scripts placed in the directories which are expected to be executed if the system boots up in that specific runlevel. These directories are
    /etc/rc.d/rc0.d (contains scripts for runlevel 0)
    /etc/rc.d/rc1.d (contains scripts for runlevel 1)
    /etc/rc.d/rc2.d (contains scripts for runlevel 2)
    /etc/rc.d/rc3.d (contains scripts for runlevel 3)
    /etc/rc.d/rc4.d (contains scripts for runlevel 4)
    /etc/rc.d/rc5.d (contains scripts for runlevel 5)
    /etc/rc.d/rc6.d (contains scripts for runlevel 6)

    The inittab would also contain entries for executing the scripts in these directries by calling the rc proces. 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 The rc process gets told about the run-level via the command line argument and it runs the scripts in the directory specific to that run-level. Each run-level has its own directory where several scripts are placed. Some of the scripts are executed at startup while some of them are executed at shutdown time. Start up scripts start with 'S' and shutdown scripts start with 'K'.

  4. The inittab file also contains entries for terminal initialization. 1:2345:respawn:/sbin/mingetty tty1 2:2345:respawn:/sbin/mingetty tty2 3:2345:respawn:/sbin/mingetty tty3 4:2345:respawn:/sbin/mingetty tty4 e.g. the above four lines specify the system to spawn four tty consoles. TTY console are normally the console screens that display the characters. It is typical to find more than one consoles configured for a single system even if there is only one monitor connected to the system. Second console can be accessed by pressing CTRL+ALT+2. Similarly third console can be accessed by pressing CTRL+ALT+3.
    TTY Initialization
    init will spawn getty processes (by doing a fork and exec). getty initalizes itself, displays the login prompt and then requests for the username.
    Once the username has been provided, it invokes the program login.
    The login program will call the getpass program to input and validate the password. If getpass fails validation, login program will exit. This will be noticed by init and it will do another fork because the entry for getty in the inittab file had respawn action
    If the getpass program succeeds, then login will proceed to log-in the user by doing the following
    • Change to user's home directory (as specified in the /etc/passwd file)
    • Change the ownership and permissions of terminal to user
    • c. Set group ids and user id of the tty
    • Initialize the environment variables (such as PATH, HOME, SHELL, etc.)
    • Invoke the shell specified in the /etc/passwd file for the user