Add element to run DHCP on all network interfaces.

Ensures that all network interfaces are present in
/etc/network/interfaces. Any interface not already defined there
will be added and configured for DHCP.

Change-Id: I27e0902e62804e8d719acd7288109bd0e294fd16
This commit is contained in:
Chris Jones 2013-03-05 17:34:19 +00:00
parent ff7eac3c65
commit a6b794fffc
4 changed files with 70 additions and 0 deletions

View File

@ -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.

View File

@ -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

View File

@ -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

View File

@ -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