summaryrefslogtreecommitdiffstats
path: root/core/rootfs/rootfs-stage31/data/inc/udhcpc-trigger
blob: 795dd8a067a9734b0709f2bc1eabb751f4afd0a6 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/ash

exec >> /run/stdout
exec 2>> /run/stderr
set -x

if [ "x$1" != "xbound" -a "x$1" != "xrenew" ] || [ "x$interface" != "xbr0" ] || [ -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 [ -s "/run/firstip" ]; then
	#...some address is already configured...
	OLD=$(cat "/run/firstip")
	if [ "x${OLD}" != "x${ip}" ]; then
		#...it's a different one, reconfigure...
		echo "..reconfiguring ${OLD} 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
		rm -f -- "/run/firstgw"
		ip addr del "${OLD}" 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 -n "$ip" > "/run/firstip"

# Same procedure for default gateway
if [ -n "${router}" ]; then
	if [ -s "/run/firstgw" ]; then
		OLD=$(cat "/run/firstgw")
		if [ "x${OLD}" != "x${router}" ]; then
			echo "..reconfiguring default gw from ${OLD} to ${router}.."
			ip route del default 2>/dev/null
			ip route add default via "$router"
		fi
	else
		ip route add default via "$router"
	fi
	echo -n "$router" > "/run/firstgw"
fi

rm -f -- "/etc/resolv.conf"

# DNS/domain?
if [ -n "$dns" ]; then
	echo "..got DNS.."
	echo "# From DHCP in stage 3.1" >> "/run/config"
	echo "SLX_DNS='$dns'" >> "/run/config"
fi
for serv in $dns; do
	echo "nameserver $serv" >> "/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
# Sanitize: domain must not be list
domain="${domain%% *}"
# Write out
if [ -n "$domain" ]; then
	echo "domain $domain" >> "/etc/resolv.conf"
	echo "SLX_NET_DOMAIN='$domain'" >> /run/config
fi
if [ -n "$search" ]; then
	echo "search $search" >> "/etc/resolv.conf"
	echo "SLX_NET_SEARCH='$search'" >> /run/config
fi

for i in $ntpsrv; do
	echo "$i" >> "/run/ntpsrv"
done

# 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}"
fi
# Fallback tp IP
if [ -z "$hostname" ]; then
	hostname="noname-${ip//./-}"
	fqdn="${hostname}.invalid"
fi
if [ -n "$hostname" ]; then
	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/config"
fi