Linux Init

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

init

It is the first process executed by the kernel during the booting of a system. It is a daemon process which runs till the system is shutdown. That is why, it is the parent of all the processes. First of all, init reads the script stored in the file /etc/inittab. Command init reads the initial configuration script which basically take care of everything that a system do at the time of system initialization like setting the clock, initializing the serial port and so on.

By reading this file, init determines how the system should be set up in each runlevel and sets default run level.

After determining default runlevel for the system, init starts all background processes required to run the system. First it runs each of the kill script (their file name starts with a K) with a stop parameter. Then it runs all start scripts (their file name starts with an S) to start all services and applications.


Runlevels

A runlevel is a software configuration of Linux system which permits only a selected group of processes to exist. It defines what services are operating on the system.

Runlevels are identified by numbers. init can be in one of eight runlevels. It is changed by a privileged user run telinit,which sends appropriate signals to init to change runlevel.

Runlevel Function
0 Halt the system
1 Single user mode
2 Multiuser mode without networking
3 Multiuser mode with networking
4 Not used
5 Multiuser with networking and X windows
6 Reboot the system
S/s Not used directly
  • 0,1 and 6 are reserved runlevels.
  • Runlevel S or s are same.
  • 7-9 are also valid runlevels, though they are not documented as traditional Unix variants, do'nt use them. But they are same as runlevels S or s. They are aliased.

Changing runlevels

There are many ways to change runlevels. To make the changes permanently, change the default level in the file /etc/inittab.

After all the processes, init waits for one of its descendent process to die, for a powerfail signal. Or wait until telinit signals to change the runlevel. When above conditions occur, it re-examines the /etc/inittab file. New entries can be added to this file any time.

When init is not in single user mode and receives a power fail signal (SIGPWR), it reads the file /etc/powerstatus.Based on the content of this file, init starts a command:

  • F (FAIL) ? When power fails, UPS provides the power. Execute the powerfail and powerwait entries.
  • O (OK) ? Power has been restored, execute powerokwait entries.
  • L (LOW) ? Power is degrading and UPS has a low battery, executes powerfailnow entries.

If file /etc/powerstatus doesn't exist or contains anything other than F, O, L, then init behaves as if it has read the letter F.

To interact with init, /dev/initctl control channel should be used rather than SIGPWR and /etc/powerstatus.

When a request to change the runlevel is send to init, init sends warning signal SIGTERM to all the undefined processes in the new runlevel. It then waits for 5 seconds before terminating processes via SIGKILL forcibly.

All these processes should remain in the same process group which was created by the init, otherwise they will not be able to receive these signals and they need to be terminated separately.

Telinit

/sbin/telinit is linked up with /sbin/init. Telinit takes up an argument and signals init to perform the respective function.

Arguments are as follows with are one character each:

Argument Function
0,1,2,3,4,5,6 Switch to specified runlevel
a,b,c Processes only file entries from /etc/inittab having runlevel a,b,c.
Q or q Re-examine file /etc/inittab
S or s Switch to single user mode
U or u init re-execute itself. No re-examine occurs, runlevel should be from S,s,1,2,3,4,5 otherwise request would be ignored silently.

Default time to wait between sending the signals SIGTERM and SIGKILL is 5 seconds, but telinit can also ask init to change this time with the option -t sec.

Only users having appropriate privileges can execute telinit.

The init binary checks by the process ID whether it is init or telinit, init's process ID is always 1. so one can also use initinstead of telinit as shortcut.