RHEL6 啓動過程

RHEL6啓動過程

在 Red Hat Enterprise Linux 6 中,sysvinit 軟件包中的 init 已經由 Upstart 替換,它是基於事件的 init 系統。這個系統可處理在引導過程中的任務和服務啓動,在關機過程中關閉它們並在系統運行時監控它們。rhel6的啓動流程發生了比較大的變化。對於熟悉其流程非常重要,對系統的排錯有很大的幫助。

--------------------------------------------------------------------------------------------------

1、BIOS加電,進行硬件自檢。然後產生int 19的終端,讀取第一個可引導的設備,比如自檢,初始化各種芯片,控制器,與端口;包括顯示器,內存,鍵盤,軟驅,串口等等最終,POST(Power-On Self Test)執行INT 19h中斷,找到可以啓動的磁盤,並將boot程序裝入內存,並將控制權交給OS的boot程序。當BIOS INT 19h被執行以後,系統進入OS Booting階段。

--------------------------------------------------------------------------------------------------

2、一塊硬盤,有一個主引導記錄,就是0面0道1扇區,又稱MBR。MBR是由BIOS自動裝載到內存中並CPU跳轉過去執行的。主引導記錄(Master Boot Record,縮寫:MBR),又叫做主引導扇區,是計算機開機後訪問硬盤時所必須要讀取的首個扇區,它在硬盤上的三維地址爲(柱面,磁頭,扇區)=(0,0,1)。共512 Byte。

_____________________________________________

|                                  |                            |                         |

| 引導代碼 446 Byte | 分區表 64 Byte | 結束符2 Byte  |

|_________________|______________|_________ ___| 

--------------------------------------------------------------------------------------------------

3、讀取第一個可引導的設備的MBR的BootLoader程序,在rhel系統上是Grub.

stage1:

/boot/grub中的stage1文件大小爲512b,它是引導扇區中引導程序(前446字節爲引導程序)的備份文件,功能是用來裝載 stage1.5或stage2的。

stage1.5:

因爲STAGE2較大,通常都是放在一個文件系統當中的,但是STAGE1並不能識別文件系統格式,所以才需要stage1.5來引導位於某個文件系統當中的STAGE2,根據文件系統格式的不同,STAGE1.5也需要相應的文件,如:e2fs_stage1_5,fat_stage1_5,分別用於識別 ext和fat的文件系統格式。但是,當stage1加載1.5時,原則上是不認識ext文件系統的,應該也是無法找到1.5程序的,而實際上在安裝 grub時,程序已經把1.5程序寫到硬盤最前面的32K中,因爲緊連着MBR的一段硬盤空間通常是空白無用的,grub就把stage1.5程序寫到這個地方,這個地方沒有文件系統,stage1程序可以直接去加載stage1.5。

--------------------------------------------------------------------------------------------------

4、Grub進程第二階段引導,讀取/boot/grub/grub.conf配置文件,根據配置文件中的定義,加載相應的內核,內核再加載相應的硬件驅動,進行必要的基礎硬件初始化

stage2:

grub能讓用戶以選項方式將OS加載、改變參數、修改選項,這些全都是stage2程序的功能。stage2可以去獲取grub.conf以及menu.lst等文件的內容。

[root@osdba~]# ls /boot/

config-2.6.32-71.el6.i686 <===系統kernel的配置文件,內核編譯完成後保存的就是這個配置文件

efi &lt;===Extensible Firmware Interface(EFI,可擴展固件接口)是 Intel 爲全新類型的 PC 固件的體系結構、接口和服務提出的建議標準。

grub &lt;===開機管理程序grub相關數據目錄

initramfs-2.6.32-71.el6.i686.img &lt;===虛擬文件系統文件(RHEL6用initramfs代替了initrd,他們的目的是一樣的,只是本身處理的方式有點不同)

initrd-2.6.32-71.el6.i686.img &lt;===此文件是linux系統啓動時的模塊供應主要來源,initrd的目的就是在kernel加載系統識別cpu和內存等內核信息之後,讓系統進一步知道還有那些硬件是啓動所必須使用的;

symvers-2.6.32-71.el6.i686.gz &lt;===模塊符號信息

System.map-2.6.32-71.el6.i686 &lt;===是系統kernel中的變量對應表;(也可以理解爲是索引文件)

vmlinuz-2.6.32-71.el6.i686 &lt;===系統使用kernel,用於啓動的壓縮內核鏡像。

[root@osdba~]# cat /boot/grub/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/sda3

# initrd /initrd-[generic-]version.img

#boot=/dev/sda

default=0

timeout=5

splashimage=(hd0,0)/grub/splash.xpm.gz

hiddenmenu

title Red Hat Enterprise Linux (2.6.32-71.el6.i686)

root (hd0,0)

kernel /vmlinuz-2.6.32-71.el6.i686 ro root=UUID=bfd97ccd-15df-4e0f-8a66-b2da62213f62

rd_NO_LUKS rd_NO_LVM rd_NO_MD rd_NO_DM

LANG=en_US.UTF-8 SYSFONT=latarcyrheb-sun16 KEYBOARDTYPE=pc KEYTABLE=us crashkernel=auto rhgb quiet

initrd /initramfs-2.6.32-71.el6.i686.img

default #啓動系統時在人爲不干預的情況下,默認讀取哪一個title,如果安裝了多個不同版本內核或者安裝了不用的操作系統,默認會產生多個title,#0第一個,1第二個,以此類推。

timeout #開機等待用戶的超時時間,單位爲秒。在超時時間結束時,如果用戶沒有人爲選擇,則以default指定的title讀取。

splashimage=(hd0,0)/grub/splash.xpm.gz #grub的背景圖案

hiddenmenu #隱藏菜單

title Red Hat Enterprise Linux (2.6.32-71.el6.i686) #title後爲指定的菜單名字

root (hd0,0)

#root 這裏不是管理員的意思,這裏的root表示爲根文件系統,hd代表硬盤(不區分IDE、SCSI),第一個0:第一個硬盤,第二個0:第一個分區。

#這裏的意思是定義第一個硬盤的第一個分區爲根文件系統。

kernel /vmlinuz-2.6.32-71.el6.i686 ro root=UUID=bfd97ccd-15df-4e0f-8a66-b2da62213f62 rd_NO_LUKS rd_NO_LVM rd_NO_MD rd_NO_DM LANG=en_US.UTF-8 SYSFONT=latarcyrheb-sun16 KEYBOARDTYPE=pc KEYTABLE=us crashkernel=auto rhgb quiet

initrd /initramfs-2.6.32-71.el6.i686.img

#kernel /vmlinuz-2.6.32-71.el6.i686

由boot loader的管理而開始讀取內核文件後,接下來,Linux 就會將內核解壓縮到主存儲器當中, 並且利用內核的功能,開始測試與驅動各個周邊設備,包括儲存設備、CPU、網卡、聲卡等等。 此時 Linux 內核會以自己的功能重新檢測一次硬件,而不一定會使用 BIOS 檢測到的硬件信息。也就是說,內核此時纔開始接管 BIOS 後的工作。內核一般來說,它會被放置到 /boot 裏面,並且取名爲 /boot/vmlinuz。

#ro root=UUID=bfd97ccd-15df-4e0f-8a66-b2da62213f62

Linux內核是可以通過動態加載內核模塊的 (就請想成驅動程序即可),這些內核模塊就放在 /lib/modules/目錄內。由於模塊放到磁盤根目錄內(這就是爲什麼/lib不可以與/ 分別放在不同的分區原因), 因此在開機的過程中內核必須要掛載根目錄,這樣才能夠讀取內核模塊提供加載驅動程序的功能。 而且爲了擔心影響到磁盤內的文件系統,開機過程中根目錄是以只讀的方式來掛載的。

#rd_NO_LUKS rd_NO_LVM rd_NO_MD rd_NO_DM LANG=en_US.UTF-8 SYSFONT=latarcyrheb-sun16 KEYBOARDTYPE=pc

KEYTABLE=us crashkernel=auto rhgb quiet

Speeding up the Boot Process

If you want to speed up the boot process, you can specify as much information for dracut on the kernel command as possible. For example, you can tell dracut, that you root partition is not on a LVM volume or not on a raid partition, or that it lives inside a specific crypto LUKS encrypted volume. By default, dracut searches everywhere. A typical dracut kernel command line for a plain primary or logica partition would contain:

rd_NO_LUKS rd_NO_LVM rd_NO_MD rd_NO_DM (elinks /usr/share/doc/dracut-004/dracut-rhel6.html or man 8 dracut)

This turns off every automatic assembly of LVM, MD raids, DM raids and crypto LUKS.

Keyboard Settings

If you have to input passwords for encrypted disk volumes, you might want to set the keyboard layout and specify a display font.

A typical german kernel command would contain:

LANG=de_DE.UTF-8 SYSFONT=latarcyrheb-sun16 KEYBOARDTYPE=pc KEYTABLE=de-latin1-nodeadkeys

rhgb表示redhat graphics boot,就是會看到圖片來代替啓動過程中顯示的文本信息,這些信息在啓動後用dmesg也可以看到

quiet表示在啓動過程中只有重要信息顯示,類似硬件自檢的消息不回顯示crashkernel=auto kdump使用的內存空間

#initrd /initramfs-2.6.32-71.el6.i686.img

boot loader裝入kernel,然後kernel需要執行/sbin/init,讀取這個文件就必須先mount根文件系統,早期是通過啓動時的 root="參數"告訴內核根文件系統在哪個設備上,隨着硬件和技術的發展,現在根文件系統可能位於一個網絡存儲如NFS上,可能由於RAID而散佈於多個設備上,可能位於一個加密設備上需要提供用戶名和密碼,這時root="參數"就顯得不夠了。爲了應付這種局面,先後出現兩種機制來作爲boot loader裝載kernel到真正的/sbin/init執行這個啓動過程的橋樑: initrd和nitramfs

initrd:

虛擬文件系統(Initial RAM Disk) 一般使用的文件名爲 /boot/initrd ,這個文件的作用是,能夠通過 boot loader 來加載到內存中, 然後這個文件會被解壓縮並且在內存當中仿真成一個根目錄, 且此仿真在內存當中的文件系統能夠提供一支可執行的程序,通過該程序來加載開機過程中所最需要的內核模塊,通常這些模塊就是 USB, RAID, LVM,SCSI 等文件系統與磁盤接口的驅動程序。等載入完成後,會幫助內核重新呼叫 /sbin/init 來開始後續的正常開機流程。

Initial RAM Disk是一個基於ram的塊設備,因此它佔據了一塊固定的內存,而且事先要使用特定的工具比如mke2fs格式化,還需要一個文件系統驅動來讀寫其上的文件。如果這個disk上的空間沒有用完,這些未用的內存就浪費掉了,並且這個disk的空間固定導致容量有限,要想裝入更多的文件就需要重新格式化。由於Linux的塊設備緩衝特性,ram disk上的數據被拷貝到page cache(對於文件數據)和dentry cache(對於目錄項),這個也導致內存浪費。

initramfs:

最初的想法是Linus提出的: 把cache當作文件系統裝載。 他在一個叫ramfs的cache實現上加了一層很薄的封裝,其它內核開發人員編寫了一個改進版tmpfs, 這個文件系統上的數據可以寫出到交換分區,而且可以設定一個tmpfs裝載點的最大尺寸以免耗盡內存。initramfs就是tmpfs的一個應用。Linux 2.6 kernel提出了這種新的實現機制,即 initramfs。顧名思義,initramfs只是一種RAM filesystem而不是disk。

initramfs的優點: 

(1)tmpfs隨着其中數據的增減自動增減容量。

(2)在tmpfs和page cache/dentry cache之間沒有重複數據。

(3)tmpfs重複利用了Linux caching的代碼,因此幾乎沒有增加內核尺寸,而caching的代碼已經經過良好測試,所以tmpfs的代碼質量也有保證。

(4)不需要額外的文件系統驅動。

另外,initrd機制被設計爲舊的"root="機制的前端,而非其替代物,它假設真正的根設備是一個塊設備,而且也假設了自己不是真正的根設備,這樣不便將NFS等作爲根文件系統,最後/linuxrc不是以PID=1執行的,因爲1這個進程ID是給/sbin/init保留的。initrd機制找到真正的根設備後將其設備號寫入/proc/sys/kernel/real-root-dev,然後控制轉移到內核由其裝載根文件系統並啓動/sbin /init。initramfs則去掉了上述假設,而且/init以PID=1執行,由init裝載根文件系統並用exec轉到真正的/sbin /init,這樣也 導致一個更爲乾淨漂亮的設計。

下面讓我們來看看initrd文件的具體內容

[root@osdba~]# mkdir /tmp/initramfs/

[root@osdba~]# cp /boot/initramfs-2.6.32-71.el6.i686.img /tmp/initramfs/

[root@osdba~]# cd /tmp/initramfs/

[root@osdba initramfs]# file initramfs-2.6.32-71.el6.i686.img

initramfs-2.6.32-71.el6.i686.img: gzip compressed data, from Unix, last modified: Wed Aug 24 23:14:47 2011, max compression

我們可以看到,這個文件是GZIP壓縮的文件,我們把後綴改成gz,然後再進行解壓

[root@osdba initramfs]# mv initramfs-2.6.32-71.el6.i686.img initramfs-2.6.32-71.el6.i686.img.gz

[root@osdba initramfs]# gunzip initramfs-2.6.32-71.el6.i686.img.gz

解壓後,我們再來看看這個文件是什麼類型的

[root@osdba initramfs]# file initramfs-2.6.32-71.el6.i686.img

initramfs-2.6.32-71.el6.i686.img: ASCII cpio archive (SVR4 with no CRC)

是cpio壓縮成的文件

[root@osdba initramfs]# cpio -iv &lt; initramfs-2.6.32-71.el6.i686.img

[root@osdba initramfs]# ls -l

total 29708

drwxr-xr-x. 2 root root 4096 Sep 19 14:52 bin

drwxr-xr-x. 2 root root 4096 Sep 19 14:52 cmdline

drwxr-xr-x. 3 root root 4096 Sep 19 14:52 dev

-rw-r--r--. 1 root root 18 Sep 19 14:52 dracut-004-32.el6

drwxr-xr-x. 2 root root 4096 Sep 19 14:52 emergency

drwxr-xr-x. 7 root root 4096 Sep 19 14:52 etc

-rwxr-xr-x. 1 root root 8088 Sep 19 14:52 init

drwxr-xr-x. 2 root root 4096 Sep 19 14:52 initqueue

drwxr-xr-x. 2 root root 4096 Sep 19 14:52 initqueue-finished

drwxr-xr-x. 2 root root 4096 Sep 19 14:52 initqueue-settled

-rw-r--r--. 1 root root 30326784 Sep 19 14:57 initramfs-2.6.32-71.el6.i686.img

drwxr-xr-x. 9 root root 4096 Sep 19 14:52 lib

drwxr-xr-x. 2 root root 4096 Sep 19 14:52 mount

drwxr-xr-x. 2 root root 4096 Sep 19 14:52 pre-pivot

drwxr-xr-x. 2 root root 4096 Sep 19 14:52 pre-trigger

drwxr-xr-x. 2 root root 4096 Sep 19 14:52 pre-udev

drwxr-xr-x. 2 root root 4096 Sep 19 14:52 proc

drwxr-xr-x. 2 root root 4096 Sep 19 14:52 sbin

drwxr-xr-x. 2 root root 4096 Sep 19 14:52 sys

drwxr-xr-x. 2 root root 4096 Sep 19 14:52 sysroot

drwxr-xr-x. 2 root root 4096 Sep 19 14:52 tmp

drwxr-xr-x. 6 root root 4096 Sep 19 14:52 usr

drwxr-xr-x. 3 root root 4096 Sep 19 14:52 var

看結果,和我們系統中的/很類似。(有興趣,慢慢了解)

--------------------------------------------------------------------------------------------------

5、在內核加載完畢、進行完硬件檢測與驅動程序加載後,此時主機硬件已經準備就緒了,這時候內核會主動的呼叫第一支程序,那就是 /sbin/init。/sbin/init 最主要的功能就是準備軟件執行的環境,包括系統的主機名、網絡設定、語言、文件系統格式及其他服務的啓動等。 而所有的動作都會通過 init的配置文件/etc/inittab來規劃,而inittab 內還有一個很重要的設定內容,那就是默認的 runlevel (開機運行級別)。先來看看運行級別Run level,Linux就是通過設定run level來規定系統使用不同的服務來啓動,讓Linux的使用環境不同。我們來看看這個inittab文件裏面的支持級別(RHEL6系統裏面的,和以前的其它版本有很大的差別).

[root@osdba~]# 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) (類似底下的 runlevel 3,但無 NFS 服務)

# 3 - Full multiuser mode (完整含有網絡功能的字符模式)

# 4 - unused (系統保留,未使用)

# 5 - X11 (與 runlevel 3 類似,但加載使用 X Window)

# 6 - reboot (Do NOT set initdefault to this) (重新啓動)

#

id:5:initdefault:

注意:0, 4, 6 不是關機、重新啓動就是系統保留的,所以不能將預設的run level設定爲這三個值。

RHEL6系統上的這個文件和以前的版本有很大的差別,目前這個文件只能設置運行級別,其它的相關配置文件,在此文件中已經做了說明如:

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.

從這個文件我們已經看出,紅帽已經使用新的Upstart啓動服務來替換以前的init.在RHEL6的版本中,我們可以把/etc/init/這個目錄裏面的內容,看成是以前/etc/inittab這個文件裏的拆分。

我們可能通過man inittab來查看

DESCRIPTION

The /etc/inittab file was the configuration file used by the original

System V init(8) daemon.

The Upstart init(8) daemon does not use this file, and instead reads

its configuration from files in /etc/init. See init(5) for more

details.

UpStart: 基於事件的啓動系統

Sysvinit:基於結果的啓動系統

SysVinit守護進程(sysvinit軟件包)是一個基於運行級別的系統,它使用運行級別(單用戶、多用戶以及其他更多級別)和鏈接(位於/etc /rc?.d目錄中,分別鏈接到/etc/init.d中的init腳本)來啓動和關閉系統服務。

Upstart init守護進程(upstart軟件包)則是基於事件的系統,它使用事件來啓動和關閉系統服務。幾個很重要的定義

event: 事件, 是指系統狀態的一種改變,這種改變會被通知給init進程。舉例來說,boot loader會觸發startup事件,系統進入runlevel 2的時候會觸發runlevel 2事件,某個文件系統被掛載時會觸發path-mounted事件, USB設備的插拔也都會產生相應的事件。這些時間會被通知給init進程,然後init進程來決定系統如何處理這些事件

job: 作業,一項作業是init進程讀入的一系列指令。你可以使用initctl start或initctl stop命令來開始或停止某項作業,這是調用作業的一種方式。另一種方式是當系統被告知發生什麼事件event後,運行該事件所對應的作業。可用sudo initctl list命令列出所有系統作業的運行狀態。

task: 任務, a job that performs its work and returns to a waiting state when it is done. 中文是一種完成指定工作後進去等待狀態的作業。

service: 服務,a job that does not normally terminate by itself. 舉個例子來說, gettys 是以服務來實現的。 init進程監視所有服務,並在服務失敗的時候重啓服務。

原有的System V init啓動過程的缺點是,它基於包含了大量啓動腳本的runlevel目錄。而Upstart則是事件驅動型的,因此,它只包含按需啓動的腳本,這將使啓動過程變得更加迅速。經過良好調優並使用Upstart啓動方式的Linux服務器的啓動速度要明顯快於原有的使用System V init的系統。

爲了使Upstart更容易理解,它仍然使用了一個init進程。所以,你仍然可以看到/sbin/init,它是所有服務的根進程。

有一個好消息,那就是RHEL 6對啓動過程的改變很少。你還是可以處理那些在目錄/etc/init.d中的包含服務腳本的服務,所以runlevel的概念一直存在。因此,在使用 yum增加一個服務後,照樣可以像以前那樣使用chkconfig命令激活它。此外,仍然可以用service命令來啓動它。

--------------------------------------------------------------------------------------------------

6、System initialization is started by /etc/init/rcS.conf

[root@osdba~]# cat /etc/init/rcS.conf

# Note: there can be no previous runlevel here, if we have one it's bad

# information (we enter rc1 not rcS for maintenance). Run /etc/rc.d/rc

# without information so that it defaults to previous=N runlevel=S.

console output

exec /etc/rc.d/rc.sysinit

--------------------------------------------------------------------------------------------------

/etc/rc.d/rc.sysinit 這個文件做了哪些工作:

1、獲得網絡環境

2、掛載設備

3、開機啓動畫面Plymouth(取替了過往的 RHGB)

4、判斷是否啓用SELinux

5、顯示於開機過程中的歡迎畫面

6、初始化硬件

7、用戶自定義模塊的加載

8、配置內核的參數

9、設置主機名

10、同步存儲器

11、設備映射器及相關的初始化

12、初始化軟件磁盤陣列(RAID)

13、初始化 LVM 的文件系統功能

14、檢驗磁盤文件系統(fsck)

15、磁盤配額(quota)

16、重新以可讀寫模式掛載系統磁盤

17、更新quota(非必要)

18、啓動系統虛擬隨機數生成器

19、配置機器(非必要)

20、清除開機過程當中的臨時文件

21、創建ICE目錄

22、啓動交換分區(swap)

23、將開機信息寫入/var/log/dmesg文件中

這個文件裏面的許多預設配置文件在/etc/sysconfig/這個目錄當中,要想更多的系統啓動信息,大家可以到/var/log/dmesg文件中查看,也可以用dmesg命令來查看。

--------------------------------------------------------------------------------------------------

7、Individual runlevels are started by /etc/init/rc.conf

[root@osdba~]# cat /etc/init/rc.conf

# rc - System V runlevel compatibility

#

# This task runs the old sysv-rc runlevel scripts. It

# is usually started by the telinit compatibility wrapper.

start on runlevel [0123456]

stop on runlevel [!$RUNLEVEL]

task

export RUNLEVEL

console output

exec /etc/rc.d/rc $RUNLEVEL

系統服務的啓動經過 /etc/rc.sysinit 的系統模塊與相關硬件信息的初始化後,RHEL6系統應該已經能順利工作了。但還需要啓動一些爲我們提供服務的服務。這個時候,依據在/etc/inittab裏面run level的設定值,就可以來決定啓動的服務項目了。大家可以打開/etc/rc這個文件來研究.

以運行級別3爲例

[root@osdba~]# ls /etc/rc3.d/

K01certmonger K80sssd S08iptables S25netfs

K01smartd K84wpa_supplicant S10network S26acpid

K02oddjobd K86cgred S11auditd S26haldaemon

K10psacct K87restorecond S12rsyslog S26udev-post

K10saslauthd K88nslcd S13cpuspeed S28autofs

K50dnsmasq K89rdisc S13irqbalance S55sshd

K50netconsole K95cgconfig S13rpcbind S64mysqld

K60nfs K95firstboot S15mdmonitor S80postfix

K69rpcsvcgssd K99stap-server S22messagebus S82abrtd

K73ypbind S00microcode_ctl S23NetworkManager S85httpd

K74nscd S01sysstat S24avahi-daemon S90crond

K74ntpd S02lvm2-monitor S24nfslock S95atd

K75ntpdate S03vmware-tools S24rpcgssd S97rhnsd

K80kdump S08ip6tables S24rpcidmapd S99local

在這個目錄下的文件主要具有2個特點:

1、全部以Sxx或Kxx(xx爲數字)開頭

2、全部是連結文件,連結到/etc/init.d/

現在來說明一下這些的目的

S表示啓動服務

K表示停止服務

後面的數字是啓動的先後順序

以S55sshd來舉例

[root@osdba~]# ls -l /etc/rc3.d/S55sshd

lrwxrwxrwx. 1 root root 14 Aug 24 23:08 /etc/rc3.d/S55sshd -> ../init.d/sshd

意思就是

/etc/rc3.d/S55sshd = /etc/init.d/sshd start

--------------------------------------------------------------------------------------------------

8、用戶自定義開機啓動腳本

上面講到的都是一些系統服務,Linux系統容許安裝其它的軟件來提供服務,那我想要自己安裝的服務也要在開機啓動,那怎麼辦,沒有關係,找 /etc/rc.local 來完成。這就是我們要講的用戶自定義開機啓動腳本。我們只要把想啓動的腳本寫到這個文件中,開機就能啓動了,注意一點,寫在這裏面的腳本要使用絕對路徑。

--------------------------------------------------------------------------------------------------

9、在/etc/init/start-ttys.conf中我們可以看到有4個純文本終端(tty[2-5])。我們可以用[Ctrl+Alt+F2~F5]來切換這些終端。如果要切換到X window終端我們可以用

[Ctrl+Alt+F1]

[root@osdba~]# cat /etc/init/start-ttys.conf

#

# This service starts the configured number of gettys.

start on stopped rc RUNLEVEL=[2345]

env ACTIVE_CONSOLES=/dev/tty[1-6]

env X_TTY=/dev/tty1

task

script

. /etc/sysconfig/init

for tty in $(echo $ACTIVE_CONSOLES) ; do

[ "$RUNLEVEL" = "5" -a "$tty" = "$X_TTY" ] && continue

initctl start tty TTY=$tty

done

end script

注:虛擬機個與真實的環境還是有區別的,有很多快捷鍵是衝突的,所以這個需要自行實驗獲得。

--------------------------------------------------------------------------------------------------

10、加載終端或X-Window接口在完成了系統所有服務的啓動後,接下來Linux就會啓動終端或者是X Window來等待使用者登陸了!

--------------------------------------------------------------------------------------------------

在文件/etc/sysconfig/init中還有一些額外的配置。在這裏,定義了一些參數來決定啓動信息的格式。除了那些不很重要的設置,有三行我們需要注意:

AUTOSWAP=no

ACTIVE_CONSOLES=/dev/tty[1-6]

SINGLE=/sbin/sushell

AUTOSWAP:這一行的值你可以設定爲Yes,這樣可以讓你的系統能夠自動檢測交換分區。使用此選項意味着你再也不必在/etc/fstab中掛載交換分區了。

ACTIVE_CONSOLES:這一行決定了虛擬控制檯的創建。在大多數情況下,tty[1-6]工作得很好,同時這個選項也允許您分配更多或者更少的虛擬控制檯。但是要注意,不要使用tty [1-8],因爲tty7是專門爲圖形界面預留的。

SINGLE=/sbin/sushell:這一行可以有兩個參數:/sbin/sushell(系統默認的參數),它會在啓動單用戶模式時將你帶入一個root的 shell,參數/sbin/sulogin會在單用戶模式啓動之前彈出一個登錄提示,你必須輸入root賬戶的密碼才能繼續下去。

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

下面是RHEL6上面Upstart大致的一個啓動過程:

1.內核啓動init

2.系統初始化:(/etc/init/rcS.conf exec /etc/rc.d/rc.sysinit)

3.init找到/etc/inittab文件,確定默認的運行級別(X) (/etc/init/rcS.conf exec telinit $runlevel)

4.觸發相應的runlevel事件(/etc/init/rc.conf exec /etc/rc.d/rc $RUNLEVEL)

5.開始運行/etc/rc.d/rc,傳入參數X

6./etc/rc.d/rc腳本進行一系列設置,最後運行相應的/etc/rcX.d/中的腳本

7./etc/rcX.d/中的腳本按事先設定的優先級依次啓動

8.最後執行/etc/rc.d/rc.local

9.加載終端或X-Window接口

想要了解更多的內容,請大家打開/etc/init/這個目錄裏面的文件看看。

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

運行級別的切換

通過上面的學習,我們知道run level是在/etc/rc.sysinit 執行完畢之後,而run level 的不同

之處僅是/etc/rc[0-6].d 裏面啓動的服務不同而已。

如果我們切換run level只需要停止與啓動一些服務就OK,那麼要如何來實現,其它很簡單,用init n(數字)來切換。

[root@osdba~]# runlevel

N 5

5:當前的運行級別

N:在當前運行級別之前運行過什麼運行級別,因爲例中開機就是運行級別5,之前沒有運行過其他運行級別,所以是N

[root@osdba~]# init 3

以上是自己的結合上課時的零星總結,如有錯誤,望大家指正。

With RHEL6 having been released last November, half a year of aging has given me the opportunity to let some of its new features sink in. Definitely my 2 favorites are the introduction of upstart, to replace the aging Sysv init startup mechanism, and the introduction of advanced resource management with Cgroups.

Upstart has been available in Ubuntu since the release of edgy in 2006, and soon after it got picked up by other distributions as well. Upstart is an event-driven init daemon. As a consequence, unlike the previous requirement of having multiple runlevels, with each service defined to start in a particular sequence, Upstart will only start a service if there is an external event requiring it. i.e. It will only start Bluetooth services if there is Bluetooth hardware detected. This has the benefit that a large part of the service configuration is handled automatically and since only necessary services will run, you will have faster startup times and less resource consumption. Upstart still regards a change in runlevel as an event, so most services will still use their old rc style init scripts. As a consequence you can still use chkconfig to control these startup scripts or use the service command to manipulate these services. But for other functionalities, like setting up serial console, you will have to use an upstart job instead of the old way of doing it through inittab.

One good example is when you want to setup a serial console for qemu if you are testing rhel6 in a KVM virtual machine. By default the upstart script /etc/init/serial.conf will start a serial console if the primary console in grub is set to a serial console. If you want a serial console that is not the primary console, you will have to create your own upstart script:

[root@localhost ~]# cat /etc/init/Stty.conf

stop on runlevel [016]

start on runlevel [345]

respawn

instance /dev/ttyS0

exec /sbin/mingetty /dev/ttyS0

The first 2 lines of the script are called the event definition. Upstart needs to now for each service defined in /etc/init, what event will trigger a change in the state of the service. In this case we tell upstart to start the service in runlevels 3, 4 and 5 and to stop the service in runlevel 0, 1 and 6

The rest of the script is what is called the process definition. Here you define what to run and how to run it. In this case we tell upstart to respawn the getty process if it dies, to ensure that there is only one instance running that uses /dev/ttyS0 and we specify the getty command to run.

Now that we’ve create the upstart service we can manipulate it with the initctl tool:

[root@localhost ~]# initctl start Stty

[root@localhost ~]# initctl status Stty

Stty (/dev/ttyS0) start/running, process 18425

Now that we have a serial console running we can connect to it on the hardware node:

[:root@somehost:/root]$ virsh list

Id Name State

---------------------------------- 

2 test running

[:root@somehost:/root]$ virsh console 2

The version of upstart in RHEL 6 lacks some of the more recent features introduced in Ubuntu like the ability to schedule cron – like jobs and a replacement for inetd with the socket upstart event. I hope these are backported to RHEL 6 soon.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章