Skip to content

Commit 5d69caa

Browse files
committed
00-check-rtc-and-wait-ntp.sh: Set the system time to th modification time of the script
The larger the difference between this system time and the NTP server time, the longer the NTP synchronization will take. By setting the system time to the modification time of this script, which is likely to be closer to the actual time, the NTP synchronization time can be shortened. Signed-off-by: Norio Nomura <[email protected]>
1 parent 804ebf3 commit 5d69caa

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

pkg/cidata/cidata.TEMPLATE.d/boot/00-check-rtc-and-wait-ntp.sh

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,55 @@ test ! -c /dev/rtc0 || exit 0
99
# This script is intended for services running with systemd.
1010
command -v systemctl >/dev/null 2>&1 || exit 0
1111

12+
echo_with_time_usec() {
13+
time_usec=$(timedatectl show --property=TimeUSec)
14+
echo "${time_usec}, ${1}"
15+
}
16+
1217
# Enable `systemd-time-wait-sync.service` to wait for NTP synchronization at an earlier stage.
1318
systemctl enable systemd-time-wait-sync.service
1419

1520
# For the first boot, where the above setting is not yet active, wait for NTP synchronization here.
1621
max_retry=60 retry=0
1722
until ntp_synchronized=$(timedatectl show --property=NTPSynchronized --value) && [ "${ntp_synchronized}" = "yes" ] ||
1823
[ "${retry}" -gt "${max_retry}" ]; do
19-
time_usec=$(timedatectl show --property=TimeUSec)
20-
echo "${time_usec}, Waiting for NTP synchronization..."
24+
if [ "${retry}" -eq 0 ]; then
25+
# If /dev/rtc is not available, the system time set during the Linux kernel build is used.
26+
# The larger the difference between this system time and the NTP server time, the longer the NTP synchronization will take.
27+
# By setting the system time to the modification time of this script, which is likely to be closer to the actual time,
28+
# the NTP synchronization time can be shortened.
29+
echo_with_time_usec "Setting the system time to the modification time of ${0}."
30+
31+
# To set the time to a specified time, it is necessary to stop systemd-timesyncd.
32+
systemctl stop systemd-timesyncd
33+
34+
# Since `timedatectl set-time` fails if systemd-timesyncd is not stopped,
35+
# ensure that it is completely stopped before proceeding.
36+
until pid_of_timesyncd=$(systemctl show systemd-timesyncd --property=MainPID --value) && [ "${pid_of_timesyncd}" -eq 0 ]; do
37+
echo_with_time_usec "Waiting for systemd-timesyncd to stop..."
38+
sleep 1
39+
done
40+
41+
# Set the system time to the modification time of this script.
42+
modification_time=$(stat -c %y "${0}")
43+
echo_with_time_usec "Setting the system time to ${modification_time}."
44+
timedatectl set-time "${modification_time}"
45+
46+
# Restart systemd-timesyncd
47+
systemctl start systemd-timesyncd
48+
else
49+
echo_with_time_usec "Waiting for NTP synchronization..."
50+
fi
2151
retry=$((retry + 1))
2252
sleep 1
2353
done
2454
# Print the result of NTP synchronization
2555
ntp_message=$(timedatectl show-timesync --property=NTPMessage)
26-
time_usec=$(timedatectl show --property=TimeUSec)
2756
if [ "${ntp_synchronized}" = "yes" ]; then
28-
echo "${time_usec}, NTP synchronization complete."
57+
echo_with_time_usec "NTP synchronization complete."
2958
echo "${ntp_message}"
3059
else
31-
echo "${time_usec}, NTP synchronization timed out."
60+
echo_with_time_usec "NTP synchronization timed out."
3261
echo "${ntp_message}"
3362
exit 1
3463
fi

0 commit comments

Comments
 (0)