Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 19 additions & 26 deletions nixos/modules/system/boot/initrd-network.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ let

cfg = config.boot.initrd.network;

dhcpinterfaces = lib.attrNames (lib.filterAttrs (iface: v: v.useDHCP == true) (config.networking.interfaces or {}));
dhcpInterfaces = lib.attrNames (lib.filterAttrs (iface: v: v.useDHCP == true) (config.networking.interfaces or {}));
doDhcp = config.networking.useDHCP || dhcpInterfaces != [];
dhcpIfShellExpr = if config.networking.useDHCP
then "$(ls /sys/class/net/ | grep -v ^lo$)"
else lib.concatMapStringsSep " " lib.escapeShellArg dhcpInterfaces;

udhcpcScript = pkgs.writeScript "udhcp-script"
''
Expand Down Expand Up @@ -95,43 +99,32 @@ in
boot.initrd.preLVMCommands = mkBefore (
# Search for interface definitions in command line.
''
ifaces=""
for o in $(cat /proc/cmdline); do
case $o in
ip=*)
ipconfig $o && hasNetwork=1
ipconfig $o && ifaces="$ifaces $(echo $o | cut -d: -f6)"
;;
esac
done
''

# Otherwise, use DHCP.
+ optionalString (config.networking.useDHCP || dhcpinterfaces != []) ''
if [ -z "$hasNetwork" ]; then

# Bring up all interfaces.
for iface in $(ls /sys/class/net/); do
echo "bringing up network interface $iface..."
ip link set "$iface" up
done
+ optionalString doDhcp ''
# Bring up all interfaces.
for iface in ${dhcpIfShellExpr}; do
echo "bringing up network interface $iface..."
ip link set "$iface" up && ifaces="$ifaces $iface"
done

# 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
# Acquire DHCP leases.
for iface in ${dhcpIfShellExpr}; do
echo "acquiring IP address via DHCP on $iface..."
udhcpc --quit --now -i $iface -O staticroutes --script ${udhcpcScript} ${udhcpcArgs}
done
''

+ ''
if [ -n "$hasNetwork" ]; then
echo "networking is up!"
${cfg.postCommands}
fi
'');
+ cfg.postCommands);

};

Expand Down
2 changes: 1 addition & 1 deletion nixos/tests/initrd-network-ssh/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import ../make-test-python.nix ({ lib, ... }:
start_all()
client.wait_for_unit("network.target")
client.wait_until_succeeds("ping -c 1 server")
client.succeed(
client.wait_until_succeeds(
"ssh -i /etc/sshKey -o UserKnownHostsFile=/etc/knownHosts server 'touch /fnord'"
)
client.shutdown()
Expand Down