Skip to content

Commit 1aa54e3

Browse files
committed
Add boot/00-check-rtc-and-wait-ntp.sh to cidata
In vz, the VM lacks an RTC when booting with a kernel image (see: https://developer.apple.com/forums/thread/760344). This causes incorrect system time until NTP synchronizes it, leading to TLS errors. To avoid TLS errors, this script waits for NTP synchronization if RTC is unavailable. This script does the following: - Exits with 0 if `/dev/rtc0` exists. - Exits with 0 if `systemctl` is not available. - Enables `systemd-time-wait-sync.service` to wait for NTP synchronization at an earlier stage on subsequent boots. - Waits for NTP synchronization within the script for the first boot. Log output during execution: ```console LIMA 2024-08-08T23:51:15+09:00| Executing /mnt/lima-cidata/boot/00-check-rtc-and-wait-ntp.sh Created symlink /etc/systemd/system/sysinit.target.wants/systemd-time-wait-sync.service → /usr/lib/systemd/system/systemd-time-wait-sync.service. TimeUSec=Thu 2024-08-08 23:51:15 JST, Waiting for NTP synchronization... TimeUSec=Thu 2024-08-08 23:51:16 JST, Waiting for NTP synchronization... ... TimeUSec=Thu 2024-08-08 23:51:41 JST, Waiting for NTP synchronization... TimeUSec=Thu 2024-08-08 23:51:42 JST, Waiting for NTP synchronization... TimeUSec=Tue 2024-11-12 11:43:37 JST, NTP synchronization complete. NTPMessage={ Leap=0, Version=4, Mode=4, Stratum=2, Precision=-25, RootDelay=991us, RootDispersion=259us, Reference=11FD1CFB, OriginateTimestamp=Thu 2024-08-08 23:51:43 JST, ReceiveTimestamp=Tue 2024-11-12 11:43:36 JST, TransmitTimestamp=Tue 2024-11-12 11:43:36 JST, DestinationTimestamp=Thu 2024-08-08 23:51:43 JST, Ignored=no, PacketCount=1, Jitter=0 } ``` Signed-off-by: Norio Nomura <[email protected]>
1 parent 9248baf commit 1aa54e3

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/sh
2+
set -eu
3+
4+
# In vz, the VM lacks an RTC when booting with a kernel image (see: https://developer.apple.com/forums/thread/760344).
5+
# This causes incorrect system time until NTP synchronizes it, leading to TLS errors.
6+
# To avoid TLS errors, this script waits for NTP synchronization if RTC is unavailable.
7+
test ! -c /dev/rtc0 || exit 0
8+
9+
# This script is intended for services running with systemd.
10+
command -v systemctl >/dev/null 2>&1 || exit 0
11+
12+
# Enable `systemd-time-wait-sync.service` to wait for NTP synchronization at an earlier stage.
13+
systemctl enable systemd-time-wait-sync.service
14+
15+
# For the first boot, where the above setting is not yet active, wait for NTP synchronization here.
16+
until ntp_synchronized=$(timedatectl show --property=NTPSynchronized --value) && [ "${ntp_synchronized}" = "yes" ]; do
17+
time_usec=$(timedatectl show --property=TimeUSec)
18+
echo "${time_usec}, Waiting for NTP synchronization..."
19+
sleep 1
20+
done
21+
# Print the result of NTP synchronization
22+
ntp_message=$(timedatectl show-timesync --property=NTPMessage)
23+
time_usec=$(timedatectl show --property=TimeUSec)
24+
echo "${time_usec}, NTP synchronization complete."
25+
echo "${ntp_message}"

0 commit comments

Comments
 (0)