簡單總結
1 啟動過程
init 進程通過/etc/inittab來決定啟動的順序和工作流程 init會執行所有/etc/runlevels/boot目錄下的符號連結所指向的/etc/init.d目錄下的指令碼, 當/etc/runlevels/boot目錄所引用的指令碼都執行完畢後,將繼續運行/etc/runlevels/default目錄下的符號連結所指向的指令碼
相關說明
在inittab有運行層級的定義
l1:S1:wait:/sbin/rc single
l2:2:wait:/sbin/rc nonetwork
l3:3:wait:/sbin/rc default
l4:4:wait:/sbin/rc default
l5:5:wait:/sbin/rc default
l6:6:wait:/sbin/rc reboot
下面的定義
si::sysinit:/sbin/rc sysinit
這是init進程將初始化交給rc 由/sbin/rc sysinit來初始化系統,/sbin/rc指令碼是負責系統初始化的
在rc::bootwait:/sbin/rc boot中
rc指令碼的參數(boot)和要用的/etc/runlevels的子目錄是一樣的。
rc 執行完了以後 init將會決定哪些虛擬終端需要被啟用以及每個終端需要運行什麼樣的命令:
在inittab中有以下定義
c1:12345:respawn:/sbin/agetty 38400 tty1 linux
c2:12345:respawn:/sbin/agetty 38400 tty2 linux
c3:12345:respawn:/sbin/agetty 38400 tty3 linux
c4:12345:respawn:/sbin/agetty 38400 tty4 linux
c5:12345:respawn:/sbin/agetty 38400 tty5 linux
c6:12345:respawn:/sbin/agetty 38400 tty6 linux
這段代碼定義了當運行在某個運行層級上的時候哪些虛擬終端需要被啟用以及每個終端需要運行什麼樣的命令
2 啟動服務和調整
/etc/init.d/xxxx status 查看服務運行狀態
/etc/init.d/xxxx zap 手動把資訊設定為停止
/etc/init.d/xxxx ineed 查看依賴服務
/etc/init.d/xxxx needsme哪些服務需要這個服務(needsme)或者哪些服務可以使用這個服務(usesme)
rc-update add xxx default 添加服務到default 層級
rc-update del xxx default 刪除服務
rc-update -v show 命令將會顯示出所有已存在的初始化指令碼,並列出它們在哪個運行層級中運行
增加獨立的額外配置 這樣做的好處是重裝某個軟體後不會覆蓋自己的配置
gentoo的配置是在/etc/conf.d
gentoo官方文檔的參考
www.gentoo.org/doc/zh_cn/handbook/handbook-x86.xml
首先,你的引導程式會把你在引導程式設定檔中定義的核心鏡像載入到記憶體中,之後它就告訴CPU可以運行核心了。當核心被載入且運行後,核心會初始化所有核心專有的結構體和任務,並開啟init進程。
然後,這個進程確保所有的檔案系統(在/etc/fstab中定義的)都已被掛載且能使用。接著,該進程會執行位於/etc/init.d下的一些指令碼,這些指令碼會啟動一些你需要的服務,以使你能獲得一個成功啟動的系統。
最終,當所有的指令碼執行完畢,init將啟用終端(大多情況下只是啟用虛擬終端,可以使用Alt-F1、Alt-F2等來訪問),並把一個叫agetty的特殊進程附於其上。這個進程會確保你可以通過運行login從這些終端登入到你的系統中。
gentoo不會隨機的執行/etc/init.d下的指令碼。甚至,它不會運行/etc/init.d下所有的指令碼,只會去執行/etc/runlevels 下的
init會運行所有/etc/runlevels/boot目錄下的符號連結所指向的/etc/init.d目錄下的指令碼。通常,它會按照字母順序執行這些指令碼,但是有些指令碼中含有依賴關係,意味著系統要在執行另一個指令碼之後才能運行此指令碼。
當/etc/runlevels/boot目錄所引用的指令碼都執行完畢後,init將繼續運行/etc/runlevels/default目錄下的符號連結所指向的指令碼。同樣它們會按照字母順序執行這些指令碼,除非一個指令碼有依賴關係,那樣的話現有次序就會被改變以使啟動順序更加合理。
當然init自己不會決定所有的啟動順序。它需要一個設定檔來指定它的工作流程。這個設定檔就是/etc/inittab。
如果你還記得我們剛剛描述的啟動順序,你會記得init首先做的是掛載所有的檔案系統。這個功能其實是在/etc/inittab這個設定檔中定義好的。如下:
si::sysinit:/sbin/rc sysinit
這一行告訴init必須運行/sbin/rc sysinit來初始化系統。/sbin/rc指令碼是負責系統初始化的,所以你可能會說init它本身並沒做太多的事情——它只是把初始化系統任務交給了另一個進程。
接下來,init會執行所有在/etc/runlevels/boot目錄下的具有符號連結的指令碼。這是由下面這行定義的:
rc::bootwait:/sbin/rc boot
什麼是運行層級
已經看到init使用一種數位方式來決定需要啟用的運行層級。運行層級表示你系統啟動並執行狀態,它包含了你進入或退出一個運行層級時需要執行的一組指令碼(運行層級指令碼或者初始化指令碼)。
在Gentoo中定義了七種運行層級:三個內部運行層級和四個使用者自訂運行層級。這些內部運行層級分別叫做sysinit、shutdown和reboot,它們所做的就如同像它們的名字那樣:初始化系統、關閉系統和重啟系統。
使用者定義的運行層級都在/etc/runlevels目錄下有同名的子目錄:boot、default、nonetwork和single。運行層級boot會啟動所有其他運行層級必須要使用到的系統服務。其餘的三個運行層級的不同之處主要在於它們要啟動的服務:default是用來日常工作用的;nonetwork是在無網路的情況下使用的;還有single是使用者修複系統時用的。
使用初始化指令碼
實際上rc進程調用的指令碼都稱為初始化指令碼。每個在/etc/init.d下的指令碼都可以在執行時帶上以下參數,如:start、stop、restart、pause、zap、status、ineed、iuse、needsme、usesme或者broken。
要啟動、停止或者重啟一個服務(和所有依賴於它的服務),應該用參數
start
、
stop
和
restart
。
如果狀態資訊告訴你服務正在運行,但是你知道它實際上沒有運行,這種情況下你可以使用參數zap將狀態資訊重設為“停止”
Gentoo的初始化系統使用相依樹狀結構(dependency-tree)來決定什麼服務會首先被啟動。因為這是個很乏味的工作,我們不想讓我們的使用者去手動來完成它,所以我們建立了簡化運行層級和初始化指令碼的管理工具(rc-update)。
使用rc-update你可以從一個運行層級中添加或刪除初始化指令碼。rc-update工具會自動調用/sbin/depscan.sh指令碼來重新建立相依樹狀結構。
添加和刪除服務
在Gentoo的安裝過程中你已經添加初始化指令碼到“default”運行層級。那時你可能還不清楚“default”是幹什麼的,但是現在你應該知道了。rc-update指令碼需要由第二個參數來決定其行為:add、del或者是show。
要添加或刪除一個初始化指令碼,只需要給rc-update add或者del參數,並隨後跟上初始化指令碼和運行層級。如下:
| 代碼 2.1: 從default層級中刪除Postfix服務 |
# rc-update del postfix default
|
rc-update -v show命令將會顯示出所有已存在的初始化指令碼,並列出它們在哪個運行層級中運行:
| 代碼 2.2: 獲得初始化指令碼的資訊 |
# rc-update -v show
|
你也可以運行rc-update show(沒有-v參數)來只查看已經啟用的初始化指令碼和他們的運行層級。
*************************************************************************
啟動時掛載裝置 檔案系統
*************************************************************************
/etc/fstab
一個例子說明
/dev/sda1 /boot ext3 defaults 1 2
/dev/sda2 none swap sw 0 0
/dev/sda3 / ext3 defaults 0 1
none /proc proc defaults 0 0
none /dev/shm tmpfs defaults 0 0
1. 需要掛載的裝置或者檔案系統
The first field, (fs_spec), describes the block special device or remote filesystem to be mounted.
For ordinary mounts it will hold (a link to) a block special device node (as created by mknod(8)) for the device to be mounted, like `/dev/cdrom' or `/dev/sdb7'. For NFS mounts one will have <host>:<dir>, e.g., `knuth.aeb.nl:/'. For procfs, use `proc'.
2. 沒啥說的掛載點 注意swap等
The second field, (fs_file), describes the mount point for the filesystem. For swap partitions, this field should be specified as `none'. If the name of the mount point contains spaces these can be escaped as `/040'.
3. 檔案系統的格式
The third field, (fs_vfstype), describes the type of the filesystem. Linux supports lots of filesystem types, such as adfs, affs, autofs, coda, coher-ent, cramfs, devpts, efs, ext2, ext3, hfs, hpfs, iso9660, jfs, minix, msdos, ncpfs, nfs, ntfs, proc, qnx4, reiserfs, romfs, smbfs, sysv, tmpfs, udf, ufs,umsdos, vfat, xenix, xfs, and possibly others. For more details, see mount(8). For the filesystems currently supported by the running kernel, see /proc/filesystems. An entry swap denotes a file or partition to be used for swapping, cf. swapon(8). An entry ignore causes the line to be ignored.This is useful to show disk partitions which are currently unused.
4. 掛載之後的訪問狀態
The fourth field, (fs_mntops), describes the mount options associated with the filesystem.
It is formatted as a comma separated list of options. It contains at least the type of mount plus any additional options appropriate to the filesystem type. For documentation on the available options for non-nfs file systems, see mount(8). For documentation on all nfs-specific options have a look at nfs(5). Common for all types of file system are the options ``noauto'' (do not mount when "mount -a" is given, e.g., at boot time), ``user'' (allow a user to mount), and ``owner'' (allow device owner to mount), and ``comment'' (e.g., for use by fstab-maintaining programs). The ``owner'' and ``com-ment'' options are Linux-specific. For more details, see mount(8).
5. 提供DUMP功能,在系統DUMP時是否需要BACKUP的標誌位,其內定值是0。
The fifth field, (fs_freq), is used for these filesystems by the dump(8) command to determine which filesystems need to be dumped. If the fifth field is not present, a value of zero is returned and dump will assume that the filesystem does not need to be dumped.
6. 設定此filesystem是否要在開機時做check的動作,除了root的filesystem其必要的check為1之外,其它皆可視需要設定,內定值是0
The sixth field, (fs_passno), is used by the fsck(8) program to determine the order in which filesystem checks are done at reboot time. The root filesystem should be specified with a fs_passno of 1, and other filesystems should have a fs_passno of 2. Filesystems within a drive will be checked sequentially, but filesystems on different drives will be checked at the same time to utilize parallelism available in the hardware. If the sixth field is not present or zero, a value of zero is returned and fsck will assume that the filesystem does not need to be checked.