1、启动流程
- UEFi或BIOS初始化,运行POST开机自检
- 选择启动设备
- 引导装载程序, centos7是grub2,加载装载程序的配置文件
- /etc/grub.d/
- /etc/default/grub
- /boot/grub2/grub.cfg
- 加载initramfs驱动模块
- 加载内核选项
- 内核初始化,centos7使用systemd代替init
- 执行initrd.target所有单元,包括挂载/etc/fstab
- 从initramfs根文件系统切换到磁盘根目录
- systemd执行默认target配置,配置文件/etc/systemd/system/default.target
- systemd执行sysinit.target初始化系统及basic.target准备操作系统
- systemd启动multi-user.target下的本机与服务器服务
- systemd执行multi-user.target下的/etc/rc.d/rc.local
- Systemd执行multi-user.target下的getty.target及登录服务
- systemd执行graphical需要的服务
# 通过systemd-analyze 工具可以了解启动的详细过程
[root@centos79 ~]# systemd-analyze blame
1.487s kdump.service
1.003s dev-mapper-centos\x2droot.device
950ms lvm2-monitor.service
808ms tuned.service
685ms dracut-initqueue.service
488ms postfix.service
205ms httpd.service
174ms network.service
142ms sysroot.mount
136ms lvm2-pvscan@8:17.service
132ms boot.mount
129ms auditd.service
128ms NetworkManager-wait-online.service
121ms dracut-pre-pivot.service
114ms initrd-switch-root.service
113ms polkit.service
98ms lvm2-pvscan@8:2.service
90ms dracut-cmdline.service
81ms lvm2-pvscan@8:33.service
65ms rsyslog.service
56ms plymouth-quit-wait.service
53ms plymouth-quit.service
49ms NetworkManager.service
44ms gssproxy.service
36ms systemd-vconsole-setup.service
35ms rhel-readonly.service
30ms sshd.service
29ms systemd-udev-trigger.service
28ms dracut-pre-udev.service
26ms initrd-parse-etc.service
25ms data.mount
25ms plymouth-start.service
22ms systemd-logind.service
22ms rpcbind.service
22ms rhel-dmesg.service
22ms dev-mapper-centos\x2dswap.swap
21ms systemd-random-seed.service
21ms var-lib-nfs-rpc_pipefs.mount
21ms systemd-journal-flush.service
20ms rpc-statd-notify.service
16ms systemd-udevd.service
16ms plymouth-read-write.service
15ms systemd-tmpfiles-setup-dev.service
15ms plymouth-switch-root.service
15ms systemd-remount-fs.service
12ms systemd-sysctl.service
11ms rhel-import-state.service
11ms kmod-static-nodes.service
10ms systemd-rfkill@rfkill0.service
10ms rhel-domainname.service
8ms systemd-modules-load.service
8ms dev-mqueue.mount
7ms initrd-cleanup.service
7ms dev-hugepages.mount
6ms sys-kernel-debug.mount
6ms systemd-user-sessions.service
5ms systemd-fsck-root.service
5ms nfs-config.service
5ms sys-fs-fuse-connections.mount
4ms systemd-journald.service
4ms systemd-tmpfiles-setup.service
3ms systemd-update-utmp-runlevel.service
2ms systemd-update-utmp.service
2ms sys-kernel-config.mount
2ms systemd-tmpfiles-clean.service
1ms initrd-udevadm-cleanup-db.service
# 生成网页
systemd-analyze plot > boot.html
![图片[1]-CentOS 7之后版本引导顺序-李佳程的个人主页](http://www.lijiach.com/wp-content/uploads/2022/11/image-77.png)
2、systemd
2.1、systemd 特性
Systemd:从 CentOS 7 版本之后开始用 systemd 实现init进程,系统启动和服务器守护进程管理器,负责在系统启动或运行时,激活系统资源,服务器进程和其它进程。
Systemd新特性
- 系统引导时实现服务并行启动
- 按需启动守护进程
- 自动化的服务依赖关系管理
- 同时采用socket式与D-Bus总线式激活服务
- socket与服务程序分离
- 向后兼容sysv init脚本
- 使用systemctl 命令管理,systemctl命令固定不变,不可扩展,非由systemd启动的服务,
- systemctl无法与之通信和控制
- 系统状态快照
systemd 核心概念:unit
unit表示不同类型的systemd对象,通过配置文件进行标识和配置;文件中主要包含了系统服务、监听
socket、保存的系统快照以及其它与init相关的信息。
#查看unit类型
[root@centos79 ~]# systemctl -t help
Available unit types:
service
socket
busname
target
snapshot
device
mount
automount
swap
timer
path
slice
scope
- service unit: 文件扩展名为.service, 用于定义系统服务
- Socket unit: .socket, 定义进程间通信用的socket文件,也可在系统启动时,延迟启动服务,实现按需启动
- Target unit: 文件扩展名为.target,用于模拟实现运行级别
- Device unit: .device, 用于定义内核识别的设备
- Mount unit: .mount, 定义文件系统挂载点
- Snapshot unit: .snapshot, 管理系统快照
- Swap unit: .swap, 用于标识swap设备
- Automount unit: .automount,文件系统的自动挂载点
- Path unit: .path,用于定义文件系统中的一个文件或目录使用,常用于当文件系统变化时,延迟激活服务,如:spool 目录
# unit的配置文件
/usr/lib/systemd/system #每个服务最主要的启动脚本设置,类似于之前的/etc/init.d/
/lib/systemd/system #ubutun的对应目录,兼容于CentOS7,8和Ubuntu
/run/systemd/system #系统执行过程中所产生的服务脚本,比上面目录优先运行
/etc/systemd/system #管理员建立的执行脚本,类似于/etc/rcN.d/Sxx的功能,比上面
目
录优先运行
2.2、systemctl管理系统服务service unit
systemctl COMMAND name.service
#启动:相当于service name start
systemctl start name.service
#停止:相当于service name stop
systemctl stop name.service
#重启:相当于service name restart
systemctl restart name.service
#查看状态:相当于service name status
systemctl status name.service
#禁止自动和手动启动:
systemctl mask name.service
#取消禁止
systemctl unmask name.service
#查看某服务当前激活与否的状态:
systemctl is-active name.service
#查看service文件内容
systemctl cat sshd
#查看所有已经激活的服务:
systemctl list-units --type|-t service
#查看所有服务:
systemctl list-units --type service --all|-a
#设定某服务开机自启,相当于chkconfig name on
systemctl enable name.service
#设定某服务开机禁止启动:相当于chkconfig name off
systemctl disable name.service
#查看所有服务的开机自启状态,相当于chkconfig --list
systemctl list-unit-files --type service
#用来列出该服务在哪些运行级别下启用和禁用:chkconfig –list name
ls /etc/systemd/system/*.wants/name.service
#查看服务是否开机自启:
systemctl is-enabled name.service
#列出失败的服务
systemctl --failed --type=service
#开机并立即启动或停止
systemctl enable --now postfix
systemctl disable --now postfix
#查看服务的依赖关系:
systemctl list-dependencies name.service
#杀掉进程:
systemctl kill unitname
# 显示状态
systemctl list-unit-files --type service --all
- loaded Unit配置文件已处理
- active(running) 一次或多次持续处理的运行
- active(exited) 成功完成一次性的配置
- active(waiting) 运行中,等待一个事件
- inactive 不运行
- enabled 开机启动
- disabled 开机不启动
- static 开机不启动,但可被另一个启用的服务激活
- indirect 重定向到别处
2.3、service unit文件格式
/etc/systemd/system:系统管理员和用户使用
/usr/lib/systemd/system:发行版打包者使用
- unit 格式说明
- 以 “#” 开头的行后面的内容会被认为是注释
- 相关布尔值,1、yes、on、true 都是开启,0、no、off、false 都是关闭
- 时间单位默认是秒,所以要用毫秒(ms)分钟(m)等须显式说明
- service unit file文件通常由三部分组成
- [Unit]:定义与Unit类型无关的通用选项;用于提供unit的描述信息、unit行为及依赖关系等
- [Service]:与特定类型相关的专用选项;此处为Service类型
- [Install]:定义由“systemctl enable”以及”systemctl disable“命令在实现服务启用或禁用时用到的一些选项
- Unit段的常用选项
- Description:描述信息
- After:定义unit的启动次序,表示当前unit应该晚于哪些unit启动,其功能与Before相反
- Requires:依赖到的其它units,强依赖,被依赖的units无法激活时,当前unit也无法激活
- Wants:依赖到的其它units,弱依赖
- Conflicts:定义units间的冲突关系
- Service段的常用选项:
- Type:定义影响ExecStart及相关参数的功能的unit进程启动类型
- simple:默认值,这个daemon主要由ExecStart接的指令串来启动,启动后常驻于内存中
- forking:由ExecStart启动的程序透过spawns延伸出其他子程序来作为此daemon的主要服务。原生父程序在启动结束后就会终止
- oneshot:与simple类似,不过这个程序在工作完毕后就结束了,不会常驻在内存中
- dbus:与simple类似,但这个daemon必须要在取得一个D-Bus的名称后,才会继续运作.因此通常也要同时设定BusNname= 才行
- notify:在启动完成后会发送一个通知消息。还需要配合 NotifyAccess 来让 Systemd 接收消息
- idle:与simple类似,要执行这个daemon必须要所有的工作都顺利执行完毕后才会执行。这类的daemon通常是开机到最后才执行即可的服务
- EnvironmentFile:环境配置文件
- ExecStart:指明启动unit要运行命令或脚本的绝对路径
- ExecStartPre: ExecStart前运行
- ExecStartPost: ExecStart后运行
- ExecStop:指明停止unit要运行的命令或脚本
- Restart:当设定Restart=1 时,则当次daemon服务意外终止后,会再次自动启动此服务
- RestartSec: 设置在重启服务( Restart= )前暂停多长时间。 默认值是100毫秒(100ms)。 如果未指定时间单位,那么将视为以秒为单位。 例如设为”20″等价于设为”20s”。
- PrivateTmp:设定为yes时,会在生成/tmp/systemd-private-UUID-NAME.service-XXXXX/tmp/目录
- Type:定义影响ExecStart及相关参数的功能的unit进程启动类型
- Install段的常用选项:
- Alias:别名,可使用systemctl command Alias.service
- RequiredBy:被哪些units所依赖,强依赖
- WantedBy:被哪些units所依赖,弱依赖
- Also:安装本服务的时候还要安装别的相关服务
[root@centos79 ~]# head -n 5 /lib/systemd/system/postfix.service
[Unit]
Description=Postfix Mail Transport Agent
After=syslog.target network.target
Conflicts=sendmail.service exim.service
[root@centos79 ~]# cat /lib/systemd/system/postfix.service
[Unit]
Description=Postfix Mail Transport Agent
After=syslog.target network.target
Conflicts=sendmail.service exim.service
[Service]
Type=forking
PIDFile=/var/spool/postfix/pid/master.pid
EnvironmentFile=-/etc/sysconfig/network
ExecStartPre=-/usr/libexec/postfix/aliasesdb
ExecStartPre=-/usr/libexec/postfix/chroot-update
ExecStart=/usr/sbin/postfix start
ExecReload=/usr/sbin/postfix reload
ExecStop=/usr/sbin/postfix stop
# 对于新创建的unit文件,或者修改了的unit文件,要通知systemd重载此配置文件,而后可以选择
重启
systemctl daemon-reload
systemctl restart name.service
# nginx服务Unit文件
[Unit]
# 描述信息
Description=The Nginx HTTP Server daemon
# 指定启动nginx之前需要其他的其他服务,如network.target等
After=network.target remote-fs.target nss-lookup.target
[Service]
# Type为服务类型,仅启动一个主进程的服务为simple,需要启动若干子进程的服务为forking
Type=forking
# 设置执行systemctl start nginx后需要启动的具体命令
ExecStart=/usr/local/nginx/sbin/nginx
# 设置执行systemctl reload nginx后需要执行的具体命令
ExecReload=/usr/local/nginx/sbin/nginx -s reload
# 设置执行systemctl stop nginx后需要执行的具体命令
ExecStop=/bin/kill -s QUIT ${MAINPID}
[Install]
# 设置在什么模式下被安装,设置开机启动的时候需要
WantedBy=multi-user.target
2.4、运行级别
target units:相当于CentOS 6之前的runlevel ,unit配置文件:.target
ls /usr/lib/systemd/system/*.target
systemctl list-unit-files --type target --all
# 和运行级别对应关系
0 ==> runlevel0.target, poweroff.target
1 ==> runlevel1.target, rescue.target
2 ==> runlevel2.target, multi-user.target
3 ==> runlevel3.target, multi-user.target
4 ==> runlevel4.target, multi-user.target
5 ==> runlevel5.target, graphical.target
6 ==> runlevel6.target, reboot.target
# 查看依赖性
systemctl list-dependencies graphical.target
# 级别切换:相当于 init N
systemctl isolate name.target
# 进入默认target
systemctl default
# 切换至字符模式
systemctl isolate multi-user.target
# 只有/lib/systemd/system/*.target文件中AllowIsolate=yes 才能切换(修改文件需执行systemctl daemon-reload才能生效)
# 获取默认运行级别: 相当于查看 /etc/inittab
systemctl get-default
# 修改默认级别:相当于修改 /etc/inittab
systemctl set-default name.target
#修改以命令行模式启动
systemctl set-default multi-user.target
[root@centos79 ~]# ls -l /etc/systemd/system/default.target
lrwxrwxrwx. 1 root root 37 Nov 8 14:53 /etc/systemd/system/default.target -> /lib/systemd/system/multi-user.target
# 切换至紧急救援模式
systemctl rescue
# 切换至emergency模式
systemctl emergency
# rescue.target 比emergency 支持更多的功能,例如日志等
# 传统命令init,poweroff,halt,reboot都成为systemctl的软链接
#关机
systemctl halt、systemctl poweroff
#重启:
systemctl reboot
#挂起:
systemctl suspend
#休眠:
systemctl hibernate
#休眠并挂起:
systemctl hybrid-sleep
2.5、设置内核参数
设置内核参数,只影响当次启动
启动时,到启动菜单,按e键,找到在linux 开头的行后添加systemd.unit=desired.target
# 例如
systemd.unit=emergency.target
systemd.unit=rescue.target
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END