Linux Start up and Run Levels
Linux 启动与运行级别
The Init Program
初始化程序
As seen in the previous section, the kernel will start a program called init, if it finds it. The init process reads the file "/etc/inittab" and uses this file to determine how to create processes. Read the init man page for more information. Also note that init is always running and can dynamically do things and run processes based upon various signals. The administrator can also cause it to dynamically change system processes and runlevels by using the telinit program or editing the "/etc/inittab" file.
正如在前面章节中看到的,如果能找到init程序,内核将会调用之。init读取文件“/etc/inittab”,并根据该文件确定创建进程。通过init手册获取更多init信息。同时需要注意的是:init总是在运行,并且能够动态执行不同的动作,根据不同的信号运行进程。管理员可以通过使用telinit程序,或者编辑“/etc/inittab”动态的改变系统进程以及其运行级别。
Runlevels
运行级别
Linux utilizes what is called "runlevels". A runlevel is a software configuration of the system that allows only a selected group of processes to exist. Init can run the system in one of eight runlevels. These runlevels are 0-6 and S or s. The system runs in only one of these runlevels at a time. Typically these runlevels are used for different purposes. Runlevels 0, 1, and 6 are reserved. For Redhat Linux version 6, the runlevels are:
Linux使用被称作“运行级别”的术语。运行级别是系统的一种软件配置,它仅允许选择的一组进程存在。Init能够使系统运行在8种级别上。运行级别为0-6
以及S或s。系统每次仅能运行在一种运行级别上。通常情况下,这些运行级别有不同的目的。运行级别0,1以及6是保留的。对于Redhat Linux Version 6
,运行级别为:
0 - halt
关机
1 - Single user mode
单用户模式
2 - Multiuser, without NFS (The same as 3, if you don't have networking)
多用户模式,无NFS
3 - Full multiuser mode
完整多用户模式
4 - unused
保留
5 - X11
图形
6 - Reboot
重启
The inittab file
文件inittab
The "/etc/inittab" file tells init which runlevel to start the system at and describes the processes to be run at each runlevel. An entry in the inittab file has the following format:
文件“/etc/inittab”告诉init将系统启动在哪个运行级别上,以及在每个运行级别上将要执行的进程。inittab文件中的记录有如下的格式:
id:runlevels:action:process
id - A unique sequence of 1-4 characters which identifies an entry in inittab.
id - 是一个由1-4个字符组成的唯一序列,用于标识inittab中的记录
runlevels - Lists the runlevels for which the specified action should be taken. This field may contain multiple characters for different runlevels allowing a particular process to run at multiple runlevels. For example, 123 specifies that the process should be started in runlevels 1, 2, and 3.
runlevels - 列出了指定action执行时运行级别。该域可以包括多个字符,以标识多个运行级别,它允许特定的进程在不同的运行级别执行。例如,123 表示进程可以在系统运行级别为1,2或3时执行。
action - Describes which action should be taken. Valid actions are listed below:
action -描述了需要采取何种行为。有效的行为如下所列。
>respawn - The process will be restarted whenever it terminates.
如果process终止,则重启启动之。
>wait - The process will be started once when the specified runlevel is entered and init will wait for its termination.
系统进入指定的运行级别,则process将被启动,init等待process直至其终止。
>once - The process will be executed once when the specified runlevel is entered
当系统进入指定的运行级别,process将被执行一次。
>boot - The process will be executed during system boot. The runlevels field is ignored.
process将在系统引导过程中执行。忽略运行级别域。
>bootwait - Same as "boot" above, but init waits for its termination.
与上面的“boot”相同,但是init将等待其终止。
>off - This does nothing.
不做任何事情,相当于忽略该记录。
>ondemand - This process will be executed whenever the specified ondemand runlevel is called.
当进入ondemand运行级别时候,执行process。
>initdefault - Specifies the runlevel which should be entered after system boot. If none exists, init will ask for a runlevel on the console. The process field is ignored.
指定系统引导之后进入的运行级别。如果不存在,init将向控制台询问一个运行级别。忽略process域。
>sysinit - The process will be executed during system boot. It will be executed before any boot or bootwait entries. The runlevels field is ignored.
在系统引导起见,process将被执行。其在boot与bootwait记录之前执行。运行级别域忽略。
>powerwait - The process will be executed when init receives the SIGPWR signal. Init will wait for the process to finish before continuing.
当init收到SIGPWR信号,process将被执行。Init将等待该Process完成,然后继续。
>powerfail - Same as powerwait but init does not wait for the process to complete.
与powerwait一样,但是init不会等待process完成。
>powerokwait - The process will be executed when init receives the SIGPWR signal provided there is a file called "/etc/powerstatus" containing the word "OK". This means that the power has come back again.
假如文件/etc/powerstatus包含单词"OK",则,当init接收到SIGPWR信号时候,process将被执行。这意味着电源可用。
>ctrlaltdel - This process is executed when init receives the SIGINT signal. This means someone on the system console has pressed the
"CTRL-ALT-DEL" key combination.
当init收到SIGINIT信号时候,Process将被执行。这意味着控制台上收到了用户按“CTRL-ALT-DEL”组合键消息。
>kbrequest - The process will be executed when init receives a signal from the keyboard handler that a special key combination was pressed on the console keyboard.
如果用户在键盘上按了组合键,则process将被执行。
Below is an example file:
下面是一个实例inittab文件:
# inittab This file describes how the INIT process should set up
# the system in a certain run-level.
#
# Author: Miquel van Smoorenburg,
# Modified for RHS Linux by Marc Ewing and Donnie Barnes
#
# 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)
#
1) id:3:initdefault:
# System initialization.
2) si::sysinit:/etc/rc.d/rc.sysinit
3) l0:0:wait:/etc/rc.d/rc 0
4) l1:1:wait:/etc/rc.d/rc 1
5) l2:2:wait:/etc/rc.d/rc 2
6) l3:3:wait:/etc/rc.d/rc 3
7) l4:4:wait:/etc/rc.d/rc 4
8) l5:5:wait:/etc/rc.d/rc 5
9) l6:6:wait:/etc/rc.d/rc 6
# Things to run in every runlevel.
10) ud::once:/sbin/update
# Trap CTRL-ALT-DELETE
11) ca::ctrlaltdel:/sbin/shutdown -t3 -r now
# 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.
12) pf::powerfail:/sbin/shutdown -f -h +2 "Power Failure; System Shutting Down"
# If power was restored before the shutdown kicked in, cancel it.
13) pr:12345:powerokwait:/sbin/shutdown -c "Power Restored; Shutdown Cancelled"
# Run gettys in standard runlevels
14) 1:2345:respawn:/sbin/mingetty tty1
15) 2:2345:respawn:/sbin/mingetty tty2
16) 3:2345:respawn:/sbin/mingetty tty3
17) 4:2345:respawn:/sbin/mingetty tty4
18) 5:2345:respawn:/sbin/mingetty tty5
19) 6:2345:respawn:/sbin/mingetty tty6
# Run xdm in runlevel 5
# xdm is now a separate service
20) x:5:respawn:/etc/X11/prefdm -nodaemon
On the left side of the file listing, above, are added numbers to help describe lines. Those lines without line numbers are either blank or begin with a "#" which means the line is a comment. Those line numbers are not part of the original file and are added here for reference purposes.
在列出文件的左部分,增加了行数以方便描述。空行或者以“#”开头的注释行没有标号。这些行号不是原始文件的内容,而是为了这里引用的目的增加的。
> On line 1 above you see "id:3:initdefault:". The id is "id" which stands for initdefault. Note that it is unique on all the numbered lines. The runlevel is 3 which sets the default starting runlevel to runlevel 3. The action is initdefault which tells init to make this runlevel the default runlevel. Note that the process field is blank since it is ignored by the initdefault action.
上面的第1行,你可以看到“id:3:initdefault:”。这里的id是“id”,该id标识了initdefault记录。注意,在所有标记了行号中的记录中,该id是唯一的。运行级别是3,该记录设置了系统的默认运行级别为3。action为initdefault,这告诉init,系统的默认运行级别为3。注意,process域为空,因为它将被initdefault action忽略。
> Line 2 tells init to run the program "/etc/rc.d/rc.sysinit" during system boot, before any other processes.
第2行告诉init在引导系统时候,运行程序“/etc/rc.d/rc.sysinit”在任何其他进程之前。
> Lines 3 through 9 tell init to run the program "/etc/rc.d/rc" for runlevels 0 through 6. Note that for each line the appropriate
runlevel is passed to the "/etc/rc.d/rc" script program on the command line. For example note on line 5 above the second field is the runlevel specifying 2. At the end of the line there is a space and a 2 which allows the variable 2 to be passed on the command line to the program.
第三行到第九行告诉init以运行级别从0到6执行程序"/etc/rc.d/rc"。注意,对于每一行,合适的运行级别将作为命令行被传递到“/etc/rc.d/rc”程序。例如,上面第五行的第二域中的运行级别为2. 在该行的末尾,有一个空格以及2,这允许2作为命令行传递给程序。
> Line 10 specifies that the program "/sbin/update" will run once for every runlevel.
第10行指定程序“/sbin/update”将在每一个运行级别上执行一次。
> Line 11 sets up the program "/sbin/shutdown" to run when someone on the system console has pressed the "CTRL-ALT-DEL" key combination.
第11行记录指定,在用户按下了“CTRL-ALT-DEL”组合键时候,执行"/sbin/shutdown"
> Line 12 specifies "/sbin/shutdown" to run if the power fails. Note that there are different options passed on the command line for lines 11 and 12 although they run the same program.
第12行指定如果电源失败,则执行“/sbin/shutdown”。注意第11与第12行中设定了不同的选项传递给相同的程序。
> Line 13 specified "/sbin/shutdown" will run if power is restored for any of runlevels 1 through 5.
> Lines 14 through 19 specifies the "/sbin/mingetty" program to run on 6 different terminals for runlevels 2 through 5. This means that you can run 6 virtual terminals from your keyboard simultaneously by pressing "ALT-F1" through "ALT-F6". Note pressing "ALT-F7" or above will do nothing, but the screen will not change from your current terminal.
第14行到19行指定:在系统为第2到第5运行级别时,通过执行“/sbin/mingetty”程序运行六种不同的终端。这意味着你可以通过按“ALT-F1”,到“ALT-F6”键,而同时运行6个虚拟终端。
Note the order of programs to run as specified above are:
注意,上面文件中指明了程序执行的顺序如下:
1、/etc/rc.d/rc.sysinit
2、/etc/sbin/update
3、/etc/rc.d/rc 3 - Note: we are running runlevel 3 here.
注意:系统的运行级别为3.
Therefore, the next thing that the system does is to run the rc.sysinit file, save buffers to the hard drive, then run system script files for the requested runlevel which will start up many system and network services as explained in the next section.
所以,系统接下来需要做的事情是运行rc。sysinit文件,保存缓冲到硬盘驱动,然后运行请求运行级别的系统脚本文件,该脚本将启动很多系统与网络服务,这将在后续部分解释。