Systemd: when during boot are network interface devices created

linux-networkingsystemd

I'm trying to create a custom network management service for a CentOS 7 netboot image. The service gets a list of all interfaces on the host, finds one with a physical connection, and then uses it to pull the DHCP configuration.

The problem is making sure that it starts after all the network devices exist on the system, so it can get a list of them. Here's my unit file:

[Unit]
Description=Configure networking
Wants=network.target
After=network-pre.target
Before=network.target multi-user.target

[Service]
ExecStart=/usr/local/bin/netboot-cfg
Type=oneshot
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

As you can see, it runs after the network-pre target, and before the network target. I confirmed this with systemd-analyze plot. However, it's unable to find any interfaces on the system at that point in the boot process. The list is created by looking at the /sys/class/net directory. The sys-subsystem-net-devices-<iface>.device units are started later, which I'm guessing populate that directory.

It seems strange to me that the network device units start after network.target.

So the question is: How can I reliably determine when all the interfaces will be available, using systemd unit file syntax?

Best Answer

it seems strange to me that the network device units start after network.target.

So the question is: How can I reliably determine when all the interfaces will be available, using systemd unit file syntax?

Parts of this question are answered by this Unix SE thread.

Basically, per the documentation for network.target:

  • network.target guarantees only that the network stack is up:

    only indicates that the network management stack is up after it has been reached. Whether any network interfaces are already configured when it is reached is undefined.

  • network-online.target

    is a target that actively waits until the nework is "up", where the definition of "up" is defined by the network management software. Usually it indicates a configured, routable IP address of some kind.

So you may want to adjust your requirements to reflect this: I think you might want to run your script After=network.target, but Before=network-online.target.