blob: 827c66aa41bdd5e876eb0e2599813fa62c46fc42 (
plain) (
tree)
|
|
#!/bin/bash
# If this doesn't exist, we don't support running VMs, so this is pointless
[ -s /opt/openslx/vmchooser/config/virtualization.conf ] || exit 0
. /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
mapfile -t out < <( 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]+' )
printf "%s" "${out[*]}"
}
# 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_NET_DOMAIN}" ] && SLX_NET_DOMAIN="$( getresolvconf domain )"
[ -z "${SLX_NET_SEARCH}" ] && SLX_NET_SEARCH="$( getresolvconf search )"
# Do not use helper here and check (loosely) for IPv4
declare -a dns
# sed any reference to localhost by our nat1 IP, in case we run something like
# dnsmasq locally later on
dns=( $( awk '$1 == "nameserver" && $2 ~ "\\..*\\..*\\." {print $2}' /etc/resolv.conf \
| sed 's/^127.*$/192.168.101.1/' ) )
[ -z "${dns}" ] && dns=( $SLX_DNS )
# Fallbacks
[ -z "${dns}" ] && dns=( "8.8.8.8" "8.8.4.4" )
# 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"
declare -a seds=(
"s#%DNSSERVER%#${dns[*]}#"
"s#%NTPSERVER%#${NTPSRV}#"
)
for var in DOMAIN SEARCH WINS; do
slxvar="SLX_NET_$var"
[ -n "${!slxvar}" ] && seds+=( "s#%${var}%#${!slxvar}#" )
[ -z "${!slxvar}" ] && seds+=( "/%${var}%/d" )
done
( IFS=';' ; sed "${seds[*]}" "${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
if [ -s "${DHCP_NAT_CONF}" ] && cmp -s "${DHCP_NAT_CONF}.$$" "${DHCP_NAT_CONF}"; then
# Files are the same, nothing to do
rm -f -- "${DHCP_NAT_CONF}.$$"
elif [ -s "${DHCP_NAT_CONF}" ] && [ "${DHCP_NAT_CONF}.$$" -ot "${DHCP_NAT_CONF}" ]; then
# Lost race
rm -f -- "${DHCP_NAT_CONF}.$$"
else
# Changed, replace and restart
echo "NAT1 dhcpd config changed; restarting service..."
diff -u "${DHCP_NAT_CONF}" "${DHCP_NAT_CONF}.$$"
mv -f -- "${DHCP_NAT_CONF}.$$" "${DHCP_NAT_CONF}"
systemctl --no-block try-restart run-virt-dhcpd.service
fi
exit 0
|