linux內核特點:
- 支持模塊化:模塊檔案的名字以.ko(kernel object)結尾
- 支持內核運行時,動態加載和卸載模塊檔案,
linux內核組成部分:
-
核心檔案:/boot/vmlinuz-VERSION-release
# ll /boot/vmlinuz-3.10.0-957.el7.x86_64 -rwxr-xr-x. 1 root root 6639904 Nov 9 2018 /boot/vmlinuz-3.10.0-957.el7.x86_64 # file /boot/vmlinuz-3.10.0-957.el7.x86_64 /boot/vmlinuz-3.10.0-957.el7.x86_64: Linux kernel x86 boot executable bzImage, version 3.10.0-957.el7.x86_64 ([email protected]) #1 S, RO-rootFS, swap_dev 0x6, Normal VGA -
模塊檔案:/lib/modules/VERSION-release
如果按照了多個內核版本,則/lib/modules下,有多個目錄
# ll /lib/modules drwxr-xr-x. 8 root root 4096 Feb 20 10:03 3.10.0-957.el7.x86_643.10.0-957.el7.x86_64目錄下的檔案:
# ls /lib/modules/3.10.0-957.el7.x86_64/ build modules.alias modules.builtin.bin modules.drm modules.softdep updates extra modules.alias.bin modules.dep modules.modesetting modules.symbols vdso kernel modules.block modules.dep.bin modules.networking modules.symbols.bin weak-updates misc modules.builtin modules.devname modules.order source其中有:kernel目錄
# ls /lib/modules/3.10.0-957.el7.x86_64/kernel/ arch crypto drivers fs kernel lib mm net sound virtarch:平臺特有;crypto:加密/解密;drivers:驅動管理;mm:記憶體管理,fs:檔案系統;net:網路管理;sound:聲卡驅動
-
ramdisk:
ramdisk的由來:作業系統不知道用戶的電腦上的磁盤設備是什么型號的,訪問特定磁盤是要使用特定的磁盤驅動程式的,所以作業系統可能就要把市面上主流的磁盤驅動程式都準備好,放入安裝盤中,這樣一來安裝盤太臃腫,為了解決這個問題,作業系統不準備任何磁盤驅動程式,而是在安裝作業系統的程序中,掃描用戶的硬體,根據硬體自動生成磁盤驅動程式,并把這個驅動程式存放入磁盤中,
開機后,內核被裝載到記憶體后,就要去從磁盤找根檔案系統,內核要想訪問磁盤,必須需要訪問磁盤的驅動程式,可是磁盤的驅動程式又在根檔案系統里,所以內核就無法找到磁盤的驅動程式,也就無法訪問磁盤,所以需要一個臨時的根檔案系統,在里面放磁盤的驅動程式,這個臨時的驅動程式和根檔案系統,是在安裝作業系統時,由安裝程式掃描機器的硬體后,根據硬體的型號自動生成的,有了這個東西后,內核就先把一段記憶體模擬成磁盤,把這個東西放到記憶體中,從它的里面取得到磁盤驅動,然后使用好不容易得到的驅動程式,去訪問真的磁盤,得到真的根檔案系統,然后真的根檔案系統,取代這個東西,
這個東西叫:ramdisk,
我們使用
free命令,可以看到buff和cache,為什么有buff和cache呢?因為磁盤IO太慢,所以把經常訪問的磁盤上的資料,載入記憶體的buff和cache區域,# free total used free shared buff/cache available Mem: 3880164 459420 2525300 12596 895444 3076684 Swap: 4063228 0 4063228centos5:/boot/initrd-VERSION-release.img
由于是用記憶體模擬磁盤,所以記憶體又使用buff和cache機制,快取了磁盤的內容,造成了雙重buff和cache,
centos6,7:/boot/initramfsVERSION-release.img
為了防止雙重buffer和cache,centos6和7使用ramfs,ramfs是檔案系統,就防止了雙重buff和cache,
# ll /boot/initramfs-3.10.0-957.el7.x86_64.img -rw-------. 1 root root 31489644 Nov 29 17:11 /boot/initramfs-3.10.0-957.el7.x86_64.imgramdisk的做成工具程式:
- centos5:
mkinitrd - centos6,7:
dracut,為了兼容centos5,也可以使用mkinitrd
- centos5:
centos5系統啟動流程
前提pc主機,MBR架構
-
第一步:post(power on system test)加電自檢,
pc機的主板上有個rom芯片(CMOS),加電后,cpu去找這個raw,然后讀取里面的指令,檢測機器上是否有:記憶體,硬碟,顯示設備等,
CMOS里有個bios(basic input output system)程式
-
第二步:boot sequence(bios里設定是用光碟啟動,還是硬碟啟動等)
按次序查找引導設備,第一個有引導程式(bootloader)的設備即為本次啟動要用到的設備,
bootloader:
-
功能:
- 找到磁盤上的grub程式,并加載運行之,
- grub提供一個可以讓用戶選擇的選單,上面寫著,可以選擇運行的內核串列
- 把用戶選定的內核程式從磁盤加載到記憶體的特定空間中,然后解壓,展開,此后,內核就開始真正運行起來了,然后bootloader退出,由內核接管一切,
注意:由于bootloader僅有446位元組,它無法讀取LVM,軟RAID的邏輯磁區,只能讀取物理磁區,所以內核程式只能存放在物理磁區上,
-
種類:
-
LILO:linux loader,它有個致命弱點,如果內核在磁盤的1024以后的柱面上存盤著的話,它無法加載內核,安卓手機用的是LILO,
-
GRUB:Grand Uniform Bootloader
centos5,6用的版本是:grub 0.x(別名:grub legacy)
cengtos7用的版本是:grub 1.x(別名:grub2)
-
bootloader程式放在哪里了?
如果是MBR架構,則放在了在0號track(磁道),0號sector(扇區)里 的前446bytes ,
由于只有446位元組,空間太小了,能寫的程式實在有限,所以linux使用GRUB機制,
GRUB機制:不讓bootloader直接加載內核,而是讓bootloader加載磁盤上的另外一個程式/boot/grub,由于/boot/grub是放在磁盤上的,所以突破了446位元組的約束,
- 第一階段:bootloader加載/boot/grub程式
- 第1.5階段:filesystem driver?
- 第二階段:/boot/grub程式加載內核,
-
-
第三步:kernel自身初始化
-
探測所以硬體設備
-
為了加載磁盤上的根檔案系統,所以先加載ramdisk上的檔案系統,找到里面的磁盤驅動程式,
注意:也有可能不使用ramdisk,當自己在自己的機器上編譯內核時,編譯程式就探測到了本地磁盤型別,所以在編譯的時候,就可以把磁盤的驅動,編譯到內核里,所以內核就不需要再去找ramdisk了,
-
使用磁盤驅動,以只讀方式,加載根檔案系統,只讀的目的:防止內核有bug,把根檔案系統里面的東西洗掉了,沒問題后,再改為讀寫方式,
-
運行用戶空間的第一個應用程式:/sbin/init
centos5之前的init程式:SysV init
? 使用的組態檔:/etc/inittab
centos6的init程式:Upstart
? 使用的組態檔:/etc/init/*.conf
centos7的init程式:Systemd
? 使用的組態檔: /usr/lib/systemd/system目錄下的檔案,和/etc/systemd/system目錄下的檔案
-
-
第四步:/sbin/init會啟動/sbin/mingetty程式,顯示可以登錄的文本界面,
Linux的運行級別
為了系統的維護等目的設定了7個級別(0~6)
- 級別0:關機 shutdown
- 級別1:單用戶模式(single user),以root用戶登錄,無需輸入root密碼,root密碼忘記了,使用級別1,輸入維護模式,
- 級別2:多用戶模式(multi user),會啟動網路功能,但不會啟動NFS,屬于維護模式,
- 級別3:多用戶模式(multi user),完全功能模式,但不啟動圖形界面,
- 級別4:預留級別,沒有被使用,但習慣以同3級別功能使用,
- 級別5:多用戶模式(multi user),完全功能模式,開機自動啟動圖形界面,
- 級別6:重啟 reboot
默認級別是3或5,server用級別3;個人主機用級別5.
切換級別的命令:init 級別
查看當前的級別:who -r或者runlevel
run命令的結果里的N:上一次的級別,由于沒切換過級別,所以上一次就是N,
# who -r
run-level 5 2020-02-20 10:03
# runlevel
N 5
init程式的組態檔說明
1,centos5,6:/etc/inittab
每行定義一種action,以及與之對應的process
每一行的格式:id:runlevel:action:process
id:標識
runlevel:運行級別
action:指明啟動process的條件
- wait:當運行級別切換至此行的級別時,執行一次process
- respawn:一旦此行的process終止,就自動重新啟動它
- initdefault:設定系統開機時默認的運行級別,所以此行的process省略
- sysinit:設定系統初始化的方式,所以runlevel省略,process一般為/etc/rc.d/rc.sysinit腳本
process:程式
例子:
-
id01:3:initdefault:
系統開機時,以級別3運行,
-
id02::sysinit:/etc/rc.d/rc.sysinit
不管以哪個級別運行,都使用/etc/rc.d/rc.sysinit腳本完成系統初始化,
-
id03:3:wait:/etc/rc.d/rc 0
當切換到級別3后,運行腳本:/etc/rc.d/rc 0一次,
腳本/etc/rc.d/rc的作用:當切換運行級別時,定義先kill哪些行程,然后再啟動哪些行程,
- 腳本引數:運行級別
解釋腳本/etc/rc.d/rc之前,先看看目錄/etc/rc.d/下的構成,
下面有rc0.d,rc1.d,rc2.d,rc3.d,rc4.d,rc5.d,rc6.d目錄
# ll /etc/rc.d/
drwxr-xr-x. 2 root root 4096 Feb 21 09:19 init.d
-rwxr-xr-x. 1 root root 2617 Jun 19 2018 rc
drwxr-xr-x. 2 root root 4096 Feb 21 09:19 rc0.d
drwxr-xr-x. 2 root root 4096 Feb 21 09:19 rc1.d
drwxr-xr-x. 2 root root 4096 Feb 21 09:19 rc2.d
drwxr-xr-x. 2 root root 4096 Feb 21 09:19 rc3.d
drwxr-xr-x. 2 root root 4096 Feb 21 09:19 rc4.d
drwxr-xr-x. 2 root root 4096 Feb 21 09:19 rc5.d
drwxr-xr-x. 2 root root 4096 Feb 21 09:19 rc6.d
-rwxr-xr-x. 1 root root 220 Jun 19 2018 rc.local
-rwxr-xr-x. 1 root root 20199 Jun 19 2018 rc.sysinit
/etc/rc.d/rc的腳本內容摘要:
變數$runlevel,就是執行此腳本時,傳進來的引數,也就是運行級別,
# First, run the KILL scripts.
for i in /etc/rc$runlevel.d/K* ; do
#省略
$i stop
#省略
done
# Now run the START scripts.
for i in /etc/rc$runlevel.d/S* ; do
#省略
$i start
#省略
done
所以當運行/etc/rc.d/rc 3的時候,實際運行的腳本的就/etc/rc3.d/目錄下的:K*和S*的所有腳本,
查看一下ls /etc/rc.d/rc3.d的目錄,發現都是符號鏈接檔案,指向的是/etc/init.d目錄下的相應的腳本,其他的rc1.d等目錄下也都是符號鏈接,而且指向的也是/etc/init.d目錄下的相應的腳本,所有不管傳的運行級別引數是什么,實際運行的腳本都是/etc/init.d下的相應的腳本,
# ll /etc/rc.d/rc3.d/K*
lrwxrwxrwx. 1 root root 15 Jan 31 14:09 /etc/rc.d/rc3.d/K01numad -> ../init.d/numad
lrwxrwxrwx. 1 root root 16 Jan 31 14:09 /etc/rc.d/rc3.d/K01smartd -> ../init.d/smartd
# ll /etc/rc.d/rc3.d/S*
lrwxrwxrwx. 1 root root 17 Jan 31 14:09 /etc/rc.d/rc3.d/S01sysstat -> ../init.d/sysstat
lrwxrwxrwx. 1 root root 22 Jan 31 14:09 /etc/rc.d/rc3.d/S02lvm2-monitor -> ../init.d/lvm2-monitor
lrwxrwxrwx. 1 root root 14 Jan 31 14:09 /etc/rc.d/rc3.d/S05rdma -> ../init.d/rdma
那么,為什么弄出這么多符號鏈接呢?
觀察這些符號鏈接,發現:K和S的后面都有2位數字,
這2位數字是為了排序用的,/etc/rc.d/rc腳本的for i in /etc/rc$runlevel.d/K* ; do陳述句,就把以K后S開頭的按后面的數字排序了,
- K##:要被結束的行程,##數字越小,先被結束,所以不被別的行程依賴的行程,數字小,
- S##:要被啟動的行程,##數字越小,先被啟動,所以被別的行程依賴的行程,數字小,
啟動服務時,都是使用service 服務程式名 start,其實service命令,呼叫的是就是/etc/init.d目錄下的腳本,所以直接運行腳本和使用service命令的效果是一樣的,試驗如下:
# /etc/init.d/crond stop
Stopping crond: [ OK ]
# /etc/init.d/crond start
Starting crond: [ OK ]
# /etc/init.d/crond status
crond (pid 4822) is running...
# /etc/init.d/crond restart
Stopping crond: [ OK ]
Starting crond: [ OK ]
# service crond status
crond (pid 4850) is running...
# service crond restart
Stopping crond: [ OK ]
Starting crond: [ OK ]
# service crond stop
Stopping crond: [ OK ]
# service crond start
Starting crond: [ OK ]
當想讓某個服務行程開機就自動運行的話,在目錄/etc/init.d下創建自己的腳本檔案:
#!/bin/bash
#
# test servvice
#
# chkconfig: 2345 90 60
# description: test service
prog=$(basename $0)
if [ $# -lt 1 ]; then
echo "Usage:$prog {start|stop|status|restart}"
exit 1
fi
if [ "$1" == "start" ]; then
echo "start $prog done"
elif [ "$1" == "stop" ]; then
echo "stop $prog done"
elif [ "$1" == "restart" ]; then
echo "restart $prog done"
elif [ "$1" == "status" ]; then
if [ pidof $prog &> /dev/null ]; then
echo "$prog is running"
else
echo "$prog is stopped"
fi
else
echo "Usage:$prog {start|stop|status|restart}"
exit 2
fi
注意:chkconfig: 2345 90 60
2345:級別
90:啟動優先級
60:終止優先級
有個命令chkconfig可以幫助我們,在rc0.d,rc1.d,rc2.d,rc3.d,rc4.d,rc5.d,rc6.d目錄下自動創建符號鏈接,符號鏈接的名字:S90mySer.sh和K60mySer.sh,但不是在所以目錄都創建,根據寫的級別創建,這里寫的是2345,所以在rc2.d,rc3.d,rc4.d,rc5.d目錄下創建S90mySer.sh和K60mySer.sh
雖然【# chkconfig: 2345 90 60】是注釋,但必須有這行,否則,執行chkconfig命令出下面的錯誤:
# chkconfig mySer.sh on
service mySer.sh does not support chkconfig
使用chkconfig mySer.sh on就是在2345目錄下創建S90mySer.sh檔案
使用chkconfig mySer.sh off就是在2345目錄下創建K90mySer.sh檔案
# chkconfig mySer.sh on
# ls /etc/rc.d/rc0.d/ | grep mySer
# ls /etc/rc.d/rc1.d/ | grep mySer
# ls /etc/rc.d/rc2.d/ | grep mySer
S90mySer.sh
# ls /etc/rc.d/rc3.d/ | grep mySer
S90mySer.sh
# ls /etc/rc.d/rc4.d/ | grep mySer
S90mySer.sh
# ls /etc/rc.d/rc5.d/ | grep mySer
S90mySer.sh
# ls /etc/rc.d/rc6.d/ | grep mySer
# chkconfig mySer.sh off
# ls /etc/rc.d/rc0.d/ | grep mySer
# ls /etc/rc.d/rc1.d/ | grep mySer
# ls /etc/rc.d/rc2.d/ | grep mySer
K60mySer.sh
# ls /etc/rc.d/rc3.d/ | grep mySer
K60mySer.sh
# ls /etc/rc.d/rc4.d/ | grep mySer
K60mySer.sh
# ls /etc/rc.d/rc5.d/ | grep mySer
K60mySer.sh
# ls /etc/rc.d/rc6.d/ | grep mySer
有了這些鏈接檔案后,就可以使用service命令,來啟動我們的服務了,
# service mySer.sh start
start mySer.sh done
# service mySer.sh stop
stop mySer.sh done
# service mySer.sh restart
restart mySer.sh done
chkconfig選項:
-
查看:--list [scriptname]
# chkconfig --list 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 # chkconfig --list mySer.sh mySer.sh 0:off 1:off 2:on 3:on 4:on 5:on 6:off -
指定運行級別:--level LEAVES
# chkconfig --list mySer.sh mySer.sh 0:off 1:off 2:on 3:on 4:on 5:on 6:off # chkconfig --level 35 mySer.sh off # chkconfig --list mySer.sh mySer.sh 0:off 1:off 2:on 3:off 4:on 5:off 6:off
當只是單純開機運行某個行程,而不需要額外的腳本的話,直接修改/etc/rc.d/rc.local檔案即可,
下面添加了touch /tmp/welcome,所以開機后,就會創建這個檔案;
添加了/usr/sbin/httpd,所以開機后,就會啟動httpd行程,
#!/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
touch /tmp/welcome
/usr/sbin/httpd
為什么編輯/etc/rc.d/rc.local檔案就好用呢?
因為這個檔案被rc2.d,rc3.d,rc4.d,rc5.d目錄里的最后一個檔案S99local鏈接了
# ll /etc/rc.d/rc2.d/ | tail -1
lrwxrwxrwx. 1 root root 11 Jan 31 14:08 S99local -> ../rc.local
# ll /etc/rc.d/rc3.d/ | tail -1
lrwxrwxrwx. 1 root root 11 Jan 31 14:08 S99local -> ../rc.local
# ll /etc/rc.d/rc4.d/ | tail -1
lrwxrwxrwx. 1 root root 11 Jan 31 14:08 S99local -> ../rc.local
# ll /etc/rc.d/rc5.d/ | tail -1
lrwxrwxrwx. 1 root root 11 Jan 31 14:08 S99local -> ../rc.local
系統初始化腳本/etc/rc.d/rc.sysinit的作用:
-
設定主機名
-
設定歡迎資訊
-
激活udev(創建設備檔案用的)和selinux
-
掛載/etc/fstab檔案中定義的所以檔案系統
-
檢測根檔案系統,沒有問題后,以讀寫方式重新掛載根檔案系統,
-
讀取硬體時鐘,設定系統時鐘
-
讀取組態檔/etc/sysctl.conf里的內核引數,修改內核的行為,
-
激活lvm及軟raid設備
-
激活swap設備
-
從根檔案系統,加載硬體的驅動程式
-
清理操作
總結:/sbin/init的啟動流程
1,通過讀取組態檔/etc/inittab,獲得運行級別
2,執行腳本/etc/rc.d/rc.sysinit,完成系統初始化
3,根據取得的運行級別,先停止這個級別不需要運行的行程(K##),再啟動這個級別需要運行的行程(S##)
4,設定登錄終端,如果是級別3則登錄終端是文本界面
5,如果是級別5,則需要啟動圖形界面服務,顯示圖形登錄界面;
centos6系統啟動流程
基本和centos5相同,只列出不同點
1,init程式:upstart,但依然為/sbin/init,其組態檔不是/etc/inittab了,而是/etc/init/*.conf,但/etc/inittab里也有用,只放默認運行級別一行(id:3:initdefault:),
2,系統初始化腳本是/etc/init/rcS.conf,但里面運行的是/etc/rc.d/rc.sysinit
3,腳本/etc/init/rc.conf里面,呼叫/etc/rc.d/rc $RUNLEVEL
centos7系統啟動流程
啟動流程是一樣的,實作的細節不一樣,
兼容centos5和6,所以也可以使用service命令,
# service crond start
Redirecting to /bin/systemctl start crond.service
# service crond status
Redirecting to /bin/systemctl status crond.service
centos7使用systemctl命令,
centos7沒有了運行級別的概念,改用target概念
- graphical.target對應運行級別5
- multi-user.target對應運行級別3
systemctl get-default
# systemctl get-default
graphical.target
centos7的init程式:Systemd
? 使用的組態檔: /usr/lib/systemd/system目錄下的檔案,和/etc/systemd/system目錄下的檔案
# c/c++ 學習互助QQ群:877684253  # 本人微信:xiaoshitou5854轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/138730.html
標籤:Linux
