內容:
CentOS6 的啟動流程
服務管理(chkconfig)
手動制定系統開機服務腳本
一、centos6系統啟動流程
綜述過程:
POST–>BIOS(Boot Sequence)–>MBR(grub,446)–>Kernel–>initramfs(虛根文件)–>(ROOTFS)/sbin/init(/etc/inittab)
步驟詳解:
1、POST:
POST:Power-On-Self-Test,加電自檢,是BIOS功能的一個主要部分。負責完成對CPU、主板、內存、硬盤子系統、顯示子系統、串并行接口、鍵盤、CD-ROM光驅等硬件情況的檢測。
2、BIOS:
決定設備的啟動順序
3、grub(bootloader)
在centos5、6中,linux系統的MBR的bootloader是grub程序,windows的bootloader程序是ntloader:
windows: ntloader,僅是啟動OS
Linux:功能豐富,提供菜單,允許用戶選擇要啟動系統或不同的內核版本;把用戶選定的內核裝載到內存中的特定空間中,解壓、展開,并把系統控制權移交給內核
由于linux的grub提供了豐富的功能,而MBR的bootloader僅僅446個字節,所以grub有分成了兩個階段,其中階段二又有兩部分(stage1_5,stage2):
stage1:bootloader,446個字節,作用就是為了引導下一個階段
stage1_5:此階段的文件在初次安裝系統時生成,只要是boot分區的文件系統驅動程序,作用是為了加載boot分區
[19:13 root@Centos6.8/etc/init.d]# ll /boot/grub/*5 -rw-r--r--. 1 root root 13428 Jul 20 02:24 /boot/grub/e2fs_stage1_5 -rw-r--r--. 1 root root 12636 Jul 20 02:24 /boot/grub/fat_stage1_5 -rw-r--r--. 1 root root 11780 Jul 20 02:24 /boot/grub/ffs_stage1_5 -rw-r--r--. 1 root root 11772 Jul 20 02:24 /boot/grub/iso9660_stage1_5 -rw-r--r--. 1 root root 13284 Jul 20 02:24 /boot/grub/jfs_stage1_5 -rw-r--r--. 1 root root 11972 Jul 20 02:24 /boot/grub/minix_stage1_5 -rw-r--r--. 1 root root 14428 Jul 20 02:24 /boot/grub/reiserfs_stage1_5 -rw-r--r--. 1 root root 12040 Jul 20 02:24 /boot/grub/ufs2_stage1_5 -rw-r--r--. 1 root root 11380 Jul 20 02:24 /boot/grub/vstafs_stage1_5 -rw-r--r--. 1 root root 13980 Jul 20 02:24 /boot/grub/xfs_stage1_5
stage2:此階段的作用是提供選擇菜單(如果有多個內核系統),引導啟動下一步的kernel內核啟動和加載initramfs文件(作用是提供真正根分區的文件系統驅動程序,以便能加載根分區),其配置文件為/etc/grub.conf
[19:16 root@Centos6.8/etc/init.d]# cat /etc/grub.conf # grub.conf generated by anaconda # # Note that you do not have to rerun grub after making changes to this file # NOTICE: You have a /boot partition. This means that # all kernel and initrd paths are relative to /boot/, eg. # root (hd0,0) # kernel /vmlinuz-version ro root=/dev/sda2 # initrd /initrd-[generic-]version.img #boot=/dev/sda default=0 timeout=5 splashimage=(hd0,0)/grub/splash.xpm.gz hiddenmenu title CentOS 6 (2.6.32-642.el6.x86_64) root (hd0,0) kernel /vmlinuz-2.6.32-642.el6.x86_64 ro root=UUID=81d6eec4-2af7-4166-bfb6-092432abb4c0 rd_NO_LUKS rd_NO_LVM.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=128M KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet initrd /initramfs-2.6.32-642.el6.x86_64.img
說明: default:設定默認啟動菜單項,默認為0開始 timeout:選擇啟動內核的停留時間 splashimage:啟動的背景圖片 hiddenmenu:隱藏選擇菜單 title:啟動時看到的選擇內核的名稱 root(hd0,0):第一塊硬盤,第一個分區,根系統的分區位置 kernel:內核文件的路徑,此時會把boot文件當跟文件,所以不需要寫成/boot/PATH initrd:根分區文件系統的驅動程序路徑 由于在grub階段只能進入了boot分區,所以kernel內核和initramfs文件都存在在boot分區
4、kernel
kernel內核階段會把內核文件進行解壓,初始化系統中各設備并做相關的配置工作,其中包括CPU、I/O、存儲設備等。關于Linux的設備驅動程序的加載,有一部分驅動程序直接被編譯進內核鏡像中,另一部分驅動程序則是以模塊的形式放在initrd(ramdisk)中。
initramfs的作用前面已經說明,下一步就是加載真正的/根分區
5、/sbin/init
(1)進入根分區后,會運行第一個進程init,PID為1,/sbin/init進程是系統其他所有進程的父進程,當它接管了系統的控制權先之后,它首先會去讀取/etc/inittab文件來執行相應的腳本進行系統初始化
[19:18 root@Centos6.8/etc/init.d]# cat /etc/inittab # inittab is only used by upstart for the default runlevel. # # ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. # # System initialization is started by /etc/init/rcS.conf # # Individual runlevels are started by /etc/init/rc.conf # # Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf # # Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf, # with configuration in /etc/sysconfig/init. # # For information on how to write upstart event handlers, or how # upstart works, see init(5), init(8), and initctl(8). # # Default runlevel. The runlevels used 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:
運行級別:為系統運行或維護等目的而設定;0-6:7個級別
0:關機
1:單用戶模式(root自動登錄), single, 維護模式,該模式就是所謂的破解root密碼
2: 多用戶模式,啟動網絡功能,但不會啟動NFS;維護模式
3:多用戶模式,正常模式;文本界面
4:預留級別;可同3級別
5:多用戶模式,正常模式;圖形界面
6:重啟
默認級別:3, 5
切換級別:init#
查看級別:runlevel; who -r
[20:20 root@Centos6.8/etc/init.d]# runlevel N 5 #N表示上次運行的級別,5表示切換的本次的運行級別
(2)運行/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) 清理操作
(3)sysinit腳本執行完成,系統就可以順利工作了,只是還需要啟動系統所需要的各種服務,這樣主機才可以提供相關的網絡和主機功能,因此還會根據相應的運行級別,運行相應的系統服務。
執行/etc/rc.d/rc腳本。該文件定義了服務啟動的順序是先K后S,而具體的每個運行級別的服務狀態是放在/etc/rc.d/rc*.d(*=0~6)目錄下,所有的文件均是指向/etc/init.d下相應文件的符號鏈接。rc.sysinit通過分析/etc/inittab文件來確定系統的啟動級別,然后才去執行/etc/rc.d/rcN.d下的文件。
以rc3.d為例:
/etc/rc.d/rc3.d目錄,該目錄下的內容全部都是以 S 或 K 開頭的鏈接文件,都鏈接到"/etc/rc.d/init.d"目錄下的各種shell腳本。S表示的是啟動時需要start的服務內容,K表示關機時需要關閉的服務內容,后面的數字代表優先次序。
[19:45 root@Centos6.8/etc/init.d]# ll /etc/rc.d/rc3.d/ total 0 lrwxrwxrwx. 1 root root 16 Jul 20 02:20 K01smartd -> ../init.d/smartd lrwxrwxrwx. 1 root root 17 Jul 20 02:18 K02oddjobd -> ../init.d/oddjobd lrwxrwxrwx. 1 root root 17 Jul 20 02:22 K05wdaemon -> ../init.d/wdaemon lrwxrwxrwx. 1 root root 16 Jul 20 02:21 K10psacct -> ../init.d/psacct lrwxrwxrwx. 1 root root 19 Jul 20 02:18 K10saslauthd -> ../init.d/saslauthd lrwxrwxrwx. 1 root root 22 Jul 20 02:18 K15htcacheclean -> ../init.d/htcacheclean lrwxrwxrwx. 1 root root 15 Jul 20 02:18 K15httpd -> ../init.d/httpd lrwxrwxrwx. 1 root root 18 Aug 26 19:04 K15svnserve -> ../init.d/svnserve lrwxrwxrwx. 1 root root 24 Jul 20 02:20 K30spice-vdagentd -> ../init.d/spice-vdagentd lrwxrwxrwx. 1 root root 16 Jul 20 02:18 K36mysqld -> ../init.d/mysqld lrwxrwxrwx. 1 root root 17 Jul 20 02:17 K50dnsmasq -> ../init.d/dnsmasq lrwxrwxrwx. 1 root root 13 Jul 20 02:16 K60nfs -> ../init.d/nfs lrwxrwxrwx. 1 root root 18 Jul 20 02:20 K61nfs-rdma -> ../init.d/nfs-rdma lrwxrwxrwx. 1 root root 20 Jul 20 02:16 K69rpcsvcgssd -> ../init.d/rpcsvcgssd lrwxrwxrwx. 1 root root 17 Jul 20 02:16 K73winbind -> ../init.d/winbind lrwxrwxrwx. 1 root root 14 Jul 20 03:15 K74ntpd -> ../init.d/ntpd lrwxrwxrwx. 1 root root 17 Jul 20 02:17 K75ntpdate -> ../init.d/ntpdate lrwxrwxrwx. 1 root root 19 Jul 20 02:20 K75quota_nld -> ../init.d/quota_nld lrwxrwxrwx. 1 root root 16 Jul 20 02:17 K76ypbind -> ../init.d/ypbind lrwxrwxrwx. 1 root root 24 Sep 1 17:09 K84NetworkManager -> ../init.d/NetworkManager lrwxrwxrwx. 1 root root 24 Jul 20 02:17 K84wpa_supplicant -> ../init.d/wpa_supplicant lrwxrwxrwx. 1 root root 21 Jul 20 02:15 K87restorecond -> ../init.d/restorecond lrwxrwxrwx. 1 root root 14 Jul 20 02:16 K88sssd -> ../init.d/sssd lrwxrwxrwx. 1 root root 20 Jul 20 02:15 K89netconsole -> ../init.d/netconsole lrwxrwxrwx. 1 root root 15 Jul 20 02:15 K89rdisc -> ../init.d/rdisc lrwxrwxrwx. 1 root root 22 Jul 20 02:17 K92pppoe-server -> ../init.d/pppoe-server lrwxrwxrwx. 1 root root 19 Jul 20 03:15 K95firstboot -> ../init.d/firstboot lrwxrwxrwx. 1 root root 14 Jul 20 02:20 K95rdma -> ../init.d/rdma lrwxrwxrwx. 1 root root 14 Jul 20 02:20 K99rngd -> ../init.d/rngd lrwxrwxrwx. 1 root root 17 Jul 20 02:19 S01sysstat -> ../init.d/sysstat lrwxrwxrwx. 1 root root 22 Jul 20 02:20 S02lvm2-monitor -> ../init.d/lvm2-monitor lrwxrwxrwx. 1 root root 19 Jul 20 02:18 S08ip6tables -> ../init.d/ip6tables lrwxrwxrwx. 1 root root 18 Jul 20 02:15 S08iptables -> ../init.d/iptables lrwxrwxrwx. 1 root root 17 Jul 20 23:42 S10network -> ../init.d/network lrwxrwxrwx. 1 root root 16 Jul 20 02:21 S11auditd -> ../init.d/auditd lrwxrwxrwx. 1 root root 21 Jul 20 02:12 S11portreserve -> ../init.d/portreserve lrwxrwxrwx. 1 root root 17 Jul 20 02:17 S12rsyslog -> ../init.d/rsyslog lrwxrwxrwx. 1 root root 18 Jul 20 02:20 S13cpuspeed -> ../init.d/cpuspeed lrwxrwxrwx. 1 root root 20 Jul 20 02:18 S13irqbalance -> ../init.d/irqbalance lrwxrwxrwx. 1 root root 17 Jul 20 02:13 S13rpcbind -> ../init.d/rpcbind lrwxrwxrwx. 1 root root 19 Jul 20 02:16 S15mdmonitor -> ../init.d/mdmonitor lrwxrwxrwx. 1 root root 20 Jul 20 02:13 S22messagebus -> ../init.d/messagebus lrwxrwxrwx. 1 root root 17 Jul 20 02:18 S24nfslock -> ../init.d/nfslock lrwxrwxrwx. 1 root root 16 Jul 20 02:17 S24openct -> ../init.d/openct lrwxrwxrwx. 1 root root 17 Jul 20 02:18 S24rpcgssd -> ../init.d/rpcgssd lrwxrwxrwx. 1 root root 26 Jul 20 02:20 S25blk-availability -> ../init.d/blk-availability lrwxrwxrwx. 1 root root 14 Jul 20 02:15 S25cups -> ../init.d/cups lrwxrwxrwx. 1 root root 15 Jul 20 02:15 S25netfs -> ../init.d/netfs lrwxrwxrwx. 1 root root 15 Jul 20 02:20 S26acpid -> ../init.d/acpid lrwxrwxrwx. 1 root root 19 Jul 20 02:17 S26haldaemon -> ../init.d/haldaemon lrwxrwxrwx. 1 root root 19 Jul 20 02:15 S26udev-post -> ../init.d/udev-post lrwxrwxrwx. 1 root root 15 Jul 20 02:17 S27pcscd -> ../init.d/pcscd lrwxrwxrwx. 1 root root 16 Jul 20 02:18 S28autofs -> ../init.d/autofs lrwxrwxrwx. 1 root root 19 Jul 20 02:18 S50bluetooth -> ../init.d/bluetooth lrwxrwxrwx. 1 root root 15 Jul 20 03:15 S50kdump -> ../init.d/kdump lrwxrwxrwx. 1 root root 14 Jul 20 02:20 S55sshd -> ../init.d/sshd lrwxrwxrwx. 1 root root 13 Aug 31 21:43 S80cmd -> ../init.d/cmd lrwxrwxrwx. 1 root root 17 Jul 20 02:18 S80postfix -> ../init.d/postfix lrwxrwxrwx. 1 root root 19 Jul 20 02:13 S82abrt-ccpp -> ../init.d/abrt-ccpp lrwxrwxrwx. 1 root root 15 Jul 20 02:13 S82abrtd -> ../init.d/abrtd lrwxrwxrwx. 1 root root 16 Sep 7 16:52 S88testrv -> ../init.d/testrv lrwxrwxrwx. 1 root root 15 Jul 20 02:18 S90crond -> ../init.d/crond lrwxrwxrwx. 1 root root 13 Jul 20 02:17 S95atd -> ../init.d/atd lrwxrwxrwx. 1 root root 20 Jul 20 02:18 S99certmonger -> ../init.d/certmonger lrwxrwxrwx. 1 root root 11 Jul 20 02:15 S99local -> ../rc.local
? 注意:正常級別下,最后啟動一個服務S99local沒有鏈接至/etc/rc.d/init.d一個服務腳本,而是指向了/etc/rc.d/rc.local腳本
? 不便或不需寫為服務腳本放置于/etc/rc.d/init.d/目錄,且又想開機時自動運行的命令,可直接放置于/etc/rc.d/rc.local文件中
6、等待用戶登錄
完成了系統服務的啟動后,linux會啟動終端或X-Window來等待用戶登錄,會執行"/sbin/mingetty",而且執行了6個,所以linux會有6個純文本終端(tty1,tty2…),mingetty就是啟動終端的命令。至此,系統就啟動完畢了!
[19:51 root@Centos6.8/etc/init.d]# cat /etc/init/tty.conf # tty - getty # # This service maintains a getty on the specified device. # # Do not edit this file directly. If you want to change the behaviour, # please create a file tty.override and put your changes there. stop on runlevel [S016] respawn instance $TTY exec /sbin/mingetty $TTY usage 'tty TTY=/dev/ttyX - where X is console id'
綜述:POST –> Boot Sequence(BIOS) –> Boot Loader –> Kernel(ramdisk) –> rootfs–> switchroot–> /sbin/init–>(/etc/inittab, /etc/init/*.conf) –> 設定默認運行級別–> 系統初始化腳本rc.sysinit–> 關閉或啟動對應級別的服務–> 啟動終端
可參考下圖(圖片來自網絡)
二、系統服務管理
根據上面說明,我們可以知道,系統在啟動后會根據想要的運行級別,運行相應的服務腳本,而這些系統服務的管理,可以用chkconfig來進行管理使用,這些服務腳本放置于/etc/rc.d/init.d中
chkconfig:跟新和確認系統服務信息
語法:chkconfig [–list] [–type type][name]:查看所有的服務列表詳細信息
chkconfig –add name :添加服務到系統服務
chkconfig –del name:刪除系統服務
chkconfig –override name
chkconfig [–level levels] [–type type] name <on|off|reset|resetpriorities>
chkconfig [–level levels] [–type type] name
[19:58 root@Centos6.8/etc/init.d]# chkconfig --list NetworkManager 0:off 1:off 2:off 3:off 4:off 5:off 6:off abrt-ccpp 0:off 1:off 2:off 3:on 4:off 5:on 6:off abrtd 0:off 1:off 2:off 3:on 4:off 5:on 6:off acpid 0:off 1:off 2:on 3:on 4:on 5:on 6:off atd 0:off 1:off 2:off 3:on 4:on 5:on 6:off auditd 0:off 1:off 2:on 3:on 4:on 5:on 6:off autofs 0:off 1:off 2:off 3:on 4:on 5:on 6:off blk-availability 0:off 1:on 2:on 3:on 4:on 5:on 6:off bluetooth 0:off 1:off 2:off 3:on 4:on 5:on 6:off certmonger 0:off 1:off 2:off 3:on 4:on 5:on 6:off cmd 0:off 1:off 2:on 3:on 4:on 5:on 6:off cpuspeed 0:off 1:on 2:on 3:on 4:on 5:on 6:off crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off cups 0:off 1:off 2:on 3:on 4:on 5:on 6:off dnsmasq 0:off 1:off 2:off 3:off 4:off 5:off 6:off firstboot 0:off 1:off 2:off 3:off 4:off 5:off 6:off haldaemon 0:off 1:off 2:off 3:on 4:on 5:on 6:off htcacheclean 0:off 1:off 2:off 3:off 4:off 5:off 6:off httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off ip6tables 0:off 1:off 2:on 3:on 4:on 5:on 6:off iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off irqbalance 0:off 1:off 2:off 3:on 4:on 5:on 6:off kdump 0:off 1:off 2:on 3:on 4:on 5:on 6:off lvm2-monitor 0:off 1:on 2:on 3:on 4:on 5:on 6:off mdmonitor 0:off 1:off 2:on 3:on 4:on 5:on 6:off messagebus 0:off 1:off 2:on 3:on 4:on 5:on 6:off mysqld 0:off 1:off 2:off 3:off 4:off 5:off 6:off netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off netfs 0:off 1:off 2:off 3:on 4:on 5:on 6:off network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
三、手動配置系統啟動服務
1、系統服務腳本特點:基本都可以提供start|stop|restart|status這幾個狀態值的接收
2、服務腳本存放位置
由上面的介紹可知,所有的系統服務腳本都放在/etc/rc.d/init.d文件夾中
[20:04 root@Centos6.8/etc/init.d]# ls abrt-ccpp bluetooth functions kdump netfs oddjobd rdisc rsyslog svnserve abrtd certmonger haldaemon killall network openct rdma sandbox sysstat abrt-oops cmd halt lvm2-lvmetad NetworkManager pcscd restorecond saslauthd udev-post acpid cpuspeed htcacheclean lvm2-monitor nfs portreserve rngd single wdaemon atd crond httpd mdmonitor nfslock postfix rpcbind smartd winbind auditd cups ip6tables messagebus nfs-rdma pppoe-server rpcgssd spice-vdagentd wpa_supplicant autofs dnsmasq iptables mysqld ntpd psacct rpcidmapd sshd ypbind blk-availability firstboot irqbalance netconsole ntpdate quota_nld rpcsvcgssd sssd
3、服務腳本的編寫格式
打開其中一個查看服務腳本的開頭格式
[20:04 root@Centos6.8/etc/init.d]# cat atd #!/bin/sh # # atd Starts/stop the "at" daemon # # chkconfig: 345 95 5 # description: Runs commands scheduled by the "at" command at the time \ # specified when "at" was run, and runs batch commands when the load \ # average is low enough. ### BEGIN INIT INFO # Provides: atd at batch # Required-Start: $local_fs # Required-Stop: $local_fs # Default-Start: 345 # Default-Stop: 95 # Short-Description: Starts/stop the "at" daemon # Description: Runs commands scheduled by the "at" command at the time # specified when "at" was run, and runs batch commands when the load # average is low enough. ### END INIT INFO
其中,有三行格式是必須要寫上的#!/bin/bash,#/chkconfig,#description
# chkconfig: runlevels SS KK【SS:表示開始運行的次序,KK:表示關閉程序的次序,-表示都不啟動】
因此可以手動編寫系統服務:
(1)把服務腳本放置/etc/init.d目錄下
(2)使用chkconfig –add 添加到系統服務
(3)重啟系統讓服務生效
[20:17 root@Centos6.8/etc/init.d]# cat !$ cat testsrv #!/bin/bash #chkconfig 35 88 22 #description: test the system service testsrv () { case $1 in start) touch /var/lock/subsys/`basename $0` &> /dev/null echo "start OK" ;; stop) rm -rf /var/lock/subsys/`basename $0` &> /dev/null echo "stop OK" ;; restart) rm -rf /var/lock/subsys/`basename $0` &> /dev/null touch /var/lock/subsys/`basename $0` &> /dev/null echo "restart OK" ;; status) [ -e /var/lock/subsys/`basename $0` ] && echo "is running" || echo "is stopping" ;; esac } CHOICE=$1 ! [[ "$CHOICE" =~ start|stop|restart|status ]] && echo "useage:start|stop|restart|status" && exit 100 testsrv $CHOICE [20:20 root@Centos6.8/etc/init.d]# chkconfig --add testsrv [20:20 root@Centos6.8/etc/init.d]# chkconfig --list testsrv testsrv 0:off 1:off 2:off 3:on 4:off 5:on 6:off [20:20 root@Centos6.8/etc/init.d]# service testsrv status is stopping [20:20 root@Centos6.8/etc/init.d]# service testsrv start start OK [20:20 root@Centos6.8/etc/init.d]# service testsrv status is running
原創文章,作者:麥德良,如若轉載,請注明出處:http://www.www58058.com/44483
總結的很詳細,贊一個