From 1308b5bcf33c1d0778d06d7eb32c1138199f4ead Mon Sep 17 00:00:00 2001 From: Aiden McClelland Date: Mon, 11 Mar 2024 16:10:59 -0600 Subject: [PATCH] wait up to 30s for ip address --- Makefile | 1 + core/startos/src/lxc/mod.rs | 38 ++++++++++++------- .../src/service/persistent_container.rs | 2 +- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index cb3d976e1..bc1edc6fe 100644 --- a/Makefile +++ b/Makefile @@ -148,6 +148,7 @@ update-overlay: $(ALL_TARGETS) $(call ssh,"sudo systemctl start startd") wormhole: core/target/$(ARCH)-unknown-linux-musl/release/startbox + @echo "Paste the following command into the shell of your start-os server:" @wormhole send core/target/$(ARCH)-unknown-linux-musl/release/startbox 2>&1 | awk -Winteractive '/wormhole receive/ { printf "sudo /usr/lib/startos/scripts/chroot-and-upgrade \"cd /usr/bin && rm startbox && wormhole receive --accept-file %s && chmod +x startbox\"\n", $$3 }' update: $(ALL_TARGETS) diff --git a/core/startos/src/lxc/mod.rs b/core/startos/src/lxc/mod.rs index 374952f98..f77837238 100644 --- a/core/startos/src/lxc/mod.rs +++ b/core/startos/src/lxc/mod.rs @@ -38,6 +38,7 @@ const LXC_CONTAINER_DIR: &str = "/var/lib/lxc"; const RPC_DIR: &str = "media/startos/rpc"; // must not be absolute path pub const CONTAINER_RPC_SERVER_SOCKET: &str = "service.sock"; // must not be absolute path pub const HOST_RPC_SERVER_SOCKET: &str = "host.sock"; // must not be absolute path +const CONTAINER_DHCP_TIMEOUT: Duration = Duration::from_secs(30); pub struct LxcManager { containers: Mutex>>, @@ -110,7 +111,6 @@ impl LxcManager { pub struct LxcContainer { manager: Weak, rootfs: OverlayGuard, - ip: Ipv4Addr, guid: Arc, rpc_bind: TmpMountGuard, config: LxcConfig, @@ -171,20 +171,9 @@ impl LxcContainer { .arg(&*guid) .invoke(ErrorKind::Lxc) .await?; - let ip = String::from_utf8( - Command::new("lxc-info") - .arg("--name") - .arg(&*guid) - .arg("-iH") - .invoke(ErrorKind::Docker) - .await?, - )? - .trim() - .parse()?; Ok(Self { manager: Arc::downgrade(manager), rootfs, - ip, guid: Arc::new(guid), rpc_bind, config, @@ -196,8 +185,29 @@ impl LxcContainer { self.rootfs.path() } - pub fn ip(&self) -> Ipv4Addr { - self.ip + pub async fn ip(&self) -> Result { + let start = Instant::now(); + loop { + let output = String::from_utf8( + Command::new("lxc-info") + .arg("--name") + .arg(&*self.guid) + .arg("-iH") + .invoke(ErrorKind::Docker) + .await?, + )?; + let out_str = output.trim(); + if !out_str.is_empty() { + return Ok(out_str.parse()?); + } + if start.elapsed() > CONTAINER_DHCP_TIMEOUT { + return Err(Error::new( + eyre!("Timed out waiting for container to acquire DHCP lease"), + ErrorKind::Timeout, + )); + } + tokio::time::sleep(Duration::from_millis(100)).await; + } } pub fn rpc_dir(&self) -> &Path { diff --git a/core/startos/src/service/persistent_container.rs b/core/startos/src/service/persistent_container.rs index 523b5f19a..a3756b37a 100644 --- a/core/startos/src/service/persistent_container.rs +++ b/core/startos/src/service/persistent_container.rs @@ -180,7 +180,7 @@ impl PersistentContainer { } let net_service = ctx .net_controller - .create_service(s9pk.as_manifest().id.clone(), lxc_container.ip()) + .create_service(s9pk.as_manifest().id.clone(), lxc_container.ip().await?) .await?; Ok(Self { s9pk,