summaryrefslogtreecommitdiffstats
path: root/core/modules/run-virt/data/opt/openslx/scripts/runvirt-start_dhcpd
blob: e920855a3e2de8e234229b85c888bb429c8ef2d9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/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" )
[ -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%#${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

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