Linux init详解

2019-07-13 01:52发布

init是Linux系统操作中不可缺少的程序之一。 所谓的init进程,它是一个由内核启动的用户级进程。 内核自行启动(已经被载入内存,开始运行,并已初始化所有的设备驱动程序和数据结构等)之后,就通过启动一个用户级程序init的方式,完成引导进程。所以,init始终是第一个进程(其进程编号始终为1)。 内核会在过去曾使用过init的几个地方查找它,它的正确位置(对Linux系统来说)是/sbin/init。如果内核找不到init,它就会试着运行/bin/sh,如果运行失败,系统的启动也会失败。 Linux执行完一些初始化以后,第一个启动init进程。init进程是所有进程的父进程,负责启动其它进程,这些进程大多数是服务进程(daemon)。随着时间的推移这个启动过程也在变化。但目前主要有两种:System V style的runlevel式启动和upstart代表的event-based启动。

1,System V style的runlevel启动

init进程会读取/etc/inittab来决定进入哪一个runlevel。
/sbin/init => /etc/inittab => runlevel  rc script [/etc/rcN.d]
runlevel有些类似Windows中你按下F8进入的安全模式,但比Windows划分的更细,除此之外你还可以通过配置决定每个runlevel加载一些什么服务。不同的Linux发行版对runlevel的解释会有所不同,但runlevel思想是一样的:init通过/etc/inittab决定它的runlevel,然后去/etc/rcN.d/(N代表runlevel的数字表示)去找相应的启动脚本。

2,upstart是的启动

通过/etc/inittab的启动已经不能满足当前的需要了,比如支持一些热插拔设备。一种event-based的init启动产生了。ubuntu中就是使用upstart来启动系统的。upstart使用/etc/init/目录来决定系统在启动时运行那些服务。你可以通过intctrl来控制upstart启动的服务。

3,Ubuntu对System V style runlevel的模拟

你若查看/etc/init/目录,你可以看见一个为rc.conf的脚本,这个脚本起到的作用就是对system v style runlevel的模拟。我们看下这个脚本: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 # rc - System V runlevel compatibility # # This task runs the old System V-style rc script when changing between # runlevels.    description "System V runlevel compatibility" author      "Scott James Remnant "    emits deconfiguring-networking emits unmounted-remote-filesystems    start on runlevel [0123456] stop on runlevel [!$RUNLEVEL]    export RUNLEVEL export PREVLEVEL    console output env INIT_VERBOSE    task    exec /etc/init.d/rc $RUNLEVEL 这个脚本通过执行/etc/init.d/rc $RUNLEVEL来运行具体的rc script。这些rc script在/etc/rcN.d/目录下。由此完成了对system v style runlevel的模拟。 不仅如此,ubuntu还提供了update-rc.d命令,你可以通过此命令来完成system v style runlevel启动脚本的配置。比如你想在启动时不启动apache:
update-rc.d apache2 disable
ubuntu将所有启动脚本放在/etc/init.d/这个目录下,当你运行update-rc.d命令时update-rc.d会根据你的参数新建一些symbol link到相应的/etc/rcN.d/目录。
文章来源:华清远见嵌入式linux培训资料,华清远见嵌入式linux视频教程免费下载!


热门文章