git.iko.soy/typeable/nixpkgs
on master help sign in
containers: Do not wait for udev for network devices

Test that adding physical devices to containers works, find that network setup then doesn't work because there is no udev in the container to tell systemd that the device is present. Fixed by not depending on the device in the container. Activate the new container test for release Bonds, bridges and other network devices need the underlying not as dependency when used inside the container. Because the device is already there. But the address configuration needs the aggregated device itself.

commitf0492bd53c3e78afc429f3b2f3792f13d6352cdb authorArnold Krille <arnold@arnoldarts.de> dateFri 30 Sep 2016, 10:45 (10 years ago) parent2e255a2
4 files changed+101−12
nixos/modules/tasks/network-interfaces-scripted.nix+25−8 nixos/modules/tasks/network-interfaces.nix+9−4 nixos/release.nix+1 nixos/tests/containers-physical_interfaces.nix+66
nixos/modules/tasks/network-interfaces-scripted.nix+25−8
@@ -46,6 +46,23 @@ in
4646 systemd.services =
4747 let
4848
49+ deviceDependency = dev:
50+ if (config.boot.isContainer == false)
51+ then
52+ # Trust udev when not in the container
53+ [ (subsystemDevice dev) ]
54+ else
55+ # When in the container, check whether the interface is built from other definitions
56+ if (hasAttr dev cfg.bridges) ||
57+ (hasAttr dev cfg.bonds) ||
58+ (hasAttr dev cfg.macvlans) ||
59+ (hasAttr dev cfg.sits) ||
60+ (hasAttr dev cfg.vlans) ||
61+ (hasAttr dev cfg.vswitches) ||
62+ (hasAttr dev cfg.wlanInterfaces)
63+ then [ "${dev}-netdev.service" ]
64+ else [];
65+
4966 networkLocalCommands = {
5067 after = [ "network-setup.service" ];
5168 bindsTo = [ "network-setup.service" ];
@@ -120,8 +137,8 @@ in
120137 # order before network-setup because the routes that are configured
121138 # there may need ip addresses configured
122139 before = [ "network-setup.service" ];
123− bindsTo = [ (subsystemDevice i.name) ];
124− after = [ (subsystemDevice i.name) "network-pre.target" ];
140+ bindsTo = deviceDependency i.name;
141+ after = [ "network-pre.target" ] ++ (deviceDependency i.name);
125142 serviceConfig.Type = "oneshot";
126143 serviceConfig.RemainAfterExit = true;
127144 path = [ pkgs.iproute ];
@@ -179,7 +196,7 @@ in
179196
180197 createBridgeDevice = n: v: nameValuePair "${n}-netdev"
181198 (let
182− deps = map subsystemDevice v.interfaces;
199+ deps = if config.boot.isContainer then [] else map subsystemDevice v.interfaces;
183200 in
184201 { description = "Bridge Interface ${n}";
185202 wantedBy = [ "network-setup.service" (subsystemDevice n) ];
@@ -220,7 +237,7 @@ in
220237
221238 createVswitchDevice = n: v: nameValuePair "${n}-netdev"
222239 (let
223− deps = map subsystemDevice v.interfaces;
240+ deps = if config.boot.isContainer then [] else map subsystemDevice v.interfaces;
224241 ofRules = pkgs.writeText "vswitch-${n}-openFlowRules" v.openFlowRules;
225242 in
226243 { description = "Open vSwitch Interface ${n}";
@@ -253,7 +270,7 @@ in
253270
254271 createBondDevice = n: v: nameValuePair "${n}-netdev"
255272 (let
256− deps = map subsystemDevice v.interfaces;
273+ deps = if config.boot.isContainer then [] else map subsystemDevice v.interfaces;
257274 in
258275 { description = "Bond Interface ${n}";
259276 wantedBy = [ "network-setup.service" (subsystemDevice n) ];
@@ -291,7 +308,7 @@ in
291308
292309 createMacvlanDevice = n: v: nameValuePair "${n}-netdev"
293310 (let
294− deps = [ (subsystemDevice v.interface) ];
311+ deps = deviceDependency v.interface;
295312 in
296313 { description = "Vlan Interface ${n}";
297314 wantedBy = [ "network-setup.service" (subsystemDevice n) ];
@@ -316,7 +333,7 @@ in
316333
317334 createSitDevice = n: v: nameValuePair "${n}-netdev"
318335 (let
319− deps = optional (v.dev != null) (subsystemDevice v.dev);
336+ deps = optional (v.dev != null) (deviceDependency v.dev);
320337 in
321338 { description = "6-to-4 Tunnel Interface ${n}";
322339 wantedBy = [ "network-setup.service" (subsystemDevice n) ];
@@ -344,7 +361,7 @@ in
344361
345362 createVlanDevice = n: v: nameValuePair "${n}-netdev"
346363 (let
347− deps = [ (subsystemDevice v.interface) ];
364+ deps = deviceDependency v.interface;
348365 in
349366 { description = "Vlan Interface ${n}";
350367 wantedBy = [ "network-setup.service" (subsystemDevice n) ];
nixos/modules/tasks/network-interfaces.nix+9−4
@@ -310,9 +310,9 @@ in
310310 generate a random 32-bit ID using the following commands:
311311
312312 <literal>cksum /etc/machine-id | while read c rest; do printf "%x" $c; done</literal>
313−
313+
314314 (this derives it from the machine-id that systemd generates) or
315−
315+
316316 <literal>head -c4 /dev/urandom | od -A none -t x4</literal>
317317 '';
318318 };
@@ -972,12 +972,17 @@ in
972972 '';
973973 };
974974 } // (listToAttrs (flip map interfaces (i:
975+ let
976+ deviceDependency = if config.boot.isContainer
977+ then []
978+ else [ (subsystemDevice i.name) ];
979+ in
975980 nameValuePair "network-link-${i.name}"
976981 { description = "Link configuration of ${i.name}";
977982 wantedBy = [ "network-interfaces.target" ];
978983 before = [ "network-interfaces.target" ];
979− bindsTo = [ (subsystemDevice i.name) ];
980− after = [ (subsystemDevice i.name) "network-pre.target" ];
984+ bindsTo = deviceDependency;
985+ after = [ "network-pre.target" ] ++ deviceDependency;
981986 path = [ pkgs.iproute ];
982987 serviceConfig = {
983988 Type = "oneshot";
nixos/release.nix+1
@@ -227,6 +227,7 @@ in rec {
227227 tests.containers-bridge = callTest tests/containers-bridge.nix {};
228228 tests.containers-imperative = callTest tests/containers-imperative.nix {};
229229 tests.containers-extra_veth = callTest tests/containers-extra_veth.nix {};
230+ tests.containers-physical_interfaces = callTest tests/containers-physical_interfaces.nix {};
230231 tests.docker = hydraJob (import tests/docker.nix { system = "x86_64-linux"; });
231232 tests.dnscrypt-proxy = callTest tests/dnscrypt-proxy.nix { system = "x86_64-linux"; };
232233 tests.ecryptfs = callTest tests/ecryptfs.nix {};
nixos/tests/containers-physical_interfaces.nixnew file+66
@@ -0,0 +1,66 @@
1+
2+import ./make-test.nix ({ pkgs, ...} : {
3+ name = "containers-physical_interfaces";
4+ meta = with pkgs.stdenv.lib.maintainers; {
5+ maintainers = [ kampfschlaefer ];
6+ };
7+
8+ nodes = {
9+ server = { config, pkgs, ... }:
10+ {
11+ virtualisation.memorySize = 256;
12+ virtualisation.vlans = [ 1 ];
13+
14+ containers.server = {
15+ privateNetwork = true;
16+ interfaces = [ "eth1" ];
17+
18+ config = {
19+ networking.interfaces.eth1 = {
20+ ip4 = [ { address = "10.10.0.1"; prefixLength = 24; } ];
21+ };
22+ networking.firewall.enable = false;
23+ };
24+ };
25+ };
26+ client = { config, pkgs, ... }: {
27+ virtualisation.memorySize = 256;
28+ virtualisation.vlans = [ 1 ];
29+
30+ containers.client = {
31+ privateNetwork = true;
32+ interfaces = [ "eth1" ];
33+
34+ config = {
35+ networking.bridges.br0.interfaces = [ "eth1" ];
36+ networking.interfaces.br0 = {
37+ ip4 = [ { address = "10.10.0.2"; prefixLength = 24; } ];
38+ };
39+ networking.firewall.enable = false;
40+ };
41+ };
42+ };
43+ };
44+
45+ testScript = ''
46+ startAll;
47+
48+ $server->waitForUnit("default.target");
49+ $server->execute("ip link >&2");
50+
51+ $server->succeed("ip link show dev eth1 >&2");
52+
53+ $server->succeed("nixos-container start server");
54+ $server->waitForUnit("container\@server");
55+ $server->succeed("systemctl -M server list-dependencies network-addresses-eth1.service >&2");
56+
57+ $server->succeed("nixos-container run server -- ip a show dev eth1 >&2");
58+
59+ $client->waitForUnit("default.target");
60+ $client->succeed("nixos-container start client");
61+ $client->waitForUnit("container\@client");
62+ $client->succeed("systemctl -M client list-dependencies network-addresses-br0.service >&2");
63+ $client->succeed("systemctl -M client status -n 30 -l network-addresses-br0.service");
64+ $client->succeed("nixos-container run client -- ping -w 10 -c 1 -n 10.10.0.1");
65+ '';
66+})