Author: Technology Achievement dream
Link: http://ixdba.blog.51cto.com/2895551/533740
1. System Operation Level
Windows has two different operating levels: safe and normal. Similarly, Linux has a system running level, and the operating level of Linux is more flexible and diversified.
Before talking about the running level, let's talk about the init in Linux. Program Because the INIT program is directly associated with the system running level, the INIT program is one of the most important programs in the Linux operating system and is a user-level process started by the system kernel, at the same time, the INIT process is also the originator of all other system processes. That is to say, the INIT process is the first process running in the system, and its Process number is always 1.
The Linux system has 7 running levels, which are specified in the/etc/inittab file. The specific implementation of the/etc/inittab file is described below.
Taking RedHat Linux as an example, the following section of/etc/inittab information is displayed.
# 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)
The preceding section lists the seven running levels of Linux:
0-Shutdown mode,
1-in single-user mode, only the system administrator can log on to a single-user mode.
2-multi-user mode, but does not support file sharing. For example, NFS is not supported. This mode is not commonly used.
3-full multi-user mode, supporting NFS service. The most common user mode, default logon to the system character interface.
4-The user mode is basically not used, and some specific login requests can be implemented.
5-in full multi-user mode, log on to the X-window system by default, that is, log on to the Linux graphic interface.
6-Restart mode: Execute to close all running processes and restart the system.
These running levels correspond to the INIT program in Linux. For example, if init 1 is executed, the system enters the single-user mode. If init 6 is executed, the system restarts.
All rows starting with # In the inittab file are comment rows. The comment line helps you understand the specific meaning of each item in the inittab file. The values in the inittab file are in the following format:
Label: runlevel: Action: Process
1. Label Field
Label is 1 ~ A four-character tag that identifies the input value. Some systems only support 2 Characters of tags. For this reason, most people limit the number of tag characters to less than 2. The tag can be a string composed of any characters. The labels used in Red Hat Linux include:
2. runlevel Field
The runlevel field specifies the system running level. You can specify multiple running levels, or do not specify specific values for the runlevel field.
3. Process Field
The process field contains the process executed by init, that is, the specific command to be executed by the INIT program. The process adopts the same format as running the process under the command line, therefore, the process field starts with the name of the process, followed by the parameters to be passed to the process during running. For example,/sbin/shutdown-T3-R now.
4. Action Field
The action field defines how the INIT program runs the command corresponding to the process field after the system enters the corresponding running level. The common values of the action field are shown in the following table:
After understanding the inittab file format, we will understand the meaning of each item, and then read the content under/etc/inittab:
ID: 5: initdefault:
Indicates that the system will be started to X-window by default. If you want to enable the system to boot to the character page by default, you only need to modify ID: 5: initdefault: ID: 3: initdefault: you can.
# System initialization.
Si: sysinit:/etc/rc. d/rc. sysinit
This section tells the INIT program to run/etc/rc. d/rc. sysinit for system initialization.
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
The above information indicates that/etc/rc. d/RC can run at 0 ~ 6. At each running level, the INIT program enters the next step only after/etc/rc. d/RC is completed.
# Trap CTRL-ALT-DELETE
CA: ctrlaltdel:/sbin/shutdown-T3-R now
The above section specifies that when CTRL + ALT + DEL are pressed at the same time, the INIT program will execute/sbin/shutdown-T3-R now, that is, restart the system.
# 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.
PF: powerfail:/sbin/shutdown-f-h + 2 "power failure; system shutting down"
The above information shows that when the system power supply is cut off, UPS notifies the INIT program that the INIT program sends a "power failure; system shutting down" signal and then performs the shutdown operation.
# If power was restored before the shutdown kicked in, cancel it.
PR: 12345: powerokwait:/sbin/shutdown-c "power restored; shutdown cancelled"
This section explains: After the ups receives the power recovery signal, it notifies the INIT process, and then the INIT program sends the "power restored; shutdown cancelled" signal to cancel the shutdown operation. This process is in 1 ~ 5.
# 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
This section describes how ~ 5. At the running level, six terminal consoles are always enabled. Even if a console is disabled, the system starts automatically.
# Run xdm in runlevel 5
X: 5: respawn:/etc/X11/preofdm-nodaemon
The last section indicates that the process that is always running on the x-window desktop is/etc/X11/preofdm.
2. System Startup Process
1. kernel boot
Turn on the system power, start BIOS self-check, the system starts the device according to the BIOS settings (usually hard disk boot), and then enters the Linux boot program. Generally, the Linux system provides two boot methods: grub and Lolo. Grub is the default Boot Mode for most Linux systems, while lilo is prepared based on special requirements or personal preferences. Once the Linux boot program is loaded into the memory, it displays a graphical interface for users. This interface contains different kernel options. Users can select different kernel boot through the up/down key. After the boot program completes boot, linux manages control over the CPU. Then, the CPU starts to execute the Linux kernel Image Program, load the kernel, find the compressed package of the initrd image at the specified place, decompress and mount the package, and load all required drivers. Then, the kernel will create a root device, mount the root partition in read-only mode, and release all unused memory. At this time, the kernel has been loaded into the memory to run. However, because no user application allows meaningful commands to be input to the system, the system cannot do anything at this time.
2. run init
The INIT process is the starting point of all processes in the system. Next to the above, after Linux completes the kernel boot, it starts to run the INIT program. The INIT program needs to read the configuration file/etc/inittab, inittab is an unexecutable text file, which will be detailed in the "system running level" section.
3. system initialization
After the INIT program starts, it first calls the RC. sysinit and RC programs. Rc. sysinit mainly performs system initialization. It is an important script to run at every running level of the system. It mainly performs the following tasks: Checking disks, loading hardware modules, activating swap partitions, and other tasks that require priority. When the RC. sysinit program is executed, the system will return to the INIT program.
4. Start the running daemon
After the RC. sysinit program is executed, the RC program starts. The RC program mainly starts the running-level daemon of the system. After the RC program is executed, the system returns the INIT program to continue the next step.
5. Establish a terminal
Now that the basic environment of the system has been set up, the INIT program will then open six terminals for the user to log on.
6. log on to the system
When we see the mingetty logon interface, we can enter the user name and password to log on to the system. In Linux, the Account Verification Program is login. After the login program is successfully executed, it enters the shell terminal.
In this way, the Linux system completes the entire process from boot to startup.
System Shutdown Process
Before learning about the Linux shutdown process, we should first learn some common commands for Linux shutdown. The most common commands for Linux shutdown include init, shutdown, halt, and reboot, these commands can achieve the purpose of shutting down and restarting, but the internal working process of each command is different. We will describe the shutdown command to learn more about the secure Shutdown Process of Linux.
1. shutdown command
The shutdown command can safely shut down the Linux system. Some Linux Beginners use the method of directly shutting down the power to shut down Linux. This is very dangerous because Linux is different from windows, many processes run in the Linux background. These processes control various Linux operations on the system. If forced shutdown, process confusion and data loss may occur, if a sudden power failure occurs when the system has a high workload, data will not only be lost, but hardware equipment may be damaged.
The shutdown command is a shell program that must be executed by a Super User. After the shutdown command is executed, all users working in the system will be notified through broadcast, the system will shut down within the specified time. Save the file, stop the job, and log off the user. At this time, the LOGIN command is frozen and new users cannot log on; when all users log off from the system or the specified time has reached, shutdown sends a signal to the INIT program, requiring the INIT program to change the system running level. Then, the INIT program changes the running level according to the parameters passed by the shutdown command. For example, if the shutdown parameter is the shutdown command, the INIT program will execute init 0 to shut down, if the shutdown parameter is set to restart the system, the INIT program executes init 6 to restart the system.
Detailed syntax of the shutdown command:
Shutdown [-ffhknrc (parameter name)] [-T seconds] time [Warning Information]
Specific parameter functions:
-F does not execute fsck upon restart (Note: fsck is a program for checking and repairing the file system in Linux, which will be detailed in later sections ).
-F: Execute fsck when restarting.
-H: Shut down the system. To some extent, the function is equivalent to that of the halt command.
-K only sends information to all users, but does not actually shut down.
-N does not call the INIT program to shut down, but is implemented by shutdown itself (usually shutdown calls init to shut down the program). Using this parameter will speed up shutdown, however, this shutdown method is not recommended.
-R shutdown and then restart the system.
-C: cancel the previous shutdown command. For example, when executing a command such as "shutdown-h", you only need to press the "Ctrl + C" key to stop the command. Run the "shutdown-H &" command to run the shutdown command in the background. In this case, you need to use the shutdown-C command to cancel the previous shutdown command.
-T <seconds> how many seconds does it take to send a warning message and a Shutdown signal. The warning information reminds you to save the current work.
[Time] How long will the shutdown command be executed. The time parameter can be hh: mm or + M.
Hh: Mm indicates the number of minutes when the shutdown command is executed. For example, "shutdown" indicates that shutdown will be executed at, and + M indicates that shutdown will be executed in M minutes. In special usage, "now" indicates that shutdown will be executed immediately, it is worth noting that this part of the parameter cannot be omitted.
[Warning Information] information to be sent to all login users.
Example:
Shutdown now restart: shutdown-R now
Shutdown now: shutdown-H now
Set to shut down after 5 minutes and send a warning to the logged-on Linux User:
Shutdown + 5 "system will shutdown after 5 minutes"
2. Halt command
Halt is the simplest shutdown command, which is equivalent to the shutdown-h combination. When halt is executed, many applications are killed and then system commands are called to sync, sync writes all memory information to the hard disk through the file system, and then stops the kernel.
Some parameters of the halt command are as follows:
[-F] Force shutdown or restart without calling shutdown.
[-I] disable all network interfaces before shutting down or restarting.
[-P] poweroff is called upon shutdown. This option is the default option.
3. Reboot Command
The execution process of the reboot command is similar to that of halt. The difference is that halt is used for shutdown, while reboot triggers system restart after shutdown.
4. init command
The INIT process is the originator of all processes, and its Process number is always 1. The INIT program is mainly used for switching between different operating levels of the system, and the switchover is completed immediately, for example, init 0 switches the system running level to 0, that is, shutdown. The init 6 command is used to switch the system running level to 6, that is, to restart the system.
Complete!