diff options
author | Eelco Dolstra <eelco.dolstra@logicblox.com> | 2016-02-02 19:03:13 +0100 |
---|---|---|
committer | Eelco Dolstra <eelco.dolstra@logicblox.com> | 2016-02-02 19:59:27 +0100 |
commit | cc925d0506ab2a049d5ee55c1173950073ed307f (patch) | |
tree | 3364d1625f558a4ac3ac9f722a9e65ef293008e3 /nixos/modules | |
parent | 901163c0c707d36323400394bf6e75adc4621e2f (diff) | |
download | nixlib-cc925d0506ab2a049d5ee55c1173950073ed307f.tar nixlib-cc925d0506ab2a049d5ee55c1173950073ed307f.tar.gz nixlib-cc925d0506ab2a049d5ee55c1173950073ed307f.tar.bz2 nixlib-cc925d0506ab2a049d5ee55c1173950073ed307f.tar.lz nixlib-cc925d0506ab2a049d5ee55c1173950073ed307f.tar.xz nixlib-cc925d0506ab2a049d5ee55c1173950073ed307f.tar.zst nixlib-cc925d0506ab2a049d5ee55c1173950073ed307f.zip |
boot.initrd.network: Support DHCP
This allows us to use it for EC2 instances.
Diffstat (limited to 'nixos/modules')
-rw-r--r-- | nixos/modules/system/boot/initrd-network.nix | 69 | ||||
-rw-r--r-- | nixos/modules/virtualisation/amazon-image.nix | 34 |
2 files changed, 58 insertions, 45 deletions
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 <literal>ip</literal> kernel - option, according to the kernel documentation. + Add network connectivity support to initrd. The network may be + configured using the <literal>ip</literal> kernel parameter, + as described in <link + xlink:href="https://www.kernel.org/doc/Documentation/filesystems/nfs/nfsroot.txt">the + kernel documentation</link>. Otherwise, if + <option>networking.useDHCP</option> 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; }; diff --git a/nixos/modules/virtualisation/amazon-image.nix b/nixos/modules/virtualisation/amazon-image.nix index dd2cdd43f31c..7d16206517d4 100644 --- a/nixos/modules/virtualisation/amazon-image.nix +++ b/nixos/modules/virtualisation/amazon-image.nix @@ -8,28 +8,7 @@ with lib; -let - - cfg = config.ec2; - - 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 +let cfg = config.ec2; in { imports = [ ../profiles/headless.nix ./ec2-data.nix ./amazon-grow-partition.nix ./amazon-init.nix ]; @@ -41,10 +20,7 @@ in autoResize = true; }; - boot.initrd.kernelModules = - [ "xen-blkfront" "xen-netfront" - "af_packet" # <- required by udhcpc - ]; + boot.initrd.kernelModules = [ "xen-blkfront" "xen-netfront" ]; boot.kernelParams = mkIf cfg.hvm [ "console=ttyS0" ]; # Prevent the nouveau kernel module from being loaded, as it @@ -67,6 +43,8 @@ in kill -9 -1 ''; + boot.initrd.network.enable = true; + # Mount all formatted ephemeral disks and activate all swap devices. # We cannot do this with the ‘fileSystems’ and ‘swapDevices’ options # because the set of devices is dependent on the instance type @@ -79,12 +57,10 @@ in boot.initrd.postMountCommands = '' metaDir=$targetRoot/etc/ec2-metadata - mkdir -m 0755 $targetRoot/etc + mkdir -m 0755 -p $targetRoot/etc mkdir -m 0700 -p "$metaDir" echo "getting EC2 instance metadata..." - ip link set eth0 up - udhcpc --interface eth0 --quit --now --script ${udhcpcScript} if ! [ -e "$metaDir/ami-manifest-path" ]; then wget -q -O "$metaDir/ami-manifest-path" http://169.254.169.254/1.0/meta-data/ami-manifest-path |