wait for time sync up to 60s

This commit is contained in:
Aiden McClelland
2022-03-28 17:11:55 -06:00
committed by Aiden McClelland
parent fdf473016b
commit eeb557860e
3 changed files with 32 additions and 6 deletions

View File

@@ -1,8 +1,8 @@
[Unit]
Description=Embassy Init
After=network.target ntp.service
After=network.target
Requires=network.target
Wants=avahi-daemon.service nginx.service tor.service ntp.service
Wants=avahi-daemon.service nginx.service tor.service
[Service]
Type=oneshot

View File

@@ -1,3 +1,5 @@
use std::time::Duration;
use tokio::process::Command;
use crate::context::rpc::RpcContextConfig;
@@ -8,6 +10,19 @@ use crate::Error;
pub const SYSTEM_REBUILD_PATH: &str = "/embassy-os/system-rebuild";
pub async fn check_time_is_synchronized() -> Result<bool, Error> {
Ok(String::from_utf8(
Command::new("timedatectl")
.arg("show")
.arg("-p")
.arg("NTPSynchronized")
.invoke(crate::ErrorKind::Unknown)
.await?,
)?
.trim()
== "NTPSynchronized=yes")
}
pub async fn init(cfg: &RpcContextConfig, product_key: &str) -> Result<(), Error> {
let should_rebuild = tokio::fs::metadata(SYSTEM_REBUILD_PATH).await.is_ok();
let secret_store = cfg.secret_store().await?;
@@ -100,6 +115,18 @@ pub async fn init(cfg: &RpcContextConfig, product_key: &str) -> Result<(), Error
};
info.save(&mut handle).await?;
let mut warn_time_not_synced = true;
for _ in 0..60 {
if check_time_is_synchronized().await? {
warn_time_not_synced = false;
break;
}
tokio::time::sleep(Duration::from_secs(1)).await;
}
if warn_time_not_synced {
tracing::warn!("Timed out waiting for system time to synchronize");
}
crate::version::init(&mut handle).await?;
if should_rebuild {

View File

@@ -37,7 +37,6 @@ apt-get install -y \
ecryptfs-utils \
cifs-utils \
samba-common-bin \
ntp \
network-manager \
vim \
jq \
@@ -90,9 +89,9 @@ rm -rf /var/lib/tor/*
raspi-config nonint enable_overlayfs
# create a copy of the cmdline *without* the quirk string, so that it can be easily amended
sudo sed -i 's/usb-storage.quirks=152d:0562:u,14cd:121c:u,0781:cfcb:u //g' /boot/cmdline.txt
sudo cp /boot/cmdline.txt /boot/cmdline.txt.orig
sudo sed -i 's/^/usb-storage.quirks=152d:0562:u,14cd:121c:u,0781:cfcb:u /g' /boot/cmdline.txt
sed -i 's/usb-storage.quirks=152d:0562:u,14cd:121c:u,0781:cfcb:u //g' /boot/cmdline.txt
cp /boot/cmdline.txt /boot/cmdline.txt.orig
sed -i 's/^/usb-storage.quirks=152d:0562:u,14cd:121c:u,0781:cfcb:u /g' /boot/cmdline.txt
systemctl disable initialization.service
sudo systemctl restart NetworkManager