標籤:
開啟PC的電源,BIOS開機自我測試 (POST),按BIOS中設定的啟動裝置(通常是硬碟)啟動,接著啟動裝置上安裝的引導程式lilo或grub開始引導Linux,Linux首先進行核心的引導,接下來執行init程式,init程式調用了rc.sysinit和rc等程式,rc.sysinit和rc當完成系統初始化和運行服務的任務後,返回init;init啟動了mingetty後,開啟了終端供使用者登入系統,使用者登入成功後進入了Shell,這樣就完成了從開機到登入的整個啟動過程。
init進程起來後,系統啟動的控制權移交給init進程。
/sbin/init進程是所有進程的父進程,當init起來之後,它首先會讀取設定檔/etc/inittab,進行以下工作:
1)執行系統初始化指令碼(/etc/rc.d/rc.sysinit),對系統進行基本的配置,以讀寫方式掛載根檔案系統及其它檔案系統,到此系統基本算運行起來了,後面需要進行運行層級的確定及相應服務的啟動;
2)確定啟動後進入的運行層級;
3)執行/etc/rc.d/rc,該檔案定義了服務啟動的順序是先K後S,而具體的每個運行層級的服務狀態是放在/etc/rc.d/rcn.d(n=0~6)目錄下,所有的檔案均連結至/etc/init.d下的相應檔案。
4)/etc/rc.d/rc$RUNLEVEL # $RUNLEVEL為預設的運行模式
5) /etc/rc.d/rc.local
6)啟動虛擬終端/sbin/mingetty
7)在運行層級5上運行X
這時呈現給使用者的就是最終的登入介面。
至此,系統啟動過程完畢。
說明:
系統啟動運行層級的概念以及服務的定製方法;
當initrd 可以正常檢測和裝載之後,最後的工作就基本上由作業系統來進行了。當系統的init進程起來之後系統啟動的控制權移交給init進程。
/sbin/init 進程是所有進程的父進程,當init起來之後,它首先會讀取設定檔/etc/inittab,進行以下工作:
1)執行系統初始化指令碼(/etc/rc.d/rc.sysinit),對系統進行基本的配置,以讀寫方式掛載根檔案系統及其它檔案系統,後面需要進行運行層級的確定及相應服務的啟動,(從這個角度可以看出如果要定義系統的init動作,需要修改/etc/rc.d/rc.sysinit指令碼)。它的主要工作有:配置selinux,系統時鐘,核心參數(/etc/sysctl.conf),hostname,啟用wap分區,根檔案系統的檢查和二次掛載(讀寫),啟用RAID和LVM裝置,啟用磁碟quota檢查並掛載其它檔案系統。
2)通過對/etc/inittab檔案
[[email protected] etc]# cat 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:5:initdefault:
[[email protected] init]# cat 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.## Do not edit this file directly. If you want to change the behaviour,# please create a file rc.override and put your changes there.start on runlevel [0123456]stop on runlevel [!$RUNLEVEL]taskexport RUNLEVELconsole outputexec /etc/rc.d/rc $RUNLEVEL[[email protected] init]#
[[email protected] ~]# cat /etc/rc.d/rc #! /bin/bash## rc This file is responsible for starting/stopping# services when the runlevel changes.## Original Author: # Miquel van Smoorenburg, <[email protected]nl.mugnet.org>#set -m# check a file to be a correct runlevel scriptcheck_runlevel (){ # Check if the file exists at all. [ -x "$1" ] || return 1 is_ignored_file "$1" && return 1 return 0}# Now find out what the current and what the previous runlevel are.argv1="$1"set $(/sbin/runlevel)runlevel=$2previous=$1export runlevel previous. /etc/init.d/functionsexport CONSOLETYPEdo_confirm="no"if [ -f /var/run/confirm ]; then do_confirm="yes"fiUPSTART=[ -x /sbin/initctl ] && UPSTART=yes# See if we want to be in user confirmation modeif [ "$previous" = "N" ]; then if [ "$do_confirm" = "yes" ]; then echo $"Entering interactive startup" else echo $"Entering non-interactive startup" fifi# Get first argument. Set new runlevel to this argument.[ -n "$argv1" ] && runlevel="$argv1"# Is there an rc directory for this new runlevel?[ -d /etc/rc$runlevel.d ] || exit 0# Set language, vc settings once to avoid doing it for every init script# through functionsif [ -f /etc/sysconfig/i18n -a -z "${NOLOCALE:-}" ] ; then . /etc/profile.d/lang.sh 2>/dev/null export LANGSH_SOURCED=1fi# First, run the KILL scripts.for i in /etc/rc$runlevel.d/K* ; do # Check if the subsystem is already up. subsys=${i#/etc/rc$runlevel.d/K??} [ -f /var/lock/subsys/$subsys -o -f /var/lock/subsys/$subsys.init ] || continue check_runlevel "$i" || continue # Bring the subsystem down. [ -n "$UPSTART" ] && initctl emit --quiet stopping JOB=$subsys $i stop [ -n "$UPSTART" ] && initctl emit --quiet stopped JOB=$subsysdone# Now run the START scripts.for i in /etc/rc$runlevel.d/S* ; do # Check if the subsystem is already up. subsys=${i#/etc/rc$runlevel.d/S??} [ -f /var/lock/subsys/$subsys ] && continue [ -f /var/lock/subsys/$subsys.init ] && continue check_runlevel "$i" || continue # If we‘re in confirmation mode, get user confirmation if [ "$do_confirm" = "yes" ]; then confirm $subsys rc=$? if [ "$rc" = "1" ]; then continue elif [ "$rc" = "2" ]; then do_confirm="no" fi fi update_boot_stage "$subsys" # Bring the subsystem up. [ -n "$UPSTART" ] && initctl emit --quiet starting JOB=$subsys if [ "$subsys" = "halt" -o "$subsys" = "reboot" ]; then export LC_ALL=C exec $i start fi $i start [ -n "$UPSTART" ] && initctl emit --quiet started JOB=$subsysdone[ "$do_confirm" = "yes" ] && rm -f /var/run/confirmexit 0[[email protected] ~]#
[[email protected] rc5.d]# pwd/etc/rc5.d[[email protected] rc5.d]# lsK01smartd K61nfs-rdma K89netconsole S10network S24nfslock S55sshdK02oddjobd K69rpcsvcgssd K89rdisc S11auditd S24rpcgssd S70spice-vdagentdK05wdaemon K73winbind K92pppoe-server S11portreserve S25blk-availability S80postfixK10psacct K74ntpd K95firstboot S12rsyslog S25cups S82abrt-ccppK10saslauthd K75ntpdate K95rdma S13cpuspeed S25netfs S82abrtdK15htcacheclean K75quota_nld K99rngd S13irqbalance S26acpid S90crondK15httpd K76ypbind S01sysstat S13rpcbind S26haldaemon S95atdK50dnsmasq K84wpa_supplicant S02lvm2-monitor S15mdmonitor S26udev-post S99certmongerK50kdump K87restorecond S08ip6tables S22messagebus S28autofs S99localK60nfs K88sssd S08iptables S23NetworkManager S50bluetooth[[email protected] rc5.d]#
CentOS RedHat啟動過程