[Linux] 启动流程分析

Linux内核启动的第一个进程是/sbin/init,它的配置文件是/etc/inittab,本文我们将分析Linux如何根据该配置文件启动的,下面是一个该文件的典型例子:

#
# inittab       This file describes how the INIT process should set up
#               the system in a certain run-level.
#
# Author:       Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
#               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)
# 
id:3:initdefault:

# System initialization.
si::sysinit:/etc/rc.d/rc.sysinit

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

# Trap CTRL-ALT-DELETE
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.  
pf::powerfail:/sbin/shutdown -f -h +2 "Power Failure; System Shutting Down"

# If power was restored before the shutdown kicked in, cancel it.
pr:12345:powerokwait:/sbin/shutdown -c "Power Restored; Shutdown Cancelled"


# 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

# Run xdm in runlevel 5
x:5:respawn:/etc/X11/prefdm -nodaemon
下面将一一讲解。

1)设置run level

# 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)
# 
id:3:initdefault:

上面已经对run level的7个等级进行了详细说明,一般常用的是3(多用户模式)和5(图像界面)。

2)设置系统环境 (/etc/rd.d/rc.sysinit)

# System initialization.
si::sysinit:/etc/rc.d/rc.sysinit
rc.sysinit脚本用于设置系统环境,有兴趣的可以去看看脚本内容。

3)启动系统服务(/etc/rc.d/rc N)

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
Linux将根据相应的run level启动相应的系统服务,本例中,我们的run level=3,故会到/etc/rc.d/rc3.d目录下启动服务:

  • 找到K??*开头的文件,执行stop操作
  • 找到S??*开头的文件,执行start操作

其中,??是两位整数,代表start或stop的执行顺序。

如果细心一点,你会发现/etc/rc.d/rc3.d目录下放的都是链接文件,链接到/etc/init.d目录下的文件。

你需要自行处理这些以S,K开头的链接文件吗?当然不需要,chkconfig这个命令就是处理这些链接文件的。

4)用户自定义开机启动程序(/etc/rc.d/local)

在/etc/rc.d/rc d目录下,一定有一个S99local,该连接文件指向/etc/rc.d/rc.local,这个文件用于配置用户自定义的开机启动程序。

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
上面的配置表上在run level=2345的情况下,都将启动两个终端(1,2),而终端3,4,5,6被注释了,故不启用。


[Linux] 启动流程分析

上一篇:wap页面之iphone设备字体偏大问题


下一篇:cocos2d-x 不规则图形碰撞检测