Skip to content

Commit 4b05820

Browse files
committed
dhcp: if first ntp invocation fails, set time manually and retry
- some machines have no RTC at all, or an RTC with a failed battery - in certain situations, ntpd fails with "Alarm Clock" when trying to do too-big a jump - in this case, set system time to the date of the dhcpd binary and retry - yet-another-case where systemd would have saved us from pain Signed-off-by: Ricardo Pardini <[email protected]>
1 parent da3e653 commit 4b05820

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

files/dhcp.sh

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,29 @@ run_dhcp_client() {
2121
# the --nobackground is not used here because when it is used, dhcpcd doesn't honor the --timeout option
2222
# and waits indefinitely for a response. For one shot, we want to timeout after the 30 second default.
2323
/sbin/dhcpcd -f /dhcpcd.conf --allowinterfaces "${al}" -1 || true
24+
2425
# use busybox's ntpd to set the time after getting an IP address; don't fail
25-
echo 'sleep 1 second before calling ntpd' && sleep 1
26-
/usr/sbin/ntpd -n -q -dd -p pool.ntp.org || true
26+
echo "sleep 1 second before calling ntpd; date: '$(date)'" && sleep 1
27+
if ! /usr/sbin/ntpd -n -q -dd -p pool.ntp.org; then
28+
echo "ntpd call failed; setting time manually and retrying"
29+
# set system time to the date of the dhcpd binary file
30+
# this should recover from ntpd failures due to time being too far off
31+
date -s "$(stat -c %y /sbin/dhcpcd | cut -d'.' -f1)" || true
32+
tries=1 # retry up to 5 times
33+
while [ $tries -le 5 ]; do
34+
echo "waiting 1 second before retrying ntpd call; try #$tries ; date is now: '$(date)'"
35+
sleep 1
36+
if /usr/sbin/ntpd -n -q -dd -p pool.ntp.org; then
37+
echo "ntpd retry call succeeded on try #$tries; date is now: '$(date)'"
38+
break
39+
else
40+
echo "ntpd retry call failed on try #$tries"
41+
fi
42+
tries=$((tries + 1))
43+
done
44+
else
45+
echo "ntpd call succeeded; date is now: '$(date)'"
46+
fi
2747
else
2848
/sbin/dhcpcd --nobackground -f /dhcpcd.conf --allowinterfaces "${al}"
2949
fi

0 commit comments

Comments
 (0)