Skip to content

Commit

Permalink
nixos/initrd.network: support predictable interface names
Browse files Browse the repository at this point in the history
When `initrd.network.enable` and `networking.usePredictableInterfaceNames`
are enabled, apply predictable interface naming in initrd.

This also fixes a bug:
Previously, setting `initrd.network.enable` effectively disabled option
`networking.usePredictableInterfaceNames`.
Reason: Interfaces brought up in initrd are not renamed by udev in boot
stage 2. So the unpredictable names assigned in initrd remained
unchanged after booting.

Implementation notes:
udhcpc uses eth0 as the default interface.
When predictable names are enabled, use the lexicographically
first Ethernet interface (en*) instead.
  • Loading branch information
erikarvstedt committed Oct 2, 2018
1 parent 2df3f7b commit 7612783
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
11 changes: 10 additions & 1 deletion nixos/modules/system/boot/initrd-network.nix
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ in

boot.initrd.kernelModules = [ "af_packet" ];

boot.initrd.extraUdevRulesCommands = mkIf config.networking.usePredictableInterfaceNames ''
cp -v ${pkgs.systemd}/lib/udev/rules.d/75-net-description.rules $out/
cp -v ${pkgs.path}/nixos/modules/services/hardware/80-net-setup-link.rules $out/
'';

boot.initrd.extraUtilsCommands = ''
copy_bin_and_libs ${pkgs.mkinitcpio-nfs-utils}/bin/ipconfig
'';
Expand Down Expand Up @@ -103,8 +108,12 @@ in
done
# Acquire a DHCP lease.
iface="${optionalString config.networking.usePredictableInterfaceNames
"--interface $(cd /sys/class/net; printf en*)"
}"
echo "acquiring IP address via DHCP..."
udhcpc --quit --now --script ${udhcpcScript} ${udhcpcArgs} && hasNetwork=1
# The interface can be overriden in `udhcpcArgs`
udhcpc --quit --now $iface --script ${udhcpcScript} ${udhcpcArgs} && hasNetwork=1
fi
''

Expand Down
2 changes: 2 additions & 0 deletions nixos/release-combined.nix
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ in rec {
(all nixos.tests.predictable-interface-names.unpredictable)
(all nixos.tests.predictable-interface-names.predictableNetworkd)
(all nixos.tests.predictable-interface-names.unpredictableNetworkd)
(all nixos.tests.predictable-interface-names.predictableInitrdNetwork)
(all nixos.tests.predictable-interface-names.unpredictableInitrdNetwork)
(all nixos.tests.printing)
(all nixos.tests.proxy)
(all nixos.tests.sddm.default)
Expand Down
30 changes: 25 additions & 5 deletions nixos/tests/predictable-interface-names.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,29 @@

let
inherit (import ../lib/testing.nix { inherit system; }) makeTest pkgs;
in pkgs.lib.listToAttrs (pkgs.lib.crossLists (predictable: withNetworkd: {
name = pkgs.lib.optionalString (!predictable) "un" + "predictable"
+ pkgs.lib.optionalString withNetworkd "Networkd";
inherit (pkgs) lib;
in lib.listToAttrs (map ({ predictable, withNetworkd ? false, withInitrdNetwork ? false }: rec {
name = lib.optionalString (!predictable) "un" + "predictable"
+ lib.optionalString withNetworkd "Networkd"
+ lib.optionalString withInitrdNetwork "InitrdNetwork";

value = makeTest {
name = "${if predictable then "" else "un"}predictableInterfaceNames${if withNetworkd then "-with-networkd" else ""}";
name = builtins.replaceStrings ["predictable"] ["predictableInterfaceNames"] name;
meta = {};

machine = { lib, ... }: {
imports = [ ../modules/profiles/minimal.nix ];
networking.usePredictableInterfaceNames = lib.mkForce predictable;
networking.useNetworkd = withNetworkd;
networking.dhcpcd.enable = !withNetworkd;
boot.initrd.network.enable = withInitrdNetwork;

# Ensure initrd DHCP works with predictable names.
# Use the same method as in tests/initrd-network.nix
boot.initrd.network.postCommands = lib.mkIf (predictable && !withNetworkd && withInitrdNetwork) ''
ip addr | grep 10.0.2.15 || exit 1
ping -c1 10.0.2.2 || exit 1
'';
};

testScript = ''
Expand All @@ -22,4 +33,13 @@ in pkgs.lib.listToAttrs (pkgs.lib.crossLists (predictable: withNetworkd: {
$machine->fail("ip link show ${if predictable then "eth0" else "ens3"}");
'';
};
}) [[true false] [true false]])
}) [
{ predictable = true; }
{ predictable = false; }

{ withNetworkd = true; predictable = true; }
{ withNetworkd = true; predictable = false; }

{ withInitrdNetwork = true; predictable = true; }
{ withInitrdNetwork = true; predictable = false; }
])

0 comments on commit 7612783

Please sign in to comment.