Linux Init and System Initialization


Init and Runlevels

The init process reads the file "/etc/inittab" and uses this file to determine how to create processes. Read the init man page for more information. Also note that init is always running and can dynamically do things and run processes based upon various signals. The administrator can also cause it to dynamically change system processes and runlevels by using the telinit program or editing the "/etc/inittab" file. Unix and Linux utilize what is called "runlevels". A runlevel is a software configuration of the system that allows only a selected group of processes to exist. Init can run the system in one of eight runlevels. These runlevels are 0-6 and S or s. The system runs in only one of these runlevels at a time. Typically these runlevels are used for different purposes. For Redhat Linux version 6, they are:

0

-

halt

1

-

Single user mode

2

-

Multiuser, without NFS (The same as 3, if you don't have networking)

3

-

Full multiuser mode

4

-

unused

5

-

X11

6

-

Reboot

The Inittab file

The "/etc/inittab" file instructs init which runlevel to start the system at and describes the processes to be run at each runlevel.. An entry in the inittab file has the following format:

id:runlevels:action:process

  • id – A unique sequence of 1-4 characters which identifies an entry in inittab.
  • runlevels – Lists the runlevels for which the specified action should be taken. This field may contain multiple characters for different runlevels allowing a particular process to run at multiple runlevels. For example, 123 specifies that the process should be started in runlevels 1, 2, and 3.
  • action - Describes which action should be taken. Valid actions are listed below
    • respawn - The process will be restarted whenever it terminates.
    • wait – The process will be started once when the specified runlevel is entered and init will wait for its termination.
    • once – The process will be executed once when the specified runlevel is entered
    • boot – The process will be executed during system boot. The runlevels field is ignored.
    • bootwait – Same as "boot" above, but init waits for its termination
    • off – This does nothing.
    • ondemand – This process will be executed whenever the specified ondemand runlevel is called.
    • initdefault – Specifies the runlevel which should be entered after system boot. If none exists, init will ask for a runlevel on the console. The process field is ignored.
    • sysinit – The process will be executed during system boot. It will be executed before any boot or bootwait entries. The runlevels field is ignored.
    • powerwait – The process 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 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 – This process 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 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).

Below is an example file:

        # inittab       This file describes how the INIT process should set up
        #               the system in a certain run-level.
        #
        # Author:       Miquel van Smoorenburg, 
        #               Modified for RHS Linux by Marc Ewing and Donnie Barnes
        #
 
        # 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)
        # 
1)        id:3:initdefault:
 
        # System initialization.
2)        si::sysinit:/etc/rc.d/rc.sysinit
 
3)        l0:0:wait:/etc/rc.d/rc 0
4)        l1:1:wait:/etc/rc.d/rc 1
5)        l2:2:wait:/etc/rc.d/rc 2
6)        l3:3:wait:/etc/rc.d/rc 3
7)        l4:4:wait:/etc/rc.d/rc 4
8)        l5:5:wait:/etc/rc.d/rc 5
9)        l6:6:wait:/etc/rc.d/rc 6
 
        # Things to run in every runlevel.
10)        ud::once:/sbin/update
 
        # Trap CTRL-ALT-DELETE
11)        ca::ctrlaltdel:/sbin/shutdown -t3 -r now
 
        # When our UPS tells us power has failed, assume we have a few minutes
        # of power left.  Schedule a shutdown for 2 minutes from now.
        # This does, of course, assume you have powerd installed and your
        # UPS connected and working correctly.  
12)        pf::powerfail:/sbin/shutdown -f -h +2 "Power Failure; System Shutting Down"
 
        # If power was restored before the shutdown kicked in, cancel it.
13)        pr:12345:powerokwait:/sbin/shutdown -c "Power Restored; Shutdown Cancelled"
 
 
        # Run gettys in standard runlevels
14)        1:2345:respawn:/sbin/mingetty tty1
15)        2:2345:respawn:/sbin/mingetty tty2
16)        3:2345:respawn:/sbin/mingetty tty3
17)        4:2345:respawn:/sbin/mingetty tty4
18)        5:2345:respawn:/sbin/mingetty tty5
19)        6:2345:respawn:/sbin/mingetty tty6
 
        # Run xdm in runlevel 5
        # xdm is now a separate service
20)        x:5:respawn:/etc/X11/prefdm -nodaemon

On the left side of the file listing, above, are added numbers to help describe lines. Those lines without line numbers are either blank or begin with a "#" which means the line is a comment.

  • On line 1 above you see "id:3:initdefault:". The id is "id" which stands for initdefault. Note it is unique on all the numbered lines. The runlevel is 3 which sets the default starting runlevel to runlevel 3. The action is initdefault which tells init to make this runlevel the default runlevel. Note that the process field is blank since it is ignored by the initdefault action.
  • Line 2 tells init to run the program "/etc/rc.d/rc.sysinit" during system boot, before any other processes.
  • Lines 3 through 9 tell init to run the program "/etc/rc.d/rc" for runlevels 0 through 6. Note that for each line the appropriate runlevel is passed to the "/etc/rc.d/rc" script program on the command line. For example note on line 5 above the second field is the runlevel specifying 2. At the end of the line there is a space and a 2 which allows the variable 2 to be passed on the command line to the program.
  • Line 10 specifies that the program "/sbin/update" will run once for every runlevel.
  • Line 11 sets up the program "/sbin/shutdown" to run when someone on the system console has pressed the "CTRL-ALT-DEL" key combination.
  • Line 12 specifies "/sbin/shutdown" to run if the power fails. Note that there are different options passed on the command line for lines 11 and 12 although they run the same program.
  • Line 13 specified "/sbin/shutdown" will run if power is restored for any of runlevels 1 through 5.
  • Lines 14 through 19 specifies the "/sbin/mingetty" program to run on 6 different terminals for runlevels 2 through 5. This means that you can run 6 virtual terminals from your keyboard simultaneously by pressing "ALT-F1" through "ALT-F6". Note pressing "ALT-F&" or above will do nothing, but the screen will not change from your current terminal.

Note the order of programs to run as specified above are:

  1. /etc/rc.d/rc.sysinit
  2. /etc/sbin/update
  3. /etc/rc.d/rc 3 - Note: we are running runlevel 3 here.

0 $type={blogger}: