diff --git a/elements/dhcp-all-interfaces/README.md b/elements/dhcp-all-interfaces/README.md index 0ab7e900..efc5b4c0 100644 --- a/elements/dhcp-all-interfaces/README.md +++ b/elements/dhcp-all-interfaces/README.md @@ -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. diff --git a/elements/dhcp-all-interfaces/install.d/50-dhcp-all-interfaces b/elements/dhcp-all-interfaces/install.d/50-dhcp-all-interfaces index 89ddcea8..df486709 100755 --- a/elements/dhcp-all-interfaces/install.d/50-dhcp-all-interfaces +++ b/elements/dhcp-all-interfaces/install.d/50-dhcp-all-interfaces @@ -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 +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 diff --git a/elements/dhcp-all-interfaces/install.d/dhcp-all-interfaces.service b/elements/dhcp-all-interfaces/install.d/dhcp-all-interfaces.service new file mode 100644 index 00000000..b38476f0 --- /dev/null +++ b/elements/dhcp-all-interfaces/install.d/dhcp-all-interfaces.service @@ -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 diff --git a/elements/dhcp-all-interfaces/install.d/generate-interfaces-file.sh b/elements/dhcp-all-interfaces/install.d/generate-interfaces-file.sh index 564e94db..81804b1a 100755 --- a/elements/dhcp-all-interfaces/install.d/generate-interfaces-file.sh +++ b/elements/dhcp-all-interfaces/install.d/generate-interfaces-file.sh @@ -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 -# Serialize runs so that we don't miss hot-add interfaces -FLOCK=${1:-} -if [ -z "$FLOCK" ] ; then - exec flock -x $INTERFACES_FILE $0 flocked +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 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