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
|
|
|
|
}
|
|
|
|
|
2014-02-07 18:33:35 +00:00
|
|
|
function config_exists() {
|
|
|
|
local interface=$1
|
|
|
|
if [ "$CONF_TYPE" == "netscripts" ]; then
|
|
|
|
if [ -f "/etc/sysconfig/network-scripts/ifcfg-$interface" ]; then
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
return ifquery $interface >/dev/null 2>&1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2014-02-14 01:41:28 +00:00
|
|
|
for interface in $(ls /sys/class/net | grep -v ^lo$) ; do
|
|
|
|
MAC_ADDR_TYPE="$(cat /sys/class/net/${interface}/addr_assign_type)"
|
2014-01-21 20:58:45 +00:00
|
|
|
|
2013-03-05 17:34:19 +00:00
|
|
|
echo -n "Inspecting interface: $interface..."
|
2014-02-07 18:33:35 +00:00
|
|
|
if config_exists $interface; then
|
2013-10-11 18:01:06 +00:00
|
|
|
echo "Has config, skipping."
|
2014-02-14 01:41:28 +00:00
|
|
|
elif [ "$MAC_ADDR_TYPE" != "0" ]; then
|
2014-01-21 20:58:45 +00:00
|
|
|
echo "Device has generated MAC, skipping."
|
2014-02-14 01:41:28 +00:00
|
|
|
else
|
|
|
|
ip link set dev $interface up >/dev/null 2>&1
|
2013-03-05 17:34:19 +00:00
|
|
|
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
|
2014-02-14 01:41:28 +00:00
|
|
|
done
|