CentOS 系統啟動流程
CentOS 系統啟動流程
POST:加電自檢;
ROM:CMOS
BIOS:Basic Input and Output System
ROM+RAM
BOOT Sequence:
按次序尋找各引導裝置,第一個有引導程式的裝置即為本次啟動用到裝置;
bootloader: 引導載入器,程式
windows: ntloader
Linux:
LILO:LInux LOader
GRUB: GRand Uniform Bootloader
GRUB 0.X: GRUB Legacy
GRUB 1.x: GRUB2
功能:提供一個菜單,允許使用者選擇要啟動系統或不同的核心版本;把使用者選定的核心裝載到記憶體中的特定空間中,解壓、展開,並把系統控制權移交給核心;
MBR:
446: bootloader
64: fat
2: 55AA
GRUB:
bootloader: 1st stage
disk: 2nd stage
kernel:
自身初始化:
探測可識別到的所有硬體裝置;
載入硬體驅動程式;(有可能會藉助於ramdisk載入驅動)
以唯讀方式掛載根檔案系統;
運行使用者空間的第一個應用程式:/sbin/init
init程式的類型:
SysV: init, CentOS 5
設定檔:/etc/inittab
Upstart: init, CentOS 6
設定檔:/etc/inittab, /etc/init/*.conf
Systemd:systemd, CentOS 7
設定檔:/usr/lib/systemd/system, /etc/systemd/system
ramdisk:
核心中的特性之一:使用緩衝和緩衝來回事對磁碟上的檔案訪問;
ramdisk --> ramfs
CentOS 5: initrd, 工具程式:mkinitrd
CentOS 6: initramfs, 工具程式:mkinitrd, dracut
系統初始化:
POST --> BootSequence (BIOS) --> Bootloader(MBR) --> kernel(ramdisk) --> rootfs(唯讀) --> init
/sbin/init
CentOS 5:
運行層級:為了系統的運行或維護等應用目的而設定;
0-6:7個層級
0:關機
1:單一使用者模式(root, 無須登入), single, 維護模式;
2: 多使用者模式,會啟動網路功能,但不會啟動NFS;維護模式;
3:多使用者模式,正常模式;文本介面;
4:預留層級;可同3層級;
5:多使用者模式,正常模式;圖形介面;
6:重啟
預設層級:
3, 5
切換層級:
init #
查看層級:
runlevel
who -r
設定檔:/etc/inittab
每一行定義一種action以及與之對應的process
id:runlevel:action:process
action:
wait: 切換至此層級運行一次;
respawn:此process終止,就重新啟動之;
initdefault:設定預設運行層級;process省略;
sysinit:設定系統初始化方式,此處一般為指定/etc/rc.d/rc.sysinit;
...
[root@slave ~]# ll /etc/rc.d/
total 60
drwxr-xr-x. 2 root root 4096 Jan 29 2015 init.d
-rwxr-xr-x. 1 root root 2617 Oct 16 2014 rc
drwxr-xr-x. 2 root root 4096 Jan 31 2015 rc0.d
drwxr-xr-x. 2 root root 4096 Jan 31 2015 rc1.d
drwxr-xr-x. 2 root root 4096 Jan 31 2015 rc2.d
drwxr-xr-x. 2 root root 4096 Jan 31 2015 rc3.d
drwxr-xr-x. 2 root root 4096 Jan 31 2015 rc4.d
drwxr-xr-x. 2 root root 4096 Jan 31 2015 rc5.d
drwxr-xr-x. 2 root root 4096 Jan 31 2015 rc6.d
-rwxr-xr-x. 1 root root 259 Jul 31 00:53 rc.local
-rwxr-xr-x. 1 root root 19914 Oct 16 2014 rc.sysinit
id:3:initdefault:
si::sysinit:/etc/rc.d/rc.sysinit
l0:0:wait:/etc/rc.d/rc 0
l1:1:wait:/etc/rc.d/rc 1
...
l6:6:wait:/etc/rc.d/rc 6
說明:rc 0 --> 意味著讀取/etc/rc.d/rc0.d/
K*: K##*:##運行次序;數字越小,越先運行;數字越小的服務,通常為依賴到別的服務;
S*: S##*:##運行次序;數字越小,越先運行;數字越小的服務,通常為被依賴到的服務;
for srv in /etc/rc.d/rc0.d/K*; do
$srv stop
done
for srv in /etc/rc.d/rc0.d/S*; do
$srv start
done
[root@slave ~]# ll /etc/rc.d/init.d/
total 356
-rwxr-xr-x. 1 root root 1288 Oct 16 2014 abrt-ccpp
-rwxr-xr-x. 1 root root 1628 Oct 16 2014 abrtd
-rwxr-xr-x. 1 root root 1642 Oct 16 2014 abrt-oops
-rwxr-xr-x. 1 root root 1725 Aug 18 2010 acpid
-rwxr-xr-x. 1 root root 2062 Jan 30 2012 atd
-rwxr-xr-x. 1 root root 3580 Oct 15 2014 auditd
-r-xr-xr-x. 1 root root 1340 Oct 15 2014 blk-availability
-rwxr-xr-x. 1 root root 710 Nov 10 2010 bluetooth
-rwxr-xr-x. 1 root root 11355 Aug 13 2013 cpuspeed
-rwxr-xr-x. 1 root root 2826 Nov 23 2013 crond
-rwxr-xr-x. 1 root root 3034 Oct 15 2014 cups
-rwxr-xr-x. 1 root root 1734 Jun 16 2014 dnsmasq
-rwxr-xr-x. 1 root root 3245 Jul 9 2013 firstboot
-rw-r--r--. 1 root root 19295 Jul 22 2014 functions
-rwxr-xr-x. 1 root root 1801 Oct 15 2014 haldaemon
-rwxr-xr-x. 1 root root 5866 Jul 22 2014 halt
-rwxr-xr-x. 1 root root 2001 Oct 16 2014 htcacheclean
-rwxr-xr-x. 1 root root 3371 Oct 16 2014 httpd
-rwxr-xr-x. 1 root root 10804 Oct 15 2014 ip6tables
-rwxr-xr-x. 1 root root 10688 Oct 15 2014 iptables
-rwxr-xr-x. 1 root root 1938 Jun 2 2014 irqbalance
-rwxr-xr-x 1 root root 9980 Dec 18 2014 jexec
-rwxr-xr-x. 1 root root 19476 Oct 15 2014 kdump
-rwxr-xr-x. 1 root root 652 Jul 22 2014 killall
-r-xr-xr-x. 1 root root 2134 Oct 15 2014 lvm2-lvmetad
-r-xr-xr-x. 1 root root 2757 Oct 15 2014 lvm2-monitor
-rwxr-xr-x. 1 root root 2571 Sep 4 2014 mdmonitor
-rwxr-xr-x. 1 root root 2200 Sep 13 2012 messagebus
-rwxr-xr-x. 1 root root 2989 Jul 22 2014 netconsole
-rwxr-xr-x. 1 root root 6064 Jul 22 2014 netfs
-rwxr-xr-x. 1 root root 6334 Jul 22 2014 network
-rwxr-xr-x. 1 root root 2205 Oct 15 2014 NetworkManager
-rwxr-xr-x. 1 root root 1923 Jul 15 2013 ntpd
-rwxr-xr-x. 1 root root 2043 Jul 15 2013 ntpdate
-rwxr-xr-x. 1 root root 2023 Apr 3 2012 portreserve
-rwxr-xr-x. 1 root root 3912 Feb 20 2014 postfix
-rwxr-xr-x. 1 root root 1556 Jul 17 2012 psacct
-rwxr-xr-x. 1 root root 2034 Mar 5 2014 quota_nld
-rwxr-xr-x. 1 root root 1513 Sep 17 2013 rdisc
-rwxr-xr-x. 1 root root 1822 Oct 15 2014 restorecond
-rwxr-xr-x. 1 root root 1808 Dec 17 2011 rngd
-rwxr-xr-x. 1 root root 2011 Aug 15 2013 rsyslog
-rwxr-xr-x. 1 root root 1698 Oct 15 2014 sandbox
-rwxr-xr-x. 1 root root 2056 Jun 23 2014 saslauthd
-rwxr-xr-x. 1 root root 647 Jul 22 2014 single
-rwxr-xr-x. 1 root root 3002 Feb 21 2013 smartd
-rwxr-xr-x. 1 root root 2162 Sep 14 2014 snmpd
-rwxr-xr-x. 1 root root 1738 Sep 14 2014 snmptrapd
-rwxr-xr-x. 1 root root 2472 Oct 15 2014 spice-vdagentd
-rwxr-xr-x. 1 root root 4621 Oct 15 2014 sshd
-rwxr-xr-x. 1 root root 1144 Oct 16 2014 sysstat
-rwxr-xr-x. 1 root root 2294 Oct 15 2014 udev-post
-rwxr-xr-x. 1 root root 39800 Jan 27 2015 vmware-tools
-rwxr-xr-x. 1 root root 15208 Jan 27 2015 vmware-tools-thinprint
-rwxr-xr-x. 1 root root 1674 Feb 21 2013 wdaemon
-rwxr-xr-x. 1 root root 1608 Oct 15 2014 winbind
-rwxr-xr-x. 1 root root 1866 Feb 4 2013 wpa_supplicant
chkconfig命令
查看服務在所有層級的啟動或關閉設定情形:
chkconfig [--list] [name]
[root@slave ~]# chkconfig --level 3 NetworkManager on
[root@slave ~]# chkconfig --list
[root@slave ~]# chkconfig --level 3 NetworkManager off
添加:
SysV的服務指令碼放置於/etc/rc.d/init.d (/etc/init.d)
chkconfig --add name
#!/bin/bash
#
# chkconfig: LLLL nn nn
chkconfig: 2345 10 90
刪除:
chkconfig --del name
修改指定的連結類型
chkconfig [--level levels] name <on|off|reset>
--level LLLL: 指定要設定的層級;省略時表示2345;
注意:正常層級下,最後啟動一個服務S99local沒有連結至/etc/rc.d/init.d一個服務指令碼,而是指向了/etc/rc.d/rc.local指令碼;因此,不便或不需寫為服務指令碼放置於/etc/rc.d/init.d/目錄,且又想開機時自動啟動並執行命令,可直接放置於/etc/rc.d/rc.local檔案中;
[root@slave ~]# ll /etc/rc.local
lrwxrwxrwx. 1 root root 13 Jan 27 2015 /etc/rc.local -> rc.d/rc.local
tty1:2345:respawn:/usr/sbin/mingetty tty1
tty2:2345:respawn:/usr/sbin/mingetty tty2
...
tty6:2345:respawn:/usr/sbin/mingetty tty6
mingetty會調用login程式
/etc/rc.d/rc.sysinit: 系統初始化指令碼
(1) 設定主機名稱;
(2) 設定歡迎資訊;
(3) 啟用udev和selinux;
(4) 掛載/etc/fstab檔案中定義的檔案系統;
(5) 檢測根檔案系統,並以讀寫方式重新掛載根檔案系統;
(6) 設定系統時鐘;
(7) 啟用swap裝置;
(8) 根據/etc/sysctl.conf檔案設定核心參數;
(9) 啟用lvm及software raid裝置;
(10) 載入額外裝置的驅動程式;
(11) 清理操作;
總結:/sbin/init --> (/etc/inittab) --> 設定預設運行層級 --> 運行系統初始指令碼、完成系統初始化 --> 關閉對應下需要關閉的服務,啟動需要啟動服務 --> 設定登入終端