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, <[email protected]> # 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.sysinitrc.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 6Linux將根據相應的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被注釋了,故不啟用。