summaryrefslogtreecommitdiffstats
path: root/builder/modules.d/slx-network/scripts/udhcpc-trigger.stage3
diff options
context:
space:
mode:
authorJonathan Bauer2019-08-08 13:53:37 +0200
committerJonathan Bauer2019-08-08 13:53:37 +0200
commitbec14f4e1ac4d1b4a7a782d6c3c54cd5ebe5208b (patch)
treeb0b9d81568f2d732988c54d432fcb5c35ccf39f9 /builder/modules.d/slx-network/scripts/udhcpc-trigger.stage3
parent[busybox] install busybox into stage4 (diff)
downloadsystemd-init-bec14f4e1ac4d1b4a7a782d6c3c54cd5ebe5208b.tar.gz
systemd-init-bec14f4e1ac4d1b4a7a782d6c3c54cd5ebe5208b.tar.xz
systemd-init-bec14f4e1ac4d1b4a7a782d6c3c54cd5ebe5208b.zip
[slx-network] support for dhcp in stage4
Diffstat (limited to 'builder/modules.d/slx-network/scripts/udhcpc-trigger.stage3')
-rwxr-xr-xbuilder/modules.d/slx-network/scripts/udhcpc-trigger.stage3114
1 files changed, 114 insertions, 0 deletions
diff --git a/builder/modules.d/slx-network/scripts/udhcpc-trigger.stage3 b/builder/modules.d/slx-network/scripts/udhcpc-trigger.stage3
new file mode 100755
index 00000000..8bed11c7
--- /dev/null
+++ b/builder/modules.d/slx-network/scripts/udhcpc-trigger.stage3
@@ -0,0 +1,114 @@
+#!/bin/ash
+
+exec &> "/run/openslx/udhcpc-trigger.log.$$"
+set -x
+
+NETWORK_CONF="/run/openslx/network.conf"
+. "$NETWORK_CONF"
+
+if [ "x$1" != "xbound" -a "x$1" != "xrenew" ] \
+ || [ "x$interface" != "x${SLX_BRIDGE:-${SLX_PXE_NETIF}}" ] \
+ || [ -z "$ip" ]; then
+ exit 0
+fi
+
+# If we already got an IP from KCL, see if it differs, and remove first if so
+# We just try to prevent everything from breaking if the DHCP server doesn't
+# objey the renew request by the client and hands out a new address
+if [ -n "$SLX_PXE_CLIENT_IP" ]; then
+ #...some address is already configured...
+ if [ "x${SLX_PXE_CLIENT_IP}" != "x${ip}" ]; then
+ #...it's a different one, reconfigure...
+ echo "..reconfiguring ${SLX_PXE_CLIENT_IP} to ${ip}.."
+ # remove default route and let it be added again below, as it might get lost when changing the primary address on the interface
+ ip route del default 2>/dev/null
+ ip addr del "$SLX_PXE_CLIENT_IP" dev "${interface}" 2>/dev/null
+ ip addr add "${ip}/$(ipcalc -s -p "${ip}" "${subnet}" | sed s/.*=//)" dev "${interface}"
+ fi
+else
+ #...no address configured yet, just add...
+ echo "..adding ${ip}.."
+ ip addr add "${ip}/$(ipcalc -s -p "${ip}" "${subnet}" | sed s/.*=//)" dev "${interface}"
+fi
+echo "SLX_DHCP_CLIENT_IP='$ip'" >> "$NETWORK_CONF"
+
+# Same procedure for default gateway
+if [ -n "${router}" ]; then
+ if [ -s "$SLX_PXE_GATEWAY" ]; then
+ if [ "x${SLX_PXE_GATEWAY}" != "x${router}" ]; then
+ echo "..reconfiguring default gw from ${SLX_PXE_GATEWAY} to ${router}.."
+ ip route replace default via "$router"
+ fi
+ else
+ ip route add default via "$router"
+ fi
+ echo "SLX_DHCP_GATEWAY='$router'" >> "$NETWORK_CONF"
+fi
+
+rm -f -- "/etc/resolv.conf"
+
+# DNS/domain?
+if [ -n "$dns" ]; then
+ echo "..got DNS.."
+ echo "# From DHCP in stage 3.1" >> "$NETWORK_CONF"
+ echo "SLX_DNS='$dns'" >> "$NETWORK_CONF"
+fi
+for srv in $dns; do
+ echo "nameserver $srv" >> "/etc/resolv.conf"
+done
+if [ -z "$domain" ]; then
+ # try to get domain via reverse lookup if empty
+ echo "..trying to get domain via DNS, as DHCP didn't supply one.."
+ fqdn=$(timeout -t 3 nslookup "$ip" | grep -E "^Address +[0-9]+: +$ip " | head -n 1 | awk '{print $4}')
+ domain="${fqdn#*.}"
+fi
+# Add domain to list of search domains if not in there yet
+if [ -n "$domain" ] && [ -n "$search" ]; then
+ FOUND=no
+ for sd in $search; do
+ [ "x$sd" = "x$domain" ] && FOUND=yes
+ done
+ [ "$FOUND" = "no" ] && search="$domain $search"
+elif [ -n "$domain" ]; then
+ search="$domain"
+fi
+# Write out
+if [ -n "$domain" ]; then
+ echo "domain $domain" >> "/etc/resolv.conf"
+ echo "SLX_NET_DOMAIN='$domain'" >> "/run/openslx/network.conf"
+fi
+if [ -n "$search" ]; then
+ echo "search $search" >> "/etc/resolv.conf"
+ echo "SLX_NET_SEARCH='$search'" >> "/run/openslx/network.conf"
+fi
+
+if [ -n "$ntpsrv" ]; then
+ echo "SLX_DHCP_NTP='$ntpsrv'" >> "/run/openslx/network.conf"
+fi
+
+# Hostname
+if [ -z "$hostname" ]; then
+ # as with domain, if there's no hostname, try to get via DNS
+ echo "..trying to get hostname via DNS, as DHCP didn't supply one.."
+ [ -z "$fqdn" ] && fqdn=$(timeout -t 3 nslookup "$ip" | grep -E "^Address +[0-9]+: +$ip " | head -n 1 | awk '{print $4}')
+ hostname="${fqdn%%.*}"
+elif [ -n "$domain" ]; then
+ fqdn="${hostname}.${domain%% *}" # in case domain is a list
+fi
+
+if [ -z "$hostname" ]; then
+ # no fallback hostname from DNS, use IP address
+ hostname="${ip//./-}"
+fi
+
+if [ -n "$hostname" ]; then
+ [ -z "$fqdn" ] && fqdn="$hostname"
+ echo "..setting hostname $hostname (fqdn: $fqdn).."
+ echo "$hostname" > "/proc/sys/kernel/hostname"
+ echo "$hostname" > "/etc/hostname"
+ echo "127.0.0.1 localhost" > "/etc/hosts"
+ echo "127.0.1.1 $fqdn $hostname" >> "/etc/hosts"
+ echo "SLX_HOSTNAME='$hostname'" >> "/run/openslx/network.conf"
+fi
+
+touch /.network