systemd stage 1 networking: Stop systemd-networkd on switch-root

This essentially backports
https://github.com/systemd/systemd/pull/27791. `systemd-networkd.service`
is sent the `SIGTERM` signal, but it is not required to be stopped
before `initrd-switch-root.target` is reached, despite the use of
`systemctl isolate initrd-switch-root.target`. This is because when
there is no ordering at all between two units, and a transaction stops
one and starts the other, the two operations can happen
simultaneously. This means the service could still be running when
`switch-root` actually occurs. Then, stage 2 systemd will see the
service still running and decide it doesn't need to add a start
operation for it to its initial transaction. Finally, the service
exits, but only after it's already too late. If, however, there is any
ordering at all between a stopping unit and a starting unit, then the
stop operation will be done first. This way, we ensure that the
service is properly exited before doing `switch-root`.

This is something to keep in mind going forward. There may be other
services that need this treatment. These `before` and `conflicts`
definitions are the correct way to ensure a unit is actually stopped
before you reach initrd-switch-root
This commit is contained in:
Will Fancher 2023-06-06 22:41:08 -04:00
parent a9e34ff905
commit 5d6ea734a1

View File

@ -3188,7 +3188,19 @@ let
systemd.contents."/etc/systemd/networkd.conf" = renderConfig cfg.config;
systemd.services.systemd-networkd.wantedBy = [ "initrd.target" ];
systemd.services.systemd-networkd = {
wantedBy = [ "initrd.target" ];
# These before and conflicts lines can be removed when this PR makes it into a release:
# https://github.com/systemd/systemd/pull/27791
before = ["initrd-switch-root.target"];
conflicts = ["initrd-switch-root.target"];
};
systemd.sockets.systemd-networkd = {
wantedBy = [ "initrd.target" ];
before = ["initrd-switch-root.target"];
conflicts = ["initrd-switch-root.target"];
};
systemd.services.systemd-network-generator.wantedBy = [ "sysinit.target" ];
systemd.storePaths = [