diff --git a/ic-os/components/setupos-scripts/check-ntp.sh b/ic-os/components/setupos-scripts/check-ntp.sh new file mode 100644 index 00000000000..fa74d8632d7 --- /dev/null +++ b/ic-os/components/setupos-scripts/check-ntp.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash + +set -o nounset +set -o pipefail + +SHELL="/bin/bash" +PATH="/sbin:/bin:/usr/sbin:/usr/bin" + +source /opt/ic/bin/functions.sh + +function check_ntp() { + echo "* Checking Chrony status..." + + systemctl is-active --quiet chrony + log_and_halt_installation_on_error "$?" "Chrony service not running or not active." + + retries=0 + max_retries=30 + while [ "$(timedatectl show -p NTPSynchronized --value)" != "yes" ]; do + if [ $retries -ge $max_retries ]; then + local service_logs=$(journalctl -u chrony.service --no-pager) + local log_message="System clock is not synchronized.\n\nChrony service logs:\n${service_logs}" + log_and_halt_installation_on_error 1 "${log_message}" + fi + + echo "* Chrony not yet synchronized. Waiting 2 seconds before retry..." + sleep 2 + ((retries++)) + done + + echo "* Chrony is running and time is in sync." +} + +function set_hwclock_utc() { + echo "* Setting hardware clock to UTC..." + timedatectl set-local-rtc 0 +} + +main() { + log_start "$(basename $0)" + check_ntp + set_hwclock_utc + log_end "$(basename $0)" +} + +main diff --git a/ic-os/components/setupos-scripts/setupos.sh b/ic-os/components/setupos-scripts/setupos.sh index 622dc3973fd..bc4065653ff 100755 --- a/ic-os/components/setupos-scripts/setupos.sh +++ b/ic-os/components/setupos-scripts/setupos.sh @@ -42,6 +42,7 @@ main() { /opt/ic/bin/check-config.sh /opt/ic/bin/check-hardware.sh /opt/ic/bin/check-network.sh + /opt/ic/bin/check-ntp.sh if kernel_cmdline_bool_default_true ic.setupos.perform_installation; then true else diff --git a/ic-os/components/setupos.bzl b/ic-os/components/setupos.bzl index b60e576cd8c..1b5606b6a53 100644 --- a/ic-os/components/setupos.bzl +++ b/ic-os/components/setupos.bzl @@ -16,6 +16,7 @@ component_files = { Label("//ic-os/components/setupos-scripts:check-hardware.sh"): "/opt/ic/bin/check-hardware.sh", Label("//ic-os/components/setupos-scripts:install-hostos.sh"): "/opt/ic/bin/install-hostos.sh", Label("//ic-os/components/setupos-scripts:check-network.sh"): "/opt/ic/bin/check-network.sh", + Label("//ic-os/components/setupos-scripts:check-ntp.sh"): "/opt/ic/bin/check-ntp.sh", Label("//ic-os/components/setupos-scripts:output-wrapper.sh"): "/opt/ic/bin/output-wrapper.sh", Label("//ic-os/components/setupos-scripts:setupos.sh"): "/opt/ic/bin/setupos.sh", Label("//ic-os/components/setupos-scripts:config.service"): "/etc/systemd/system/config.service",