17.1centos6启动流程

01.linux组成

    Linux: kernel+rootfs

        kernel: 进程管理、内存管理、网络管理、驱动程序、文件系统、安全功能 
        rootfs:程序和glibc
        库:函数集合, function, 调用接口(头文件负责描述)  
        过程调用:procedure,无返回值  
        函数调用:function

        程序:二进制执行文件 

        [root@centos6 boot ]#rpm -q kernel
        kernel-2.6.32-696.el6.x86_64

    内核设计流派:

        单内核(monolithic kernel):Linux
        把所有功能集成于同一个程序 
        微内核(micro kernel):Windows, Solaris
        每种功能使用一个单独子系统实现 

02. 内核

    Linux内核特点:

        支持模块化:.ko(内核对象)
  如:文件系统,硬件驱动,网络协议等 

        支持内核模块的动态装载和卸载   

        网卡:modprobe -r e1000;  modinfo e1000; ethtool -i eth0


     组成部分:

        核心文件:/boot/vmlinuz-VERSION-release
                ramdisk:辅助的伪根系统
                CentOS 5: /boot/initrd-VERSION-release.img 
                CentOS 6,7: /boot/initramfs-VERSION-release.img

        模块文件:/lib/modules/VERSION-release

     

[root@centos6 kernel ]#rpm -ql kernel | less
/boot/.vmlinuz-2.6.32-696.el6.x86_64.hmac
/boot/System.map-2.6.32-696.el6.x86_64
/boot/config-2.6.32-696.el6.x86_64
/boot/initramfs-2.6.32-696.el6.x86_64.img
/boot/symvers-2.6.32-696.el6.x86_64.gz
/boot/vmlinuz-2.6.32-696.el6.x86_64
/etc/ld.so.conf.d/kernel-2.6.32-696.el6.x86_64.conf
/lib/modules/2.6.32-696.el6.x86_64
/lib/modules/2.6.32-696.el6.x86_64/build
[root@centos6 boot ]#ls
config-2.6.32-696.el6.x86_64  initramfs-2.6.32-696.el6.x86_64.img  System.map-2.6.32-696.el6.x86_64
efi                           lost+found                           vmlinuz-2.6.32-696.el6.x86_64
grub                          symvers-2.6.32-696.el6.x86_64.gz
[root@centos6 app ]#ls
initramfs-2.6.32-696.el6.x86_64.img
[root@centos6 app ]#file initramfs-2.6.32-696.el6.x86_64.img 
initramfs-2.6.32-696.el6.x86_64.img: gzip compressed data, from Unix, last modified: Tue May 15 18:53:56 2018, max compression
[root@centos6 app ]#mv initramfs-2.6.32-696.el6.x86_64.img initramfs-2.6.32-696.el6.x86_64.img.gz
[root@centos6 app ]#ls
initramfs-2.6.32-696.el6.x86_64.img.gz
[root@centos6 app ]#gzip -d initramfs-2.6.32-696.el6.x86_64.img.gz 
[root@centos6 app ]#ls
initramfs-2.6.32-696.el6.x86_64.img
[root@centos6 app ]#ll initramfs-2.6.32-696.el6.x86_64.img /boot/initramfs-2.6.32-696.el6.x86_64.img -h
-rw-------. 1 root root 26M May 15 18:54 /boot/initramfs-2.6.32-696.el6.x86_64.img
-rw-------. 1 root root 75M Jun 27 21:34 initramfs-2.6.32-696.el6.x86_64.img
[root@centos6 app ]#
[root@centos6 app ]#file initramfs-2.6.32-696.el6.x86_64.img 
initramfs-2.6.32-696.el6.x86_64.img: ASCII cpio archive (SVR4 with no CRC)
[root@centos6 app ]#cpio -tv < initramfs-2.6.32-696.el6.x86_64.img
[root@centos6 app ]#cpio -tv < initramfs-2.6.32-696.el6.x86_64.img |less
drwxr-xr-x  26 root     root            0 May 15 18:53 .
drwxr-xr-x   8 root     root            0 May 15 18:53 etc
-rw-r--r--   1 root     root           28 Jul 25  2013 etc/ld.so.conf
-rw-r--r--   1 root     root           27 Mar 28  2017 etc/centos-release
drwxr-xr-x   3 root     root            0 May 15 18:53 etc/udev
drwxr-xr-x   2 root     root            0 May 15 18:53 etc/udev/rules.d
-rw-r--r--   1 root     root         4409 Sep  6  2016 etc/udev/rules.d/50-udev-default.rules
-rw-r--r--   1 root     root          316 Sep  6  2016 etc/udev/rules.d/60-cdrom_id.rules
-rw-r--r--   1 root     root         2092 May 12  2016 etc/udev/rules.d/65-md-incremental-imsm.rules
-rw-r--r--   1 root     root          779 May 12  2016 etc/udev/rules.d/61-dmraid-imsm.rules
-r--r--r--   1 root     root          492 Mar 23  2017 etc/udev/rules.d/95-dm-notify.rules



03. CentOS6启动流程

   

        1.加载BIOS的硬件信息,获取第一个启动设备

        2.读取第一个启动设备MBR的引导加载程序(grub)的启动信息

        3.加载核心操作系统的核心信息(/boot中的内核),核心开始解压缩,并尝试驱动所有的硬件设备

        4.核心执行init程序,并获取默认的运行信息

        5.init程序执行/etc/rc.d/rc.sysinit文件

        6.启动核心的外挂模块

        7.init执行运行的各个批处理文件(scripts)

        8.init执行/etc/rc.d/rc.local

        9.执行/bin/login程序,等待用户登录

        10.登录之后开始以Shell控制主机

        1)BIOS自检   

         basic input ouput system 基本输入输出系统

              POST:Power-On-Self-Test,加电自检,是BIOS功能的一个主要部分。负责完成对CPU、主  板、内存、硬盘子系统、显示子系统、串并行接口、键盘、CD-ROM光驱等硬件情况的检测。

            ROM:BIOS,Basic Input and Output System,保存着有关计算机系统最重要的基本输 入输出程序,系统信息设置、开机加电自检程序和系统启动自举程序等。

             RAM:CMOS互补金属氧化物半导体,保存各项参数的设定  按次序查找引导设备,第一个有引导程序的设备为本次启动设备, cmos 互补金属氧化物半导体 (Complementary Metal Oxide Semiconductor)  主板上的纽扣电池

            硬盘,光盘,usb(ubuntu),network

        ghost  pxelinux.0

           

            2)MBR

            Master boot record  = MBR (446 Boot Loader + 4* 16 分区表 + 55 AA )

            硬盘的0磁道0扇区的512字节存放MBR主引导记录

             ①)grub stage1

                         mbr的前446字节为bootloader,即在linux中mbr的446字节为grub的第一阶段开机记录

                           1、bootloader

                         类型:windows ntloader    lilo(linux loader)  grub grub2 macOS
                        功能:
                1.提供菜单 
                2.启动内核 
                3.转交给别的loader(linux)

                                  上述的windows ntloader(微软)不支持linux的lilo类型,而linux的lilo兼容windows的ntloader。在mbr后续的安装系统内核的分区中也有一个ntloader备份。则当安装一个操作系统时(假设为windows7),则mbr和后续分区各有一个win7的ntloader。再安装linux,mbr中的win7ntloader被linux的lilo覆盖,lilo兼容win7的ntloader,这样,在mbr的446字节中有lilo兼容win7的ntloader。而后续的两个分区中则各有win7nloader和linux的lilo,则开机可以选择不同的系统。而反过来先安装linux,则mbr中为win7的ntloader,但不兼容liunx的lilo,但可以修改mbr中的bootloader为linuxlilo。

        3)grub 

        
[root@centos6 app ]#grub
Probing devices to guess BIOS drives. This may take a long time.
Unknown partition table signature
    GNU GRUB  version 0.97  (640K lower / 3072K upper memory)
 [ Minimal BASH-like line editing is supported.  For the first word, TAB
   lists possible command completions.  Anywhere else TAB lists the possible
   completions of a device/filename.]
grub> quit
quit
[root@centos6 app ]#rpm -q grub
grub-0.97-99.el6.x86_64
        grub的stage1_5(mbr中存放不下1.5阶段,故而存放到mbr后续的分区中了)和stage2,即1.5阶段和2阶段
            1.5阶段中是一些驱动程序,1阶段中记录2阶段用到的/boot的文件系统的驱动模块在磁盘的位置,然后加载驱动,即1.5阶段,里面包含ext4文件系统驱动,从而识别/boot文件系统,进而找到2阶段(/boot/grub/grub.conf开机选项菜单,及其它grub文件)
           secondary boot loader :2ndstage,分区文件

    

[root@centos6 boot ]#df -hT
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/sda2      ext4    49G  4.0G   42G   9% /
tmpfs          tmpfs  931M   72K  931M   1% /dev/shm
/dev/sda3      ext4    20G  119M   19G   1% /app
/dev/sda1      ext4   976M   35M  891M   4% /boot
[root@centos6 boot ]#locate ext4.ko
/lib/modules/2.6.32-696.el6.x86_64/kernel/fs/ext4/ext4.ko

        4)加载内核(/boot/vm***)。加载系统的根(/)但根文件系统的驱动如下,在/下的lib中,即著名的鸡蛋问题(先有鸡还是先有蛋)

[root@centos6 boot ]#ll /lib/modules/2.6.32-696.el6.x86_64/kernel/fs/ext4/ext4.ko -h
-rwxr--r--. 1 root root 633K Mar 22  2017 /lib/modules/2.6.32-696.el6.x86_64/kernel/fs/ext4/ext4.ko
而解决办法是伪根系统 ramfs  即/boot下的initramfs-2.6.32-696.el6.x86_64.img如下:此包中有驱动,具体查看过程见本文最上面。/boot的文件系统驱动,与此处/的文件系统驱动由不用位置的驱动模块加载识别ext4
[root@centos6 boot ]#ls
config-2.6.32-696.el6.x86_64  initramfs-2.6.32-696.el6.x86_64.img  System.map-2.6.32-696.el6.x86_64
efi                           lost+found                           vmlinuz-2.6.32-696.el6.x86_64
grub                          symvers-2.6.32-696.el6.x86_64.gz
[root@centos6 boot ]#

      执行init程序

      5)/etc/rc.d/rc.sysinit文件

      

        v  init程序的类型:

        v  SysV: init, CentOS 5之前 

                    配置文件:/etc/inittab

        v  Upstart: init,CentOS 6

                    配置文件:/etc/inittab, /etc/init/*.conf 

        v  Systemd:systemd, CentOS 7

                    配置文件:/usr/lib/systemd/system

            /etc/systemd/system 

                     rc.sysinit中是一些脚本,执行一系列初始化操作

        

        init (Upstart)  ubuntu (centos6)

不关联服务,并行启动,有关联关系的,按顺序启动,启动速度比redhat5快一些

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

            如下redhat5,centos6的初始化init包的配置文件 /etc/inittab

[root@centos6 ~ ]#which init
/sbin/init
[root@centos6 ~ ]#rpm -qf /sbin/init
upstart-0.6.5-16.el6.x86_64
[root@centos6 ~ ]#rpm -qi upstart
Name        : upstart                      Relocations: (not relocatable)
Version     : 0.6.5                             Vendor: CentOS
Release     : 16.el6                        Build Date: Wed 11 May 2016 05:13:32 AM CST
Install Date: Tue 15 May 2018 06:46:25 PM CST      Build Host: worker1.bsys.centos.org
Group       : System Environment/Base       Source RPM: upstart-0.6.5-16.el6.src.rpm
Size        : 567729                           License: GPLv2 and LGPLv2+
Signature   : RSA/SHA1, Thu 12 May 2016 06:49:14 PM CST, Key ID 0946fca2c105b9de
Packager    : CentOS BuildSystem <http://bugs.centos.org>
URL         : http://upstart.ubuntu.com
Summary     : An event-driven init system
Description :
Upstart is an event-based replacement for the /sbin/init daemon which
handles starting of tasks and services during boot, stopping them
during shutdown and supervising them while the system is running.
[root@centos6 ~ ]#     
[root@centos7test:~ ]# which init
/usr/sbin/init
[root@centos7test:~ ]# ll /usr/sbin/init
lrwxrwxrwx. 1 root root 22 May 15 18:53 /usr/sbin/init -> ../lib/systemd/systemd
[root@centos7test:~ ]# 

       如下redhat5

[root@redhat5 etc ]#which init
/sbin/init
[root@redhat5 etc ]#rpm -qf /sbin/init
SysVinit-2.86-15.el5
[root@redhat5 etc ]#rpm -qi init
package init is not installed
[root@redhat5 etc ]#rpm -qi SysVinit
Name        : SysVinit                     Relocations: (not relocatable)
Version     : 2.86                              Vendor: Red Hat, Inc.
Release     : 15.el5                        Build Date: Thu 18 Sep 2008 04:53:51 AM CST
Install Date: Tue 02 Nov 2010 06:13:43 PM CST      Build Host: hs20-bc2-4.build.redhat.com
Group       : System Environment/Base       Source RPM: SysVinit-2.86-15.el5.src.rpm
Size        : 194173                           License: GPL
Signature   : DSA/SHA1, Wed 10 Dec 2008 08:42:12 PM CST, Key ID 5326810137017186
Packager    : Red Hat, Inc. <http://bugzilla.redhat.com/bugzilla>
Summary     : Programs which control basic system processes.
Description :
The SysVinit package contains a group of processes that control
the very basic functions of your system. SysVinit includes the init
program, the first program started by the Linux kernel when the
system boots. Init then controls the startup, running, and shutdown
of all other programs.
[root@redhat5 etc ]#

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

            /etc/inittab中有程序的第一个系统初始化脚本 sysinit:/etc/rc.d/rc.sysinit前的配置文件inittab

[root@redhat5 etc ]#cat 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"

# If power was restored before the shutdown kicked in, cancel it.
pr:12345:powerokwait:/sbin/shutdown -c "Power Restored; Shutdown Cancelled"


# Run gettys in standard runlevels
1:2345:respawn:/sbin/mingetty tty1   ctrl+alt+f1  2345级别,init2345   登录界面进程,杀不死,  respawn重生
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  运行图形界面在5级别
x:5:respawn:/etc/X11/prefdm -nodaemon
[root@redhat5 etc ]#
[root@redhat5 etc ]#
[root@centos6 media ]#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)   若默认为0 ,则开机又关机了
#   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)   若默认6 ,则开机反复重启
# 
id:5:initdefault:                            开机默认为5级别  即图像化界面,可以修改5为1,2,3,4
[root@centos6 media ]#
[root@centos6 ~ ]#cat /etc/init/rcS.conf           
# rcS - runlevel compatibility
#
# This task runs the old sysv-rc startup scripts.
#
# Do not edit this file directly. If you want to change the behaviour,
# please create a file rcS.override and put your changes there.

start on startup

stop on runlevel

task

# 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
pre-start script
	for t in $(cat /proc/cmdline); do
		case $t in
			emergency)
				start rcS-emergency
				break
			;;
		esac
	done
end script
exec /etc/rc.d/rc.sysinit                                  centos6和redhat5一样,都是初始化此脚本
post-stop script
	if [ "$UPSTART_EVENTS" = "startup" ]; then
		[ -f /etc/inittab ] && runlevel=$(/bin/awk -F ':' '$3 == "initdefault" && $1 !~ "^#" { print $2 }' /etc/inittab)
		[ -z "$runlevel" ] && runlevel="3"
		for t in $(cat /proc/cmdline); do
			case $t in
				-s|single|S|s) runlevel="S" ;;    单用户模式,1,-s,singgle,S,s都代表单用户模式
				[1-9])       runlevel="$t" ;;
			esac
		done
		exec telinit $runlevel
	fi
end script
[root@centos6 ~ ]#

v  运行级别:为系统运行或维护等目的而设定;0-6:7个级别

        0:关机

        1:单用户模式(root自动登录), single, 维护模式 类似window的安全模式。不加载验证模块,可直接进去,root身份,可以直接改root密吗。好多不必要的进程不加载

        2: 多用户模式,启动网络功能,但不会启动NFS;维护模式

        3:多用户模式,正常模式;文本界面

        4:预留级别;可同3级别

        5:多用户模式,正常模式;图形界面

        6:重启

v  默认级别: 3, 5 

v  切换级别:init #

v  查看级别:runlevel ; who -r 


[root@redhat5 etc ]#runlevel
N 5
[root@redhat5 etc ]#who -r
         run-level 5  2018-06-28 08:44                   last=S
[root@redhat5 etc ]#
[root@centos6 ~ ]#runlevel
N 5
[root@centos6 ~ ]#who -r
         run-level 5  2018-06-28 10:53
[root@centos6 ~ ]#

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

        执行系统初始化第一个脚本/etc/rc.d/rc.sysinit

[root@redhat5 etc ]#vim /etc/rc.d/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`
HOSTTYPE=`uname -m`
unamer=`uname -r`


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
           *******省略********
[root@centos6 media ]#vim /etc/rc.d/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
*********省略*********
------------------------------------------------------------------------------

        [root@redhat5 etc ]#cat inittab中有/etc/rc.d/rc脚本

[root@centos6 rc.d ]#vim rc
# 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=$subsys
done

# Now run the START scripts.
for i in /etc/rc$runlevel.d/S* ; do

上面的/etc/rc$runlevel.d/目录下有不同运行级别开机默认启动的服务

[root@centos6 subsys ]#i=/etc/rc$runlevle.d/K05atd
[root@centos6 subsys ]#subsys=${i#/etc/rc$runlevel.d/K??}
[root@centos6 subsys ]#echo $subsys
atd
[root@centos6 subsys ]#


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

        ⑤ 不用运行模式的服务

[root@centos6 etc ]#pwd;cd rc3.d;ls
/etc
K01smartd        K15httpd           K61nfs-rdma    K76ypbind          K92iptables      S05rdma         S13cpuspeed        S24nfslock           S26haldaemon  S82abrtd
K02oddjobd       K15svnserve        K69rpcsvcgssd  K84wpa_supplicant  K92pppoe-server  S08ip6tables    S13irqbalance      S24rpcgssd           S26udev-post  S83abrt-ccpp
K05wdaemon       K30spice-vdagentd  K73winbind     K87restorecond     K95firstboot     S10network      S13rpcbind         S25blk-availability  S28autofs     S90crond
K10psacct        K50dnsmasq         K74ntpd        K88sssd            K99rngd          S11auditd       S15mdmonitor       S25cups              S50bluetooth  S95atd
K10saslauthd     K50kdump           K75ntpdate     K89netconsole      S01sysstat       S11portreserve  S22messagebus      S25netfs             S55sshd       S99certmonger
K15htcacheclean  K60nfs             K75quota_nld   K89rdisc           S02lvm2-monitor  S12rsyslog      S23NetworkManager  S26acpid             S80postfix    S99local
[root@centos6 rc3.d ]#

/etc/rc3.d/ 等是软链接

[root@centos6 etc ]#ll | grep rc?d
lrwxrwxrwx.  1 root root     11 May 15 18:46 init.d -> rc.d/init.d   /etc/rd.d/rc.sysinit执行所有脚本的原始路径
lrwxrwxrwx.  1 root root      7 May 15 18:48 rc -> rc.d/rc
lrwxrwxrwx.  1 root root     10 May 15 18:48 rc0.d -> rc.d/rc0.d
lrwxrwxrwx.  1 root root     10 May 15 18:48 rc1.d -> rc.d/rc1.d
lrwxrwxrwx.  1 root root     10 May 15 18:48 rc2.d -> rc.d/rc2.d
lrwxrwxrwx.  1 root root     10 May 15 18:48 rc3.d -> rc.d/rc3.d
lrwxrwxrwx.  1 root root     10 May 15 18:48 rc4.d -> rc.d/rc4.d
lrwxrwxrwx.  1 root root     10 May 15 18:48 rc5.d -> rc.d/rc5.d
lrwxrwxrwx.  1 root root     10 May 15 18:48 rc6.d -> rc.d/rc6.d
drwxr-xr-x. 10 root root   4096 Jun 28 11:32 rc.d
lrwxrwxrwx.  1 root root     13 May 15 18:48 rc.local -> rc.d/rc.local
lrwxrwxrwx.  1 root root     15 May 15 18:48 rc.sysinit -> rc.d/rc.sysinit
[root@centos6 etc ]#

/etc/rc3.d/下的服务也是软链接

[root@centos6 rc3.d ]#pwd;ll
/etc/rc3.d
total 0
lrwxrwxrwx. 1 root root 16 May 15 18:51 K01smartd -> ../init.d/smartd
lrwxrwxrwx. 1 root root 17 May 15 18:49 K02oddjobd -> ../init.d/oddjobd

系统通过不同的运行模式(init 3 ,5等)不同模式可以通过软链接控制同一个地方的服务的启动与关闭

    chkconfig [--level levels] name<on|off|reset> 

            --levelLLLL: 指定要设置的级别;省略时表示2345 。此命令其实就是更改不同级别下的rc[2-5]\.d的文件由K开头变为S开头 ,K是未启动的服务,S是启动的服务。如K05atd变为S94atd。可以通过命令,也可以直接更改文件

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

         /var/lock/subsys 锁文件

通过判断此目录下的文件是否存在,从而知道是否开启此服务

[root@centos6 rc3.d ]#cd /var/lock/subsys/
[root@centos6 subsys ]#ls
abrt-ccpp  acpid  auditd  blk-availability  crond  haldaemon  local         messagebus  network         postfix  rpcbind    rsyslog
abrtd      atd    autofs  certmonger        cups   ip6tables  lvm2-monitor  netfs       NetworkManager  rdma     rpc.statd  sshd
[root@centos6 subsys ]#

    空文件,就是看存不存在,从而知道服务是否开启,存在为开启

[root@centos6 subsys ]#ll
total 0
-rw-r--r--. 1 root root 0 Jun 28 10:54 abrt-ccpp
-rw-r--r--. 1 root root 0 Jun 28 10:54 abrtd
-rw-r--r--. 1 root root 0 Jun 28 10:53 acpid
*****省略******

 假设5模式下atd服务未启动,并且确认atd进程不存在,执行init3,则会执行/etc/rc3.d/目录下的所有以S开头的服务,而以S开头的服务若有atd,则在锁文件下回生成一个atd文件


/etc/init.d/目录下的脚本其实服务

[root@centos6 subsys ]#/etc/init.d/atd start
[root@centos6 subsys ]#/etc/init.d/atd stop
Stopping atd:                                              [  OK  ]
[root@centos6 subsys ]#service atd start
Starting atd:                                              [  OK  ]
[root@centos6 subsys ]#

        自定义服务脚本

    参考/etc/init.d/下的atd服务

[root@centos6 init.d ]#cat atd
#!/bin/sh
#
# atd Starts/stop the "at" daemon
#
# chkconfig:   345 95 5      表示3,4,5模式。S95atd  K05atd
# description: Runs commands scheduled by the "at" command at the time \
#    specified when "at" was run, and runs batch commands when the load \
#    average is low enough.

### BEGIN INIT INFO

在/etc/init.d/下vim  testsrv  如下:下面的自定义服务写的不全面,仅学习

[root@centos6 init.d ]#cat testsrv
#!/bin/bash
#chkconfig: 35 96 3
#description: test service
source /etc/init.d/functions
case $1 in
start)
	[ -f /var/lock/subsys/testsrv ] && action "testsrv is started"
	touch /var/lock/subsys/testsrv
	action "echo testsrv is starting"
	;;
stop)
	rm -f /var/lock/subsys/testsrv
	action "echo testsrv is stopped"
	;;
status)
	[ -f /var/lock/subsys/testsrv ] && testsrv is starting || echo "testsrv is stopped"
	;;
*)
	echo "Usage: service testsrv start|stop|status"
	;;
esac
[root@centos6 init.d ]#
[root@centos6 init.d ]#/etc/init.d/testsrv start
echo testsrv is starting                                   [  OK  ]
[root@centos6 init.d ]#/etc/init.d/testsrv stop
echo testsrv is stopped                                    [  OK  ]
[root@centos6 init.d ]#/etc/init.d/testsrv start
echo testsrv is starting                                   [  OK  ]
[root@centos6 init.d ]#/etc/init.d/testsrv start
testsrv is started                                         [  OK  ]
echo testsrv is starting                                   [  OK  ]
[root@centos6 init.d ]#/etc/init.d/testsrv stop
echo testsrv is stopped                                    [  OK  ]
[root@centos6 init.d ]#
[root@centos6 init.d ]#cd /var/lock/subsys/
[root@centos6 subsys ]#ls
abrt-ccpp  atd     blk-availability  cups       local         netfs           postfix  rpc.statd
abrtd      auditd  certmonger        haldaemon  lvm2-monitor  network         rdma     rsyslog
acpid      autofs  crond             ip6tables  messagebus    NetworkManager  rpcbind  sshd
[root@centos6 subsys ]#/etc/init.d/testsrv start
echo testsrv is starting                                   [  OK  ]
[root@centos6 subsys ]#ls
abrt-ccpp  auditd            crond      local         network         rpcbind    testsrv
abrtd      autofs            cups       lvm2-monitor  NetworkManager  rpc.statd
acpid      blk-availability  haldaemon  messagebus    postfix         rsyslog
atd        certmonger        ip6tables  netfs         rdma            sshd
[root@centos6 subsys ]#
此时各个模式下(init3等)并没有testsrv,需要将testsrv加到服务里面
[root@centos6 subsys ]#chkconfig --add testsrv
[root@centos6 subsys ]#chkconfig --list testsrv 
testsrv        	0:off	1:off	2:off	3:on	4:off	5:on	6:off
[root@centos6 subsys ]#
[root@centos6 subsys ]#cd /etc/rc3.d
[root@centos6 rc3.d ]#ls
K01smartd          K69rpcsvcgssd      K95firstboot     S15mdmonitor         S55sshd
K02oddjobd         K73winbind         K99rngd          S22messagebus        S80postfix
K05wdaemon         K74ntpd            S01sysstat       S23NetworkManager    S82abrtd
K10psacct          K75ntpdate         S02lvm2-monitor  S24nfslock           S83abrt-ccpp
K10saslauthd       K75quota_nld       S05rdma          S24rpcgssd           S90crond
K15htcacheclean    K76ypbind          S08ip6tables     S25blk-availability  S95atd
K15httpd           K84wpa_supplicant  S10network       S25cups              S96testsrv
K15svnserve        K87restorecond     S11auditd        S25netfs             S99certmonger
K30spice-vdagentd  K88sssd            S11portreserve   S26acpid             S99local
K50dnsmasq         K89netconsole      S12rsyslog       S26haldaemon
K50kdump           K89rdisc           S13cpuspeed      S26udev-post
K60nfs             K92iptables        S13irqbalance    S28autofs
K61nfs-rdma        K92pppoe-server    S13rpcbind       S50bluetooth
[root@centos6 rc3.d ]#chkconfig --level 3 testsrv off
[root@centos6 rc3.d ]#ls
K01smartd          K61nfs-rdma        K92pppoe-server  S13rpcbind           S50bluetooth
K02oddjobd         K69rpcsvcgssd      K95firstboot     S15mdmonitor         S55sshd
K03testsrv         K73winbind         K99rngd          S22messagebus        S80postfix
K05wdaemon         K74ntpd            S01sysstat       S23NetworkManager    S82abrtd
K10psacct          K75ntpdate         S02lvm2-monitor  S24nfslock           S83abrt-ccpp
K10saslauthd       K75quota_nld       S05rdma          S24rpcgssd           S90crond
K15htcacheclean    K76ypbind          S08ip6tables     S25blk-availability  S95atd
K15httpd           K84wpa_supplicant  S10network       S25cups              S99certmonger
K15svnserve        K87restorecond     S11auditd        S25netfs             S99local
K30spice-vdagentd  K88sssd            S11portreserve   S26acpid
K50dnsmasq         K89netconsole      S12rsyslog       S26haldaemon
K50kdump           K89rdisc           S13cpuspeed      S26udev-post
K60nfs             K92iptables        S13irqbalance    S28autofs
[root@centos6 rc3.d ]#

开机自启如下:

[root@centos6 rc3.d ]#chkconfig --list testsrv
testsrv        	0:off	1:off	2:off	3:off	4:off	5:on	6:off
[root@centos6 rc3.d ]#chkconfig testsrv on
[root@centos6 rc3.d ]#chkconfig --list testsrv
testsrv        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
[root@centos6 rc3.d ]#
要是重启,会由于此服务导致机器起不来,可以重启进入单用户模式,把此服务设为开机不启动

        而centos6上面还有一个S99local服务,并且每个模式下(init2345)都有,看其描述可以自定义脚本。centos7上面此服务脚本没有执行权限,若需要运行可以加执行权限(应该快淘汰了,过渡作用,照护老运维)。

lrwxrwxrwx. 1 root root 11 May 15 18:48 /etc/rc3.d/S99local -> ../rc.local
[root@centos6 etc ]#ll /etc/rc.local
lrwxrwxrwx. 1 root root 13 May 15 18:48 /etc/rc.local -> rc.d/rc.local
[root@centos6 etc ]#cat /etc/rc.d/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
[root@centos6 etc ]#

v  注意:正常级别下,最后启动一个服务S99local没有链接至/etc/rc.d/init.d一个服务脚本,而是指向了/etc/rc.d/rc.local脚本

v  服务脚本放置于/etc/rc.d/init.d/目录;自己编写又想开机时自动运行  的命令,可直接放置于/etc/rc.d/rc.local文件中

v  centos6总结:/sbin/init --> (/etc/inittab) --> 设置默认运行级别 --> 运行系统初始 脚本、完成系统初始化 --> (关闭对应下需要关闭的服务)启动需要启动服务 --  > 设置登录终端

v  CentOS 6 init程序为: upstart, 其配置文件:

                    /etc/inittab,/etc/init/*.conf,配置文件的语法 遵循upstart配置文件语 法格式,和CentOS5不同


centos6的开机流程中国的grub1 ,1_5,2阶段:
grub1阶段和1.5阶段是存放在磁盘上,以二进制形式存放,1.5阶段的xfs驱动加载/boot所在分区的文件xfs文件系统
而后加载/boot/grub/阶段里面的/boot/grub/grub.conf文件的内容,找到/boot/vmlinuz,initramfs,vmlinuz及其后的root开始挂载硬盘的根/即本虚拟机的/dev/sda2;/boot/grub/grub.conf下指定的initrd路径,再加载/boot/initramfs,initramfs放的是/文件系统的驱动,加载/根文件系统的驱动
另外,/boot的文件系统可能和/的文件系统不一样,不过此虚拟机的/boot和/都是xfs文件系统





猜你喜欢

转载自blog.csdn.net/csdn_immortal/article/details/80833968
今日推荐