From 502b37ae6391e52b3a319c4ebf49e3358c50a440 Mon Sep 17 00:00:00 2001 From: volth Date: Mon, 10 Sep 2018 02:10:47 +0000 Subject: nixos/initrd-network: multiple fixes * acquire DHCP on the interfaces with networking.interface.$name.useDHCP == true or on all interfaces if networking.useDHCP == true (was only only "eth0") * respect "mtu" if it was in DHCP answer (it happens in the wild) * acquire and set up staticroutes (unlike others clients, udhcpc does not do the query by default); this supersedes https://github.com/NixOS/nixpkgs/pull/41829 --- nixos/modules/system/boot/initrd-network.nix | 30 ++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/nixos/modules/system/boot/initrd-network.nix b/nixos/modules/system/boot/initrd-network.nix index 33862b0965cc..1019b8db6dab 100644 --- a/nixos/modules/system/boot/initrd-network.nix +++ b/nixos/modules/system/boot/initrd-network.nix @@ -6,11 +6,23 @@ let cfg = config.boot.initrd.network; + dhcpinterfaces = lib.attrNames (lib.filterAttrs (iface: v: v.useDHCP == true) (config.networking.interfaces or {})); + udhcpcScript = pkgs.writeScript "udhcp-script" '' #! /bin/sh if [ "$1" = bound ]; then - ip address add "$ip/$mask" dev "$interface" + if [ -n "$mtu" ]; then + ip address add "$ip/$mask" dev "$interface" mtu "$mtu" + else + ip address add "$ip/$mask" dev "$interface" + fi + if [ -n "$staticroutes" ]; then + echo "$staticroutes" \ + | sed -r "s@(\S+) (\S+)@ ip route add \"\1\" via \"\2\" dev \"$interface\" ; @g" \ + | sed -r "s@ via \"0\.0\.0\.0\"@@g" \ + | /bin/sh + fi if [ -n "$router" ]; then ip route add default via "$router" dev "$interface" fi @@ -92,18 +104,24 @@ in '' # Otherwise, use DHCP. - + optionalString config.networking.useDHCP '' + + optionalString (config.networking.useDHCP || dhcpinterfaces != []) '' if [ -z "$hasNetwork" ]; then # Bring up all interfaces. - for iface in $(cd /sys/class/net && ls); do + for iface in $(ls /sys/class/net/); do echo "bringing up network interface $iface..." ip link set "$iface" up done - # Acquire a DHCP lease. - echo "acquiring IP address via DHCP..." - udhcpc --quit --now --script ${udhcpcScript} ${udhcpcArgs} && hasNetwork=1 + # Acquire DHCP leases. + for iface in ${ if config.networking.useDHCP then + "$(ls /sys/class/net/ | grep -v ^lo$)" + else + lib.concatMapStringsSep " " lib.escapeShellArg dhcpinterfaces + }; do + echo "acquiring IP address via DHCP on $iface..." + udhcpc --quit --now -i $iface -O staticroutes --script ${udhcpcScript} ${udhcpcArgs} && hasNetwork=1 + done fi '' -- cgit 1.4.1 From 16edfb22b814bf689a112de978bae97c334dd5e0 Mon Sep 17 00:00:00 2001 From: volth Date: Mon, 10 Sep 2018 02:39:15 +0000 Subject: oops --- nixos/modules/system/boot/initrd-network.nix | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/nixos/modules/system/boot/initrd-network.nix b/nixos/modules/system/boot/initrd-network.nix index 1019b8db6dab..720a9ffd53d6 100644 --- a/nixos/modules/system/boot/initrd-network.nix +++ b/nixos/modules/system/boot/initrd-network.nix @@ -12,10 +12,9 @@ let '' #! /bin/sh if [ "$1" = bound ]; then + ip address add "$ip/$mask" dev "$interface" if [ -n "$mtu" ]; then - ip address add "$ip/$mask" dev "$interface" mtu "$mtu" - else - ip address add "$ip/$mask" dev "$interface" + ip link set mtu "$mtu" dev "$interface" fi if [ -n "$staticroutes" ]; then echo "$staticroutes" \ -- cgit 1.4.1