Implement dhcp-all-interfaces for systemd.

Includes:

 * Enhancements to the generate-interfaces-file.sh script
 so that it generates ifcfg network-scripts for
 distros like Fedora/RHEL.

 * Includes a new dhcp-all-interfaces.service systemd
 script which ensures network interfaces get generated
 before the network service starts on Fedora.

 * Add a new disable_interface function to
 generate-interfaces-file.sh which (on Fedora)
 will delete the ifcfg config for an interface which
 isn't plugged in.
 This is important because ifcfg-eth0 exists by default
 and we want to remove it if the NIC isn't connected.
 Previous behaviour on Ubuntu is unchanged and the
 generate-interfaces-file.sh just logs it as skipped.

 * General doc updates...

I tested this on a multi-nic machine w/ Fedora where the first NIC
exists but was not connected.

Change-Id: Ia99e312539da43caefd72aa60398d43dac5dcc8f
Closes-Bug: 1239880
This commit is contained in:
Dan Prince 2014-01-09 15:32:52 -05:00
parent f5d4d8c711
commit 7511faa2b1
4 changed files with 66 additions and 24 deletions

View File

@ -8,7 +8,4 @@ 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.
ensure they are configured properly before networking services are started.

View File

@ -1,16 +1,14 @@
#!/bin/bash
set -x
# Prepare the target system for regenerating /etc/network/interfaces
# on its first boot.
SCRIPTDIR=$(dirname $0)
# Currently only installed on Debian-derived distributions
# Installing these on Fedora causes problems with os-svc-daemon
# https://bugs.launchpad.net/tripleo/+bug/1239880
if [ -d /etc/init ] ; then
install -D -g root -o root -m 0755 ${SCRIPTDIR}/generate-interfaces-file.sh /usr/local/sbin/generate-interfaces-file.sh
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
fi

View File

@ -0,0 +1,14 @@
[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/generate-interfaces-file.sh
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
Alias=dhcp-all-interfaces.service

View File

@ -1,21 +1,55 @@
#!/bin/bash
# Generate $INTERFACES_FILE on first boot
# This will add any unconfigured network interfaces to /etc/network/interfaces
# and configure them for DHCP
ENI_FILE="/etc/network/interfaces"
INTERFACES_FILE="/etc/network/interfaces"
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
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 $INTERFACES_FILE $0 flocked
exec flock -x $ENI_FILE $0 flocked
fi
fi
function get_if_link() {
cat /sys/class/net/${1}/carrier
}
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
}
for interface in $(ls /sys/class/net | grep -v ^lo$) ; do
echo -n "Inspecting interface: $interface..."
if ifquery $interface >/dev/null 2>&1 ; then
@ -35,10 +69,9 @@ for interface in $(ls /sys/class/net | grep -v ^lo$) ; do
TRIES=$(( TRIES - 1 ))
done
if [ "$HAS_LINK" == "1" ] ; then
printf "auto $interface\niface $interface inet dhcp\n\n" >>$INTERFACES_FILE
echo "Configured"
enable_interface "$interface"
else
echo "No link detected, skipping"
disable_interface "$interface"
fi
fi
done