Merge "Add element to run DHCP on all network interfaces."
This commit is contained in:
commit
2ea0430acb
14
elements/dhcp-all-interfaces/README.md
Normal file
14
elements/dhcp-all-interfaces/README.md
Normal 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.
|
9
elements/dhcp-all-interfaces/install.d/50-dhcp-all-interfaces
Executable file
9
elements/dhcp-all-interfaces/install.d/50-dhcp-all-interfaces
Executable 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
|
@ -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
|
37
elements/dhcp-all-interfaces/install.d/generate-interfaces-file.sh
Executable file
37
elements/dhcp-all-interfaces/install.d/generate-interfaces-file.sh
Executable 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
|
Loading…
Reference in New Issue
Block a user