diff options
author | Jonathan Bauer | 2019-08-08 14:55:56 +0200 |
---|---|---|
committer | Jonathan Bauer | 2019-08-08 14:55:56 +0200 |
commit | ce1558966a8481d04f1ff78c1b58a91740513e69 (patch) | |
tree | 58c2042c79d57eea8ecefb413c1da805203a033f /builder/modules.d | |
parent | [slx-network] support for dhcp in stage4 (diff) | |
download | systemd-init-ce1558966a8481d04f1ff78c1b58a91740513e69.tar.gz systemd-init-ce1558966a8481d04f1ff78c1b58a91740513e69.tar.xz systemd-init-ce1558966a8481d04f1ff78c1b58a91740513e69.zip |
[slx-network] only care about main bootif
+ rdns
Diffstat (limited to 'builder/modules.d')
13 files changed, 142 insertions, 132 deletions
diff --git a/builder/modules.d/slx-network/TODO b/builder/modules.d/slx-network/TODO new file mode 100644 index 00000000..bd119512 --- /dev/null +++ b/builder/modules.d/slx-network/TODO @@ -0,0 +1 @@ +# autostart dhcp in stage4 diff --git a/builder/modules.d/slx-network/hooks/activate-bootif-dhcp.sh b/builder/modules.d/slx-network/hooks/activate-bootif-dhcp.sh index 93ca850d..179254d0 100644 --- a/builder/modules.d/slx-network/hooks/activate-bootif-dhcp.sh +++ b/builder/modules.d/slx-network/hooks/activate-bootif-dhcp.sh @@ -12,11 +12,8 @@ done # copy udhcpc@ systemd service, backup existing ones for debugging mkdir -p "${NEWROOT}/etc/systemd/system" -if [ -e "${NEWROOT}/etc/systemd/system/udhcpc@.service" ]; then - mv "${NEWROOT}/etc/systemd/system/udhcpc@.service"{,.stage4} -fi -cp -f "/opt/openslx/services/udhcpc@.service" "${NEWROOT}/etc/systemd/system" +cp -f "/opt/openslx/services/udhcpc-bootif.service" "${NEWROOT}/etc/systemd/system" # activate it for the bridge or the physical interface if not bridged -systemctl --root "$NEWROOT" enable "udhcpc@${SLX_BRIDGE:-${SLX_PXE_NETIF}}" +systemctl --root "$NEWROOT" enable "udhcpc-bootif" diff --git a/builder/modules.d/slx-network/hooks/copy-network-config.sh b/builder/modules.d/slx-network/hooks/copy-network-config.sh deleted file mode 100644 index 0ccafdbc..00000000 --- a/builder/modules.d/slx-network/hooks/copy-network-config.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash - -if [ -n "$NEWROOT" ]; then - for file in /etc/{hostname,resolv.conf,hosts}; do - if [ -e "${NEWROOT}/${file}" ]; then - mv "${NEWROOT}/${file}" "${NEWROOT}/${file}.stage4" - fi - cp -f "$file" "${NEWROOT}/etc" - done -fi -true diff --git a/builder/modules.d/slx-network/hooks/copy-network-files.sh b/builder/modules.d/slx-network/hooks/copy-network-files.sh new file mode 100644 index 00000000..6e76a27f --- /dev/null +++ b/builder/modules.d/slx-network/hooks/copy-network-files.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +if [ -n "$NEWROOT" ]; then + for file in /etc/{hostname,resolv.conf,hosts}; do + if [ -e "${NEWROOT}/${file}" ]; then + mv "${NEWROOT}/${file}" "${NEWROOT}/${file}.stage4" + fi + cp -f "$file" "${NEWROOT}/etc" + done + # move resolv.conf to /opt/openslx to detect we are managing it + mkdir -p "${NEWROOT}/opt/openslx" + mv "${NEWROOT}/etc/resolv.conf" "${NEWROOT}/opt/openslx/resolv.conf" + # finally make sure we have rdns helper + rdns="$(type -p rdns)" + if [ -n "$rdns" ]; then + cp "$rdns" "${NEWROOT}/opt/openslx/bin" + fi +fi +true diff --git a/builder/modules.d/slx-network/hooks/parse-ipxe-network-kcl.sh b/builder/modules.d/slx-network/hooks/parse-ipxe-network-kcl.sh index 0811d370..56abb680 100644 --- a/builder/modules.d/slx-network/hooks/parse-ipxe-network-kcl.sh +++ b/builder/modules.d/slx-network/hooks/parse-ipxe-network-kcl.sh @@ -91,13 +91,5 @@ save_network_config & # Create the udev rule to rename the boot interface to the declared BOOTIF_NAME create_udev_bootif_name_rule & -## Make dracut wait for network during the udev loop (initqueue) to make -## sure we have network access in the pre-mount hook as it is needed -## to get configurations and the root filesystem -NETIF= -[ -n "${BOOTIF_NAME}" ] && NETIF="${BOOTIF_NAME}" -[ -n "${BRIDGED}" ] && [ -n "${BRIDGE_NAME}" ] && NETIF="${BRIDGE_NAME}" -[ -n "${VLAN}" ] && NETIF="${BOOTIF_NAME}.${VLAN}" - /sbin/initqueue --settled /usr/local/bin/setup-bootif-network /sbin/initqueue --finished [ -e "/.network" ] diff --git a/builder/modules.d/slx-network/module-setup.sh b/builder/modules.d/slx-network/module-setup.sh index 560b3e5c..20e1fc31 100755 --- a/builder/modules.d/slx-network/module-setup.sh +++ b/builder/modules.d/slx-network/module-setup.sh @@ -28,13 +28,14 @@ install() { inst "${moddir}/scripts/udhcpc-trigger.stage3" "/usr/local/bin/udhcpc-trigger" # files for stage4, park them in /opt/openslx + gcc -o "${initdir}/usr/local/bin/rdns" "${moddir}/rdns.c" inst "${moddir}/scripts/setup-bootif-network.stage4" "/opt/openslx/scripts/setup-bootif-network.stage4" inst "${moddir}/scripts/udhcpc-trigger.stage4" "/opt/openslx/scripts/udhcpc-trigger.stage4" - inst "${moddir}/services/udhcpc@service" "/opt/openslx/services/udhcpc@service" + inst "${moddir}/services/udhcpc-bootif.service" "/opt/openslx/services/udhcpc-bootif.service" # hooks inst_hook cmdline 10 "${moddir}/hooks/parse-ipxe-network-kcl.sh" - inst_hook pre-pivot 50 "${moddir}/hooks/copy-network-config.sh" + inst_hook pre-pivot 50 "${moddir}/hooks/copy-network-files.sh" inst_hook pre-pivot 60 "${moddir}/hooks/activate-bootif-dhcp.sh" } diff --git a/builder/modules.d/slx-network/rdns.c b/builder/modules.d/slx-network/rdns.c new file mode 100644 index 00000000..218f7400 --- /dev/null +++ b/builder/modules.d/slx-network/rdns.c @@ -0,0 +1,28 @@ +#include <stdio.h> +#include <errno.h> +#include <netdb.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> + +int main(int argc, char *argv[]) +{ + if (argc != 2) { + fprintf(stderr,"usage: %s <IPADDRESS>\n", argv[0]); + return 1; + } + + struct hostent *he; + struct in_addr ipv4addr; + struct in6_addr ipv6addr; + + inet_pton(AF_INET, argv[1], &ipv4addr); + he = gethostbyaddr(&ipv4addr, sizeof ipv4addr, AF_INET); + if (he == NULL) return 2; + if (he->h_name == NULL) return 3; + printf("%s\n", he->h_name); + + return 0; +} + diff --git a/builder/modules.d/slx-network/scripts/setup-bootif-network.stage3 b/builder/modules.d/slx-network/scripts/setup-bootif-network.stage3 index 1f9eaddf..24bf0245 100755 --- a/builder/modules.d/slx-network/scripts/setup-bootif-network.stage3 +++ b/builder/modules.d/slx-network/scripts/setup-bootif-network.stage3 @@ -1,12 +1,16 @@ #!/bin/bash +# +# This script sets up the main network interface we booted from, +# as designated by SLX_PXE_NETIF (parsed from the PXE KCL in the +# cmdline dracut hook). +# It is run inside dracut's initqueue, on settles to detect the +# physical network interface as fast as possible. type emergency_shell >/dev/null 2>&1 || . /lib/dracut-lib.sh . /run/openslx/network.conf -_logfile="/run/openslx/network.log" - -# for the boot interface? +# guard to not run until the phsyical interface is not ready yet if [ ! -e "/sys/class/net/${SLX_PXE_NETIF}/device" ]; then exit 1 fi @@ -33,7 +37,7 @@ if ! wait_for_iface "$SLX_PXE_NETIF" 100; then fi if [ -n "$SLX_BRIDGE" ]; then - for try in 1 2 3 fail; do + for try in {1..10} ""; do ( set -e brctl addbr "$SLX_BRIDGE" @@ -48,13 +52,12 @@ if [ -n "$SLX_BRIDGE" ]; then [ "$?" -eq 0 ] && break # nope, handle - if [ "$try" = "fail" ]; then + if [ -z "$try" ]; then emergency_shell "Failed to setup main network bridge, giving up!" fi warn "Failed to setup main network bridge on try $try. Retrying ..." # delete bridge, inc try and sleep 100ms before trying again [ -e "/sys/class/net/${SLX_BRIDGE}" ] && brctl delbr "$SLX_BRIDGE" - try=$(( try + 1 )) usleep 100000 done fi @@ -85,4 +88,4 @@ for i in 1 1 1 fail; do done set +x -} &>> "${_logfile}.$$" +} &>> "/run/openslx/network.log.$$" diff --git a/builder/modules.d/slx-network/scripts/setup-bootif-network.stage4 b/builder/modules.d/slx-network/scripts/setup-bootif-network.stage4 index 7ccb94e6..bd89fec6 100755 --- a/builder/modules.d/slx-network/scripts/setup-bootif-network.stage4 +++ b/builder/modules.d/slx-network/scripts/setup-bootif-network.stage4 @@ -1,13 +1,18 @@ #!/bin/bash -# For arrays +# +# This script sets up the main network interface we booted from, +# as designated by SLX_PXE_NETIF in /opt/openslx/config +# It will run on either the bridge (SLX_BRIDGE is set) or the +# physical interface directly. export PATH=$PATH:/opt/openslx/sbin:/opt/openslx/bin -. /run/openslx/network.conf +. /opt/openslx/config -net_if="$1" -net_ip="$(ip addr show dev "${net_if}" | grep -m1 '^\s*inet ' | \ - awk -F " " '{print $2}' | awk -F "/" '{print $1}')" +if [ -z "$SLX_PXE_NETIF" ] || [ -z "$SLX_PXE_CLIENT_IP" ]; then + echo "Missing network information of the main boot interface." + exit 1 +fi # set default options declare -a udhcpc_opts @@ -19,16 +24,8 @@ udhcpc_opts+=("-O" "wpad") udhcpc_opts+=("-O" "search") udhcpc_opts+=("-O" "wins") -# need to renew? -if [ -n "$net_ip" ]; then - udhcpc_opts+=( "-r" "$net_ip" ) -fi - -primary="br0" -[ -n "$SLX_BRIDGE" ] && primary="$SLX_BRIDGE" - -# send machine uuid during DHCP if acting on primary interface -if [ "$primary" = "$net_if" ] && [ "$SLX_NET_DHCP_UUID" = "yes" ]; then +# send machine uuid during DHCP if configured +if [ "$SLX_NET_DHCP_UUID" = "yes" ]; then uid=$(dmidecode -s system-uuid | \ sed -r 's/^(..)(..)(..)(..)-(..)(..)-(..)(..)-(....)-/00\4\3\2\1\6\5\8\7\9/') if [ "${#uid}" = 34 ]; then @@ -39,7 +36,9 @@ fi mkdir -p /run/udhcpc || echo "Could not create '/run/udhcpc'." -udhcpc "${udhcpc_opts[@]}" -i "${net_if}" \ +udhcpc "${udhcpc_opts[@]}" \ + -i "${SLX_BRIDGE:-${SLX_PXE_NETIF}}" \ + -r "${SLX_PXE_CLIENT_IP}" \ -s /opt/openslx/scripts/udhcpc-trigger \ -p "/run/udhcpc/udhcpc.${net_if}.pid" ret=$? diff --git a/builder/modules.d/slx-network/scripts/udhcpc-trigger.stage3 b/builder/modules.d/slx-network/scripts/udhcpc-trigger.stage3 index 8bed11c7..2621b76f 100755 --- a/builder/modules.d/slx-network/scripts/udhcpc-trigger.stage3 +++ b/builder/modules.d/slx-network/scripts/udhcpc-trigger.stage3 @@ -1,8 +1,11 @@ -#!/bin/ash +#!/bin/bash +# +# This script is triggered by udhcpc in stage3 and handle the +# DHCP information given as parameters -exec &> "/run/openslx/udhcpc-trigger.log.$$" +{ +# for debugging set -x - NETWORK_CONF="/run/openslx/network.conf" . "$NETWORK_CONF" @@ -112,3 +115,5 @@ if [ -n "$hostname" ]; then fi touch /.network +set +x +} &>> "/run/openslx/network.log.$$" diff --git a/builder/modules.d/slx-network/scripts/udhcpc-trigger.stage4 b/builder/modules.d/slx-network/scripts/udhcpc-trigger.stage4 index 92aa9596..b51328ca 100755 --- a/builder/modules.d/slx-network/scripts/udhcpc-trigger.stage4 +++ b/builder/modules.d/slx-network/scripts/udhcpc-trigger.stage4 @@ -1,32 +1,19 @@ #!/bin/bash -# Needs bash for printf -v VARNAME -# ----------------------------------------------------------------------------- # -# Copyright (c) 2011..2018 bwLehrpool-Projektteam -# -# This program/file is free software distributed under the GPL version 2. -# See https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html -# -# If you have any feedback please consult https://bwlehrpool.de and -# send your feedback to bwlehrpool@hs-offenburg.de. -# -# General information about bwLehrpool can be found at https://bwlehrpool.de -# -# ----------------------------------------------------------------------------- -# -# Mini-Linux Toolkit -# -# ----------------------------------------------------------------------------- +# This script is triggered by udhcpc and handle the DHCP information +# given to this script as parameters. export PATH=$PATH:/opt/openslx/sbin:/opt/openslx/bin . /opt/openslx/config -primary="br0" -[ -n "$SLX_BRIDGE" ] && primary="$SLX_BRIDGE" +if [ -z "$SLX_PXE_NETIF" ] || [ -z "$SLX_PXE_CLIENT_IP" ]; then + echo "Missing PXE network interface information." + exit 1 +fi RESOLV_CONF="/opt/openslx/resolv.conf" -THIS_RESOLV="/run/network/${interface}.resolv" +THIS_RESOLV="/run/network/${SLX_PXE_NETIF}.resolv" rebuild_resolv_conf () { # Don't do anything if the active resolv.conf is not ours @@ -59,23 +46,17 @@ check_env() { echo "interface = '$interface'" >&2 exit 1 fi + # only run for the boot network interface + # TODO VLAN support + if [ "$interface" != "${SLX_BRIDGE:-${SLX_PXE_NETIF}}" ]; then + echo "Ignoring '$interface'..." + exit 1 + fi } -if [ ! -d /run ]; then - echo -n "Waiting for /run." >&2 - while [ ! -d /run ]; do - echo -n "." >&2 - usleep 500000 - done - echo "" >&2 -fi - -mkdir -p "/run/network" - case "$1" in bound|renew) check_env "$1" - mkdir -p "/run/dhcpc" # Set address on interface ip addr add "$ip/$(ipcalc -s -p "$ip" "$subnet" | sed s/.*=//)" dev "$interface" # Set default route, if given @@ -140,42 +121,39 @@ case "$1" in rebuild_resolv_conf fi - - # Things that should only happen for the main interface that was used for booting - if [ "$interface" = "$primary" ]; then - # Update IP, TODO: check if we really want/need to overwrite the PXE_IP, - # it might be better/clearer to change the SLX_DHCP_CLIENT_IP... - sed -i "s/^\(SLX_PXE_CLIENT_IP=\).*$/\1'$ip'/" /opt/openslx/config - # Write DOMAIN and SEARCH to /opt/openslx/config if empty - if [ -z "$SLX_NET_DOMAIN" ] && [ -n "$domain" ]; then - sed -i "/^SLX_NET_DOMAIN=/d" /opt/openslx/config - echo "SLX_NET_DOMAIN='$domain'" >> /opt/openslx/config - fi - if [ -z "$SLX_NET_SEARCH" ] && [ -n "$search" ]; then - sed -i "/^SLX_NET_SEARCH=/d" /opt/openslx/config - echo "SLX_NET_SEARCH='$search'" >> /opt/openslx/config - fi - # Same for WINS servers - if [ -z "$SLX_NET_WINS" ] && [ -n "$wins" ]; then - sed -i "/^SLX_NET_WINS=/d" /opt/openslx/config - echo "SLX_NET_WINS='$wins'" >> /opt/openslx/config - fi - - # Update /etc/issue for proper spacing - [ -x "/opt/openslx/scripts/openslx-create_issue" ] && /opt/openslx/scripts/openslx-create_issue - - # Remove any stray addresses; we expect the primary interface to only have one - # address supplied via DHCP. We do this after adding the new one, obviously. - rem_list=$( ip -o addr show "$interface" | awk '{ for (i=1;i<NF;++i) if ($i == "inet") print $(i+1) }' | grep -v "^${ip}/" ) - if [ -n "$rem_list" ]; then - echo "PRIMARY: Removing $rem_list since we just got assigned $ip" - echo 1 > "/proc/sys/net/ipv4/conf/$interface/promote_secondaries" - for addr in $rem_list; do - ip addr del "$addr" dev "$interface" - sed -i "/^$(escape_search "${addr%/*}")(\s|$)/d" /etc/hosts - done - fi - fi # end "primary only" + # TODO: check if we really want/need to overwrite the PXE_IP, + # it might be better/clearer to change the SLX_DHCP_CLIENT_IP... + sed -i "s/^\(SLX_PXE_CLIENT_IP=\).*$/\1'$ip'/" /opt/openslx/config + # Write DOMAIN and SEARCH to /opt/openslx/config if empty + if [ -z "$SLX_NET_DOMAIN" ] && [ -n "$domain" ]; then + sed -i "/^SLX_NET_DOMAIN=/d" /opt/openslx/config + echo "SLX_NET_DOMAIN='$domain'" >> /opt/openslx/config + fi + if [ -z "$SLX_NET_SEARCH" ] && [ -n "$search" ]; then + sed -i "/^SLX_NET_SEARCH=/d" /opt/openslx/config + echo "SLX_NET_SEARCH='$search'" >> /opt/openslx/config + fi + # Same for WINS servers + if [ -z "$SLX_NET_WINS" ] && [ -n "$wins" ]; then + sed -i "/^SLX_NET_WINS=/d" /opt/openslx/config + echo "SLX_NET_WINS='$wins'" >> /opt/openslx/config + fi + + # TODO find a better way to trigger additional code, hook dir? + # Update /etc/issue for proper spacing + [ -x "/opt/openslx/scripts/openslx-create_issue" ] && /opt/openslx/scripts/openslx-create_issue + + # Remove any stray addresses; we expect the primary interface to only have one + # address supplied via DHCP. We do this after adding the new one, obviously. + rem_list=$( ip -o addr show "$interface" | awk '{ for (i=1;i<NF;++i) if ($i == "inet") print $(i+1) }' | grep -v "^${ip}/" ) + if [ -n "$rem_list" ]; then + echo "PRIMARY: Removing $rem_list since we just got assigned $ip" + echo 1 > "/proc/sys/net/ipv4/conf/$interface/promote_secondaries" + for addr in $rem_list; do + ip addr del "$addr" dev "$interface" + sed -i "/^$(escape_search "${addr%/*}")(\s|$)/d" /etc/hosts + done + fi # Write to openslx-config echo "# Config written by openslx-dhcp-script (2)" >> /opt/openslx/config diff --git a/builder/modules.d/slx-network/services/udhcpc-bootif.service b/builder/modules.d/slx-network/services/udhcpc-bootif.service new file mode 100644 index 00000000..9578ad33 --- /dev/null +++ b/builder/modules.d/slx-network/services/udhcpc-bootif.service @@ -0,0 +1,10 @@ +[Unit] +Description=DHCP Client for the main boot interface +Before=network.target + +[Service] +Type=forking +ExecStart=/opt/openslx/scripts/setup-bootif-network + +[Install] +WantedBy=network.target diff --git a/builder/modules.d/slx-network/services/udhcpc@.service b/builder/modules.d/slx-network/services/udhcpc@.service deleted file mode 100644 index f2bcb840..00000000 --- a/builder/modules.d/slx-network/services/udhcpc@.service +++ /dev/null @@ -1,12 +0,0 @@ -[Unit] -Description=DHCP Client for %i -Before=network.target - -[Service] -Type=forking -PIDFile=/run/udhcpc/udhcpc.%i.pid -ExecStart=/opt/openslx/scripts/setup-bootif-network-stage4 %i -ExecStopPost=/opt/openslx/bin/rm /run/udhcpc/udhcpc.%i.pid - -[Install] -WantedBy=network.target |