Explain the mechanism of sysvinit in Linux system in detail


SYSV init is a Systemv style init system. As the name suggests, it originates from Systemv series UNIX. It provides higher flexibility than BSD style init system. It is a UNIX init system that has been popular for decades and has been adopted by various Linux distributions.

(1) What is Systemv

Systemv, once also known as at & T Systemv, is one of many versions of UNIX operating system. It was originally developed by at & T and first released in 1983. Four major versions of System V have been released: versions 1, 2, 3, and 4. Systemv Release4, or SVR4, is the most successful version and the source of some common UNIX features. For example, “SYSV initialization script” (/ etc / init. D) is used to control system startup and shutdown. Systemv interface definition (SVID) is a standard definition of how Systemv works.

(2) Run level of SYSV init

SYSV init uses the term runlevel to define the “operation mode of subscription”. SYSV init checks whether the ‘/ etc / inittab’ file contains an ‘initdefault’ entry. To tell init whether the system has a default operation mode. If there is no default operation mode, the user will enter the system console to manually decide which operation mode to enter.
The operation mode in SYSV init describes the operation modes of various subscriptions of the system. There are usually 8 operation modes, namely operation modes 0 to 6 and s or S.
Each Linux distribution has a different definition of running mode. However, 0, 1 and 6 were unanimously agreed by everyone:
0 shutdown
1 single user mode
6 restart
The working scope of various operation modes is usually defined in the / etc / inittab file. For example, red hat defines runlevels 3 and 5. Operation mode 3 initializes the system to shell mode of character interface; Operation mode 5 initializes the system to Gui mode. Whether command line interface or GUI, operation modes 3 and 5 are complete and formal operation states compared with other operation modes, and the computer can complete the tasks required by users. Mode 1 and s are often used for troubleshooting and recovery after system failure.
Obviously, under these different operation modes, the system needs to initialize the running process, and the initialization preparations are different. For example, operating mode 3 does not need to start the X system. The user only needs to specify which mode to enter, and SYSV init is responsible for all the initialization work necessary for this mode.

(3) SYSV init run sequence

SYSV init cleverly implements different runlevels with scripts, file naming rules and soft links. First, SYSV init needs to read the / etc / inittab file. After analyzing the contents of this file, it obtains the following configuration information:
Runlevel that the system needs to enter;
Capture the definition of the combination key;
Define the power fail / restore script;
Start Getty and virtual console;
After obtaining the configuration information, SYSV init performs the following steps in sequence to initialize the system as the subscribed runlevelx:
/Etc / rc.d/rc and / etc / RC. D / RCX. D / (x for runlevels 0-6)
Xdisplaymanager (if required)

1) Rc.sysinit script function

First, run RC. Sysinit to perform some important system initialization tasks. In RHEL5 of red hat (rhel6 has used upstart), rc.sysinit mainly completes the following work:
Activate udev and SELinux;
Set the kernel parameters defined in / etc / sysctl.conf;
Set the system clock;
Load keymaps;
Activate the switching partition;
Set the host name (hostname);
Root partition check and remount;
Activate raid and LVM devices;
Open disk quota;
Check and mount all file systems;
Clear expired locks and PID files;

2) RC. D script

After all this is done, SYSV init starts running the / etc / rc.d/rc script. According to different runlevels, the RC script will open the RCX. D directory of the corresponding runlevel (x is runlevel), find and run all startup scripts stored in this directory. Each runlevelx has one such directory named / etc / RC. D / RCX. D.
Many different scripts are stored in these directories. Scripts whose file name starts with s are the scripts that should be run at startup, and the number followed by s defines the execution order of these scripts. The scripts in the / etc / RC. D / RCX. D directory are actually soft link files, and the real script files are stored in the / etc / init. D directory. As follows:
Scripts in RC5. D directory

Copy code

The code is as follows:

[[email protected]~]#ll/etc/rc5.d/
… (omitted in the middle)
… (omitted in the middle)
… (omitted in the middle)
… (omitted below)

When all initialization scripts are executed. SYSV init runs the / etc / rc.d/rc.local script.
Rc.local is a place left by Linux for users to personalize. You can put things you want to set up and start here. There are usually more than one user of a Linux server, so you have this consideration.

(4) SYSV init and system shutdown

SYSV init is responsible not only for initializing the system, but also for shutting down the system. When the system is shut down, in order to ensure the consistency of data, it is necessary to carefully finish and clean up in sequence.
For example, you should stop reading and writing to the file system first, and then umount the file system. Otherwise, the data will be lost.
This order is also controlled by the naming rules of all scripts in the / etc / rc.d/rcx.d/ directory. In this directory, all scripts starting with K will be called when the system is shut down, and the number after the letter K defines their execution order.
These scripts are responsible for safely stopping the service or other shutdown work.

(5) Management and control functions of SYSV init

In addition, after the system is started, the administrator also needs to manage and control the started process. The SYSV init package contains a series of tools to control the startup, operation and shutdown of all other programs.
Halt the system.
Init is the init process entity of SYSV init itself. It runs as pid1 and is the parent process of all user processes. The main function is to create a process using the / etc / inittab file during startup.
Killall5 is the killall command of system v. It signals other processes than its own session process, so it cannot kill the currently used shell.
Last backtracks the / var / log / wtmp file (or the file specified by the – f option) to display the login status of all users since the file was created.
The function of lastb is similar to that of last. By default, the / var / log / BTMP file is used to display all failed login attempts.
MESG controls the access of other users to the user terminal.
Pidof finds out the process identification number (PID) of the program and outputs it to the standard output device.
Poweroff is equal to shutdown-h – P, or telinit0. Turn off the system and turn off the power.
Reboot equals shutdown – R or telinit6. Restart the system.
Runlevel reads the login record file of the system (usually / var / run / utmp) and outputs the previous and current system operation level to the standard output device.
Shutdown terminates the system in a safe way. All logged in users will receive the notification that the system is about to terminate, and no new login is allowed.
Sulogin is called by init when the system enters single user mode. Init also calls sulogin when it receives the – B option passed by the boot loader.
Telinit is actually a connection of init, which is used to transmit single character parameters and signals to init.
Utmpdump displays the contents of the / var / run / utmp file to standard output devices in a user-friendly format.
Wall sends messages to all logged in users with information permissions.
Based on these basic tools of SYSV init, different Linux distributions have developed some auxiliary tools to simplify the management of init system. For example, RHEL of RedHat has developed the initscripts software package based on SYSV init, including a large number of startup scripts (such as rc.sysinit), command-line tools such as service and chkconfig, and even a set of graphical interfaces to manage the init system. Other Linux distributions also have their own initscript or init packages of other names to simplify the management of SYSV init.
As long as you understand the mechanism of SYSV init, in a simplest system with only SYSV init, you can directly call scripts to start and stop services, manually create inittabs and create soft connections to complete these tasks. Therefore, it is most important to understand the basic principles and commands of SYSV init. You can even develop your own set of management tools.