|
| 1 | +# Systemd进程启动 |
| 2 | + |
| 3 | +分析Linux系统进程何启动起来 |
| 4 | + |
| 5 | +## 参考文档 |
| 6 | + |
| 7 | +* [systemd (简体中文)](https://wiki.archlinux.org/index.php/Systemd_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)) |
| 8 | +* [udev (简体中文)](https://wiki.archlinux.org/index.php/Udev_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)) |
| 9 | +* https://www.raspberrypi.org/documentation/linux/usage/systemd.md |
| 10 | +* https://www.raspberrypi.org/documentation/linux/usage/rc-local.md |
| 11 | + |
| 12 | +## init真相 |
| 13 | + |
| 14 | +* stat /sbin/init |
| 15 | + ``` |
| 16 | + File: /sbin/init -> /lib/systemd/systemd |
| 17 | + Size: 20 Blocks: 0 IO Block: 4096 symbolic link |
| 18 | + Device: b302h/45826d Inode: 286014 Links: 1 |
| 19 | + Access: (0777/lrwxrwxrwx) Uid: ( 0/ root) Gid: ( 0/ root) |
| 20 | + Access: 2021-01-11 13:31:33.086942487 +0000 |
| 21 | + Modify: 2020-12-10 00:18:16.000000000 +0000 |
| 22 | + Change: 2021-01-11 13:31:33.086942487 +0000 |
| 23 | + Birth: - |
| 24 | + ``` |
| 25 | + |
| 26 | +## Systemd应用原理 |
| 27 | + |
| 28 | +在sysvinit中有明确定义的运行级别(如:0、1、3、5、6)与systemd中特定的 目标 存在一一对应的关系。然而,对于用户自定义运行级别(2、4)却没有。如需要同样功能,建议你以原有运行级别所对应的systemd目标为基础,新建一个/etc/systemd/system/\<目标名\>.target(可参考/usr/lib/systemd/system/graphical.target), 然后创建目录/etc/systemd/system/\<目标名\>.wants,并向其中加入需启用的服务链接(指向/ur/lib/systemd/system/)。 |
| 29 | + |
| 30 | +**"SysV 运行级别" 与 "systemd 目标" 对照表** |
| 31 | + |
| 32 | +SysV 运行级别 | Systemd 目标 | 注释 |
| 33 | +--|--|-- |
| 34 | +0 | runlevel0.target, poweroff.target | 中断系统(halt) |
| 35 | +1, s, single | runlevel1.target, rescue.target | 单用户模式 |
| 36 | +2, 4 | runlevel2.target, runlevel4.target, multi-user.target | 用户自定义运行级别,通常识别为级别3。 |
| 37 | +3 | runlevel3.target, multi-user.target | 多用户,无图形界面。用户可以通过终端或网络登录。 |
| 38 | +5 | runlevel5.target, graphical.target | 多用户,图形界面。继承级别3的服务,并启动图形界面服务。 |
| 39 | +6 | runlevel6.target, reboot.target | 重启 |
| 40 | +emergency | emergency.target | 急救模式(Emergency shell) |
| 41 | + |
| 42 | +* systemctl get-default |
| 43 | + ``` |
| 44 | + graphical.target |
| 45 | + ``` |
| 46 | +* cd /etc/systemd |
| 47 | +* find * -iname default.target |
| 48 | + ``` |
| 49 | + system/default.target |
| 50 | + ``` |
| 51 | +* ls -al system/default.target |
| 52 | + ``` |
| 53 | + lrwxrwxrwx 1 root root 36 Jan 11 13:01 system/default.target -> /lib/systemd/system/graphical.target |
| 54 | + ``` |
| 55 | +* cat /lib/systemd/system/graphical.target |
| 56 | + ``` |
| 57 | + # ...省略 |
| 58 | +
|
| 59 | + [Unit] |
| 60 | + Description=Graphical Interface |
| 61 | + Documentation=man:systemd.special(7) |
| 62 | + Requires=multi-user.target |
| 63 | + Wants=display-manager.service |
| 64 | + Conflicts=rescue.service rescue.target |
| 65 | + After=multi-user.target rescue.service rescue.target display-manager.service |
| 66 | + AllowIsolate=yes |
| 67 | + ``` |
| 68 | + * Requires=multi-user.target |
| 69 | + |
| 70 | +## rc.local启动分析 |
| 71 | + |
| 72 | +* systemctl status rc.local |
| 73 | + ``` |
| 74 | + Warning: The unit file, source configuration file or drop-ins of rc-local.service changed on disk. Run 'systemctl daemon-reload' to reload units. |
| 75 | + ● rc-local.service - /etc/rc.local Compatibility |
| 76 | + Loaded: loaded (/lib/systemd/system/rc-local.service; enabled-runtime; vendor preset: enabled) |
| 77 | + Drop-In: /usr/lib/systemd/system/rc-local.service.d |
| 78 | + └─debian.conf |
| 79 | + /etc/systemd/system/rc-local.service.d |
| 80 | + └─ttyoutput.conf |
| 81 | + Active: active (exited) since Thu 2021-02-11 08:08:35 GMT; 6h ago |
| 82 | + Docs: man:systemd-rc-local-generator(8) |
| 83 | + Process: 461 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS) |
| 84 | + |
| 85 | + Feb 11 08:08:35 raspberrypi systemd[1]: Starting /etc/rc.local Compatibility... |
| 86 | + Feb 11 08:08:35 raspberrypi systemd[1]: Started /etc/rc.local Compatibility. |
| 87 | + ``` |
| 88 | + * Loaded: loaded (/lib/systemd/system/rc-local.service; enabled-runtime; vendor preset: enabled) |
| 89 | + * /lib/systemd/system/rc-local.service |
| 90 | + * Active: active (exited) since Thu 2021-02-11 08:08:35 GMT; 6h ago |
| 91 | +*  |
| 92 | + |
| 93 | +## Wifi配置启动分析 |
| 94 | + |
| 95 | +* /etc/systemd/system/multi-user.target.wants/raspberrypi-net-mods.service |
| 96 | + ``` |
| 97 | + [Unit] |
| 98 | + Description=Copy user wpa_supplicant.conf |
| 99 | + ConditionPathExists=/boot/wpa_supplicant.conf |
| 100 | + Before=dhcpcd.service |
| 101 | + After=systemd-rfkill.service |
| 102 | + |
| 103 | + [Service] |
| 104 | + Type=oneshot |
| 105 | + RemainAfterExit=yes |
| 106 | + ExecStart=/bin/mv /boot/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.conf |
| 107 | + ExecStartPost=/bin/chmod 600 /etc/wpa_supplicant/wpa_supplicant.conf |
| 108 | + ExecStartPost=/usr/sbin/rfkill unblock wifi |
| 109 | + |
| 110 | + [Install] |
| 111 | + WantedBy=multi-user.target |
| 112 | + ``` |
| 113 | + |
| 114 | +## SSH开启设置分析 |
| 115 | + |
| 116 | +* /etc/systemd/system/multi-user.target.wants/sshswitch.service |
| 117 | + ``` |
| 118 | + [Unit] |
| 119 | + Description=Turn on SSH if /boot/ssh is present |
| 120 | + ConditionPathExistsGlob=/boot/ssh{,.txt} |
| 121 | + After=regenerate_ssh_host_keys.service |
| 122 | + |
| 123 | + [Service] |
| 124 | + Type=oneshot |
| 125 | + ExecStart=/bin/sh -c "systemctl enable --now ssh && rm -f /boot/ssh ; rm -f /boot/ssh.txt" |
| 126 | + |
| 127 | + [Install] |
| 128 | + WantedBy=multi-user.target |
| 129 | + ``` |
| 130 | + |
0 commit comments