1. 程式人生 > >Linux 系統引導過程

Linux 系統引導過程

系統引導

系統引導過程:


POST(Power On Self Test) 硬件檢測沒有問題後,如果是硬盤引導會找到MBR(bootloader:446字節),通過MBR來找到/boot所在的分區並加載M,需要加載boot目錄所在的分區,boot目錄所在的分區是放在文件系統中的,要加載boot分區就需要驅動,initramfs中保存的是/文件系統的驅動



需要有模板文件,才可以使用以下修復

grub> root (hd0,0)

grub> setup (hd0)


系統引導過程:

1.POST(Power On Self Test) 硬件自檢

2.讀取MBR(bootloader:446字節 磁盤分區:64字節 分區標誌位:2字節(55AA))

通過讀取MBR中的將bootloader加載至內存,而該bootloader的程序通常就是Grub,

Grub主要的功能加載系統內核(進入操作系統提示的菜單選項),而內核在boot分區上,可以通過分區表查找到boot分區(因為安裝操作系統前需要創建/boot分區),並將/boot/vmlinuz-2.6.32-696.el6.x86_64內核文件解壓縮並加載內核,內核加載內存後需要引導操作系統(內核並沒有包含所有的驅動包,所以/boot/initramfs-2.6.32-696.el6.x86_64.img就是提供操作系統驅動的另一個文件,內部包含文件系統的驅動),而操作系統的/文件系統的驅動程序在initramfs中,而訪問initramfs又需要先存在操作系統,於是便有了通過虛擬的文件系統來引導,即initramfs中存在一個虛擬文件系統可以加載/分區,進而找到/sbin/init程序來加載文件系統


補充: 查看initramfs-2.6.32-696.el6.x86_64.img文件內容的辦法:

首先 # file initramfs-2.6.32-696.el6.x86_64.img 查看文件類型是gzip壓縮文件,壓縮文件在解壓縮時需要查看文件的後綴名,所以先要修改文件的後綴名為.gz。

1.mv initramfs-2.6.32-696.el6.x86_64.img initramfs-2.6.32-696.el6.x86_64.gz

2.gunzip initramfs-2.6.32-696.el6.x86_64.img 解壓縮

3.file initramfs-2.6.32-696.el6.x86_64 發現文件的類型為cpio壓縮類型

4.cpio -tv < initramfs-2.6.32-696.el6.x86_64 查看壓縮文件內部內容

5.cpio -idv < initramfs-2.6.32-696.el6.x86_64 解壓縮文件至當前文件夾


通過查看iinitramfs-2.6.32-696.el6.x86_64內容會發現裏面的目錄結構類似與操作系統的目錄結構,並且存在大量的ko後綴的庫文件,包括用於加載/文件系統的驅動


REHL5的/etc/initab配置文件

********************************************************************************************

[root@station1 grub]# cat /etc/inittab

#

# inittab This file describes how the INIT process should set up

# the system in a certain run-level.

#

# Author: Miquel van Smoorenburg, <[email protected]>

# Modified for RHS Linux by Marc Ewing and Donnie Barnes

#


# Default runlevel. The runlevels used by RHS 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:5:initdefault:


# 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" #UPS電源在失敗前發送的警告信息,提示用戶2分鐘後關機


# If power was restored before the shutdown kicked in, cancel it.

pr:12345:powerokwait:/sbin/shutdown -c "Power Restored; Shutdown Cancelled" #如果UPS電源在關機前修復發送消息提示用戶“電源恢復,關機取消”



# 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

********************************************************************************************************

3.加載文件系統以後嘗試驅動所有的硬件設備並執行/sbin/init程序,獲取默認的運行信息(id:5:initdefault:)

4.init程序執行/etc/rc.d/rc.sysinit

si::sysinit:/etc/rc.d/rc.sysinit

5.啟動核心的服務模塊,如運行界別為3,則找到/etc/rc.d/rc3.d下面啟動服務模塊

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


([root@station1 rc3.d]# pwd

/etc/rc.d/rc3.d

[root@station1 rc3.d]# ls

K01dnsmasq K24irda K74ntpd K89rdisc S08iptables S13portmap S25netfs S55sshd S95anacron

K02avahi-dnsconfd K35vncserver K80kdump K91capi S08mcstrans S14nfslock S25pcscd S56cups S95atd

K02NetworkManager K35winbind K85mdmpd K99readahead_later S09isdn S15mdmonitor S26acpid S56rawdevices S97rhnsd

K05conman K50netconsole K87multipathd S02lvm2-monitor S10network S18rpcidmapd S26apmd S56xinetd S97yum-updatesd

K05saslauthd K69rpcsvcgssd K88wpa_supplicant S04readahead_early S11auditd S19rpcgssd S26haldaemon S80sendmail S98avahi-daemon

K05wdaemon K73ypbind K89dund S05kudzu S12restorecond S22messagebus S26hidd S85gpm S99firstboot

K10psacct K74ipmi K89netplugd S06cpuspeed S12syslog S23setroubleshoot S28autofs S90crond S99local

K20nfs K74nscd K89pand S08ip6tables S13irqbalance S25bluetooth S50hplip S90xfs S99smartd

)


K(Kill)開頭的文件,表示服務是默認不啟動的,S(Start)開頭文件,表示該服務默認是啟動的,標誌位後面的數字是啟動服務或是關閉服務的順序

6.啟動至最後服務會發現S99local 執行的是另外的一個可執行腳本,與其他服務不同

[root@station1 ~]# ll /etc/rc.d/rc3.d/

lrwxrwxrwx 1 root root 11 Nov 2 2010 S99local -> ../rc.local #指向父目錄的rc.local文件

lrwxrwxrwx 1 root root 16 Nov 2 2010 S99smartd -> ../init.d/smartd #指向父目錄的init.d下面的smatd服務腳本


[root@station1 ~]# cat /etc/rc.local

#!/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


Centos7中需要手動為該文件添加執行權限,Centos6默認有執行權限,可以編輯該腳本來讓腳本在開機前執行

7.執行/bin/login程序,等待用戶登錄

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


respawn(再生), /sbin/mingetty登錄終端不能被kill


Linux 系統引導過程