From be512e7f346a3550d52bbc3421d2f531b2c65f70 Mon Sep 17 00:00:00 2001 From: Jeremi Piotrowski Date: Fri, 24 Feb 2023 13:43:26 +0100 Subject: [PATCH] agent: determine value of use_systemd_cgroup before LinuxContainer::new() Right now LinuxContainer::new() gets passed a CreateOpts struct, but then modifies the use_systemd_cgroup field inside that struct. Pull the cgroups path parsing logic into do_create_container, so that CreateOpts can be immutable in LinuxContainer::new. This is just moving things around, there should be no functional changes. Signed-off-by: Jeremi Piotrowski (cherry picked from commit b0691806f1437951c38436e28aa086ae9c6f00d9) Signed-off-by: Greg Kurz --- src/agent/rustjail/src/container.rs | 11 ++--------- src/agent/src/rpc.rs | 10 ++++++++-- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/agent/rustjail/src/container.rs b/src/agent/rustjail/src/container.rs index f75b6e6d54..ed69ef60d1 100644 --- a/src/agent/rustjail/src/container.rs +++ b/src/agent/rustjail/src/container.rs @@ -1449,7 +1449,7 @@ impl LinuxContainer { pub fn new + Display + Clone>( id: T, base: T, - mut config: Config, + config: Config, logger: &Logger, ) -> Result { let base = base.into(); @@ -1475,21 +1475,14 @@ impl LinuxContainer { .context(format!("Cannot change owner of container {} root", id))?; let spec = config.spec.as_ref().unwrap(); - let linux = spec.linux.as_ref().unwrap(); - - // determine which cgroup driver to take and then assign to config.use_systemd_cgroup - // systemd: "[slice]:[prefix]:[name]" - // fs: "/path_a/path_b" - let cpath = if SYSTEMD_CGROUP_PATH_FORMAT.is_match(linux.cgroups_path.as_str()) { - config.use_systemd_cgroup = true; + let cpath = if config.use_systemd_cgroup { if linux.cgroups_path.len() == 2 { format!("system.slice:kata_agent:{}", id.as_str()) } else { linux.cgroups_path.clone() } } else { - config.use_systemd_cgroup = false; if linux.cgroups_path.is_empty() { format!("/{}", id.as_str()) } else { diff --git a/src/agent/src/rpc.rs b/src/agent/src/rpc.rs index c52d866d65..0081765416 100644 --- a/src/agent/src/rpc.rs +++ b/src/agent/src/rpc.rs @@ -36,7 +36,7 @@ use protocols::health::{ use protocols::types::Interface; use protocols::{agent_ttrpc_async as agent_ttrpc, health_ttrpc_async as health_ttrpc}; use rustjail::cgroups::notifier; -use rustjail::container::{BaseContainer, Container, LinuxContainer}; +use rustjail::container::{BaseContainer, Container, LinuxContainer, SYSTEMD_CGROUP_PATH_FORMAT}; use rustjail::process::Process; use rustjail::specconv::CreateOpts; @@ -210,9 +210,15 @@ impl AgentService { // restore the cwd for kata-agent process. defer!(unistd::chdir(&olddir).unwrap()); + // determine which cgroup driver to take and then assign to use_systemd_cgroup + // systemd: "[slice]:[prefix]:[name]" + // fs: "/path_a/path_b" + let cgroups_path = oci.linux.as_ref().map_or("", |linux| &linux.cgroups_path); + let use_systemd_cgroup = SYSTEMD_CGROUP_PATH_FORMAT.is_match(cgroups_path); + let opts = CreateOpts { cgroup_name: "".to_string(), - use_systemd_cgroup: false, + use_systemd_cgroup, no_pivot_root: s.no_pivot_root, no_new_keyring: false, spec: Some(oci.clone()),