?? sysv init 2.6 的開機過程.htm
字號:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>SysV init 2</title>
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
<meta name="Microsoft Border" content="none">
</head>
<body background="../images/waybkgnd.gif" text="#C0C0C0" link="#00FF00" vlink="#C0C0C0">
<font SIZE="2">
<p></font><font size="6" color="#0080FF"><u><strong>SysV init 2.6 的開機過程 </strong></u></font></p>
<pre>發(fā)信人: Hanky@FruitTea1 (騙誰啊?), 信區(qū): Linux
標(biāo) 題: SysV init 2.6 的開機過程
發(fā)信站: 果茶小站 (Wed May 29 19:38:46 1996)</pre>
<pre>首先我們先講一下為什麼是 sysv init 2.6:
因為很多 distributions 用了有問題的 2.5x 版......
所以我們以 Linux 為平臺, 講 2.6 的。</pre>
<pre>這樣開場白好像怪怪的, 先不管它。</pre>
<pre>一般來說, 系統(tǒng)在跑完 kernel bootstrapping 後, 就去跑 init
這個『萬process之父』, 有了它, 才能開始生小孩......
另外 Linux 有兩個 kernel 類的 process 也開始跑了起來,
一個是 kflushd, 另一個是 kswapd:</pre>
<pre> * process ID 1: init
* process ID 2: kflushd
* process ID 3: kswapd</pre>
<pre>只有這個 init 是完全屬於 user 類的 process, 後兩者是 kernel
假藉 process 之名掛在排程上。</pre>
<pre>init 一開始就去讀 /etc/inittab, 這個 inittab 中對於各個
runlevel 要跑哪些 rc 或 spawn 出什麼有很清楚的設(shè)定。</pre>
<pre>--------[/etc/inittab □例檔]---------------------------------</pre>
<pre># /etc/inittab: init(8) configuration.
# $Id: inittab,v 1.4 1996/03/10 11:47:55 miquels Exp $</pre>
<pre># The default runlevel, 我們設(shè)為 3; runlevel 的概念我們等會兒提。
id:3:initdefault:</pre>
<pre># 開機的 system configuration/initialization script.
# This is run first except when booting in emergency (-b) mode.
si::sysinit:/etc/init.d/boot</pre>
<pre># What to do in single-user mode. sulogin 即為 Single User LOGIN.
~~:S:wait:/sbin/sulogin</pre>
<pre># /etc/init.d executes the S and K scripts upon change
# of runlevel. 其中 /etc/init.d/rc 是一個 shell script,
# 後面的 0~6 參數(shù)表示要跑該 runlevel 所應(yīng)跑的設(shè)定 script.
# Runlevel 0 is halt.
# Runlevel 1 is single-user.
# Runlevels 2-5 are multi-user.
# Runlevel 6 is reboot.</pre>
<pre>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</pre>
<pre># 當(dāng) CTRL-ALT-DEL 按下去了, 該做什麼?一般都是 shutdown -r now
ca:12345:ctrlaltdel:/sbin/shutdown -t1 -r now
# ^^^^^先等個一秒好了</pre>
<pre># Action on special keypress (ALT-UpArrow). 這個我暫時查不到, 抱歉。
kb::kbrequest:/bin/echo "Keyboard Request--edit /etc/inittab to let this work."</pre>
<pre># What to do when the power fails/returns. 這個我晚一點講 (有關(guān) UPS 的部份)
pf::powerwait:/etc/init.d/genpowerfail start
#pn::powerfailnow:/etc/init.d/genpowerfail now
po::powerokwait:/etc/init.d/genpowerfail stop
#pg::powerokwait:/etc/init.d/genpowerfail stop</pre>
<pre># /sbin/getty invocations for the runlevels.
# ^^^^^^^^^^^開 console 出來了。
# The "id" field MUST be the same as the last
# characters of the device (after "tty").
# 例如在 runlevel==3 時, 會有六個 virtual console (tty1~tty6)
# Format:
# <id>:<runlevels>:<action>:<process>
1:2345:respawn:/sbin/agetty 19200 tty1
2:23:respawn:/sbin/agetty 19200 tty2
3:23:respawn:/sbin/agetty 19200 tty3
4:23:respawn:/sbin/agetty 19200 tty4
5:23:respawn:/sbin/agetty 19200 tty5
6:23:respawn:/sbin/agetty 19200 tty6</pre>
<pre>----------[End of /etc/inittab]---------------------------------</pre>
<pre>上面的設(shè)定, 令系統(tǒng)在一跑完 kernel bootstrapping 後, 就去執(zhí)
行 /etc/init.d/boot 這個 shell script, 如果沒什麼問題, 就
進入 default runlevel, runlevel 在 sysv 通常是有如下意義:</pre>
<pre>0: halt (只有 shutdown, 不 reboot 了)
1: Single-user
2: Multi-user
3: Multi-user & 把 network resource export 出來
4: 除了有 Multi-user & export network resource, 一般來講是留給 xdm
5: 不太用到了.
6: reboot (shutdown 後會 reboot)</pre>
<pre>其中 2-5 都是 multi-user 的 runlevel, 通常 runlevel(2~5)
越高, 所提供的服務(wù)也就越多。當(dāng)系統(tǒng)資源有所變動 (例如電力)
時, 我們可以用 telinit 去告知 init 要變換 runlevel (例如
原來是 runlevel=3, 用 telinit 2 使 runlevel 降為 2), 這
樣子可以關(guān)掉一些網(wǎng)路資源服務(wù); 或例如 telinit S 與 telinit 1
都是到 Single-user mode (但前者不同的是, telinit S 根本
就直接在 /dev/console 上執(zhí)行一個 /bin/sh 給你用; 後者會
去執(zhí)行 /etc/init.d/rc 1 這個指令); telinit 6 就等於 reboot.</pre>
<pre>OK, 有問題請先提出來。</pre>
<pre>下面我們要講到的是 /etc/init.d/boot 這個 script 應(yīng)該做些
什麼, 才是我們要的。
</pre>
<pre>再來我們講一下 /etc/init.d/boot 這個 script.</pre>
<pre>既然 kernel bootstrapping 完, 我們就要開始做一些
很基本的檢查、設(shè)定, 以及做一些『準(zhǔn)備工作』。</pre>
<pre>在 kernel bootstrapping 後, 在 mount root as read-only
前會先做一些工作, 以 Linux 為例:</pre>
<pre> * 先設(shè)定這個 script 的 PATH 及 umask
* 掛入 kerneld 這個 daemon (這個和 kernel modules 有關(guān), 後面再講)
* mdadd -ar, 把 md device 跑起來 (這也是後面再講)
* swapon -a, 把所有的 swap partition 打開來用.
* 掛入 update (bdflush) 這個 daemon</pre>
<pre>kerneld 是 Linux kernel 1.3.xx 有了 modules 化後,
一個會自動插 modules 進 kernel 的 daemon, 也會把
經(jīng)一段時間後不曾用到的 modules 拔出 kernel。 有關(guān)
modules 的概念, 我們等到 kernel 的介紹再談詳細的。</pre>
<pre>md device 是 Linux kernel 1.3.69 後新加入的功能,
它可以把兩個以上的 partition 合成一個大的 md device
之後, 直接做出 file system 或 swap space, 而且可以
『交錯地』安排 block 位置, 這就像 RAID-0 一樣, 所以
不但可以將一堆小的 partition 合成大的來用, 也可以增
進速度。</pre>
<pre>update (bdflush) 這個 daemon 是每隔一段時間 (預(yù)設(shè)值
是 5 秒) 就把 'dirty blocks' flush 回 disk 中. 這個
一定要在跑 fsck 等主要的 I/O 動作前就先掛入的了。</pre>
<pre>好, 最基本的準(zhǔn)備完畢後, 我們就要先來 fsck 了。
首先是把『根』mount 起來, 而且是 read-only:</pre>
<pre> * mount -n -o remount,ro /</pre>
<pre>其中 -n 的參數(shù)是不把 mount 的動作寫入 /etc/mtab 中,
因為現(xiàn)在是把 '/' mount 成 read-only, 根本不能寫入。
然後我們開始 fsck:</pre>
<pre> * fsck -A -a</pre>
<pre>參數(shù) -A 是對 /etc/fstab 中的東西全部 check 一次, -a
的參數(shù)是指 auto-repair. 在檢查後如果有東西實在是不能
修好, 就會執(zhí)行 sulogin, 然後 reboot. 如果正常, 那就
把 '/' remount 成 read-write:</pre>
<pre> * mount -n -o remount,rw /</pre>
<pre>因為後面還會 mount -a, 所以這次我們還是用了 -n 參數(shù)。</pre>
<pre>接下來是跑一些『當(dāng) '/' 可以 write 了』之後, 立刻要做的
事:</pre>
<pre> * 跑 modules 的設(shè)定
* 把一些 /etc 及 / 下的檔案清除
* 更新 psdatabase</pre>
<pre>□例如下:</pre>
<pre>----------[/etc/init.d/boot 部份內(nèi)容]-------------------------
# Load the appropriate modules.
if [ -x /etc/init.d/modules ]
then
/etc/init.d/modules
fi</pre>
<pre># Remove /etc/mtab*, /etc/rmtab, /etc/nologin and /fastboot.
rm -f /etc/mtab* /etc/nologin /fastboot /etc/rmtab</pre>
<pre># update /etc/psdatabase
psupdate 2> /dev/null
# or
#ps -U 2> /dev/null
---------[□例結(jié)束]--------------------------------------------</pre>
<pre>上面的東西, 我想大家大概都看得懂......</pre>
<pre>好, 再來就是把所有的 local partitions 都給它 mount 起來.</pre>
<pre> * mount -avt nonfs</pre>
<pre>那為何是 -t nonfs 呢? 很簡單, 因為我們還沒開始設(shè)定 network.
然後, 如果我們有一些 swap file 是在 mount -a 後才出現(xiàn)的,
這時就要再跑一次 swapon:</pre>
<pre> * swapon -a 2>/dev/null</pre>
<pre>才會把 swap file 開來用。
OK, 然後設(shè)定網(wǎng)路 (這時是去叫用一個獨立的 script,
如果這個 script 不存在, 我們就無法設(shè)定 network)
及主機名稱, 然後再 mount -a -t nfs 來加掛人家 export 出來的 fs.</pre>
<pre>------[□例如下]------------------------------------------------
if [ -x /etc/init.d/network ]
then
/etc/init.d/network
fi
# 然後設(shè)定 hostname
# If there's no /etc/HOSTNAME, fall back on this default:
if [ ! -r /etc/HOSTNAME ]; then
echo "Henry.Dorm10.NCTU.edu.tw" > /etc/HOSTNAME
fi
cat /etc/HOSTNAME | cut -f1 -d . > /etc/hostname
hostname --file /etc/hostname</pre>
<pre># Now that TCP/IP is configured, mount the NFS file systems in /etc/fstab.
echo "Mounting remote file systems..."
mount -a -t nfs
------[□例結(jié)束]------------------------------------------------</pre>
<pre>好, 這時才把所有的 file system(含 nfs) 都 mount 起來了,
所以現(xiàn)在立刻要做的事, 就是更新 /etc/ld.so.cache 這個檔,
設(shè)定 system clock, 然後清除 /tmp, /var/run 及 /var/lock
下的大部份垃圾:</pre>
<pre> * /sbin/ldconfig
* clock -s
* 清除 /tmp, /var/run, /var/lock 下的垃圾</pre>
<pre>OK, /tmp, /var/run 及 /var/lock 這些目錄下的垃圾都清空了,
這時才去執(zhí)行 /etc/rc.boot/ 下的所有 script (其中 run-parts
是一個工具程式, 它會把你給的參數(shù)[目錄]下所有的 scripts 都
給它跑個一次):</pre>
<pre> * run-parts /etc/rc.boot</pre>
<pre>如果沒有 run-parts 這個工具, 不妨自己學(xué)著用 shell script
寫一個; 或是用笨方法: 還是乖乖地寫在這個 /etc/init.d/boot
script 內(nèi)吧......</pre>
<pre>然後修改 /dev/ttyXX 的屬性 (關(guān)於 pty256 容後再介紹):</pre>
<pre> * chmod 666 /dev/tty[pqrstuvwxyzabcde]*
* chown root.tty /dev/tty[pqrstuvwxyzabcde]*</pre>
<pre>再來就看你還有什麼事打算在這兒就先處理掉的, 也一并在此寫入,
或是寫個 script 丟到 /etc/rc.boot/ 下也是一樣的。 例如把
powerd 跑起來、建立 /etc/motd、建立 /etc/issue.net、建立一
些 links......都是不錯的主意。</pre>
<pre>以下是我的 /etc/init.d/boot 這個 script:</pre>
<pre>[附件一]
</pre>
<pre>PATH="/sbin:/bin:/usr/sbin:/usr/bin"
umask 022</pre>
<pre>echo
echo "Running /etc/init.d/boot..."
echo</pre>
<pre># enable kerneld
if [ -x /sbin/kerneld ]; then
/sbin/kerneld
fi</pre>
<pre># 把 md 跑起來
if [ -s /etc/mdtab -a -f /sbin/mdadd ]
then
mdadd -ar
fi</pre>
<pre>echo "Activating swap..."
swapon -a 2>/dev/null</pre>
<pre># Ensure that bdflush (update) is running before any major I/O is
# performed (the following is a good example of such activity :).
update &</pre>
<pre># Check the integrity of all file systems (if not a fastboot).
if [ ! -f /fastboot ]
then
# Ensure that root is quiescent and read-only before fsck'ing.
mount -n -o remount,ro /
if [ $? = 0 ]
then
echo "Checking file systems..."
fsck -A -a
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -