2013-03-05 17:34:19 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2014-01-09 20:32:52 +00:00
|
|
|
ENI_FILE="/etc/network/interfaces"
|
2013-03-05 17:34:19 +00:00
|
|
|
|
2014-01-09 20:32:52 +00:00
|
|
|
if [ -d "/etc/network" ]; then
|
|
|
|
CONF_TYPE="eni"
|
|
|
|
elif [ -d "/etc/sysconfig/network-scripts/" ]; then
|
|
|
|
CONF_TYPE="netscripts"
|
|
|
|
else
|
|
|
|
echo "Unsupported network configuration type!"
|
|
|
|
exit 1
|
|
|
|
fi
|
2013-03-05 17:34:19 +00:00
|
|
|
|
2014-01-09 20:32:52 +00:00
|
|
|
if [ "$CONF_TYPE" == "eni" ]; then
|
|
|
|
# Serialize runs so that we don't miss hot-add interfaces
|
|
|
|
FLOCK=${1:-}
|
|
|
|
if [ -z "$FLOCK" ] ; then
|
|
|
|
exec flock -x $ENI_FILE $0 flocked
|
|
|
|
fi
|
2013-10-11 18:02:52 +00:00
|
|
|
fi
|
|
|
|
|
2013-03-05 17:34:19 +00:00
|
|
|
function get_if_link() {
|
|
|
|
cat /sys/class/net/${1}/carrier
|
|
|
|
}
|
|
|
|
|
2014-01-09 20:32:52 +00:00
|
|
|
function enable_interface() {
|
|
|
|
local interface=$1
|
|
|
|
|
|
|
|
if [ "$CONF_TYPE" == "eni" ]; then
|
|
|
|
printf "auto $interface\niface $interface inet dhcp\n\n" >>$ENI_FILE
|
|
|
|
elif [ "$CONF_TYPE" == "netscripts" ]; then
|
|
|
|
printf "DEVICE=\"$interface\"\nBOOTPROTO=\"dhcp\"\nONBOOT=\"yes\"\nTYPE=\"Ethernet\"" >"/etc/sysconfig/network-scripts/ifcfg-$interface"
|
|
|
|
fi
|
|
|
|
echo "Configured $1"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function disable_interface() {
|
|
|
|
local interface=$1
|
|
|
|
|
|
|
|
if [ "$CONF_TYPE" == "netscripts" ]; then
|
|
|
|
local IFCFG_FILE="/etc/sysconfig/network-scripts/ifcfg-$interface"
|
|
|
|
if [ -f "$IFCFG_FILE" ]; then
|
|
|
|
rm $IFCFG_FILE
|
|
|
|
else
|
|
|
|
echo "No link detected, skipping"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "No link detected, skipping"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2013-03-05 17:34:19 +00:00
|
|
|
for interface in $(ls /sys/class/net | grep -v ^lo$) ; do
|
2014-01-21 20:58:45 +00:00
|
|
|
MAC_ADDR_TYPE="$(cat /sys/class/net/${interface}/addr_assign_type)"
|
|
|
|
|
2013-03-05 17:34:19 +00:00
|
|
|
echo -n "Inspecting interface: $interface..."
|
2013-10-11 18:01:06 +00:00
|
|
|
if ifquery $interface >/dev/null 2>&1 ; then
|
|
|
|
echo "Has config, skipping."
|
2014-01-21 20:58:45 +00:00
|
|
|
elif [ "$MAC_ADDR_TYPE" != "0" ]; then
|
|
|
|
echo "Device has generated MAC, skipping."
|
|
|
|
else
|
2013-03-05 17:34:19 +00:00
|
|
|
ip link set dev $interface up >/dev/null 2>&1
|
|
|
|
HAS_LINK="$(get_if_link $interface)"
|
|
|
|
|
2014-01-13 18:58:42 +00:00
|
|
|
TRIES=10
|
2013-03-05 17:34:19 +00:00
|
|
|
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
|
2014-01-09 20:32:52 +00:00
|
|
|
enable_interface "$interface"
|
2013-03-05 17:34:19 +00:00
|
|
|
else
|
2014-01-09 20:32:52 +00:00
|
|
|
disable_interface "$interface"
|
2013-03-05 17:34:19 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|