5

ubuntu 11.04的启动流程(启动服务的配置)

 3 years ago
source link: https://blog.csdn.net/yanxiangtianji/article/details/14491105
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

ubuntu 11.04的启动流程(启动服务的配置)

原文转自 http://blog.sina.com.cn/s/blog_9e8acf550100y4e9.html

现行的Linux 系统的启动方式主要有两种:一种是广为流传的System V initialization,另一种是近几年提出的Upstart方式,基于事件机制,系统的所有服务,任务都是由事件驱动的。采用后一种方式的目前 主要Ubuntu(6.10 and later),Fedora(9.10 and later),Debian(optional)。

Ubuntu11.04上是没有这个initab文件的,因为ubuntu 11.04采用Upstart方式的。在旧式的System V initialization中,/etc/inittab可是个相当重要的文件。init进程启动后第一时间找的就是它!inittab负责初始化系 统,设置系统runlevel及进 入各runlevel对应要执行的命令。假设当前inittab中设置的默认runlevle是5,则init会运行/etc/init.d/rc 5命令,该命令会依据系统服务的依赖关系遍历执行/etc/rc5.d中的脚本/程序。进入/etc/rc5.d目录可以发现里面的文件都是到/etc /init.d/下对应的脚本/程序的软链接。以S开头的为启动的意思,以K开头的为停止。并且S/K后面的两位数数字代表了服务的启动顺序(由服务依赖 关系决定)。

那么Upstart job是怎么样的呢?我们知道,System V initializaiton是以runlevel为核心,依据服务间依赖关系的init方式,但在Upstart job,runlevel虽说对于服务的启动也有影响但已不是关键所在。Upstart job是事件驱动的,系统服务的启动、停止等等均是由事件决定的,反过来,系统服务的启动、停止也可以作为事件源触发其他服务。并且事件并不一定得由系统 内部产生,用户可以手工的键入start/stop [Service]产生事件来启动/终止服务。man upstart-evnets查看upstart job所定义的事件,可以发现,runlevel也被当作事件来对待(因runlevel的改变而产生的事件),诸如此类还有其他如 startup,started,filesystem等等。那么系统服务又是如何知道自己应该什么时候启动,什么时候终止的呢?答案就在于/etc /init中(有的distros可能是在/etc/event.d)。进入/etc/init目录下一看,均是系统服务的配置文件,或者说,是job definition files。(实际上Upstart init只需要/etc/init这么一个目录,不像System V init,“拐弯抹脚”转好多圈才到达目的地,在性能上不如前者)。随便打开一个文件,比如cron.conf:

# cron - regular background program processing daemon
# cron is a standard UNIX program that runs user-specified programs at
# periodic scheduled times

description "regular background program processing daemon"

start on runlevel [2345]
stop on runlevel [!2345]

expect fork

respawn

exec cron

start on runlevel [2345];stop on runlevel [!2345]配置文件就是通过这个来设置服务何时启动,何时终止的。

实际上并不仅仅在系统启动初期,在系统运转的任何时期都可以通过发送事件来启 动或终止服务。这便是Upstart job的优点之一,除了用于系统初始化,还可以在系统运行阶段发挥作用。相比之下System V initialization方式下的配置文件一般只用于系统初始化阶段,当然系统运行阶段我们可以通过/etc/init.d/Service start/stop/otherCommand来操作服务,但很明显不如Upstart方式简洁明白。

介绍完System V initialization和Upstart,那么现在就能介绍Ubuntu11.04i系统初始化流程。前面提过Ubuntu11.04使用的是Upstart方式的initialization,考虑到6.10之前的版本采用的 System V init及某些服务的需要,Ubuntu11.04采用的是兼容模式,即:系统中既有System V-style启动的服务,也有Upstart启动的服务。如果你使用的是Ubuntu11.04(我目前PC上的系统),那么你可以看到系统中有这么几 个目录:/etc/init/etc/init.d/etc/rc${runlevel}.d

作为两种init方式各自特征的/etc/init.d,/etc /rc${runlevel}.d目录和/etc/init目录在Ubuntu11.04中都有了,那么Ubuntu是如何实现兼容的?实际上,Ubuntu11.04中并没 有直接采用System V-style启动服务,要知道,Ubuntu11.04中的init已被替换为Upstart init,而System V-style的服务是存放于/etc/rc${runlevel}.d目录中的,(而/etc/rc${runlevle}.d/下的文件是到/etc /init.d的软链接)可Upstart init并不会直接跑到这里面去启动服务。它是通过间接调用来启动这类服务的。换句话说,Ubuntu中的init并不会直接奔着/etc/init.d 或者/etc/rc${runlevel}.d/而去,它采用了折衷的办法,通过/etc/init下的某些配置文件调用/etc /rc${runlevel}.d/中的脚本以启动采用旧式System V-style的服务,进入/etc/init目录(Upstart init会到该目录下读取配置文件),会发现几个跟rc有关的配置文件: rc.conf rc-sysinit.conf rcS.conf 。rc-sysinit在startup事件发生时被启动,rc在系统runlevel变化时被启动,rcS在系统runlevel为S时启动。在配置文件的注释中说明了,这几个文件,正是Upstart init处理System V-style服务的关键。

rc-sysinit在startup事件发生时被启动,即,Upstart


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK