summaryrefslogtreecommitdiffstats
path: root/core/rootfs/rootfs-stage31/data/inc/ntp_sync
blob: ff2f0027b42848c17f892f59687fa2ea7089ab6f (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
#!/bin/ash
# Sync time via network

func_sync_net_time() {
	local SERVER SUCCESS NTPSRV
	# Try to merge with servers from DHCP. Prefer DHCP, skip duplicates
	touch /run/ntpsrv
	for SERVER in $SLX_NTP_SERVER; do
		if ! grep -Fxq "$SERVER" /run/ntpsrv; then
			echo "$SERVER" >> /run/ntpsrv
		fi
	done
	NTPSRV=$(cat /run/ntpsrv)
	SUCCESS=
	for SERVER in $NTPSRV; do
		if ntpdate -u -p 2 "$SERVER"; then
			echo "Successfully queried $SERVER for time."
			if [ "x$SLX_BIOS_CLOCK" = "xlocal" ]; then
				usleep 100000
				hwclock -l -w || echo "... but could not set BIOS clock to localtime"
			elif [ "x$SLX_BIOS_CLOCK" = "xutc" ]; then
				usleep 100000
				hwclock -u -w || echo "... but could not set BIOS clock to UTC"
			fi
			SUCCESS=1
			break
		fi
		echo "Error querying $SERVER for current time."
	done
	if [ -z "$SUCCESS" ]; then
		echo "No NTP server reachable"
		# See if we have a timestamp in our server-config - should only be a few seconds off by now
		if [ -n "$SLX_NOW" ] && [ "$SLX_NOW" -gt 1234567890 ]; then
			TTS="$SLX_NOW"
			if [ -n "$CONFIG_DOWNLOAD_TIME" ]; then
				NOW_TIME=$(sed -r 's/^([0-9]+)\.([0-9]+).*$/\1\2/' /proc/uptime)
				if [ "$CONFIG_DOWNLOAD_TIME" -gt 0 ] && [ "$NOW_TIME" -gt 0 ]; then
					TTS=$(( TTS + ( NOW_TIME - CONFIG_DOWNLOAD_TIME ) / 100 ))
				fi
			fi
			echo "Setting time to SLX_NOW ($SLX_NOW, corrected $TTS)"
			date -s "@$TTS"
		else
			echo "No fallback option for timesync available, relying on correct RTC setup"
			if [ "x$SLX_BIOS_CLOCK" = "xlocal" ]; then
				# Linux defaults to RTC = UTC, so read again in this case
				hwclock -l -s
			fi
		fi
	fi
}

func_sync_net_time &

true