内容:
CentOS6 的启动流程
服务管理(chkconfig)
手动制定系统开机服务脚本
一、centos6系统启动流程
综述过程:
POST–>BIOS(Boot Sequence)–>MBR(grub,446)–>Kernel–>initramfs(虚根文件)–>(ROOTFS)/sbin/init(/etc/inittab)
步骤详解:
1、POST:
POST:Power-On-Self-Test,加电自检,是BIOS功能的一个主要部分。负责完成对CPU、主板、内存、硬盘子系统、显示子系统、串并行接口、键盘、CD-ROM光驱等硬件情况的检测。
2、BIOS:
决定设备的启动顺序
3、grub(bootloader)
在centos5、6中,linux系统的MBR的bootloader是grub程序,windows的bootloader程序是ntloader:
windows: ntloader,仅是启动OS
Linux:功能丰富,提供菜单,允许用户选择要启动系统或不同的内核版本;把用户选定的内核装载到内存中的特定空间中,解压、展开,并把系统控制权移交给内核
由于linux的grub提供了丰富的功能,而MBR的bootloader仅仅446个字节,所以grub有分成了两个阶段,其中阶段二又有两部分(stage1_5,stage2):
stage1:bootloader,446个字节,作用就是为了引导下一个阶段
stage1_5:此阶段的文件在初次安装系统时生成,只要是boot分区的文件系统驱动程序,作用是为了加载boot分区
[19:13 root@Centos6.8/etc/init.d]# ll /boot/grub/*5 -rw-r--r--. 1 root root 13428 Jul 20 02:24 /boot/grub/e2fs_stage1_5 -rw-r--r--. 1 root root 12636 Jul 20 02:24 /boot/grub/fat_stage1_5 -rw-r--r--. 1 root root 11780 Jul 20 02:24 /boot/grub/ffs_stage1_5 -rw-r--r--. 1 root root 11772 Jul 20 02:24 /boot/grub/iso9660_stage1_5 -rw-r--r--. 1 root root 13284 Jul 20 02:24 /boot/grub/jfs_stage1_5 -rw-r--r--. 1 root root 11972 Jul 20 02:24 /boot/grub/minix_stage1_5 -rw-r--r--. 1 root root 14428 Jul 20 02:24 /boot/grub/reiserfs_stage1_5 -rw-r--r--. 1 root root 12040 Jul 20 02:24 /boot/grub/ufs2_stage1_5 -rw-r--r--. 1 root root 11380 Jul 20 02:24 /boot/grub/vstafs_stage1_5 -rw-r--r--. 1 root root 13980 Jul 20 02:24 /boot/grub/xfs_stage1_5
stage2:此阶段的作用是提供选择菜单(如果有多个内核系统),引导启动下一步的kernel内核启动和加载initramfs文件(作用是提供真正根分区的文件系统驱动程序,以便能加载根分区),其配置文件为/etc/grub.conf
[19:16 root@Centos6.8/etc/init.d]# cat /etc/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/sda2 # initrd /initrd-[generic-]version.img #boot=/dev/sda default=0 timeout=5 splashimage=(hd0,0)/grub/splash.xpm.gz hiddenmenu title CentOS 6 (2.6.32-642.el6.x86_64) root (hd0,0) kernel /vmlinuz-2.6.32-642.el6.x86_64 ro root=UUID=81d6eec4-2af7-4166-bfb6-092432abb4c0 rd_NO_LUKS rd_NO_LVM.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=128M KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet initrd /initramfs-2.6.32-642.el6.x86_64.img
说明: default:设定默认启动菜单项,默认为0开始 timeout:选择启动内核的停留时间 splashimage:启动的背景图片 hiddenmenu:隐藏选择菜单 title:启动时看到的选择内核的名称 root(hd0,0):第一块硬盘,第一个分区,根系统的分区位置 kernel:内核文件的路径,此时会把boot文件当跟文件,所以不需要写成/boot/PATH initrd:根分区文件系统的驱动程序路径 由于在grub阶段只能进入了boot分区,所以kernel内核和initramfs文件都存在在boot分区
4、kernel
kernel内核阶段会把内核文件进行解压,初始化系统中各设备并做相关的配置工作,其中包括CPU、I/O、存储设备等。关于Linux的设备驱动程序的加载,有一部分驱动程序直接被编译进内核镜像中,另一部分驱动程序则是以模块的形式放在initrd(ramdisk)中。
initramfs的作用前面已经说明,下一步就是加载真正的/根分区
5、/sbin/init
(1)进入根分区后,会运行第一个进程init,PID为1,/sbin/init进程是系统其他所有进程的父进程,当它接管了系统的控制权先之后,它首先会去读取/etc/inittab文件来执行相应的脚本进行系统初始化
[19:18 root@Centos6.8/etc/init.d]# 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) # 3 - Full multiuser mode # 4 - unused # 5 - X11 # 6 - reboot (Do NOT set initdefault to this) # id:3:initdefault:
运行级别:为系统运行或维护等目的而设定;0-6:7个级别
0:关机
1:单用户模式(root自动登录), single, 维护模式,该模式就是所谓的破解root密码
2: 多用户模式,启动网络功能,但不会启动NFS;维护模式
3:多用户模式,正常模式;文本界面
4:预留级别;可同3级别
5:多用户模式,正常模式;图形界面
6:重启
默认级别:3, 5
切换级别:init#
查看级别:runlevel; who -r
[20:20 root@Centos6.8/etc/init.d]# runlevel N 5 #N表示上次运行的级别,5表示切换的本次的运行级别
(2)运行/etc/rc.d/rc.sysinit脚本,此脚本的大概功能如下
(1) 设置主机名
(2) 设置欢迎信息
(3) 激活udev和selinux
(4) 挂载/etc/fstab文件中定义的文件系统
(5) 检测根文件系统,并以读写方式重新挂载根文件系统
(6) 设置系统时钟
(7) 激活swap设备
(8) 根据/etc/sysctl.conf文件设置内核参数
(9) 激活lvm及software raid设备
(10) 加载额外设备的驱动程序
(11) 清理操作
(3)sysinit脚本执行完成,系统就可以顺利工作了,只是还需要启动系统所需要的各种服务,这样主机才可以提供相关的网络和主机功能,因此还会根据相应的运行级别,运行相应的系统服务。
执行/etc/rc.d/rc脚本。该文件定义了服务启动的顺序是先K后S,而具体的每个运行级别的服务状态是放在/etc/rc.d/rc*.d(*=0~6)目录下,所有的文件均是指向/etc/init.d下相应文件的符号链接。rc.sysinit通过分析/etc/inittab文件来确定系统的启动级别,然后才去执行/etc/rc.d/rcN.d下的文件。
以rc3.d为例:
/etc/rc.d/rc3.d目录,该目录下的内容全部都是以 S 或 K 开头的链接文件,都链接到"/etc/rc.d/init.d"目录下的各种shell脚本。S表示的是启动时需要start的服务内容,K表示关机时需要关闭的服务内容,后面的数字代表优先次序。
[19:45 root@Centos6.8/etc/init.d]# ll /etc/rc.d/rc3.d/ total 0 lrwxrwxrwx. 1 root root 16 Jul 20 02:20 K01smartd -> ../init.d/smartd lrwxrwxrwx. 1 root root 17 Jul 20 02:18 K02oddjobd -> ../init.d/oddjobd lrwxrwxrwx. 1 root root 17 Jul 20 02:22 K05wdaemon -> ../init.d/wdaemon lrwxrwxrwx. 1 root root 16 Jul 20 02:21 K10psacct -> ../init.d/psacct lrwxrwxrwx. 1 root root 19 Jul 20 02:18 K10saslauthd -> ../init.d/saslauthd lrwxrwxrwx. 1 root root 22 Jul 20 02:18 K15htcacheclean -> ../init.d/htcacheclean lrwxrwxrwx. 1 root root 15 Jul 20 02:18 K15httpd -> ../init.d/httpd lrwxrwxrwx. 1 root root 18 Aug 26 19:04 K15svnserve -> ../init.d/svnserve lrwxrwxrwx. 1 root root 24 Jul 20 02:20 K30spice-vdagentd -> ../init.d/spice-vdagentd lrwxrwxrwx. 1 root root 16 Jul 20 02:18 K36mysqld -> ../init.d/mysqld lrwxrwxrwx. 1 root root 17 Jul 20 02:17 K50dnsmasq -> ../init.d/dnsmasq lrwxrwxrwx. 1 root root 13 Jul 20 02:16 K60nfs -> ../init.d/nfs lrwxrwxrwx. 1 root root 18 Jul 20 02:20 K61nfs-rdma -> ../init.d/nfs-rdma lrwxrwxrwx. 1 root root 20 Jul 20 02:16 K69rpcsvcgssd -> ../init.d/rpcsvcgssd lrwxrwxrwx. 1 root root 17 Jul 20 02:16 K73winbind -> ../init.d/winbind lrwxrwxrwx. 1 root root 14 Jul 20 03:15 K74ntpd -> ../init.d/ntpd lrwxrwxrwx. 1 root root 17 Jul 20 02:17 K75ntpdate -> ../init.d/ntpdate lrwxrwxrwx. 1 root root 19 Jul 20 02:20 K75quota_nld -> ../init.d/quota_nld lrwxrwxrwx. 1 root root 16 Jul 20 02:17 K76ypbind -> ../init.d/ypbind lrwxrwxrwx. 1 root root 24 Sep 1 17:09 K84NetworkManager -> ../init.d/NetworkManager lrwxrwxrwx. 1 root root 24 Jul 20 02:17 K84wpa_supplicant -> ../init.d/wpa_supplicant lrwxrwxrwx. 1 root root 21 Jul 20 02:15 K87restorecond -> ../init.d/restorecond lrwxrwxrwx. 1 root root 14 Jul 20 02:16 K88sssd -> ../init.d/sssd lrwxrwxrwx. 1 root root 20 Jul 20 02:15 K89netconsole -> ../init.d/netconsole lrwxrwxrwx. 1 root root 15 Jul 20 02:15 K89rdisc -> ../init.d/rdisc lrwxrwxrwx. 1 root root 22 Jul 20 02:17 K92pppoe-server -> ../init.d/pppoe-server lrwxrwxrwx. 1 root root 19 Jul 20 03:15 K95firstboot -> ../init.d/firstboot lrwxrwxrwx. 1 root root 14 Jul 20 02:20 K95rdma -> ../init.d/rdma lrwxrwxrwx. 1 root root 14 Jul 20 02:20 K99rngd -> ../init.d/rngd lrwxrwxrwx. 1 root root 17 Jul 20 02:19 S01sysstat -> ../init.d/sysstat lrwxrwxrwx. 1 root root 22 Jul 20 02:20 S02lvm2-monitor -> ../init.d/lvm2-monitor lrwxrwxrwx. 1 root root 19 Jul 20 02:18 S08ip6tables -> ../init.d/ip6tables lrwxrwxrwx. 1 root root 18 Jul 20 02:15 S08iptables -> ../init.d/iptables lrwxrwxrwx. 1 root root 17 Jul 20 23:42 S10network -> ../init.d/network lrwxrwxrwx. 1 root root 16 Jul 20 02:21 S11auditd -> ../init.d/auditd lrwxrwxrwx. 1 root root 21 Jul 20 02:12 S11portreserve -> ../init.d/portreserve lrwxrwxrwx. 1 root root 17 Jul 20 02:17 S12rsyslog -> ../init.d/rsyslog lrwxrwxrwx. 1 root root 18 Jul 20 02:20 S13cpuspeed -> ../init.d/cpuspeed lrwxrwxrwx. 1 root root 20 Jul 20 02:18 S13irqbalance -> ../init.d/irqbalance lrwxrwxrwx. 1 root root 17 Jul 20 02:13 S13rpcbind -> ../init.d/rpcbind lrwxrwxrwx. 1 root root 19 Jul 20 02:16 S15mdmonitor -> ../init.d/mdmonitor lrwxrwxrwx. 1 root root 20 Jul 20 02:13 S22messagebus -> ../init.d/messagebus lrwxrwxrwx. 1 root root 17 Jul 20 02:18 S24nfslock -> ../init.d/nfslock lrwxrwxrwx. 1 root root 16 Jul 20 02:17 S24openct -> ../init.d/openct lrwxrwxrwx. 1 root root 17 Jul 20 02:18 S24rpcgssd -> ../init.d/rpcgssd lrwxrwxrwx. 1 root root 26 Jul 20 02:20 S25blk-availability -> ../init.d/blk-availability lrwxrwxrwx. 1 root root 14 Jul 20 02:15 S25cups -> ../init.d/cups lrwxrwxrwx. 1 root root 15 Jul 20 02:15 S25netfs -> ../init.d/netfs lrwxrwxrwx. 1 root root 15 Jul 20 02:20 S26acpid -> ../init.d/acpid lrwxrwxrwx. 1 root root 19 Jul 20 02:17 S26haldaemon -> ../init.d/haldaemon lrwxrwxrwx. 1 root root 19 Jul 20 02:15 S26udev-post -> ../init.d/udev-post lrwxrwxrwx. 1 root root 15 Jul 20 02:17 S27pcscd -> ../init.d/pcscd lrwxrwxrwx. 1 root root 16 Jul 20 02:18 S28autofs -> ../init.d/autofs lrwxrwxrwx. 1 root root 19 Jul 20 02:18 S50bluetooth -> ../init.d/bluetooth lrwxrwxrwx. 1 root root 15 Jul 20 03:15 S50kdump -> ../init.d/kdump lrwxrwxrwx. 1 root root 14 Jul 20 02:20 S55sshd -> ../init.d/sshd lrwxrwxrwx. 1 root root 13 Aug 31 21:43 S80cmd -> ../init.d/cmd lrwxrwxrwx. 1 root root 17 Jul 20 02:18 S80postfix -> ../init.d/postfix lrwxrwxrwx. 1 root root 19 Jul 20 02:13 S82abrt-ccpp -> ../init.d/abrt-ccpp lrwxrwxrwx. 1 root root 15 Jul 20 02:13 S82abrtd -> ../init.d/abrtd lrwxrwxrwx. 1 root root 16 Sep 7 16:52 S88testrv -> ../init.d/testrv lrwxrwxrwx. 1 root root 15 Jul 20 02:18 S90crond -> ../init.d/crond lrwxrwxrwx. 1 root root 13 Jul 20 02:17 S95atd -> ../init.d/atd lrwxrwxrwx. 1 root root 20 Jul 20 02:18 S99certmonger -> ../init.d/certmonger lrwxrwxrwx. 1 root root 11 Jul 20 02:15 S99local -> ../rc.local
注意:正常级别下,最后启动一个服务S99local没有链接至/etc/rc.d/init.d一个服务脚本,而是指向了/etc/rc.d/rc.local脚本
不便或不需写为服务脚本放置于/etc/rc.d/init.d/目录,且又想开机时自动运行的命令,可直接放置于/etc/rc.d/rc.local文件中
6、等待用户登录
完成了系统服务的启动后,linux会启动终端或X-Window来等待用户登录,会执行"/sbin/mingetty",而且执行了6个,所以linux会有6个纯文本终端(tty1,tty2…),mingetty就是启动终端的命令。至此,系统就启动完毕了!
[19:51 root@Centos6.8/etc/init.d]# cat /etc/init/tty.conf # tty - getty # # This service maintains a getty on the specified device. # # Do not edit this file directly. If you want to change the behaviour, # please create a file tty.override and put your changes there. stop on runlevel [S016] respawn instance $TTY exec /sbin/mingetty $TTY usage 'tty TTY=/dev/ttyX - where X is console id'
综述:POST –> Boot Sequence(BIOS) –> Boot Loader –> Kernel(ramdisk) –> rootfs–> switchroot–> /sbin/init–>(/etc/inittab, /etc/init/*.conf) –> 设定默认运行级别–> 系统初始化脚本rc.sysinit–> 关闭或启动对应级别的服务–> 启动终端
可参考下图(图片来自网络)
二、系统服务管理
根据上面说明,我们可以知道,系统在启动后会根据想要的运行级别,运行相应的服务脚本,而这些系统服务的管理,可以用chkconfig来进行管理使用,这些服务脚本放置于/etc/rc.d/init.d中
chkconfig:跟新和确认系统服务信息
语法:chkconfig [–list] [–type type][name]:查看所有的服务列表详细信息
chkconfig –add name :添加服务到系统服务
chkconfig –del name:删除系统服务
chkconfig –override name
chkconfig [–level levels] [–type type] name <on|off|reset|resetpriorities>
chkconfig [–level levels] [–type type] name
[19:58 root@Centos6.8/etc/init.d]# chkconfig --list NetworkManager 0:off 1:off 2:off 3:off 4:off 5:off 6:off abrt-ccpp 0:off 1:off 2:off 3:on 4:off 5:on 6:off abrtd 0:off 1:off 2:off 3:on 4:off 5:on 6:off acpid 0:off 1:off 2:on 3:on 4:on 5:on 6:off atd 0:off 1:off 2:off 3:on 4:on 5:on 6:off auditd 0:off 1:off 2:on 3:on 4:on 5:on 6:off autofs 0:off 1:off 2:off 3:on 4:on 5:on 6:off blk-availability 0:off 1:on 2:on 3:on 4:on 5:on 6:off bluetooth 0:off 1:off 2:off 3:on 4:on 5:on 6:off certmonger 0:off 1:off 2:off 3:on 4:on 5:on 6:off cmd 0:off 1:off 2:on 3:on 4:on 5:on 6:off cpuspeed 0:off 1:on 2:on 3:on 4:on 5:on 6:off crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off cups 0:off 1:off 2:on 3:on 4:on 5:on 6:off dnsmasq 0:off 1:off 2:off 3:off 4:off 5:off 6:off firstboot 0:off 1:off 2:off 3:off 4:off 5:off 6:off haldaemon 0:off 1:off 2:off 3:on 4:on 5:on 6:off htcacheclean 0:off 1:off 2:off 3:off 4:off 5:off 6:off httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off ip6tables 0:off 1:off 2:on 3:on 4:on 5:on 6:off iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off irqbalance 0:off 1:off 2:off 3:on 4:on 5:on 6:off kdump 0:off 1:off 2:on 3:on 4:on 5:on 6:off lvm2-monitor 0:off 1:on 2:on 3:on 4:on 5:on 6:off mdmonitor 0:off 1:off 2:on 3:on 4:on 5:on 6:off messagebus 0:off 1:off 2:on 3:on 4:on 5:on 6:off mysqld 0:off 1:off 2:off 3:off 4:off 5:off 6:off netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off netfs 0:off 1:off 2:off 3:on 4:on 5:on 6:off network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
三、手动配置系统启动服务
1、系统服务脚本特点:基本都可以提供start|stop|restart|status这几个状态值的接收
2、服务脚本存放位置
由上面的介绍可知,所有的系统服务脚本都放在/etc/rc.d/init.d文件夹中
[20:04 root@Centos6.8/etc/init.d]# ls abrt-ccpp bluetooth functions kdump netfs oddjobd rdisc rsyslog svnserve abrtd certmonger haldaemon killall network openct rdma sandbox sysstat abrt-oops cmd halt lvm2-lvmetad NetworkManager pcscd restorecond saslauthd udev-post acpid cpuspeed htcacheclean lvm2-monitor nfs portreserve rngd single wdaemon atd crond httpd mdmonitor nfslock postfix rpcbind smartd winbind auditd cups ip6tables messagebus nfs-rdma pppoe-server rpcgssd spice-vdagentd wpa_supplicant autofs dnsmasq iptables mysqld ntpd psacct rpcidmapd sshd ypbind blk-availability firstboot irqbalance netconsole ntpdate quota_nld rpcsvcgssd sssd
3、服务脚本的编写格式
打开其中一个查看服务脚本的开头格式
[20:04 root@Centos6.8/etc/init.d]# cat atd #!/bin/sh # # atd Starts/stop the "at" daemon # # chkconfig: 345 95 5 # 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 # Provides: atd at batch # Required-Start: $local_fs # Required-Stop: $local_fs # Default-Start: 345 # Default-Stop: 95 # Short-Description: Starts/stop the "at" daemon # 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. ### END INIT INFO
其中,有三行格式是必须要写上的#!/bin/bash,#/chkconfig,#description
# chkconfig: runlevels SS KK【SS:表示开始运行的次序,KK:表示关闭程序的次序,-表示都不启动】
因此可以手动编写系统服务:
(1)把服务脚本放置/etc/init.d目录下
(2)使用chkconfig –add 添加到系统服务
(3)重启系统让服务生效
[20:17 root@Centos6.8/etc/init.d]# cat !$ cat testsrv #!/bin/bash #chkconfig 35 88 22 #description: test the system service testsrv () { case $1 in start) touch /var/lock/subsys/`basename $0` &> /dev/null echo "start OK" ;; stop) rm -rf /var/lock/subsys/`basename $0` &> /dev/null echo "stop OK" ;; restart) rm -rf /var/lock/subsys/`basename $0` &> /dev/null touch /var/lock/subsys/`basename $0` &> /dev/null echo "restart OK" ;; status) [ -e /var/lock/subsys/`basename $0` ] && echo "is running" || echo "is stopping" ;; esac } CHOICE=$1 ! [[ "$CHOICE" =~ start|stop|restart|status ]] && echo "useage:start|stop|restart|status" && exit 100 testsrv $CHOICE [20:20 root@Centos6.8/etc/init.d]# chkconfig --add testsrv [20:20 root@Centos6.8/etc/init.d]# chkconfig --list testsrv testsrv 0:off 1:off 2:off 3:on 4:off 5:on 6:off [20:20 root@Centos6.8/etc/init.d]# service testsrv status is stopping [20:20 root@Centos6.8/etc/init.d]# service testsrv start start OK [20:20 root@Centos6.8/etc/init.d]# service testsrv status is running
原创文章,作者:麦德良,如若转载,请注明出处:http://www.178linux.com/44483
评论列表(1条)
总结的很详细,赞一个