Skip to content

Commit

Permalink
Add boot/00-check-rtc-and-wait-ntp.sh to cidata
Browse files Browse the repository at this point in the history
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 <norio.nomura@gmail.com>
  • Loading branch information
norio-nomura committed Nov 12, 2024
1 parent 84054a4 commit cfb418e
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions pkg/cidata/cidata.TEMPLATE.d/boot/00-check-rtc-and-wait-ntp.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/sh
set -eu

# 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.
test ! -c /dev/rtc0 || exit 0

# This script is intended for services running with systemd.
command -v systemctl >/dev/null 2>&1 || exit 0

# Enable `systemd-time-wait-sync.service` to wait for NTP synchronization at an earlier stage.
systemctl enable systemd-time-wait-sync.service

# For the first boot, where the above setting is not yet active, wait for NTP synchronization here.
until ntp_synchronized=$(timedatectl show --property=NTPSynchronized --value) && [ "${ntp_synchronized}" = "yes" ]; do
time_usec=$(timedatectl show --property=TimeUSec)
echo "${time_usec}, Waiting for NTP synchronization..."
sleep 1
done
# Print the result of NTP synchronization
ntp_message=$(timedatectl show-timesync --property=NTPMessage)
time_usec=$(timedatectl show --property=TimeUSec)
echo "${time_usec}, NTP synchronization complete."
echo "${ntp_message}"

0 comments on commit cfb418e

Please sign in to comment.