# 版權宣告: 1999 Copyright OLS3 All rights reserved.
# 作者: OLS3 (臥龍小三)
# 本講義僅供台南縣 87 學年度網路管理進階研習班上課之用.
# 學員可保存一份自用, 供日後網管時參考備查.
# 作者保有一切形式的著作權.
# 欲作其它用途者, 需經作者授權同意.
# 未經作者授權同意之前, 請勿轉載刊登.
RedHat 開機起動流程 :
1. LILO 取得控制權.
PC 起動時, 會讀取開機磁碟第一條 cylinder 的第一個 sector(此即 MBR), 然後它會試著將讀取到的程式碼載入記憶體中並且予以執行. 這些程式碼, 就是 LILO. 記不記得, 當初我們安裝時, 曾選擇將 LILO 放入MBR 呢?!
2. 載入 kernel
Red Hat 的 kernel 放在 /boot 中, 通常檔名類似 vmlinuz-2.0.34-0.6, vmlinuz-2.2.5-15
3. kernel 執行第一支程式 /sbin/init
init 會根據 /etc/inittab 這個 initialization table 來決定不同的 run-level 應該如何設定系統.
Red Hat 的 run-level 有以下 7 層: (定義在 /etc/inittab 中)
- 0 --- halt : 關機 level
- 1 --- Single user mode : 單人模式, 如果你忘記 root 密碼, 這是補救的方式之一.
- 2 --- Multiuser, without NFS : 多人使用模式, 但沒有 NFS 功能, 如果安裝時沒有使用網路功能, 那麼 level 3 是一樣的.
- 3 --- Full multiuser mode : 這是預定的 run-level
- 4 --- unused : 這個 run-level 目前尚未定義使用
- 5 --- X11 : X Windows 使用的 level
- 6 --- reboot : 重新開機時使用的 level
Linux 系統開機時, 最重要的觀念便是這個 run-level , run-level 可以說是 Linux 的系統狀態(system states of Linux), 根據不同的情況進入不同的系統狀態, 以執行不同的初始化動作.
另外一點, Linux 雖然融合了 System V 及 BSD 的特色, 但在開機起動的部份, 比較接近 System V 的作法, 並且這種方式幾乎已成為 Linux 世界的一項標準. 因為它具有容易使用, 功能強大以及富有彈性的特色.
其目錄檔案結構如下:
/etc/rc.d 中包含:
目錄 script 檔案
- /etc/rc.d/init.d
- /etc/rc.d/rc0.d
- /etc/rc.d/rc1.d
- /etc/rc.d/rc2.d
- /etc/rc.d/rc3.d
- /etc/rc.d/rc4.d
- /etc/rc.d/rc5.d
- /etc/rc.d/rc6.d
- rc
- rc.local
- rc.sysinit
rc0.d 便是 run-level 0 起動 script 存放的目錄, rc3.d 是 run-level 3, 其它依此類推
不過, rc0.d ~ rc6.d 中的 script 並不是各自獨立的, 其實它們都是 symbolic file, 連結到 /etc/rc.d/init.d 中的 script.
比如 rc3.d 目錄中的檔案列表如下:
total 0 lrwxrwxrwx 1 root root 20 May 23 15:56 K15postgresql -> ../init.d/postgresql lrwxrwxrwx 1 root root 17 May 23 15:56 K20rusersd -> ../init.d/rusersd lrwxrwxrwx 1 root root 15 May 23 15:56 K20rwhod -> ../init.d/rwhod lrwxrwxrwx 1 root root 16 May 23 15:56 K55routed -> ../init.d/routed lrwxrwxrwx 1 root root 17 May 23 15:56 S01kerneld -> ../init.d/kerneld lrwxrwxrwx 1 root root 17 May 23 15:56 S10network -> ../init.d/network lrwxrwxrwx 1 root root 15 May 23 15:56 S15nfsfs -> ../init.d/nfsfs lrwxrwxrwx 1 root root 16 May 23 15:56 S20random -> ../init.d/random lrwxrwxrwx 1 root root 16 May 23 15:56 S30syslog -> ../init.d/syslog lrwxrwxrwx 1 root root 13 May 23 15:56 S40atd -> ../init.d/atd lrwxrwxrwx 1 root root 15 May 23 15:56 S40crond -> ../init.d/crond lrwxrwxrwx 1 root root 17 May 23 15:56 S40portmap -> ../init.d/portmap lrwxrwxrwx 1 root root 15 May 23 15:56 S40snmpd -> ../init.d/snmpd lrwxrwxrwx 1 root root 16 May 23 15:56 S45pcmcia -> ../init.d/pcmcia lrwxrwxrwx 1 root root 14 May 23 15:56 S50inet -> ../init.d/inet lrwxrwxrwx 1 root root 15 May 23 15:56 S55named -> ../init.d/named lrwxrwxrwx 1 root root 13 May 23 15:56 S60lpd -> ../init.d/lpd lrwxrwxrwx 1 root root 18 May 23 15:56 S75keytable -> ../init.d/keytable lrwxrwxrwx 1 root root 18 May 23 15:56 S80sendmail -> ../init.d/sendmail lrwxrwxrwx 1 root root 13 May 23 15:56 S85gpm -> ../init.d/gpm lrwxrwxrwx 1 root root 15 May 23 15:56 S85httpd -> ../init.d/httpd lrwxrwxrwx 1 root root 15 May 23 15:56 S85sound -> ../init.d/sound lrwxrwxrwx 1 root root 13 May 23 15:56 S91smb -> ../init.d/smb lrwxrwxrwx 1 root root 11 May 23 15:48 S99local -> ../rc.local
各位可以發現: 這些 script 檔不是以 S 開頭, 就是以 K 開頭, 其後再接上二位數字, 並且皆連結到 init.d 目錄中的相關 scripts 檔.
以 S 開頭的, 表示 Start 起動之意, 而 K 開頭的, 則是 Kill 殺除之意.
4. init 執行第一支 script : /etc/rc.d/rc.sysinit
rc.sysinit 會設定許多系統變數, 如:
- 設定 PATH
- 設定網路: 讀取 /etc/sysconfig/network, 以設定 NETWORKING, FORWARD_IPV4, HOSTNAME, DOMAINNAME, GATEWARY, GATEWAYDEV(eth0)
- 設定 NIS domain name
並且執行 run-level 啟動之前所需的許多準備動作, 如:
- 起動 swapping
- checking root filesystems : 檢查 root 檔案系統
- 檢查系統參數 (/proc), 設定 PNP
- 清除 /etc/mtab
- mount root 及 /proc 檔案系統
- 決定是否使用模組, 載入模組
- 檢查檔案系統 (使用 fsck)
- 掛上其它檔案系統
- 設定 console 字型
- 打開 quota
- 清除不必要的檔案, 如 lock, pid
- 設定 clock
- serial port 初始化
- 將開機訊息經由 dmesg 放入 /var/log/dmesg 中
5. init 執行預設的 run-level 目錄中所有的 scripts
正常情況下就是 rc3.d, 因為 /etc/inittab 中有一行設定是: id:3:initdefault: 表示預設要執行的 run-level 是 level 3. 因此, 剛剛上面各位看到的 rc3.d 中的 Sxx 及 Kxx 都會被執行.
6. 最後 init 執行 /etc/rc.d/rc.local 這支 script.
rc.local 會在各 run-level 的 script 執行完之後再執行. 您可以將自己想在開機時處理的動作加在這個 script 之中.
7. init 執行完之後, 接著執行 /bin/login 程式
login 程式會提示使用者需輸入帳號及密碼, 接著編碼並確認密碼的正確性, 若二者相合, 則為使用者進行初始化環境, 並將控制權交給 shell.
8. shell
假設預設的 shell 是 bash, 則 bash 會先尋找 /etc/profile, 執行其中的指令, 然後搜尋使用者目錄中, 是否有 .bash_profile, .bash_login, 或 .profile, 執行其中一個, 接著命令提示符號 $ 便出現在螢幕上, 等待您輸入命令.
至此, 完成了整個開機的流程.