Analysis of Centos system running level and startup Mechanism

Source: Internet
Author: User

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 program in linux. 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, it is a user-level process started by the system kernel, and the init process is also the originator of all other system processes. That is to say, the init process is the first process that the system runs, 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 loads 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 takes over control of the CPU, and 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 it, 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. The login command is frozen and new users cannot log on; when all users log off from the system or the specified time is reached, shutdown sends a signal to the init program, asking 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 of the ghost application:
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.