F.2.4. /sbin/init 程序
/sbin/init
程序(也称作 init
)协调余下的引导过程并为用户配置环境。
当
init
命令启动时,它成为系统里所有自动启动程序的父程序或者祖父(grandparent)程序。首先,它运行 /etc/rc.d/rc.sysinit
脚本设置环境路径、启动 swap、检查文件系统并执行所有系统初始化所需的其他步骤。例如,多数系统会使用时钟,因此 rc.sysinit
读取 /etc/sysconfig/clock
配置文件来初始化硬件时钟。另外一个示例是,如果有必须初始化的特殊串口程序,rc.sysinit
会执行 /etc/rc.serial
脚本。
然后
init
命令处理 /etc/event.d
目录中的任务,这些任务描述了在每个SysV 初始化运行级别中应该如何设置系统。运行级别(runlevel)是一个状态或者模式,它由 SysV /etc/rc.d/rc<x>.d/
目录中列出的服务定义,其中 <x> 是运行级别号。有关 SysV init 运行级别的详情,请参考 第 F.4 节 “SysV Init 运行级别”。
接下来
init
命令为系统设置源功能库,/etc/rc.d/init.d/functions
,它可配置如何启动、杀死程序以及确定程序的程序号(PID)。
init
通过在正确 rc
目录中查找 /etc/inittab
中指定的运行级别启动所有后台进程。对 rc
目录编号对应其代表的运行级别。例如:/etc/rc.d/rc5.d/
是代表运行级别 5 的目录。
当引导至运行级别 5 时,
init
程序会在 /etc/rc.d/rc5.d/
目录中查看并确定要启动和停止的进程。
下面是
/etc/rc.d/rc5.d/
目录内容的示例:
如这个列表所示,所有真正启动和停止服务的脚本都不在
/etc/rc.d/rc5.d/
目录中。相反,/etc/rc.d/rc5.d/
中的所有文件都是指向 /etc/rc.d/init.d/
中的脚本的符号链接。在 rc
里使用符号链接以便在不影响其参考的脚本的情况下,通过重新创建、修改和删除这些符号链接重新配置运行级别。
每个符号链接的名字都以
K
或者 S
开头。以 K
开头的链接是在这个运行级别需要杀死的程序,而以 S
开头的链接是需要启动的程序。
首先,
init
命令通过 /etc/rc.d/init.d/<command> stop
命令来停止目录中的所有 K
符号链接,其中 <command> 是需要杀死的程序名。然后,它他通过 /etc/rc.d/init.d/<command> start
启动所有 S
符号链接。
注意
在系统完成引导后,也可以 root 用户登录,并执行这些同样的脚本启动和停止服务。例如,
/etc/rc.d/init.d/httpd stop
可用来停止 Apache HTTP 服务器。
为每个符号链接编号来指定启动顺序。可以更改这个数字可改变服务启动或停止的顺序。数字越小,就越早启动。有着相同数字的符号链接则按字母顺序启动。
注意
After the
init
command has progressed through the appropriate rc
directory for the runlevel, Upstart forks an /sbin/mingetty
process for each virtual console (login prompt) allocated to the runlevel by the job definition in the /etc/event.d
directory. Runlevels 2 through 5 have all six virtual consoles, while runlevel 1 (single user mode) has one, and runlevels 0 and 6 have none. The /sbin/mingetty
process opens communication pathways to tty devices[18], sets their modes, prints the login prompt, accepts the user's username and password, and initiates the login process.
In runlevel 5, Upstart runs a script called
/etc/X11/prefdm
. The prefdm
script executes the preferred X display manager[19] — gdm
, kdm
, or xdm
, depending on the contents of the /etc/sysconfig/desktop
file.
完成后,系统将在运行级别 5 中运行并显示登录屏幕。
[19]
Refer to the Red Hat Enterprise Linux Deployment Guide for more information about display managers.