i THE INIT PROCESS IN LINUX – All things in moderation

THE INIT PROCESS IN LINUX

When we boot up the computer, the first thing the kernel does is to execute init program. This is the root/parent of all process executing on Linux. The first processes that init starts is a script /etc/rc.d/rc.sysinit/. Based on the appropriate run-level, scripts are executed to start various processes to run the system and make it functional.

1. THE LINUX INIT PROCESSES:

The Linux and Unix SysV heritage began with the “sysvinit” process which used the /etc/inittab configuration file to drive the execution of init scripts and configuration of terminal and GUI interfaces. SysV init launches scripts and “daemon” processes one at a time, in order for a given run level found in /etc/rc.#/ (Where # is a run level number from 0 to 6).
Systemd is used in later versions of Linuxwhich is compatible with Unix SysV and Linux Standard Base (LSB) init scripts. Systemd supports parallelization (faster boot), socket management, process tracking, mounting of file systems and dependency-based service control. Daemon processes are categorized into “cgroups” which have categorized resource limits.
Ubuntu 12.04+, 14.04 uses a hybrid system called upstart which includes SysV runlevel specific start scripts in /etc/rc.#/ but also systemd scripts for those services which have been ported in /lib/systemd/system/.
The init process is the last step in the boot procedure and identified by process id “1”. Init is responsible for starting system processes as defined in:
* SysV systems like RHEL 1-6, Fedora 1-14: /etc/inittab
* Systemd Fedora 15+: /lib/systemd/system/default.target (This is a symbolic link). And the files in /etc/systemd/system/ and /lib/systemd/system/
Init typically will start multiple instances of “getty” which waits for console logins which spawn one’s user shell process. Upon shutdown, init controls the sequence and processes for shutdown. The init process is never shut down. It is a user process and not a kernel system process although it does run as root.
System Processes:

1.1. SYSTEMD INIT:

Systemd is becoming the default init system for Linux and has brought the following changes:
* Boot scripts in /etc/systemd/system/ and /lib/systemd/system/
* Service control command:
* Fedora 15+: systemctl
* Ubuntu 12.04+, 14.04: initctl
* Requires kernel support: (Kernel compile config options)
* CONFIG_DEVTMPFS=y
* CONFIG_CGROUPS=y
* CONFIG_AUTOFS4_FS=[y|m]
* The kernel command line in GRUB will include “init=/bin/systemd”
* Gnome desktop D-Bus dependency on systemd

1.2. SYSV INIT:

Init config file (Red Hat EL6): /etc/inittab

id:3:initdefault:

Init config file (Red Hat 7.3-9.0, Fedora 1-14, RHEL5): /etc/inittab

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

Note that this config file directs the init process to run the shell script /etc/rc.d/rc.sysinit.
Local system boot processes can be placed in file: /etc/rc.d/rc.local
The system will then boot to the runlevel set by the directive initdefault.

2. LINUX INIT RUN LEVELS:

SysV init and systemd both support the notion of run levels but manage them in different ways. SysV init uses symbolically linked start/stop init scrips located in directories defining the services to be run (/etc/rc.d/rc[0-6].d/) while systemd manages the services with “cgroups”.
Typical run levels include:
* 0 — Halt
* 1 — Single-user text mode
* 2 — Not used (user-definable)
* 3 — Full multi-user text mode
* 4 — Not used (user-definable)
* 5 — Full multi-user graphical mode (with an X-based login screen)
* 6 — Reboot

2.1. SYSTEMD RUN LEVELS:

The default boot level is set by /etc/systemd/system/default.target which is a symbolic link to the file defining the appropriate run level target.
The default boot level is set by /etc/systemd/system/default.target which is a symbolic link to the file defining the appropriate run level target.
Fedora 15-19:
* Set run level 3 (multi-user with networking and console only login):
rm /etc/systemd/system/default.target
ln -sf /lib/systemd/system/runlevel3.target /etc/systemd/system/default.target
* Set run level 5 (graphic mode):
rm /etc/systemd/system/default.target
ln -sf /lib/systemd/system/runlevel5.target /etc/systemd/system/default.target
The run level targets are defined as runlevel0.target, runlevel1.target, runlevel2.target, runlevel3.target, …
Ubuntu 12.04+: (Upstart: Mix of SysV init and systemd. Not all services have been migrated)
* Run level defined by the services in directory /etc/systemd/system/multi-user.target.wants/
ls -l /etc/systemd/system/multi-user.target.wants/
This will show symbolic links to service files defined in /lib/systemd/system/
Runlevel “3” will boot to text or console mode and “5” will boot to the graphical login mode.

Note that the user friendly name is just a symbolic link to the runlevel name:

# ls -l /lib/systemd/system/runlevel3.target
lrwxrwxrwx. 1 root root 17 May 24 10:55 /lib/systemd/system/runlevel3.target -> multi-user.target

Ubuntu names the directories with an additionaol suffix: “.wants”. For example multi-user.target on Fedora would be named multi-user.target.wants for an Ubuntu system.

2.2. SYSV RUN LEVELS:

The Linux boot process has six states of operation of which “0” is the shutdown state and “3” and above are fully operational with all essential processes running for user interaction. Upon system boot the LINUX system /sbin/init program starts other processes by performing the following:
* Init will bring up the machine by starting processes as defined in the /etc/inittab file.
* The computer will be booted to the runlevel as defined by the initdefault directive in the /etc/inittab file. In this example a runlevel of “5” is chosen.

id:5:initdefault:
  • One of these process started by init is /sbin/rc. This script runs a series of scripts in the directories /etc/rc.d/rc0.d/, /etc/rc.d/rc1.d/, /etc/rc.d/rc2.d/, etc
  • Scripts in these directories are executed for each boot state of operation until it becomes fully operational. Scripts beginning with S denote startup scripts while scripts beginning with K denote shutdown (kill) scripts. Numbers follow these letters to denote the order of execution. (lowest to highest)
    Runlevel “3” will boot to text or console mode and “5” will boot to the graphical login mode ( “4” for slackware)

List state and run level of all services which can be started by init: chkconfig –list
or
service –status-all | grep running (Red Hat/Fedora Core based systems).

3. INIT SCRIPT ACTIVATION:

3.1. SYSV INIT:

Adding a script to the /etc/rc.d/rc#.d/ directory with either an S or K prefix, adds the script to the boot or shutdown process. The scripts are run in numerical order. S20abc is run before S30xyz. The extensibility to the boot and shutdown procedures of the operating system is one of the strengths of UNIX. The orderly sequential initiation of processes can be coordinated for dependent processes. The orderly shutdown of processes is often required of complex programs such as databases. This is how it is done. Individual processes may be monitored, shutdown and started at any time using these scripts. i.e. /etc/rc.d/rc2.d/httpd start. The modifiers start, stop or status may be used.
The start/stop/status scripts actually reside in the directory:
* /etc/rc.d/init.d/ (Red Hat/Fedora) Also /etc/init.d/ which is linked to /etc/rc.d/init.d/
* /etc/init.d/ (S.u.s.e. and Ubuntu / Debian)
and are linked to the appropriate directories. These links may be created or destroyed using the chkconfig command. i.e. chkconfig –del httpd will remove the web server from the startup and shutdown process. Inversely chkconfig –add httpd will add it to the startup/shutdown process by generating links from the script in /etc/rc.d/init.d/ to the appropriate /etc/rc.d/rc#.d/ directory. For more information see the LINUX manual page on init.

3.2. UPSTART:

Ubuntu 14.04:
Init scripts are found in /etc/init/
List services which are to be turned on at boot time: grep “start on” /etc/init/ *
Also see man upstart-events to view events which can be configured.

3.3. SYSTEM SERVICE:

Recommended basic services: anacron, ard, autofs, crond, gpm, iptables, keytable, kudzu, microcode_ctl (Intel32 hardware only), network, random. syslog
Graphics Workstation – add: xfs
File Server for PC clients – add: smb
Print Server – add: lpd or cups (hplip – HP Linux Imaging and Printing)
File server Linux/Unix clients – add: nfs, netfs, nfslock, portmap, ypbind, yppasswd, ypserv; NFSv4 add: rpcgssd, rpcidmapd, rpcsvcgssd
Web Server – add: httpd, tux, xinetdi, sshd

CONCLUSION:

There will be differences in each Linux distribution. In this article, I will primarily refer to ubuntu 14.04. Thanks!

Leave a Reply