确保 systemd 单元在*之前*任何网络启动

Ensuring that a systemd unit starts *before* any networking

我有一些代码需要确保在任何网络单元启动之前运行直到完成,除此之外代码生成 dhcpcd.conf 和 wpa_supplicant.conf.

这应该很简单,但到目前为止我所有的努力都是徒劳的...

我现在的单位是这样的:

[Unit]
Description=Config generation from DB
Before=networking.service

[Service]
Type=oneshot
ExecStart=/home/mark/bin/db2config.py

[Install]
RequiredBy=network.target

我尝试了这个主题的几种变体(例如,包括将 dhcpcd.service 添加到 Before= 列表中),但 none 达到了预期的效果。

我对 Before= 的理解是,将要启动的任何列出的服务都不会在本单元之后启动。但这种理解显然是错误的!

这感觉像是已经出现的问题,但如果是这样的话,我还没有在关于确保网络在其他单元启动之前启动的更常见的问题中找到它。

答案相当简单,但它需要排除 OS 提供的设备必然按照您的想法行事的假设。

首先,我所在的单位:

[Unit]
Description=Config generation from DB
Before=network-pre.service
Wants=network-pre.service

[Service]
Type=oneshot
ExecStart=/home/mark/bin/db2config.py

[Install]
RequiredBy=network.target

但所有重要的变化是使 dhcpcd 依赖于网络-pre.target,它在 many/most 发行版(例如 Debian、Redhat)上开箱即用:

sudo systemctl edit dhcpcd.service

.. 并添加:

[Unit]
After=network-pre.target

感谢 systemd-devel mailing list 帮助我: