手机版
你好,游客 登录 注册
背景:
阅读新闻

CentOS 6的系统启动流程

[日期:2016-09-24] 来源:Linux社区  作者:xsllqs [字体:大 中 小]

常用的命令行命令有下面这些:

1. help
    查看命令用法,显示所有可用命令
    help search 
    search 命令用法
2. ls
    列出当前的所有设备。如 (hd0) (hd0,1) (hd0,5) (hd1) (hd1,1) (hd1,2) .......
    ls -l
    详细列出当前的所有设备。对于分区,会显示其label及uuid。
    ls /
    列出当前设为root的分区下的文件
    ls (hd1,1)/
    列出(hd1,1)分区下文件
3. search
    search -f /ntldr
    列出根目录里包含ntldr文件的分区,返回为分区号
    search -l LINUX
    搜索label是LINUX的分区。
    search --set -f /ntldr
    搜索根目录包含ntldr文件的分区并设为root,注意如果多外分区含有ntldr文件,set 失去作用
4. loopback
    loopback命令可用于建立回放设备,如
    loopback lo0 (hd1,1)/abc.iso
    可以使用lo0设备来访问abc.iso里的内容,比如说,可以从abc.iso里的软盘映像中启动
    loopback lo0 (hd1,1)/aa.iso
    linux (lo0)/memdisk
    initrd (lo0)/abc.img
    要删除某一回放设备,可以使用-d参数:
    loopback -d lo0
5. set
    使用set可以设置变量的值
    set root=
    set timeout=
    需要调用变量的值时,使用${AA},如set root=(hd1,1)
    则${root}=(hd1,1)
6. pager
    分页显示。
    set pager=1
    满页时暂停,按space继续
    set pager=0
    取消分页
7. linux
    linux取代grub中的kernel

    CentOS 7的/boot/grub2/grub.conf官方建议是不要随便修改的,一般都是在grub.d文件下生成新的启动目录,再重新生成


    默认启动的修改可通过/boot/grub2/grubenv文件修改
[root@localhost grub2]# vim grubenv 
 
# GRUB Environment Block
saved_entry=CentOS Linux (3.10.0-327.el7.x86_64) 7 (Core)
#########################################################

    可以进入/etc/grub.d下查看

[root@localhost grub.d]# ls
00_header  01_users  20_linux_xen    30_os-prober  41_custom
00_tuned  10_linux  20_ppc_terminfo  40_custom    README

    进入README可以查看grub.d中文件的说明
[root@localhost grub.d]# cat README 
 
All executable files in this directory are processed in shell expansion order.
 
  00_*: Reserved for 00_header.
  10_*: Native boot entries.
  20_*: Third party apps (e.g. memtest86+).
 
The number namespace in-between is configurable by system installer and/or
administrator.  For example, you can add an entry to boot another OS as
01_otheros, 11_otheros, etc, depending on the position you want it to occupy in
the menu; and then adjust the default setting via /etc/default/grub.

#00 _ *:为00 _header保留
#10 _ *:本机引导条目
#20 _ *:第三方应用程序(例如memtest86)

五、进入kernel
    在GRUB中选择对应的kernel进入,然后kernel会对自身进行初始化
    探测可识别到的所有硬件设备。
    加载硬件驱动程序。
    以只读方式挂载根文件系统。
    运行用户空间的第一个应用程序,在CentOS 6上是init,在CentOS 7上是Systemd
    内核组成部分:
        kernel: 内核核心,一般为bzImage,通常在/boot目录下,名称为vmlinuz-VERSION-RELEASE;
        kernel object: 内核对象,一般放置于/lib/modules/VERSION-RELEASE/

六、运行init
    CentOS 6上init所在的位置是在/sbin/init,init共分为7个级别

#0:关机
#1:单用户模式(root, 无须登录), single, 维护模式;
#2: 多用户模式,会启动网络功能,但不会启动NFS;维护模式;
#3:多用户模式,正常模式;文本界面;
#4:预留级别;可同3级别;
#5:多用户模式,正常模式;图形界面;
#6:重启

    例如你要关机可直接输入init 0进行关机操作

[root@localhost sbin]# init 0

    如果要查看当前系统运行的init级别可使用runlevel或者who -r

[root@localhost sbin]# runlevel 
N 5
[root@localhost sbin]# who -r
        运行级别 5 2016-04-10 18:25

    init的配置文件在/etc/inittab
[root@localhost sbin]# vim /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)
#  3 - Full multiuser mode
#  4 - unused
#  5 - X11
#  6 - reboot (Do NOT set initdefault to this)
# 
id:5:initdefault:
#这里定义的默认运行级别为5

    配置文件中的格式是:id:runlevel:action:process
    此配置文件每行定义一个级别
id:指入口标识符,它是一个字符串,对于getty或mingetty等其他login程序项,要求id与tty的编号相同,否则getty程序将不能正常工作。
runlevel:init的系统运行级别
action:是用来定义后面process的运行方式,常用的有下面几种
        wait: 切换至此级别运行一次;
        respawn:此process终止,就重新启动之;
        initdefault:设定默认运行级别;process省略;
        sysinit:设定系统初始化方式,此处一般为指定/etc/rc.d/rc.sysinit
process:为具体的执行程序,

    例如设置为l0:5:wait:/etc/rc.d/rc 5

    /etc/rc.d/rc是一个Shell脚本,它接受5作为参数,去执行/etc /rc.d/rc5.d/目录下的所有的rc启动脚本,/etc/rc.d/rc5.d/目录中的这些启动脚本实际上都是一些链接文件,而不是真正的rc 启动脚本,真正的rc启动脚本实际上都是放在/etc/rc.d/init.d/目录下。而这些rc启动脚本有着类似的用法,它们一般能接受start、stop、restart、status等参数。

    /etc/rc.d/rc5.d/中的rc启动脚本通常是K或S开头的链接文件。

#K开头的文件:运行次序是数字越小,越先运行;数字越小的服务,通常为依赖到别的服务;关闭服务。
#S开头的文件:运行次序是数字越小,越先运行;数字越小的服务,通常为被依赖到的服务;启动服务。

[root@localhost rc5.d]# pwd
/etc/rc.d/rc5.d
[root@localhost rc5.d]# ls
K01numad        K69rpcsvcgssd      S05cgconfig    S23NetworkManager    S80postfix
K01smartd        K73winbind        S07iscsid      S24nfslock          S82abrt-ccpp
K02oddjobd      K75ntpdate        S08ip6tables    S24rpcgssd          S82abrtd
K05wdaemon      K75quota_nld      S08iptables    S25blk-availability  S84ksm
K10psacct        K76ypbind          S10network      S25cups              S85ksmtuned
K10saslauthd    K84wpa_supplicant  S11auditd      S25netfs            S90crond
K15htcacheclean  K85ebtables        S11portreserve  S26acpid            S95atd
K15httpd        K86cgred          S12rsyslog      S26haldaemon        S97libvirtd
K15svnserve      K87restorecond    S13cpuspeed    S26hypervkvpd        S99certmonger
K46radvd        K88sssd            S13irqbalance  S26udev-post        S99libvirt-guests
K50dnsmasq      K89rdisc          S13iscsi        S28autofs            S99local
K50netconsole    K95firstboot      S13rpcbind      S50bluetooth
K50snmpd        K99rngd            S15mdmonitor    S55sshd
K50snmptrapd    S01sysstat        S20kdump        S58ntpd
K60nfs          S02lvm2-monitor    S22messagebus  S70spice-vdagentd

七、运行系统初始化脚本

    系统初始化脚本对应的文件是/etc/rc.d/rc.sysinit
    大概就是这个样子,因为太长,省略了很多

[root@localhost rc.d]# vim rc.sysinit 
 
#!/bin/bash
#
# /etc/rc.d/rc.sysinit - run once at boot time
#
# Taken in part from Miquel van Smoorenburg's bcheckrc.
#
 
HOSTNAME=$(/bin/hostname)
 
set -m
 
if [ -f /etc/sysconfig/network ]; then
    . /etc/sysconfig/network
fi
if [ -z "$HOSTNAME" -o "$HOSTNAME" = "(none)" ]; then
    HOSTNAME=localhost
fi
 
if [ ! -e /proc/mounts ]; then
        mount -n -t proc /proc /proc
        mount -n -t sysfs /sys /sys >/dev/null 2>&1
fi
######################后面的省略######################

其中主要的内容就是
#(1) 设置���机名;
#(2) 设置欢迎信息;
#(3) 激活udev和selinux; 
#(4) 挂载/etc/fstab文件中定义的文件系统;
#(5) 检测根文件系统,并以读写方式重新挂载根文件系统;
#(6) 设置系统时钟;
#(7) 激活swap设备;
#(8) 根据/etc/sysctl.conf文件设置内核参数;
#(9) 激活lvm及software raid设备;
#(10) 加载额外设备的驱动程序;
#(11) 清理操作;

八、启动系统服务
    centos6下的系统服务脚本都放在/etc/rc.d/init.d或者/etc/init.d中,可以看到两个文件中的内容是一样的,因为/etc/init.d链接到了/etc/rc.d/init.d中
[root@localhost init.d]# ll /etc/init.d
lrwxrwxrwx. 1 root root 11 2月  20 18:57 /etc/init.d -> rc.d/init.d
[root@localhost rc.d]# cd -
/etc/rc.d/init.d
[root@localhost init.d]# ls
abrt-ccpp        cups          iscsi              netfs          rdisc        spice-vdagentd
abrtd            dnsmasq      iscsid            network        restorecond  sshd
abrt-oops        ebtables      kdump              NetworkManager  rngd        sssd
acpid            firstboot    killall            nfs            rpcbind      svnserve
atd              functions    ksm                nfslock        rpcgssd      sysstat
auditd            haldaemon    ksmtuned          ntpd            rpcidmapd    udev-post
autofs            halt          libvirtd          ntpdate        rpcsvcgssd  wdaemon
blk-availability  hsqldb        libvirt-guests    numad          rsyslog      winbind
bluetooth        htcacheclean  lvm2-lvmetad      oddjobd        sandbox      wpa_supplicant
certmonger        httpd        lvm2-monitor      portreserve    saslauthd    ypbind
cgconfig          hypervkvpd    mdmonitor          postfix        single
cgred            ip6tables    messagebus        psacct          smartd
cpuspeed          iptables      netcf-transaction  quota_nld      snmpd
crond            irqbalance    netconsole        radvd          snmptrapd

    通过chkconfig命令可以查看什么系统级别下开启了哪些服务,以及添加服务到系统中并在指定级别下启动
    查看chkconfig --list

[root@localhost rc5.d]# chkconfig --list
NetworkManager  0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
abrt-ccpp      0:关闭 1:关闭 2:关闭 3:启用 4:关闭 5:启用 6:关闭
abrtd          0:关闭 1:关闭 2:关闭 3:启用 4:关闭 5:启用 6:关闭
acpid          0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
atd            0:关闭 1:关闭 2:关闭 3:启用 4:启用 5:启用 6:关闭
auditd          0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
autofs          0:关闭 1:关闭 2:关闭 3:启用 4:启用 5:启用 6:关闭

    添加chkconfig --add 服务名,在添加的时候需要将对应的服务放入/etc/init.d中,并在服务脚本中加入chkconfig: LLLL nn nn
    写到LLLL的init级别就是S的,没写得就是K的,第一个nn代表S的数字,第二个nn代表K的数字
[root@localhost init.d]# chkconfig --add httpd

    删除chkconfig --del 服务名
[root@localhost init.d]# chkconfig --del httpd

      【注意】:正常级别下,最后启动一个服务S99local没有链接至/etc/rc.d/init.d一个服务脚本,而是指向了/etc/rc.d/rc.local脚本;因此,不便或不需写为服务脚本放置于/etc/rc.d/init.d/目录,且又想开机时自动运行的命令,可直接放置于/etc/rc.d/rc.local文件中。
九、打印登录提示符
    系统在启动完服务后会打印登录提示符,然后输入账号密码,进入对应的命令行界面或图形界面。
十、总结
    系统的启动流程总的来说分成以下几个步骤:

    1、加电自检

    2、按照BIOS中设置的Boot Sequence查找有bootloader程序的设备

    3、加载MBR中的bootloader程序GRUB

    4、进入GRUB设置的默认kernel

    5、kernel初始化

    6、运行init程序

    7、运行系统初始化脚本

    8、启动对应服务

    9、打印登录提示符

以上只是本人对系统启动流程的理解,若有不对之处请指出,本人会对其进行修改。

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-09/135461.htm

linux
相关资讯       CentOS启动流程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款