From cc925d0506ab2a049d5ee55c1173950073ed307f Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 2 Feb 2016 19:03:13 +0100 Subject: boot.initrd.network: Support DHCP This allows us to use it for EC2 instances. --- nixos/modules/system/boot/initrd-network.nix | 69 +++++++++++++++++++++------- 1 file changed, 53 insertions(+), 16 deletions(-) (limited to 'nixos/modules/system/boot') diff --git a/nixos/modules/system/boot/initrd-network.nix b/nixos/modules/system/boot/initrd-network.nix index e1e49bce6938..abf88734a558 100644 --- a/nixos/modules/system/boot/initrd-network.nix +++ b/nixos/modules/system/boot/initrd-network.nix @@ -6,6 +6,23 @@ let cfg = config.boot.initrd.network; + udhcpcScript = pkgs.writeScript "udhcp-script" + '' + #! /bin/sh + if [ "$1" = bound ]; then + ip address add "$ip/$mask" dev "$interface" + if [ -n "$router" ]; then + ip route add default via "$router" dev "$interface" + fi + if [ -n "$dns" ]; then + rm -f /etc/resolv.conf + for i in $dns; do + echo "nameserver $dns" >> /etc/resolv.conf + done + fi + fi + ''; + in { @@ -16,10 +33,13 @@ in type = types.bool; default = false; description = '' - Add network connectivity support to initrd. - - Network options are configured via ip kernel - option, according to the kernel documentation. + Add network connectivity support to initrd. The network may be + configured using the ip kernel parameter, + as described in the + kernel documentation. Otherwise, if + is enabled, an IP address + is acquired using DHCP. ''; }; @@ -43,18 +63,35 @@ in copy_bin_and_libs ${pkgs.mkinitcpio-nfs-utils}/bin/ipconfig ''; - boot.initrd.preLVMCommands = '' - # Search for interface definitions in command line - for o in $(cat /proc/cmdline); do - case $o in - ip=*) - ipconfig $o && hasNetwork=1 - ;; - esac - done - - ${cfg.postCommands} - ''; + boot.initrd.preLVMCommands = + # Search for interface definitions in command line. + '' + for o in $(cat /proc/cmdline); do + case $o in + ip=*) + ipconfig $o && hasNetwork=1 + ;; + esac + done + '' + + # Otherwise, use DHCP. + + optionalString config.networking.useDHCP '' + if [ -z "$hasNetwork" ]; then + + # Bring up all interfaces. + for iface in $(cd /sys/class/net && ls); 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} + fi + '' + + + cfg.postCommands; }; -- cgit 1.4.1