1{ runTest }:
2
3let
4 common =
5 { pkgs, ... }:
6 {
7 boot.initrd.systemd = {
8 enable = true;
9 network.wait-online.timeout = 10;
10 network.wait-online.anyInterface = true;
11 targets.network-online.requiredBy = [ "initrd.target" ];
12 services.systemd-networkd-wait-online.requiredBy = [ "network-online.target" ];
13 initrdBin = [
14 pkgs.iproute2
15 pkgs.iputils
16 pkgs.gnugrep
17 ];
18 };
19 testing.initrdBackdoor = true;
20 boot.initrd.network.enable = true;
21 };
22
23 mkFlushTest =
24 flush: script:
25 runTest (
26 { lib, ... }:
27 {
28 name = "systemd-initrd-network-${lib.optionalString (!flush) "no-"}flush";
29 meta.maintainers = with lib.maintainers; [ elvishjerricco ];
30
31 nodes.machine =
32 { pkgs, ... }:
33 {
34 imports = [ common ];
35
36 boot.initrd.network.flushBeforeStage2 = flush;
37 systemd.services.check-flush = {
38 requiredBy = [ "multi-user.target" ];
39 before = [
40 "network-pre.target"
41 "multi-user.target"
42 "shutdown.target"
43 ];
44 conflicts = [ "shutdown.target" ];
45 wants = [ "network-pre.target" ];
46 unitConfig.DefaultDependencies = false;
47 serviceConfig.Type = "oneshot";
48 path = [
49 pkgs.iproute2
50 pkgs.iputils
51 pkgs.gnugrep
52 ];
53 inherit script;
54 };
55 };
56
57 testScript = ''
58 machine.wait_for_unit("network-online.target")
59 machine.succeed(
60 "ip addr | grep 10.0.2.15",
61 "ping -c1 10.0.2.2",
62 )
63 machine.switch_root()
64
65 machine.wait_for_unit("multi-user.target")
66 '';
67 }
68 );
69in
70{
71 basic = runTest (
72 { lib, ... }:
73 {
74 name = "systemd-initrd-network";
75 meta.maintainers = with lib.maintainers; [ elvishjerricco ];
76
77 nodes.machine = common;
78
79 testScript = ''
80 machine.wait_for_unit("network-online.target")
81 machine.succeed(
82 "ip addr | grep 10.0.2.15",
83 "ping -c1 10.0.2.2",
84 )
85 machine.switch_root()
86
87 # Make sure the systemd-network user was set correctly in initrd
88 machine.wait_for_unit("multi-user.target")
89 machine.succeed("[ $(stat -c '%U,%G' /run/systemd/netif/links) = systemd-network,systemd-network ]")
90 machine.succeed("ip addr show >&2")
91 machine.succeed("ip route show >&2")
92 '';
93 }
94 );
95
96 doFlush = mkFlushTest true ''
97 if ip addr | grep 10.0.2.15; then
98 echo "Network configuration survived switch-root; flushBeforeStage2 failed"
99 exit 1
100 fi
101 '';
102
103 dontFlush = mkFlushTest false ''
104 if ! (ip addr | grep 10.0.2.15); then
105 echo "Network configuration didn't survive switch-root"
106 exit 1
107 fi
108 '';
109}