From d0d756a02a6e417bb7194cbd805e6383feed1e43 Mon Sep 17 00:00:00 2001 From: Furisto <24721048+Furisto@users.noreply.github.com> Date: Fri, 14 Jan 2022 22:07:33 +0100 Subject: [PATCH] Ensure youki runs under podman --- crates/libcgroups/src/systemd/dbus/client.rs | 7 +++++ crates/libcgroups/src/systemd/manager.rs | 16 ++++++++---- crates/youki/src/commands/delete.rs | 6 ++++- crates/youki/src/commands/mod.rs | 27 ++++++++++++++++---- 4 files changed, 45 insertions(+), 11 deletions(-) diff --git a/crates/libcgroups/src/systemd/dbus/client.rs b/crates/libcgroups/src/systemd/dbus/client.rs index e274481f..bb489b9f 100644 --- a/crates/libcgroups/src/systemd/dbus/client.rs +++ b/crates/libcgroups/src/systemd/dbus/client.rs @@ -9,6 +9,8 @@ use std::time::Duration; pub trait SystemdClient { fn is_system(&self) -> bool; + fn transient_unit_exists(&self, unit_name: &str) -> bool; + fn start_transient_unit( &self, container_name: &str, @@ -67,6 +69,11 @@ impl SystemdClient for Client { self.system } + fn transient_unit_exists(&self, unit_name: &str) -> bool { + let proxy = self.create_proxy(); + proxy.get_unit(unit_name).is_ok() + } + /// start_transient_unit is a higher level API for starting a unit /// for a specific container under systemd. /// See https://www.freedesktop.org/wiki/Software/systemd/dbus for more details. diff --git a/crates/libcgroups/src/systemd/manager.rs b/crates/libcgroups/src/systemd/manager.rs index 7b411e3e..72aeae69 100644 --- a/crates/libcgroups/src/systemd/manager.rs +++ b/crates/libcgroups/src/systemd/manager.rs @@ -370,11 +370,13 @@ impl CgroupManager for Manager { fn remove(&self) -> Result<()> { log::debug!("remove {}", self.unit_name); - self.client - .stop_transient_unit(&self.unit_name) - .with_context(|| { - format!("could not remove control group {}", self.destructured_path) - })?; + if self.client.transient_unit_exists(&self.unit_name) { + self.client + .stop_transient_unit(&self.unit_name) + .with_context(|| { + format!("could not remove control group {}", self.destructured_path) + })?; + } Ok(()) } @@ -405,6 +407,10 @@ mod tests { true } + fn transient_unit_exists(&self, _: &str) -> bool { + true + } + fn start_transient_unit( &self, _container_name: &str, diff --git a/crates/youki/src/commands/delete.rs b/crates/youki/src/commands/delete.rs index 24eee161..3097f3de 100644 --- a/crates/youki/src/commands/delete.rs +++ b/crates/youki/src/commands/delete.rs @@ -1,4 +1,4 @@ -use crate::commands::load_container; +use crate::commands::{container_exists, load_container}; use anyhow::{Context, Result}; use std::path::PathBuf; @@ -6,6 +6,10 @@ use liboci_cli::Delete; pub fn delete(args: Delete, root_path: PathBuf) -> Result<()> { log::debug!("start deleting {}", args.container_id); + if !container_exists(&root_path, &args.container_id)? && args.force { + return Ok(()); + } + let mut container = load_container(root_path, &args.container_id)?; container .delete(args.force) diff --git a/crates/youki/src/commands/mod.rs b/crates/youki/src/commands/mod.rs index a4cf2056..facd1dc1 100644 --- a/crates/youki/src/commands/mod.rs +++ b/crates/youki/src/commands/mod.rs @@ -1,5 +1,8 @@ use anyhow::{bail, Context, Result}; -use std::{fs, path::Path}; +use std::{ + fs, + path::{Path, PathBuf}, +}; use libcgroups::common::CgroupManager; use libcontainer::container::Container; @@ -21,12 +24,21 @@ pub mod start; pub mod state; pub mod update; -fn load_container>(root_path: P, container_id: &str) -> Result { +fn construct_container_root>(root_path: P, container_id: &str) -> Result { // resolves relative paths, symbolic links etc. and get complete path - let root_path = fs::canonicalize(&root_path) - .with_context(|| format!("failed to canonicalize {}", root_path.as_ref().display()))?; + let root_path = fs::canonicalize(&root_path).with_context(|| { + format!( + "failed to canonicalize {} for container {}", + root_path.as_ref().display(), + container_id + ) + })?; // the state of the container is stored in a directory named after the container id - let container_root = root_path.join(container_id); + Ok(root_path.join(container_id)) +} + +fn load_container>(root_path: P, container_id: &str) -> Result { + let container_root = construct_container_root(root_path, container_id)?; if !container_root.exists() { bail!("container {} does not exist.", container_id) } @@ -35,6 +47,11 @@ fn load_container>(root_path: P, container_id: &str) -> Result>(root_path: P, container_id: &str) -> Result { + let container_root = construct_container_root(root_path, container_id)?; + Ok(container_root.exists()) +} + fn create_cgroup_manager>( root_path: P, container_id: &str,