Debian's init process initialization work principle

xiaoxiao2021-03-06  58

Debian's init process initialization work principle

INIT and INITTAB

=============

Why do you have an init in linux? Those who have used Windows 9.x should know that there is a batch of file autoexec.bat, people who have used the Windows NT / 2000 system should have seen the System Service tool in the control panel, and their purpose is the same. Just compare these things under Windows too weak (of course, the usage is also simpler).

INIT is the last step of Linux launch, which helps users complete some repetitive tasks that must be completed each startup system, such as loading file systems, various network services, etc., it still has an important purpose, allowing users to customize the system Operating the environment, just start the required processes, shut down the processes, release memory and processor resources, so that the system runs faster and more stable.

INIT will perform our commands by the task table, this task table is the / etc / inittab file. We can develop multiple operating environments to meet the needs of different tasks, in the inittab, which is Runlevel. For example, a computer is used to complete a networked compilation task (share CPU cycles, cooperate with other systems) so that the situation should take up a large number of CPU capabilities and memory, so when the computer is used as this use, other programs are running As little as possible. You can set a run level (such as Runlevel 4), including X Window full turn, only the networking compilation process; other time periods, your computer is shared with the Windows system, you need to start Samba, you can define a run level (, Such as: Runlevel 2).

Now you want to run the init 4 when you want to be compiled, don't manually turn off the process that is not required; you want to share the file with the Windows system to run init 2.

The Debian system is pre-configured for some run levels (and is unmodable)

Runlevel 0: Its task is to close all programs, shut down. If your computer and kernel support APM, the system power is automatically cut off.

Runlevel 1: Reserved for single user mode. Under this run level, there is no other program to run except for a root shell. In addition to the root file system, do not install other file systems outside of the read-only mode. This run level is usually used when restored the system.

Runlevel 6: Very similar to Runlevel 0, just it is a restart system instead of a shutdown system.

Runlevel 2, 3, 4, 5: Debian system does not specify them, usually free custom, all of which belong to multi-user mode. Where Runlevel 3 is the default run level used by DEBIAN. When you start the Debian system, it is this running level.

Below is an example of the inittab file

# / etc / inittab: init (8) Configuration.

# $ ID: INITTAB, V 1.91 2002/01/25 13:35:21 Miquels Exp $

# The default runlevel.

ID: 2: INitDefault:

# Boot-Time System Configuration / INITIALIZATION SCRIPT.

# This is run first except when booting in Emergency (-B) Mode.

Si :: sysinit: /etc/init.d/rcs# what to do in single-user mode.

~~: s: Wait: / sbin / sulogin

# /etc/init.d Executes The S and K Scripts Upon Change

# of runlevel.

#

# Runlevel 0 Is Halt.

# Runlevel 1 is Single-User.

# Runlevels 2-5 Are Multi-User.

# Runlevel 6 is Reboot.

L0: 0: Wait: /etc/init.d/rc 0

L1: 1: Wait: /etc/init.d/rc 1

L2: 2: Wait: /etc/init.d/rc 2

L3: 3: Wait: /etc/init.d/rc 3

L4: 4: Wait: /etc/init.d/rc 4

L5: 5: Wait: /etc/init.d/rc 5

L6: 6: Wait: /etc/init.d/rc 6

# NORMALLY NOT Reached, But Fallthrough In Case OF Emergency.

Z6: 6: Respawn: / sbin / sulogin

# What to do when ctrl-alt-del ispese.

CA: 12345: CTRLALTDEL: / SBIN / SHUTDOWN -T1-A -R NOW

# Action On Special KeyPress (Alt-UpARROW).

#KB :: kbrequest: / bin / echo "Keyboard Request - Edit / etc / inittab to let this work."

# What to do when the power failS / return.

PF :: Powerwait: /etc/init.d/powerfail start

Pn :: PowerFailnow: /etc/init.d/powerfail now

PO :: Powerokwait: /etc/init.d/powerfail stop

# / sbin / getty invocations for the runlevels.

#

# The "ID" Field Must Be The Same as The Last

# Characters of The Device (after "TTY").

#

# Format:

# : : :

#

# Note That ON MOST Debian Systems Tty7 is buy by the x window system,

# So if you want to add more getty's go ahead but skip tty7 if you run x.

#

1: 2345: Respawn: / sbin / getty 38400 TTY1

2: 23: Respawn: / sbin / getty 38400 TTY2

3: 23: Respawn: / sbin / getty 38400 TTY3

4: 23: Respawn: / sbin / getty 38400 TTY4

5: 23: Respawn: / sbin / getty 38400 TTY5

6: 23: Respawn: / sbin / getty 38400 TTY6

# Example How To Putto Getty On A Serial Line (for a Terminal)

#

# T0: 23: Respawn: / sbin / getty -l ttys0 9600 VT100

# T1: 23: Respawn: / sbin / getty -l ttys1 9600 VT100

# Example how to put a getty on a modem line. #

# T3: 23: Respawn: / sbin / mgetty-x0 -s 57600 TTYS3

This file has a basic type of instruction that specifies the command line, the action taken by the command line, where the command is activated under the run level. This causes the format of the command line, which is generally as follows:

ID: Runlevels: Action: Command

The ID is any name (what is not important), Runlevels is a numeric string (representative run level), and the action describes when the command is executed, and Command specifies the actual command executed. Examples of the command line to launch getty:

1: 2345: Respawn: / sbin / getty 38400 TTY1

This command tells init, which should run command / sbin / getty 38400 tty1 after booting. The action mark respawn indicates that the command is re-running when exiting (when the user enters Exit at the shell).

Aspects can be seen, running level 2 and 3 have six events GetTys, while level 4 and 5 have one. Assume that there are three shells run in the virtual console 4 under the Run Level 4. You add a number 4 in the run level field of the command line of the ID 2 and 3, which means you will

2: 23: Respawn: / sbin / getty 38400 TTY2

3: 23: Respawn: / sbin / getty 38400 TTY3

Replace

2: 234: Respawn: / sbin / getty 38400 TTY2

3: 234: Respawn: / sbin / getty 38400 TTY3

Then restart or execute the following command:

Telinit Q

Modify the inittab. The Telinit Q command indicates that INIT reloads its configuration. See Telinit (8).

The other rows of / etc / inittab look complicated, let's take a look at what action tags:

· Respawn: Start commands and monitor the execution of the command, and execute the command again when the process exits (usually used on the login prompt on the virtual terminal device). · Wait: When entering the specified run level, start the process. INIT will stop until it is completed. This tag is used to set up the start of software under specific levels.

· Once: When entering the specified run level, start a process.

· Boot: This command runs when the system boots. This type of instruction ignores the run level.

· BootWait: This command runs when the system is booted, init is waiting until it is exited to continue to do something else.

· OFF: Disables a command under all run levels.

· INITDEFAULT: This type of item specifies which run level when the system boot.

• PowerWait: This type of instruction gives the command that needs to be executed when the power is insufficient. INIT continues until the process is completed.

PowerFail: Similar to Powerwait, but init is not waiting for the process.

· PowerWorkWait: The power supply is normal. INIT is suspended until the process ends.

· PowerFailNow: Performs when the battery or external UPS battery is insufficient.

Ctrlattdel: Specifies the command that init in capturing the Ctrl Alt DEL key button.

· KBDREQUEST: Map the special action to a specific button. ALT UPARROW in Debian.

Is it clear that it is clear about the file now?

More powerful sysvinit

================ InitTab main feature is to describe what is started under operation level, and what program should be started under operation, each running level can be usually / Etc / inittab is defined, but Debian has a more robust program sysvinit, which is considered one of the most powerful applications in init. The DEBIAN organization initTab is to move most of the rules of operation from the initTab, move to a script hierarchy. Only the programs that are always starting from the inittab have only Getty, which is used to start the login prompt on the virtual device, keep it because they require special processing, and have more difficult to handle in inittab.

INITTAB is Of course, it is certainly possible, but it is not convenient to see all configurations in the same file. It is not convenient to maintain, so there will be many lines in the file:

L0: 0: Wait: /etc/init.d/rc 0

L1: 1: Wait: /etc/init.d/rc 1

L2: 2: Wait: /etc/init.d/rc 2

L3: 3: Wait: /etc/init.d/rc 3

L4: 4: Wait: /etc/init.d/rc 4

L5: 5: Wait: /etc/init.d/rc 5

L6: 6: Wait: /etc/init.d/rc 6

These lines actually determine the behavior of the system under various operational levels. How do they do it may not be obvious, but at least we know the main meaning: First, each line has a symbol ID LX, LX represents Runlevel X; secondly, each line is only activated under a run level, the run level corresponds to the symbol Number x in the ID. When the command is executed, init is stopped until the process ends. Finally, each command line calls a script /etc/init.d/rc x, where x represents the number of the current run level. Obviously the specific tasks of each run level are arranged in the /etc/init.d/rc script.

The core of debian init is in the /etc/init.d directory. This directory contains scripts that start or stop important software, in order to simplify operations, specify that each script uses the same set of parameters --Start or STOP. Individual packages maintained by users, add other features to these scripts, but INIT will not call them.

For example, you can call: # / etc / init.d / apache start to launch Apache, call: # / etc / init.d / apache stop to close it.

As a user tool, it is very useful, it allows users to easily start and close the complex programs belled in Debian, but inspecting how to start the program, check the /etc/init.d first, see if there is already a script Do this. With this mechanism, you can adjust the run level according to your own needs, / etc / init.d / rc can introduce the system to the required run level. /etc/rcx.d is a set of symbolic links to /etc/init.d. When running, RC handles each script in the logical order in the run level directory. Each of the prefixes is executed in parameter STOP; secondly, each script prefix is ​​performed in parameter start (in order of alphabetics and digital order).

Understand the working principle, try to construct a run level. We use the Run Level 5 and the related RC directory /etc/rc5.d. First, look at the content under this directory: find the work made by each symbolic link; check the file, which file it comes from it. After carefully viewing, find the script that starts the use of the useless program, deletes them, finds the script that you need to run but not included in /etc/init.d, for example, you want to start on the run level 5 Samba, execute command: ln -s /etc/init.d/samba /etc/rc5.d/s20samba

Seeing this is a new understanding of INIT, then test the learning results: Turn off the GDM / XDM / KDM process loaded when Debian is installed, let DEBIAN start to the command line terminal instead of X.

In-depth study:

INITTAB (5)

INIT (8)

Telinit (8)

/ usr / doc / sysvinit / *

转载请注明原文地址:https://www.9cbs.com/read-70976.html

New Post(0)