Skip to content

Commit

Permalink
Add support for cgroups managed by systemd
Browse files Browse the repository at this point in the history
  • Loading branch information
nimrodshn committed Jun 5, 2021
1 parent cbcc4a3 commit 987a312
Show file tree
Hide file tree
Showing 7 changed files with 354 additions and 13 deletions.
98 changes: 93 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ once_cell = "1.6.0"
futures = { version = "0.3", features = ["thread-pool"] }
regex = "1.5"
oci_spec = { version = "0.1.0", path = "./oci_spec" }
systemd = "0.8.2"
18 changes: 16 additions & 2 deletions src/cgroups/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use anyhow::{bail, Result};
use nix::unistd::Pid;
use oci_spec::LinuxResources;
use procfs::process::Process;
use systemd::daemon::booted;

use crate::cgroups::v1;
use crate::cgroups::v2;
Expand Down Expand Up @@ -50,7 +51,10 @@ pub fn write_cgroup_file<P: AsRef<Path>>(path: P, data: &str) -> Result<()> {
Ok(())
}

pub fn create_cgroup_manager<P: Into<PathBuf>>(cgroup_path: P) -> Result<Box<dyn CgroupManager>> {
pub fn create_cgroup_manager<P: Into<PathBuf>>(
cgroup_path: P,
systemd_cgroup: bool,
) -> Result<Box<dyn CgroupManager>> {
let cgroup_mount = Process::myself()?
.mountinfo()?
.into_iter()
Expand Down Expand Up @@ -78,13 +82,23 @@ pub fn create_cgroup_manager<P: Into<PathBuf>>(cgroup_path: P) -> Result<Box<dyn
match cgroup_override {
Ok(v) if v == "true" => {
log::info!("cgroup manager V2 will be used");
if systemd_cgroup {
if !booted()? {
bail!("systemd cgroup flag passed, but systemd support for managing cgroups is not available");
}
log::info!("systemd cgroup manager will be used");
return Ok(Box::new(v2::SystemDCGroupManager::new(
cgroup2.mount_point,
cgroup_path.into(),
)?));
}
Ok(Box::new(v2::manager::Manager::new(
cgroup2.mount_point,
cgroup_path.into(),
)?))
}
_ => Ok(Box::new(v1::manager::Manager::new(cgroup_path.into())?)),
}
}
}
_ => bail!("could not find cgroup filesystem"),
}
Expand Down
2 changes: 2 additions & 0 deletions src/cgroups/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ mod io;
pub mod manager;
mod memory;
mod pids;
pub mod systemd_manager;
pub use systemd_manager::SystemDCGroupManager;
Loading

0 comments on commit 987a312

Please sign in to comment.