systemd启动流程分析

systemd启动过程

systemd启动时会默认启动default.target配置,并且递归的处理它的依赖关系,一般这个配置为graphical.target 或者 multi-user.target。第一种会启动图形界面,第二种会启动多用户字符模式。下图是关键服务配置的启动依赖:

systemd启动流程分析_第1张图片

下面将简单介绍启动中的4个关键步骤:

第一步
systemd 执行默认target 配置,配置文件/etc/systemd/system/default.target,它一般是一个软链接,指向multi-user.target或者graphical.target。可以通过如下命令查询和修改default.target:

systemctl get-default #查询
systemctl set-default multi-user.target #设置

第二步
systemd 执行启动所依赖的目标basic.target和sysinit.target初始化系统
通过查看cat /etc/systemd/system/default.target查看依赖:

  8 [Unit]
  9 Description=Multi-User System
 10 Documentation=man:systemd.special(7)
 11 Requires=basic.target
 12 Conflicts=rescue.service rescue.target
 13 After=basic.target rescue.service rescue.target
 14 AllowIsolate=yes

After指定的target需要在default.target之前运行。

第三步
systemd 启动multi-user.target 下的本机与服务器服务,由于default.target指向multi-user.target,那么这一步就启动对应的target服务。它的服务存在于 /etc/systemd/system/multi-user.target.wants 目录中:

lrwxrwxrwx. 1 root root 37 Aug  8  2018 acpid.service -> /usr/lib/systemd/system/acpid.service
lrwxrwxrwx. 1 root root 35 Aug  8  2018 atd.service -> /usr/lib/systemd/system/atd.service
lrwxrwxrwx. 1 root root 38 Aug  8  2018 auditd.service -> /usr/lib/systemd/system/auditd.service
lrwxrwxrwx. 1 root root 37 Aug  8  2018 crond.service -> /usr/lib/systemd/system/crond.service
lrwxrwxrwx. 1 root root 37 Aug  8  2018 kdump.service -> /usr/lib/systemd/system/kdump.service
lrwxrwxrwx. 1 root root 46 Aug  8  2018 libstoragemgmt.service -> /usr/lib/systemd/system/libstoragemgmt.service
lrwxrwxrwx  1 root root 36 Aug 13  2018 ntpd.service -> /usr/lib/systemd/system/ntpd.service
lrwxrwxrwx. 1 root root 39 Aug  8  2018 postfix.service -> /usr/lib/systemd/system/postfix.service
lrwxrwxrwx. 1 root root 40 Aug  8  2018 remote-fs.target -> /usr/lib/systemd/system/remote-fs.target
lrwxrwxrwx. 1 root root 46 Aug  8  2018 rhel-configure.service -> /usr/lib/systemd/system/rhel-configure.service
lrwxrwxrwx. 1 root root 39 Aug  8  2018 rpcbind.service -> /usr/lib/systemd/system/rpcbind.service
lrwxrwxrwx. 1 root root 39 Aug  8  2018 rsyslog.service -> /usr/lib/systemd/system/rsyslog.service
lrwxrwxrwx. 1 root root 36 Aug  8  2018 sshd.service -> /usr/lib/systemd/system/sshd.service
lrwxrwxrwx. 1 root root 37 Aug  8  2018 tuned.service -> /usr/lib/systemd/system/tuned.service
lrwxrwxrwx. 1 root root 35 Aug  8  2018 vdo.service -> /usr/lib/systemd/system/vdo.service

第四步
systemd 执行multi-user.target 下的/etc/rc.d/rc.local。systemd是可以兼容systemv init中的rc.local配置的,通过rc-local.service来实现兼容的,systemd在启动的很早就会判断/etc/rc.local是否存在并且是可执行的,如果满足条件,那么systemd会调用/usr/lib/systemd/system-generators/下面的小程序来把rc-local.service服务加入到default.target中来。这样在后面的执行时就会触发rc.local的运行:

[Unit]
Description=/etc/rc.d/rc.local Compatibility
ConditionFileIsExecutable=/etc/rc.d/rc.local
After=network.target

[Service]
Type=simple
ExecStart=/etc/rc.d/rc.local start
TimeoutStartSec=0
TimeoutStopSec=1
RemainAfterExit=yes

启动耗时分析

systemd-analyze blame  #列举出每个服务的启动耗时
systemd-analyze plot > boot.svg #图形化展示启动耗时

systemd启动流程分析_第2张图片

查看log

对于大多数人来说,可能知道syslogd是用来做log系统的,实际上systemd实现了自己的一套log系统:

journalctl     #查看systemd所有日志
journalctl -k  #查看内核日志

参考:
http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html
https://www.jianshu.com/p/22bed497c9c0
https://blog.csdn.net/xing_huo95/article/details/90246050
https://www.freedesktop.org/software/systemd/man/bootup.html#System%20Manager%20Bootup

你可能感兴趣的:(Linux笔记)