一般從打開電源之後,CentOS在經過了這些開機流程
(初始化BIOS -> 執行loader -> 載入kernel 與 initrd image -> 執行init),
到了init這部份,init會去讀取/etc/inittab,根據該檔案的內容,初始化系統環境的工作。
測試環境 CentOS 5.2:
*/etc/inittab內容如下:
$ cat /etc/inittab
#
# 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
0
1
2
3
(1) 而第一個被執行的rc script即是/etc/rc.sysinit,因為根據/etc/inittab中的有段設定:
si::sysinit:/etc/rc.d/rc.sysinit
依照man page來看,sysinit是這個ACTIONS欄位最先被執行的script檔。
HTML:
$ man 5 inittab
...
sysinit
The process will be executed during system boot. It will be executed before any boot or bootwait entries. The runlevels field is ignored.
...
rc.sysinit通常會設定大部分系統相關的環境,例如設定許多系統的變數、啟動swap、設定主機名稱、打開quota、檢查並掛載
所有檔案系統、設定clock、檢查是否使用模組並且載入之、清除不必要的鎖定檔(即在/var/lock/subsys/下的檔案)等。
===============================================================================================
0
1
(2)而第二個被執行的rc script即是/etc/rc,rc會根據initdefault預設要跑的runlevel,
執行 /etc/rc.d/rcX.d 下的所有script,這邊 X 的範例是runlevel 3,因為根據/etc/inittab有幾段設定:
id:3:initdefault:
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
依照man page來看,rc會等待/etc/rc.d/rc3.d底下所有的script都執行完畢之後才會去/etc/inittab後面的敘述。
HTML:
$ man 5 inittab
...
wait The process will be started once when the specified runlevel is entered and init will wait for its termination.
...
附註:/etc/rc.d/rcX.d底下的script檔,都只是從/etc/rc.d/init.d下的service startup script
做soft link過來而已。而/etc/rc.d/init.d是從/etc/rc.d/init.d做soft link,因為必須遵守LSB標準。
rc做的是建置runlevel環境,例如開啟sshd服務、httpd服務、gpm服務、crond服務等。
===============================================================================================
0
1
(3)而最後執行的rc script即是/etc/rc.d/rc.local,因為根據/etc/rc.d/rc3.d/S99local,
由於S99local是/etc/rc.d/rc.local的 soft link,另外S99的英文字跟數字意思代表各項
服務都啟動完之後最後才會去執行它。
===============================================================================================
測試環境OpenSuSE 11.1:
*/etc/inittab內容如下:
$ cat /etc/inittab
#
# /etc/inittab
#
# Copyright (c) 1996-2002 SuSE Linux AG, Nuernberg, Germany. All rights reserved.
#
# Author: Florian La Roche, 1996
# Please send feedback to http://www.suse.de/feedback
#
# This is the main configuration file of /sbin/init, which
# is executed by the kernel on startup. It describes what
# scripts are used for the different run-levels.
#
# All scripts for runlevel changes are in /etc/init.d/.
#
# This file may be modified by SuSEconfig unless CHECK_INITTAB
# in /etc/sysconfig/suseconfig is set to "no"
#
# The default runlevel is defined here
id:3:initdefault:
# First script to be executed, if not booting in emergency (-b) mode
si::bootwait:/etc/init.d/boot
# /etc/init.d/rc takes care of runlevel handling
#
# runlevel 0 is System halt (Do not use this for initdefault!)
# runlevel 1 is Single user mode
# runlevel 2 is Local multiuser without remote network (e.g. NFS)
# runlevel 3 is Full multiuser with network
# runlevel 4 is Not used
# runlevel 5 is Full multiuser with network and xdm
# runlevel 6 is System reboot (Do not use this for initdefault!)
#
l0:0:wait:/etc/init.d/rc 0
l1:1:wait:/etc/init.d/rc 1
l2:2:wait:/etc/init.d/rc 2
l3:3:wait:/etc/init.d/rc 3
#l4:4:wait:/etc/init.d/rc 4
l5:5:wait:/etc/init.d/rc 5
l6:6:wait:/etc/init.d/rc 6
# what to do in single-user mode
ls:S:wait:/etc/init.d/rc S
~~:S:respawn:/sbin/sulogin
# what to do when CTRL-ALT-DEL is pressed
ca::ctrlaltdel:/sbin/shutdown -r -t 4 now
# special keyboard request (Alt-UpArrow)
# look into the kbd-0.90 docs for this
kb::kbrequest:/bin/echo "Keyboard Request -- edit /etc/inittab to let this work."
# what to do when power fails/returns
pf::powerwait:/etc/init.d/powerfail start
pn::powerfailnow:/etc/init.d/powerfail now
#pn::powerfail:/etc/init.d/powerfail now
po::powerokwait:/etc/init.d/powerfail stop
# for ARGO UPS
sh:12345:powerfail:/sbin/shutdown -h now THE POWER IS FAILING
# getty-programs for the normal runlevels
# <id>:<runlevels>:<action>:<process>
# The "id" field MUST be the same as the last
# characters of the device (after "tty").
1:2345:respawn:/sbin/mingetty --noclear 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
#
#S0:12345:respawn:/sbin/agetty -L 9600 ttyS0 vt102
#cons:12345:respawn:/sbin/smart_agetty -L 38400 console
#
# Note: Do not use tty7 in runlevel 3, this virtual line
# is occupied by the programm xdm.
#
# This is for the package xdmsc, after installing and
# and configuration you should remove the comment character
# from the following line:
#7:3:respawn:+/etc/init.d/rx tty7
# modem getty.
# mo:235:respawn:/usr/sbin/mgetty -s 38400 modem
# fax getty (hylafax)
# mo:35:respawn:/usr/lib/fax/faxgetty /dev/modem
# vbox (voice box) getty
# I6:35:respawn:/usr/sbin/vboxgetty -d /dev/ttyI6
# I7:35:respawn:/usr/sbin/vboxgetty -d /dev/ttyI7
# end of /etc/inittab
0
1
2
3
(1) 而第一個被執行的rc script即是/etc/init.d/boot,因為根據/etc/inittab中的有段設定:
si::bootwait:/etc/init.d/boot
依照man page來看,bootwait是這個ACTIONS欄位中除了sysinit外會先被執行的。
HTML:
bootwait
The process will be executed during system boot, while init waits for its termination (e.g. /etc/rc). The runlevels field is ignored.
/etc/init.d/boot會掛載proc、sysfs、devpts這一類的虛擬檔案系統,另外也會執行/etc/init.d/boot.d/下的所有
script檔,以設置系統需要的組態,最後在/etc/init.d/boot會去執行/etc/init.d/boot.local。
===============================================================================================
0
1
(2)而第二個被執行的rc script即是/etc/rc,rc會根據initdefault預設要跑的runlevel,
執行 /etc/rc.d/rcX.d 下的所有script,這邊 X 的範例是runlevel 3,因為根據/etc/inittab有幾段設定:
id:3:initdefault:
l0:0:wait:/etc/init.d/rc 0
l1:1:wait:/etc/init.d/rc 1
l2:2:wait:/etc/init.d/rc 2
l3:3:wait:/etc/init.d/rc 3
#l4:4:wait:/etc/init.d/rc 4
l5:5:wait:/etc/init.d/rc 5
l6:6:wait:/etc/init.d/rc 6
依照man page來看,rc會等待/etc/init.d/rc3.d底下所有的script都執行完畢之後才會去/etc/inittab後面的敘述。
HTML:
$ man 5 inittab
...
wait The process will be started once when the specified runlevel is entered and init will wait for its termination.
...
附註:/etc/rc.d/rcX.d底下的script檔,都只是從/etc/init.d下的service startup script
做soft link過來而已。
rc做的是建置runlevel環境,例如開啟sshd服務、httpd服務、gpm服務、crond服務等。
===============================================================================================
0
1
結論:Red Hat與SuSE的rc sript不太一樣,在系統環境部份:
rh的/etc/rc.d/rc.sysinit相當於suse的/etc/init.d/boot,但是rc.sysinit這個script本身就包括了建置系統環境的
所有命令,而boot這個script則是把大多的建置系統環境的組態分成數個script放於/etc/init.d/boot.d/中。
在runlevel部份:
rh與suse都遵循LSB的規範,都會有/etc/init.d這個目錄,並且放置了service startup script,在於rh部份放的都是在
runlevel要啟動的script,但是在suse下就不一樣了,因為他除了放runlevel所要的script之外,上面談到的,還包括建置
系統環境時的script。
在local command部份:
rh是在runlevel環境都建置完畢之後才會去執行/etc/rc.d/rc.local,而suse則是在/etc/init.d/boot的工作快要執行
完成之後,才會去執行/etc/init.d/boot.local,所以執行的層次有點不一樣,一個是在建置runlevel時所執行的,另外
一個則是在建置系統環境時所執行的。