You've already forked debian-unattended
Fix blank network settings when DHCP is slow.
This commit is contained in:
@@ -1,13 +1,48 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
# Get the current DHCP network address, gateway and DNS servers
|
# Get the current DHCP network address, gateway and DNS servers. DHCP can
|
||||||
NIC=$(ip link | grep "^2:" | awk '{print $2}' | tr -d ":")
|
# still be completing when this service starts, even after network-online.
|
||||||
IP_ADDRESS=$(ip address show ${NIC} | grep -E "inet " | awk '{print $2}')
|
get_network_settings() {
|
||||||
NETMASK=$(echo ${IP_ADDRESS} | sed 's/.*\///')
|
local cidr_address
|
||||||
IP_ADDRESS=$(echo ${IP_ADDRESS} | sed 's/\/.*//')
|
|
||||||
GATEWAY=$(ip route | grep "default" | awk '{print $3}')
|
# Clear every value before each attempt so a partial/stale DHCP lease can
|
||||||
|
# never be written to the static network configuration.
|
||||||
|
NIC=""
|
||||||
|
IP_ADDRESS=""
|
||||||
|
NETMASK=""
|
||||||
|
GATEWAY=""
|
||||||
|
DNS_SERVERS=""
|
||||||
|
|
||||||
|
NIC=$(ip link | awk -F ': ' '$1 == 2 {sub(/@.*/, "", $2); print $2; exit}')
|
||||||
|
[ -n "${NIC}" ] || return 1
|
||||||
|
|
||||||
|
cidr_address=$(ip -4 address show "${NIC}" | awk '/inet / {print $2; exit}')
|
||||||
|
[ -n "${cidr_address}" ] || return 1
|
||||||
|
|
||||||
|
IP_ADDRESS=${cidr_address%/*}
|
||||||
|
NETMASK=${cidr_address#*/}
|
||||||
|
GATEWAY=$(ip route show default | awk 'NR == 1 {print $3}')
|
||||||
DNS_SERVERS=$(awk '/^nameserver/ {print $2}' /etc/resolv.conf | xargs)
|
DNS_SERVERS=$(awk '/^nameserver/ {print $2}' /etc/resolv.conf | xargs)
|
||||||
|
|
||||||
|
[ -n "${IP_ADDRESS}" ] && [ -n "${NETMASK}" ] && \
|
||||||
|
[ -n "${GATEWAY}" ] && [ -n "${DNS_SERVERS}" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
MAX_NETWORK_ATTEMPTS=10
|
||||||
|
for attempt in $(seq 1 "${MAX_NETWORK_ATTEMPTS}"); do
|
||||||
|
if get_network_settings; then
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "${attempt}" -eq "${MAX_NETWORK_ATTEMPTS}" ]; then
|
||||||
|
echo "DHCP network settings were incomplete after ${MAX_NETWORK_ATTEMPTS} attempts; aborting postinstall." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "DHCP network settings are not ready (attempt ${attempt}/${MAX_NETWORK_ATTEMPTS}); retrying in 3 seconds." >&2
|
||||||
|
sleep 3
|
||||||
|
done
|
||||||
|
|
||||||
# Tear down DHCP first, while interfaces is still dhcp, so dhcpcd is gone
|
# Tear down DHCP first, while interfaces is still dhcp, so dhcpcd is gone
|
||||||
# before we write a static resolv.conf (otherwise it rewrites it on exit)
|
# before we write a static resolv.conf (otherwise it rewrites it on exit)
|
||||||
ifdown "${NIC}" || true
|
ifdown "${NIC}" || true
|
||||||
|
|||||||
Reference in New Issue
Block a user