Support adding DHCP interfaces one at a time.

Refactors dhcp-all-interfaces.sh so that if an optional
INTERFACE argument (the first argument) is passed to the script
it only inspects that single interface. If no argument is
passed then the previous default behaviour is used which
causes all interfaces to be inspected.

To avoid a collision with the previous $1 we move to using
$FLOCKED for the exec flock command which runs on distributions
using ENI.

Also sets PATH so that the commands within the script
can all be found if it isn't set properly (/sbin/ip, /bin/cat, etc.)

This is a move towards using udev rules to add these types
of interfaces automatically.

Change-Id: I3ec8fd2cc2071bfc6943c744ca619e31b71146fc
This commit is contained in:
Dan Prince 2014-03-06 10:56:31 -08:00
parent bae43c91c6
commit f10e614579
4 changed files with 24 additions and 25 deletions

View File

@ -9,6 +9,5 @@ DIB_INIT_SYSTEM=$(dib-init-system)
if [ "$DIB_INIT_SYSTEM" == "upstart" ]; then
install -D -g root -o root -m 0755 ${SCRIPTDIR}/dhcp-all-interfaces.conf /etc/init/dhcp-all-interfaces.conf
elif [ "$DIB_INIT_SYSTEM" == "systemd" ]; then
install -D -g root -o root -m 0755 ${SCRIPTDIR}/dhcp-all-interfaces.service /usr/lib/systemd/system/dhcp-all-interfaces.service
systemctl enable dhcp-all-interfaces.service
install -D -g root -o root -m 0644 ${SCRIPTDIR}/udev.rules /etc/udev/rules.d/99-dhcp-all-interfaces.rules
fi

View File

@ -1,14 +0,0 @@
[Unit]
Description=DHCP All Interfaces Service
Wants=local-fs.target systemd-udev-settle.service
After=local-fs.target systemd-udev-settle.service
Before=network.service
[Service]
Type=oneshot
ExecStart=/usr/local/sbin/dhcp-all-interfaces.sh
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
Alias=dhcp-all-interfaces.service

View File

@ -1,7 +1,10 @@
#!/bin/bash
INTERFACE=${1:-} #optional, if not specified configure all available interfaces
ENI_FILE="/etc/network/interfaces"
PATH=/sbin:$PATH
if [ -d "/etc/network" ]; then
CONF_TYPE="eni"
elif [ -d "/etc/sysconfig/network-scripts/" ]; then
@ -13,9 +16,9 @@ fi
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
FLOCKED=${FLOCKED:-}
if [ -z "$FLOCKED" ] ; then
FLOCKED=true exec flock -x $ENI_FILE $0
fi
fi
@ -63,16 +66,17 @@ function config_exists() {
fi
}
for interface in $(ls /sys/class/net | grep -v ^lo$) ; do
MAC_ADDR_TYPE="$(cat /sys/class/net/${interface}/addr_assign_type)"
function inspect_interface() {
local interface=$1
local mac_addr_type="$(cat /sys/class/net/${interface}/addr_assign_type)"
echo -n "Inspecting interface: $interface..."
if config_exists $interface; then
echo "Has config, skipping."
elif [ "$MAC_ADDR_TYPE" != "0" ]; then
elif [ "$mac_addr_type" != "0" ]; then
echo "Device has generated MAC, skipping."
else
ip link set dev $interface up >/dev/null 2>&1
else
ip link set dev $interface up &>/dev/null
HAS_LINK="$(get_if_link $interface)"
TRIES=10
@ -91,4 +95,13 @@ for interface in $(ls /sys/class/net | grep -v ^lo$) ; do
disable_interface "$interface"
fi
fi
done
}
if [ -n "$INTERFACE" ]; then
inspect_interface $INTERFACE
else
for iface in $(ls /sys/class/net | grep -v ^lo$); do
inspect_interface $iface
done
fi

View File

@ -0,0 +1 @@
SUBSYSTEM=="net", ACTION=="add", RUN+="/usr/local/sbin/dhcp-all-interfaces.sh $name", RUN+="/sbin/ifup $name"