From 6383574aca155d18e4302974fb3e8468420236e6 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 11 Oct 2021 13:22:31 +0200 Subject: [run-virt/dhcpc-busybox] Restart nat1 dhcpd if network config changes --- .../data/opt/openslx/scripts/runvirt-start_dhcpd | 64 +++++++++++++++++++++ .../data/opt/openslx/scripts/systemd-run_virt_env | 66 +--------------------- 2 files changed, 67 insertions(+), 63 deletions(-) create mode 100755 core/modules/run-virt/data/opt/openslx/scripts/runvirt-start_dhcpd (limited to 'core/modules/run-virt') diff --git a/core/modules/run-virt/data/opt/openslx/scripts/runvirt-start_dhcpd b/core/modules/run-virt/data/opt/openslx/scripts/runvirt-start_dhcpd new file mode 100755 index 00000000..3b2ef5e4 --- /dev/null +++ b/core/modules/run-virt/data/opt/openslx/scripts/runvirt-start_dhcpd @@ -0,0 +1,64 @@ +#!/bin/bash + +. /opt/openslx/config +. /opt/openslx/vmchooser/config/virtualization.conf + +# Same as in service file for udhcpd +DHCP_NAT_CONF="/opt/openslx/vmchooser/config/udhcpd-nat1.conf" + +FALLBACK_DOMAIN="virtual.localnet" + +getips () { + [ -z "$1" ] && return + [ "$1" = "$FALLBACK_DOMAIN" ] && return + # Output in one line by using echo without quotes + echo $(busybox timeout 2 nslookup "$1" 2>/dev/null | grep -A 4 '^Name:' | grep -E '^Address\s*[0-9]*: ' | awk -F': ' '{print $2}' | grep -oE '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | awk '{print $1}') +} + +# read the DNS configuration and configure the udhcpd +getresolvconf() { + awk -vattr="$1" '$1 == attr {for (i=2; i<=NF; i++) printf "%s ",$i}' /etc/resolv.conf +} +[ -z "${SLX_DNS}" ] && SLX_DNS="$( getresolvconf nameserver )" +[ -z "${SLX_NET_DOMAIN}" ] && SLX_NET_DOMAIN="$( getresolvconf domain )" +[ -z "${SLX_NET_SEARCH}" ] && SLX_NET_SEARCH="$( getresolvconf search )" +[ -z "${SLX_DNS}" ] && SLX_DNS="8.8.8.8 8.8.4.4" +[ -z "${SLX_NET_DOMAIN}" ] && SLX_NET_DOMAIN="$FALLBACK_DOMAIN" +[ -z "${SLX_NET_SEARCH}" ] && SLX_NET_SEARCH="$FALLBACK_DOMAIN" +# WINS - if not supplied, try to get it from the search base of our ldap config +if [ -z "${SLX_NET_WINS}" ]; then + DC=$(grep -m1 -i '^BASE\s*DC=' "/etc/ldap.conf" | grep -o -i 'DC=.*' | sed -r 's/\s*,\s*DC=/./gI;s/^\s*DC=//I') + [ -z "$DC" ] && DC=$(grep -m1 -i '^ldap_search_base\s*=\s*DC=' "/etc/sssd/sssd.conf" | grep -o -i 'DC=.*' | sed -r 's/\s*,\s*DC=/./gI;s/^\s*DC=//I') + [ -n "$DC" ] && SLX_NET_WINS=$(getips "$DC") +fi +# NTP - default to pool.ntp.org +NTPSRV= +[ -z "$SLX_NTP_SERVER" ] && SLX_NTP_SERVER="pool.ntp.org" +for ips in $SLX_NTP_SERVER; do + if ! printf "%s" "$ips" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$'; then + ips="$( getips "$ips" )" + [ -z "$ips" ] && continue + fi + NTPSRV="$NTPSRV $ips" +done +[ -z "$NTPSRV" ] && NTPSRV="0.0.0.0" +[ -z "${SLX_NET_WINS}" ] && SLX_NET_WINS=$(getips "$SLX_NET_DOMAIN") +[ -z "${SLX_NET_WINS}" ] && SLX_NET_WINS="0.0.0.0" +sed "s#%DNSSERVER%#${SLX_DNS}#;s#%DOMAIN%#${SLX_NET_DOMAIN}#;s#%SEARCH%#${SLX_NET_SEARCH}#;s#%WINS%#${SLX_NET_WINS}#;s#%NTPSERVER%#${NTPSRV}#" "${DHCP_NAT_CONF}.template" > "${DHCP_NAT_CONF}.tmp" + +# Make sure the primary vm running (we most likely never run more than one at a time anyways) always gets the same ip +echo "static_lease $(echo "$MACADDRPREFIX:$MACADDRSUFFIX" | sed 's/%VMID%/01/') 192.168.101.20" >> "${DHCP_NAT_CONF}.tmp" + +mkdir -p /var/lib/udhcpd + +if [ -s "${DHCP_NAT_CONF}" ] && cmp -s "${DHCP_NAT_CONF}.tmp" "${DHCP_NAT_CONF}"; then + # Same, nothing to do + rm -f -- "${DHCP_NAT_CONF}.tmp" +else + # Changed, replace and restart + mv -f -- "${DHCP_NAT_CONF}.tmp" "${DHCP_NAT_CONF}" + echo "NAT1 dhcpd config changed; restarting service..." + systemctl --no-block try-restart run-virt-dhcpd.service +fi + +exit 0 diff --git a/core/modules/run-virt/data/opt/openslx/scripts/systemd-run_virt_env b/core/modules/run-virt/data/opt/openslx/scripts/systemd-run_virt_env index 526f69a6..0022bfd0 100755 --- a/core/modules/run-virt/data/opt/openslx/scripts/systemd-run_virt_env +++ b/core/modules/run-virt/data/opt/openslx/scripts/systemd-run_virt_env @@ -30,8 +30,6 @@ VMCHOOSER_DIR="/opt/openslx/vmchooser" VMCHOOSER_CONF_DIR="${VMCHOOSER_DIR}/config" VMCHOOSER_VIRT_CONF="${VMCHOOSER_CONF_DIR}/virtualization.conf" -# Same as in service file for udhcpd -DHCP_NAT_CONF="${VMCHOOSER_CONF_DIR}/udhcpd-nat1.conf" echo "# Generated $(date)" > "$VMCHOOSER_VIRT_CONF" @@ -74,23 +72,6 @@ fi chmod a+rwxt /tmp/virt -# enable normal users to setup the extended virtual machine redo file space -# and image source via network block device (NBD only at the moment) -# allow to start/stop samba on virtual devices (latter one ignores multiple -# independent daemons) - -#cat >> /mnt/etc/sudoers << EOF -# allow to start and stop the special /tmp redo file space (RAM + NBD/NFS server RAID) -#ALL ALL=NOPASSWD: /sbin/losetup /dev/loop* -#ALL ALL=NOPASSWD: /sbin/mdadm -C /dev/md0 -l linear --raid-devices=2 /dev/loop* /dev/* -#ALL ALL=NOPASSWD: /opt/openslx/rootfs/usr/sbin/nbd-client * * /dev/nbd* -# allow to configure and start/stop samba services -#ALL ALL=NOPASSWD: /opt/openslx/rootfs/bin/sed -i /etc/*/smb.conf -e * -#ALL ALL=NOPASSWD: /usr/sbin/nmbd -s /etc/*/smb.conf -#ALL ALL=NOPASSWD: /usr/sbin/smbd -s /etc/*/smb.conf -#ALL ALL=NOPASSWD: /opt/openslx/rootfs/usr/bin/killall nmbd smbd -#EOF - ################################################################################ echo "Determine host's hardware configuration" ################################################################################ @@ -185,56 +166,15 @@ echo "1" >/proc/sys/net/ipv4/conf/nat1/forwarding echo "1" >/proc/sys/net/ipv4/conf/br0/forwarding 2>/dev/null # iptables masquerade rule is now inserted by /opt/openslx/iptables/rules.d/50-virt-nat1-masquerading -FALLBACK_DOMAIN="virtual.localnet" - -getips () { - [ -z "$1" ] && return - [ "$1" = "$FALLBACK_DOMAIN" ] && return - # Output in one line by using echo without quotes - echo $(busybox timeout 2 nslookup "$1" 2>/dev/null | grep -A 4 '^Name:' | grep -E '^Address\s*[0-9]*: ' | awk -F': ' '{print $2}' | grep -oE '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | awk '{print $1}') -} - -# read the DNS configuration and configure the udhcpd -getresolvconf() { - awk -vattr="$1" '$1 == attr {for (i=2; i<=NF; i++) printf "%s ",$i}' /etc/resolv.conf -} -[ -z "${SLX_DNS}" ] && SLX_DNS="$( getresolvconf nameserver )" -[ -z "${SLX_NET_DOMAIN}" ] && SLX_NET_DOMAIN="$( getresolvconf domain )" -[ -z "${SLX_NET_SEARCH}" ] && SLX_NET_SEARCH="$( getresolvconf search )" -[ -z "${SLX_DNS}" ] && SLX_DNS="8.8.8.8 8.8.4.4" -[ -z "${SLX_NET_DOMAIN}" ] && SLX_NET_DOMAIN="$FALLBACK_DOMAIN" -[ -z "${SLX_NET_SEARCH}" ] && SLX_NET_SEARCH="$FALLBACK_DOMAIN" -# WINS - if not supplied, try to get it from the search base of our ldap config -if [ -z "${SLX_NET_WINS}" ]; then - DC=$(grep -m1 -i '^BASE\s*DC=' "/etc/ldap.conf" | grep -o -i 'DC=.*' | sed -r 's/\s*,\s*DC=/./gI;s/^\s*DC=//I') - [ -z "$DC" ] && DC=$(grep -m1 -i '^ldap_search_base\s*=\s*DC=' "/etc/sssd/sssd.conf" | grep -o -i 'DC=.*' | sed -r 's/\s*,\s*DC=/./gI;s/^\s*DC=//I') - [ -n "$DC" ] && SLX_NET_WINS=$(getips "$DC") -fi -# NTP - default to pool.ntp.org -NTPSRV= -[ -z "$SLX_NTP_SERVER" ] && SLX_NTP_SERVER="pool.ntp.org" -for ips in $SLX_NTP_SERVER; do - if ! printf "%s" "$ips" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$'; then - ips="$( getips "$ips" )" - [ -z "$ips" ] && continue - fi - NTPSRV="$NTPSRV $ips" -done -[ -z "$NTPSRV" ] && NTPSRV="0.0.0.0" -[ -z "${SLX_NET_WINS}" ] && SLX_NET_WINS=$(getips "$SLX_NET_DOMAIN") -[ -z "${SLX_NET_WINS}" ] && SLX_NET_WINS="0.0.0.0" -sed "s#%DNSSERVER%#${SLX_DNS}#;s#%DOMAIN%#${SLX_NET_DOMAIN}#;s#%SEARCH%#${SLX_NET_SEARCH}#;s#%WINS%#${SLX_NET_WINS}#;s#%NTPSERVER%#${NTPSRV}#" "${DHCP_NAT_CONF}.template" > "${DHCP_NAT_CONF}" - -# Make sure the primary vm running (we most likely never run more than one at a time anyways) always gets the same ip -echo "static_lease $(echo "$MACADDRPREFIX:$MACADDRSUFFIX" | sed 's/%VMID%/01/') 192.168.101.20" >> "${DHCP_NAT_CONF}" - -mkdir -p /var/lib/udhcpd # creating and configuring vsw2 brctl addbr vsw2 ip link set dev vsw2 up echo "1" >/proc/sys/net/ipv4/conf/vsw2/forwarding +# Write proper DHCPd config for VM network +/opt/openslx/scripts/runvirt-start_dhcpd + # estimate free mem during clean boot # TODO: Does that even make sense? We determine available memory during bootup and then blindly use # this value to configure the VM's memory size later on. Shouldn't we query this right before starting the VM? -- cgit v1.2.3-55-g7522