diskimage-builder/elements/dhcp-all-interfaces/install.d/generate-interfaces-file.sh
Clint Byrum 2129d78124 Fix ifquery call in dhcp-all-interfaces
Ifquery does not print anything for interfaces which only have a single
iface line. It does, however, return non-zero if the interface is not
configured at all, so we can use that to indicate whether or not there
is a configuration.

Fixes bug #1233579

Change-Id: Ia2fdafbea57e806eba99ae8ddaf395ebdcc306e1
2013-10-11 11:06:16 -07:00

39 lines
1005 B
Bash
Executable File

#!/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..."
if ifquery $interface >/dev/null 2>&1 ; then
echo "Has config, skipping."
else
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