1. 程式人生 > >centos7 啟動流程

centos7 啟動流程


systemdPOST --> Boot Sequence --> Bootloader --> kernel + initramfs(initrd) --> rootfs --> /sbin/initinit: CentOS 5: SysV initCentOS 6: UpstartCentOS 7: SystemdSystemd:系統啟動和伺服器守護程序管理器,負責在系統啟動或執行時,啟用系統資源,伺服器程序和其它程序Systemd新特性:系統引導時實現服務並行啟動按需啟動守護程序 (儘可能的啟動必要的服務)自動化的服務依賴關係管理同時採用socket式與D-Bus匯流排式啟用服務系統狀態快照核心概念
:unitunit表示不同型別的systemd物件,通過配置檔案進行標識和配置;檔案中主要包含了系統服務、監聽socket、儲存的系統快照以及其它與init相關的資訊配置檔案:/usr/lib/systemd/system:每個服務最主要的啟動指令碼設定,類似於之前的/etc/init.d/ 【存放了不同型別的unit單元】/run/systemd/system:系統執行過程中所產生的服務指令碼,比上面目錄優先執行/etc/systemd/system:管理員建立的執行指令碼,類似於/etc/rc.d/rcN.d/Sxx類的功能,比上面目錄優先執行unit型別:systemctl –t help 檢視unit型別Service unit: 副檔名為.service, 用於定義系統服務Target unit: 副檔名為.target,用於模擬實現執行級別Device unit: .device, 用於定義核心識別的裝置Mount unit: .mount, 定義檔案系統掛載點Socket unit: .socket, 用於標識程序間通訊用的socket檔案,也可在系統啟動時,延遲啟動服務,實現按需啟動Snapshot unit: .snapshot, 管理系統快照Swap unit: .swap, 用於標識swap裝置Automount unit: .automount,檔案系統的自動掛載點 Path unit: .path,用於定義檔案系統中的一個檔案或目錄使用,常用於當檔案系統變化時,延遲啟用服務,如:spool 目錄特性:
關鍵特性:基於socket的啟用機制:socket與服務程式分離基於d-bus的啟用機制:基於device的啟用機制:基於path的啟用機制:系統快照:儲存各unit的當前狀態資訊於持久儲存裝置中向後相容sysv init指令碼不相容:systemctl命令固定不變,不可擴充套件非由systemd啟動的服務,systemctl無法與之通訊和控制管理服務管理系統服務:CentOS 7: service unit注意:能相容早期的服務指令碼命令:systemctl COMMAND name.service 注意:systemctl 優勢,可一次性控制多個服務啟動:service name start ==> systemctl start name.service停止:service name stop ==> systemctl stop name.service重啟:service name restart ==> systemctl restart name.service狀態:service name status ==> systemctl status name.service條件式重啟:已啟動才重啟,否則不做操作service name condrestart ==> systemctl try-restart name.service過載或重啟服務:先載入,再啟動systemctl reload-or-restart name.service過載或條件式重啟服務:systemctl reload-or-try-restart name.service禁止自動和手動啟動:
systemctl mask name.service //禁止某服務啟動取消禁止:systemctl unmask name.service //取消禁止服務檢視檢視某服務當前啟用與否的狀態:systemctl is-active name.service檢視所有已經啟用的服務:systemctl list-units --type|-t service檢視所有服務:systemctl list-units --type service --all|-achkconfig命令的對應關係:設定某服務開機自啟:chkconfig name on ==> systemctl enable name.service // 若有軟連結一併建立設定某服務開機禁止啟動:chkconfig name off ==> systemctl disable name.service //若有軟連結一併刪除檢視所有服務的開機自啟狀態:chkconfig --list ==> systemctl list-unit-files --type service用來列出該服務在哪些執行級別下啟用和禁用chkconfig sshd –list ==> ls /etc/systemd/system/*.wants/sshd.service檢視服務是否開機自啟:systemctl is-enabled name.service其它命令:檢視服務的依賴關係:systemctl list-dependencies name.service殺掉程序:systemctl kill unitname服務狀態systemctl list-unit-files --type service --all顯示狀態
  • loaded:Unit配置檔案已處理
  • active(running):一次或多次持續處理的執行
  • active(exited):成功完成一次性的配置
  • active(waiting):執行中,等待一個事件
  • inactive:不執行
  • enabled:開機啟動
  • disabled:開機不啟動
  • static:開機不啟動,但可被另一個啟用的服務啟用
systemctl 命令示例:顯示所有單元狀態systemctl 或 systemctl list-units只顯示服務單元的狀態systemctl --type=service顯示sshd服務單元systemctl–l status sshd.service驗證sshd服務當前是否活動systemctl is-active sshd啟動,停止和重啟sshd服務systemctl start sshd.servicesystemctl stop sshd.servicesystemctl restart sshd.service重新載入配置systemctl reload sshd.service列出活動狀態的所有服務單元systemctl list-units --type=service列出所有服務單元systemctl list-units --type=service --all檢視服務單元的啟用和禁用狀態systemctl list-unit-files --type=service列出失敗的服務systemctl --failed --type=service列出依賴的單元systemctl list-dependencies sshd驗證sshd服務是否開機啟動systemctl is-enabled sshd禁用network,使之不能自動啟動,但手動可以systemctl disable network啟用networksystemctl enable network禁用network,使之不能手動或自動啟動systemctl mask network啟用networksystemctl unmask networkservice unit檔案格式/etc/systemd/system:系統管理員和使用者使用/usr/lib/systemd/system:發行版打包者使用以 “#” 開頭的行後面的內容會被認為是註釋相關布林值,1、yes、on、true 都是開啟,0、no、off、false 都是關閉時間單位預設是秒,所以要用毫秒(ms)分鐘(m)等須顯式說明service unit file檔案通常由三部分組成:• [Unit]:定義與Unit型別無關的通用選項;用於提供unit的描述資訊、unit行為及依賴關係等• [Service]:與特定型別相關的專用選項;此處為Service型別• [Install]:定義由“systemctl enable”以及"systemctl disable“命令在實現服務啟用或禁用時用到的一些選項service unit檔案格式Unit段的常用選項:Description:描述資訊After:定義unit的啟動次序,表示當前unit應該晚於哪些unit啟動,其功能與Before相反Requires:依賴到的其它units,強依賴,被依賴的units無法啟用時,當前unit也無法啟用Wants:依賴到的其它units,弱依賴Conflicts:定義units間的衝突關係 Service段的常用選項:Type:定義影響ExecStart及相關引數的功能的unit程序啟動型別• simple:預設值,這個daemon主要由ExecStart接的指令串來啟動,啟動後常駐於記憶體中• forking:由ExecStart啟動的程式透過spawns延伸出其他子程式來作為此daemon的主要服務。原生父程式在啟動結束後就會終止• oneshot:(一次性)與simple類似,不過這個程式在工作完畢後就結束了,不會常駐在記憶體中• dbus:與simple類似,但這個daemon必須要在取得一個D-Bus的名稱後,才會繼續運作.因此通常也要同時設定BusNname= 才行• notify:在啟動完成後會發送一個通知訊息。還需要配合 NotifyAccess 來讓 Systemd接收訊息• idle:與simple類似,要執行這個daemon必須要所有的工作都順利執行完畢後才會執行。這類的daemon通常是開機到最後才執行即可的服務EnvironmentFile:環境配置檔案ExecStart:指明啟動unit要執行命令或指令碼的絕對路徑ExecStartPre: ExecStart前執行ExecStartPost: ExecStart後執行ExecStop:指明停止unit要執行的命令或指令碼Restart:當設定Restart=1 時,則當次daemon服務意外終止後,會再次自動啟動此服務Install段的常用選項:• Alias:別名,可使用systemctl command Alias.service• RequiredBy:被哪些units所依賴,強依賴• WantedBy:被哪些units所依賴,弱依賴• Also:安裝本服務的時候還要安裝別的相關服務注意:對於新建立的unit檔案,或者修改了的unit檔案,要通知systemd過載此配置檔案,而後可以選擇重啟 systemctl daemon-reload(重新載入才生效)服務Unit檔案示例:vim /etc/systemd/system/bak.service[Unit]Description=backup /etcRequires=atd.service[Service]Type=simpleExecStart=/bin/bash -c "echo /testdir/bak.sh |at now"[Install]WantedBy=multi-user.target注意:在這需手動新增bak.sh指令碼systemctl daemon-reload //重新載入使之生效systemctl start bak //啟動執行級別:target units:unit配置檔案:.targetls /usr/lib/systemd/system/*.targetsystemctl list-unit-files --type target --all執行級別:0 ==> runlevel0.target, poweroff.target1 ==> runlevel1.target, rescue.target2 ==> runlevel2.target, multi-user.target3 ==> runlevel3.target, multi-user.target4 ==> runlevel4.target, multi-user.target5 ==> runlevel5.target, graphical.target6 ==> runlevel6.target, reboot.target檢視依賴性:systemctl list-dependencies graphical.target //檢視圖形的target依賴情況級別切換:init N ==> systemctl isolate name.targetsystemctl isolate multi-user.target注:只有/lib/systemd/system/*.target檔案中AllowIsolate=yes 才能切換(修改檔案需執行systemctl daemon-reload才能生效)檢視target:runlevel ; who -rsystemctl list-units --type target獲取預設執行級別:/etc/inittab ==> systemctl get-default修改預設級別:/etc/inittab ==> systemctl set-default name.targetsystemctl set-default multi-user.targetls –l /etc/systemd/system/default.target其他命令:切換至緊急救援模式:systemctl rescue切換至emergency模式:systemctl emergency其它常用命令:傳統命令init,poweroff,halt,reboot都成為systemctl的軟連結
  • 關機:systemctl halt、systemctl poweroff
  • 重啟:systemctl reboot
  • 掛起:systemctl suspend
  • 休眠:systemctl hibernate
  • 休眠並掛起:systemctl hybrid-sleep
centos7引導順序
  • UEFi或BIOS初始化,執行POST開機自檢
  • 選擇啟動裝置
  • 引導裝載程式, centos7是grub2
  • 載入裝載程式的配置檔案:/etc/grub.d/ /etc/default/grub
/boot/grub2/grub.cfg
  • 載入initramfs驅動模組
  • 載入核心選項
  • 核心初始化,centos7使用systemd代替init
  • 執行initrd.target所有單元,包括掛載/etc/fstab
  • 從initramfs根檔案系統切換到磁碟根目錄
  • systemd執行預設target配置,配置檔案/etc/systemd/system/default.target
  • systemd執行sysinit.target初始化系統及basic.target準備作業系統
  • systemd啟動multi-user.target下的本機與伺服器服務
  • systemd執行multi-user.target下的/etc/rc.d/rc.local
  • Systemd執行multi-user.target下的getty.target及登入服務
  • systemd執行graphical需要的服務
設定核心引數
  • 設定核心引數,隻影響當次啟動
  • 啟動時,在linux16行後新增systemd.unit=desired.target //修改執行模式
  • systemd.unit=emergency.target
  • systemd.unit=rescue.target
  • rescue.target 比emergency 支援更多的功能,例如日誌等
  • systemctl default 進入預設target
啟動排錯:檔案系統損壞先嚐試自動修復,失敗則進入emergency shell,提示使用者修復在/etc/fstab不存在對應的裝置和UUID等一段時間,如不可用,進入emergency shell在/etc/fstab不存在對應掛載點systemd 嘗試建立掛載點,否則提示進入emergency shell.在/etc/fstab不正確的掛載選項 提示進入emergency shell一.破解root口令方法1: 啟動時任意鍵暫停啟動按e鍵進入編輯模式將游標移動linux16開始的行,新增核心引數rd.break按ctrl + x啟動mount –o remount,rw /sysroot //重新掛載,給寫許可權chroot /sysroot 切根passwd root 修改口令touch /.autorelabel 如若SELinux是啟動的,必須建立該檔案exitreboot方法2: 啟動時任意鍵暫停啟動按e鍵進入編輯模式將游標移動linux16開始的行,改為rw init=/sysroot/bin/sh按ctrl + x啟動chroot /sysrootpasswd roottouch /.autorelabel exitreboot修復grub2GRUB“the Grand Unified Bootloader”引導提示時可以使用命令列介面可從檔案系統引導主要配置檔案 /boot/grub2/grub.cfg修復配置檔案grub2-mkconfig > /boot/grub2/grub.cfg修復grubgrub2-install /dev/sda BIOS環境grub2-install UEFI環境調整預設啟動核心vim /etc/default/grubGRUB_DEFAULT=0grub2-mkcofig -o /boot/grub2/grub.cfg //重新生成grub.conf檔案二.刪除/boot/grub2下所有檔案方法:1.修復grub2檔案:進入救援模式 ;chroot /mnt/sysimagegrub2-install /dev/sda //恢復grub2下的檔案(缺少grub.conf 檔案)2. 修復grub.conf 檔案重啟,insmod xfx 載入xfs驅動set root=(hd0,1)linux16 /vmlinuz-xxx root=/dev/sda2 selinux=0linitrd16 /initrafs-xxxx三.刪除/boot/*方法:進入救援模式,chroot /mnt/sysimage mount /dev/sr0 /mnt (沒掛就掛載光碟) rpm -ivh /mnt/Packages/kernel-xxx --force grub2-install /dev/sda grub2-mkcofig -o /boot/grub2/grub.cfg //重新生成grub.conf檔案四.刪除軟連結rm -f /etc/systemd/system/default.target;重新建立軟連結 ln -s /usr/lib/systemd/system/reboot.target /etc/systemd/system/default.target 效果:不斷重啟修復方法: 重啟,ESC,選中核心,按e鍵, 在Linux16 行的末尾寫:systemd.unit=multi-user.target ;在按ctrl+x重啟,在修改為正常