系統的啓動過程大體能夠分爲以下幾個步驟:centos
1. 加載BIOS的硬件信息與進行自我測試,並依據設置取得第一個可啓動的設備安全
2. 讀取並執行第一個啓動設備內MBR的bootloaderbash
3. 依據boot loader的設置加載kernel,kernel會開始檢測硬件與加載驅動程序網絡
4. 在硬件驅動成功後,Kernel會主動調用init進程,而init進程回去的run-level信息ide
5. Init執行/etc/rc.d/rc.sysinit文件來準備軟件執行的操做系統(網絡,時區等)測試
6. Init執行run-level的各個服務的啓動spa
7. Init執行/etc/rc.d/rc.local文件操作系統
8. Init執行終端機模擬程序mingetty來啓動login進程,最後就等待用戶登陸命令行
名詞解釋unix
BIOS:開機的時候計算機系統會主動執行的程序,它會識別第一個可開機的設備
MBR:第一個可開機設備的第一個扇區內的主引導分區,內包含bootloader
Boot loader:可進行內核與虛擬文件系統加載的軟件
虛擬文件系統(initrd):內存中仿真的根目錄,用於當loader不能進行根目錄掛載時使用
根據以上啓動流程:個人理解上這樣的一個流程
設備通電開機èBIOS執行è認識第一個開機設備è將MBR內容載入內存
èbootloader執行è載入內核與initrdè根目錄掛載èinitè登錄系統
Bootloader 可以識別操做系統文件格式,因此能夠解壓縮內核到內存中執行,內核在執行中進行測試與驅動各個設備,因爲驅動(內核模塊)是掛載到/lib/modules下,如要讀取必要先掛在根目錄,可掛載根目錄必須讀取驅動。此處就產生的了矛盾,所以爲了解決這個問題引入了initrd, BootLoader將initrd解壓縮到內存並在內存中造成一個仿真的根目錄,加載啓動時是必需的驅動,如磁盤的驅動,並完成根目錄實際的掛載,以完成後續的操做
(此流程是針對centos,不一樣的UNIX LIKE流程不盡相同,但思路是一致的)
在內核加載完驅動後,硬件就已經準備完畢了,此時內核會主動調用第一個進程,即,Init進程,init進程開始開始執行軟件環境,如服務的開啓,網絡的設置等。Init因此操做都會記錄在其配置文件中/etc/inittab
其大體流程以下
Initè讀取init配置文件並執行裏面的命令è執行runlevelè執行相應的服務和服務è登陸圖形頁面(runlevel 5)
d:5:initdefault: =>默認啓動級別是5 # 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. # 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
init處理工做
1. 取得runlevel 默認級別。這裏是5
2. 執行script /etc/rc.d/rc.sysinit進行初始化
3. 由於run level 是5,因此執行5:5:wait:/etc/rc.d/rc 5
4. 設置重啓組合鍵[ctrl]+[alt]+[del]
5. 設置不斷電系統pr與pf
6. 啓動6個終端機
7. 啓動圖形界面
Init配置文件(/etc/inittab)最重要的就是這個運行時啓動級別,對於XWindow來講將run level分爲7個等級,每一個等級只是所啓動的服務不盡相同
0- halt:系統直接關機
1- single user mode:單用戶模式
2- multi user without NFS
3- full multi user mode: 命令行模式
4- unused
5- X11:圖形模式
6- rboot:重啓模式
舉例:
[root@bogon ~]# runlevel =>查看當前運行等級 N 5 [root@bogon ~]# init 3 =>執行runlevel 3
/etc/rc.d/下包含不一樣等級的run level的啓動內容,前面提到過不一樣的run level差異在於啓動的服務不一樣
[root@bogon ~]# ll /etc/rc.d/rc5.d/ lrwxrwxrwx 1 root root 17 02-18 20:06 K01dnsmasq -> ../init.d/dnsmasq lrwxrwxrwx 1 root root 19 02-18 20:14 K01rgmanager -> ../init.d/rgmanager ….. lrwxrwxrwx 1 root root 16 02-18 20:07 S56xinetd -> ../init.d/xinetd lrwxrwxrwx 1 root root 18 02-18 20:06 S80sendmail -> ../init.d/sendmail ……. lrwxrwxrwx 1 root root 11 02-18 20:05 S99local -> ../rc.local lrwxrwxrwx 1 root root 21 02-18 20:12 S99modclusterd -> ../init.d/modclusterd
上面能夠看到的runlevel 5下面啓動的服務
K【數字】:K表明中止。後面的數字表明的中止的順序
S【數字】:S表明開啓。後面的數字表明的開啓的順序
在服務裏面提到過爲解決服務的依賴性所以須要制定服務啓動中止的順序
從上面服務runlevel5啓動內容中有一項是
lrwxrwxrwx 1 root root 11 02-18 20:05S99local -> ../rc.local
這個script能夠添加自定義的命令,從整個啓動流程來看,它處於用戶登陸以前進行的
#!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don't # want to do the full Sys V style init stuff. touch /var/lock/subsys/local
~/.bash_profile文件
文件加載時機:用戶登陸完畢。
特色:只針對單一登陸用戶的我的設置
~./bash_loyout:
文件加載時機:用戶註銷時。
特色:只針對單一登陸用戶的我的設置
/etc/rc.local
文件加載時機:init啓動流程。在用戶爲登陸以前
特色:不區分用戶的命令
/etc/init.d/
文件加載時機:以服務的形式加載
特色:能夠針對不一樣的runlevel設置是否開啓服務
(此處無時機項目經驗,只是簡單對比,對於安全性方面無考慮)
前面提到了initrd的做用,如今查看下這個裏面究竟包含什麼
[root@localhost tmp]# mkdir /tmp/initrd [root@localhost tmp]# cp /boot/initrd-2.6.18-371.el5.img /tmp/initrd/ [root@localhost tmp]# ll ./initrd/ -rw------- 1 root root 2748313 03-31 09:59 initrd-2.6.18-371.el5.img [root@localhost tmp]# mv ./initrd/initrd-2.6.18-371.el5.img ./initrd/initrd-2.6.18-371.el5.gz [root@localhost tmp]# ll ./initrd/ -rw------- 1 root root 2748313 03-31 09:59 initrd-2.6.18-371.el5.gz [root@localhost tmp]# cd initrd/ [root@localhost initrd]# gzip -d ./initrd-2.6.18-371.el5.gz [root@localhost initrd]# ll -rw------- 1 root root 6332928 03-31 09:59 initrd-2.6.18-371.el5 [root@localhost initrd]# file initrd-2.6.18-371.el5 initrd-2.6.18-371.el5: ASCII cpio archive (SVR4 with no CRC) [root@localhost initrd]# cpio -ivcdu <./initrd-2.6.18-371.el5 ……. [root@localhost initrd]# ll drwx------ 2 root root 4096 03-31 10:03 bin drwx------ 3 root root 4096 03-31 10:03 dev drwx------ 2 root root 4096 03-31 10:03 etc -rwx------ 1 root root 2708 03-31 10:03 init drwx------ 3 root root 4096 03-31 10:03 lib drwx------ 2 root root 4096 03-31 10:03 proc lrwxrwxrwx 1 root root 3 03-31 10:03 sbin -> bin drwx------ 2 root root 4096 03-31 10:03 sys drwx------ 2 root root 4096 03-31 10:03 sysroot =>和根目錄結構很像 [root@localhost initrd]# cd ./lib/;ll -rw------- 1 root root 31664 03-31 10:03 ata_piix.ko -rw------- 1 root root 18060 03-31 10:03 dm-log.ko ………. =>這裏包含裏就是啓動時必需的內核模塊
正常安裝unix like版本是initrd是不須要額外建立的,可是有時候咱們可能須要將一些其餘的內核模塊加到initrd中,此時就須要建立一個新的initrd
語法:mkinitrd [-v] [--with=模塊名稱] initrd文件名內核版本