From 656254918425e4f0f46904eb1b27141f07f0e998 Mon Sep 17 00:00:00 2001 From: Jonathan Bauer Date: Fri, 13 Aug 2021 15:26:59 +0200 Subject: [slx-network] rework hostname setup try to cope for more corner cases and still do the right thing... --- .../slx-network/scripts/udhcpc-trigger.stage3 | 51 +++++++++++++--------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/modules.d/slx-network/scripts/udhcpc-trigger.stage3 b/modules.d/slx-network/scripts/udhcpc-trigger.stage3 index afac2027..402670d0 100755 --- a/modules.d/slx-network/scripts/udhcpc-trigger.stage3 +++ b/modules.d/slx-network/scripts/udhcpc-trigger.stage3 @@ -81,10 +81,13 @@ for srv in $dns; do echo "nameserver $srv" >> "/etc/resolv.conf" done +# always check DNS +echo "Checking DNS record for this host..." +[ -z "$fqdn" ] && fqdn="$(reverse_lookup "$ip")" +[ -n "$fqdn" ] && fqdn_hostname="${fqdn%%.*}" + 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="$(reverse_lookup "$ip")" + echo "Getting domain via DNS, as DHCP didn't supply one.." domain="${fqdn#*.}" fi @@ -115,33 +118,41 @@ if [ -n "$ntpsrv" ]; then fi # Hostname -# Ignore hostname/fqdn it if hostname was forced per KCL. -if [ -n "$SLX_PXE_HOSTNAME" ]; then - echo "Hostname set via KCL, ignoring DHCP hostname: '$hostname'" - hostname="$SLX_PXE_HOSTNAME" +if [ -n "$hostname" ]; then + if [ -n "$SLX_PXE_HOSTNAME" ] && [ "$hostname" != "$SLX_PXE_HOSTNAME" ]; then + echo "KCL and DHCP hostnames differ... assuming some KCL hack." + echo "Prefering KCL: '$SLX_PXE_HOSTNAME'." + hostname="$SLX_PXE_HOSTNAME" + fi else - 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="$(reverse_lookup "$ip")" - hostname="${fqdn%%.*}" - elif [ -n "$domain" ]; then - fqdn="${hostname}.${domain%% *}" # in case domain is a list + echo "No hostname specified in DHCP, checking DNS record." + # We ignore the corner case where DHCP didn't supply a hostname but + # there is a hardcoded one via KCL as it seems unlikely, log it nonetheless + if [ -n "$SLX_PXE_HOSTNAME" ]; then + echo "DHCP didn't supply a hostname but one was supplied by KCL?" \ + "This seems strange but using KCL hostname anyways." + hostname="$SLX_PXE_HOSTNAME" + else + # regular fallback to DNS and noname-* if needed + if [ -n "$fqdn_hostname" ]; then + echo "Using DNS record hostname: $fqdn_hostname." + hostname="$fqdn_hostname" + else + # no fallback hostname from DNS, use IP address + hostname="noname-${ip//./-}" + fi + if [ -n "$domain" ]; then + fqdn="${hostname}.${domain%% *}" # in case domain is a list + fi fi fi -if [ -z "$hostname" ]; then - # no fallback hostname from DNS, use IP address - hostname="noname-${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'" >> "$NETWORK_CONF" fi -- cgit v1.2.3-55-g7522