diff options
author | volth <volth@webmaster.ms> | 2018-09-10 02:10:47 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-10 02:10:47 +0000 |
commit | 502b37ae6391e52b3a319c4ebf49e3358c50a440 (patch) | |
tree | 96379eb1dfe843d4f5f8c65f56fe103663119de1 /nixos/modules | |
parent | 857bf71655823b1e5140e11d1046afa89eca1634 (diff) | |
download | nixlib-502b37ae6391e52b3a319c4ebf49e3358c50a440.tar nixlib-502b37ae6391e52b3a319c4ebf49e3358c50a440.tar.gz nixlib-502b37ae6391e52b3a319c4ebf49e3358c50a440.tar.bz2 nixlib-502b37ae6391e52b3a319c4ebf49e3358c50a440.tar.lz nixlib-502b37ae6391e52b3a319c4ebf49e3358c50a440.tar.xz nixlib-502b37ae6391e52b3a319c4ebf49e3358c50a440.tar.zst nixlib-502b37ae6391e52b3a319c4ebf49e3358c50a440.zip |
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
Diffstat (limited to 'nixos/modules')
-rw-r--r-- | nixos/modules/system/boot/initrd-network.nix | 30 |
1 files 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 '' |