如何确保 systemd 在 udev 创建设备节点之前不会启动单元

如何确保 systemd 在 udev 创建设备节点之前不会启动单元

我想在启动时挂载 UBI 文件系统。根据配置,MTD 设备号可能因版本而异,但它始终是名为 的分区add

我添加了以下文件

# /usr/lib/udev/rules.d/76-mtd-alias.rules
ACTION!="add", GOTO="mtd_alias_end"
SUBSYSTEM=="mtd", SYMLINK="mtd-byname/$attr{name}", TAG+="systemd"
LABEL="mtd_alias_end"

这会创建类似于/dev/mtd-by-name/add相关/dev/mtdblockX文件的符号链接的文件(X取决于配置)。

# /usr/lib/systemd/system/mnt-data.mount
[Unit]
Description=Mount data partition
Requires=data-attach.service
After=data-attach.service
[Mount]
What=ubi0_0
Where=/mnt/data
Type=ubifs

和...一起

#/usr/lib/systemd/system/data-attach.service
[Unit]
Description=Attach data ubi partition

[Service]
Type=oneshot
RemainAfterExit=yes
Requires=dev-mtd\x2dbyname-add.device
After=dev-mtd\x2dbyname-add.device
ExecStart=/usr/sbin/ubiattach /dev/ubi_ctrl -p /dev/mtd-byname/add
ExecStop=/usr/sbin/ubidetach /dev/ubi_ctrl -p /dev/mtd-byname/add

问题是,尽管systemd 启动data-attach.service,它仍然在udev创建符号链接之前执行此操作。

系统启动后,我可以看到已经device启动,尽管挂载失败:

# systemctl status data-attach.service
● data-attach.service - Attach data ubi partition
   Loaded: loaded (/lib/systemd/system/data-attach.service; static; vendor preset: enabled)
   Active: failed (Result: exit-code) since Fri 2018-06-22 11:11:50 UTC; 7min ago
  Process: 164 ExecStart=/usr/sbin/ubiattach /dev/ubi_ctrl -p /dev/mtd-byname/add (code=exited, status=255)
 Main PID: 164 (code=exited, status=255)

Jun 22 11:11:50 LogiPeru ubiattach[164]: libubi: error!: cannot stat "/dev/mtd-byname/add"
Jun 22 11:11:50 LogiPeru ubiattach[164]:         error 2 (No such file or directory)
Jun 22 11:11:50 LogiPeru ubiattach[164]: ubiattach: error!: cannot attach "/dev/mtd-byname/add"
Jun 22 11:11:50 LogiPeru ubiattach[164]:            error 2 (No such file or directory)
Jun 22 11:11:50 LogiPeru systemd[1]: Starting Attach data ubi partition...
Jun 22 11:11:50 LogiPeru systemd[1]: data-attach.service: Main process exited, code=exited, status=255/n/a
Jun 22 11:11:50 LogiPeru systemd[1]: data-attach.service: Failed with result 'exit-code'.
Jun 22 11:11:50 LogiPeru systemd[1]: Failed to start Attach data ubi partition.

但确实device加载了:

# systemctl status 'dev-mtd\x2dbyname-add.device'
● dev-mtd\x2dbyname-add.device - /dev/mtd-byname/add
   Follow: unit currently follows state of sys-devices-virtual-mtd-mtd10.device
   Loaded: loaded
   Active: active (plugged) since Fri 2018-06-22 11:11:50 UTC; 10min ago
   Device: /sys/devices/virtual/mtd/mtd10
# ls -l /dev/mtd-byname/add
lrwxrwxrwx    1 root     root             8 Jun 22 11:11 /dev/mtd-byname/add -> ../mtd10

我哪里做错了?为什么RequiresAfter标签data-attach.service被忽略了?如果我等到我可以登录,那么我就可以运行systemctl start mnt-data.mount并且挂载成功。

答案1

[Service]
<...>
Requires=dev-mtd\x2dbyname-add.device
After=dev-mtd\x2dbyname-add.device

你必须把Requires=After=指令放入[Unit]单元文件的部分。它们不会在或[Service]任何其他部分中起作用。

未来诊断的建议:systemctl daemon-reload与其他systemctl动词一样, 仅将其警告和错误写入系统日志,而不写入 的systemctlstdout/stderr(即控制台)。systemctl daemon-reload特别是, 仅报告严重错误,例如无法与 通信。 因此,在任何命令之后systemd使用类似命令手动检查系统日志可能会有所帮助。journalctl -e _PID=1systemctl

相关内容