diff --git a/postinstall/init.service b/postinstall/init.service index b69f3b9..e31628c 100644 --- a/postinstall/init.service +++ b/postinstall/init.service @@ -1,5 +1,7 @@ [Unit] Description=Postinstall Init +Wants=network-online.target +After=network-online.target networking.service [Service] ExecStart=/usr/bin/env bash /debian-postinstall-init.sh diff --git a/postinstall/init.sh b/postinstall/init.sh index 887eb91..16ba387 100644 --- a/postinstall/init.sh +++ b/postinstall/init.sh @@ -1,8 +1,5 @@ #!/usr/bin/env bash -# Sometimes the network is slow, wait a bit -sleep 10 - # Get the current DHCP network address, gateway and DNS servers NIC=$(ip link | grep "^2:" | awk '{print $2}' | tr -d ":") IP_ADDRESS=$(ip address show ${NIC} | grep -E "inet " | awk '{print $2}') @@ -11,6 +8,10 @@ IP_ADDRESS=$(echo ${IP_ADDRESS} | sed 's/\/.*//') GATEWAY=$(ip route | grep "default" | awk '{print $3}') DNS_SERVERS=$(awk '/^nameserver/ {print $2}' /etc/resolv.conf | xargs) +# 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) +ifdown "${NIC}" || true + # Network interfaces (static address) cat << EOF > /etc/network/interfaces auto lo @@ -25,7 +26,10 @@ EOF # Tabs instead of spaces, neat the file sed -i 's/ /\t/gm' /etc/network/interfaces -# write a static resolv.conf + +ifup "${NIC}" || true + +# write a static resolv.conf after dhcpcd is gone so it survives reboot rm -f /etc/resolv.conf for ns in ${DNS_SERVERS}; do echo "nameserver ${ns}" >> /etc/resolv.conf