diff --git a/elements/dhcp-all-interfaces/README.md b/elements/dhcp-all-interfaces/README.md new file mode 100644 index 00000000..0ab7e900 --- /dev/null +++ b/elements/dhcp-all-interfaces/README.md @@ -0,0 +1,14 @@ +Autodetect network interfaces during boot and configure them for DHCP + +The rationale for this is that we are likely to require multiple +network interfaces for use cases such as baremetal and there is no way +to know ahead of time which one is which, so we will simply run a +DHCP client on all interfaces (except lo) that are visible on the first +boot. + +The script /usr/local/sbin/generate-interfaces-file.sh will be called +early in each boot and will scan available network interfaces and +ensure they are all present in /etc/network/interfaces. + +Note that this element is only expected to be useful on Debian-derived +distributions, currently. diff --git a/elements/dhcp-all-interfaces/install.d/50-dhcp-all-interfaces b/elements/dhcp-all-interfaces/install.d/50-dhcp-all-interfaces new file mode 100755 index 00000000..7775f6db --- /dev/null +++ b/elements/dhcp-all-interfaces/install.d/50-dhcp-all-interfaces @@ -0,0 +1,9 @@ +#!/bin/sh -x + +# Prepare the target system for regenerating /etc/network/interfaces +# on its first boot. + +SCRIPTDIR=$(dirname $0) + +install -D -g root -o root -m 0755 ${SCRIPTDIR}/generate-interfaces-file.sh /usr/local/sbin/generate-interfaces-file.sh +install -D -g root -o root -m 0755 ${SCRIPTDIR}/dhcp-all-interfaces.conf /etc/init/dhcp-all-interfaces.conf diff --git a/elements/dhcp-all-interfaces/install.d/dhcp-all-interfaces.conf b/elements/dhcp-all-interfaces/install.d/dhcp-all-interfaces.conf new file mode 100644 index 00000000..54724c89 --- /dev/null +++ b/elements/dhcp-all-interfaces/install.d/dhcp-all-interfaces.conf @@ -0,0 +1,10 @@ +# Call a script to generate an /etc/network/interfaces file to DHCP all available interfaces +# Then remove this config file so the script is never run again + +description "DHCP any connected, but unconfigured network interfaces" + +start on starting network-interface + +task + +exec /usr/local/sbin/generate-interfaces-file.sh diff --git a/elements/dhcp-all-interfaces/install.d/generate-interfaces-file.sh b/elements/dhcp-all-interfaces/install.d/generate-interfaces-file.sh new file mode 100755 index 00000000..ba0e7a86 --- /dev/null +++ b/elements/dhcp-all-interfaces/install.d/generate-interfaces-file.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +# Generate $INTERFACES_FILE on first boot +# This will add any unconfigured network interfaces to /etc/network/interfaces +# and configure them for DHCP + +INTERFACES_FILE="/etc/network/interfaces" + +function get_if_link() { + cat /sys/class/net/${1}/carrier +} + +for interface in $(ls /sys/class/net | grep -v ^lo$) ; do + echo -n "Inspecting interface: $interface..." + HAS_CONFIG=$(ifquery $interface >/dev/null 2>&1) + if [ "$HAS_CONFIG" == "" ]; then + ip link set dev $interface up >/dev/null 2>&1 + HAS_LINK="$(get_if_link $interface)" + + TRIES=3 + while [ "$HAS_LINK" == "0" -a $TRIES -gt 0 ]; do + HAS_LINK="$(get_if_link $interface)" + if [ "$HAS_LINK" == "1" ]; then + break + else + sleep 1 + fi + TRIES=$(( TRIES - 1 )) + done + if [ "$HAS_LINK" == "1" ] ; then + printf "auto $interface\r\niface $interface inet dhcp\r\n\r\n" >>$INTERFACES_FILE] + echo "Configured" + else + echo "No link detected, skipping" + fi + fi +done