1
0
mirror of https://github.com/containers/youki synced 2024-11-23 09:21:57 +01:00

Support removal of systemd unit

This commit is contained in:
Furisto 2021-10-28 22:20:43 +02:00
parent 692f0adf86
commit 2bdf0fb80b
2 changed files with 32 additions and 14 deletions

@ -27,7 +27,7 @@ impl Client {
/// 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.
pub fn start_transient_unit_for_container(
pub fn start_transient_unit(
&self,
container_name: &str,
pid: u32,
@ -37,11 +37,7 @@ impl Client {
// To view and introspect the methods under the 'org.freedesktop.systemd1' destination
// and object path under it use the following command:
// `gdbus introspect --system --dest org.freedesktop.systemd1 --object-path /org/freedesktop/systemd1`
let proxy = self.conn.with_proxy(
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
Duration::from_millis(5000),
);
let proxy = self.create_proxy();
// To align with runc, yuoki will always add the following properties to its container units:
// - CPUAccounting=true
@ -61,8 +57,6 @@ impl Client {
if unit_name.ends_with("slice") {
properties.push(("Wants", Variant(Box::new(parent.to_owned()))));
} else {
log::debug!("SELECTED SCOPE");
log::debug!("{}", parent);
properties.push(("Slice", Variant(Box::new(parent.to_owned()))));
properties.push(("Delegate", Variant(Box::new(true))));
}
@ -77,7 +71,21 @@ impl Client {
proxy
.start_transient_unit(unit_name, "replace", properties, vec![])
.context("failed to start transient unit")?;
.with_context(|| {
format!(
"failed to start transient unit {}, parent is {}",
unit_name, parent
)
})?;
Ok(())
}
pub fn stop_transient_unit(&self, unit_name: &str) -> Result<()> {
let proxy = self.create_proxy();
proxy
.stop_unit(unit_name, "replace")
.with_context(|| format!("failed to stop unit {}", unit_name))?;
Ok(())
}

@ -1,4 +1,5 @@
use std::{
fmt::Display,
fs::{self},
os::unix::fs::PermissionsExt,
path::Component::RootDir,
@ -41,10 +42,16 @@ pub struct Manager {
#[derive(Debug)]
struct CgroupsPath {
parent: String,
scope: String,
prefix: String,
name: String,
}
impl Display for CgroupsPath {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{}:{}", self.parent, self.prefix, self.name)
}
}
impl Manager {
pub fn new(root_path: PathBuf, cgroups_path: PathBuf, container_name: String) -> Result<Self> {
// TODO: create the systemd unit using a dbus client.
@ -91,7 +98,7 @@ impl Manager {
Ok(CgroupsPath {
parent: parent.to_owned(),
scope: prefix.to_owned(),
prefix: prefix.to_owned(),
name: name.to_owned(),
})
}
@ -101,7 +108,7 @@ impl Manager {
fn get_unit_name(cgroups_path: &CgroupsPath) -> String {
// By default we create a scope unless specified explicitly.
if !cgroups_path.name.ends_with(".slice") {
return format!("{}-{}.scope", cgroups_path.scope, cgroups_path.name);
return format!("{}-{}.scope", cgroups_path.prefix, cgroups_path.name);
}
cgroups_path.name.clone()
}
@ -234,7 +241,7 @@ impl CgroupManager for Manager {
}
self.client
.start_transient_unit_for_container(
.start_transient_unit(
&self.container_name,
pid.as_raw() as u32,
&self.destructured_path.parent,
@ -265,7 +272,10 @@ impl CgroupManager for Manager {
}
fn remove(&self) -> Result<()> {
Ok(())
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))
}
fn freeze(&self, state: FreezerState) -> Result<()> {