Skip to content

Commit

Permalink
Add support for systemd managed cgroups
Browse files Browse the repository at this point in the history
  • Loading branch information
nimrodshn committed Jun 19, 2021
1 parent 2f14718 commit e50e73d
Show file tree
Hide file tree
Showing 13 changed files with 525 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:
steps:
- uses: actions/checkout@v2
- run: rustup component add clippy
- run: sudo apt-get install -y pkg-config libsystemd-dev libdbus-glib-1-dev
- uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
Expand All @@ -30,6 +31,7 @@ jobs:
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
- run: sudo apt-get install -y pkg-config libsystemd-dev libdbus-glib-1-dev
- run: cargo install cargo-when
- name: Build
run: ./build.sh
Expand Down
120 changes: 114 additions & 6 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ 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 = { version = "0.8", default-features = false }
dbus = "0.9.2"

[dev-dependencies]
oci_spec = { version = "0.1.0", path = "./oci_spec", features = ["proptests"] }
Expand Down
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,29 @@ For other platforms, please use the devcontainer that we prepared.
- Rust(See [here](https://www.rust-lang.org/tools/install))
- Docker(See [here](https://docs.docker.com/engine/install))

## Building
## Dependencies
```sh
$ cargo install cargo-when
```

### Debian, Ubuntu and related distributions
```sh
$ sudo dnf install \
pkg-config \
libsystemd-dev \
libdbus-glib-1-dev
```


### Fedora, Centos, RHEL and related distributions
```sh
$ cargo install cargo-when # installs prerequisite for building youki
$ sudo dnf install \
pkg-config \
systemd-dev \
dbus-devel
```

## Build
```sh
$ git clone git@github.com:containers/youki.git
$ cd youki
Expand Down
27 changes: 25 additions & 2 deletions src/cgroups/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use std::{
path::{Path, PathBuf},
};


use anyhow::{bail, Context, 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 @@ -91,7 +91,10 @@ pub fn get_supported_cgroup_fs() -> Result<Vec<Cgroup>> {
Ok(cgroups)
}

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 All @@ -109,6 +112,16 @@ pub fn create_cgroup_manager<P: Into<PathBuf>>(cgroup_path: P) -> Result<Box<dyn
}
(None, Some(cgroup2)) => {
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(),
Expand All @@ -119,6 +132,16 @@ 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(),
Expand Down
2 changes: 1 addition & 1 deletion src/cgroups/v1/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ pub fn get_subsystem_mount_points(subsystem: &str) -> Result<PathBuf> {
})
.map(|m| m.mount_point)
.ok_or_else(|| anyhow!("could not find mountpoint for {}", subsystem))
}
}
2 changes: 2 additions & 0 deletions src/cgroups/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ mod io;
pub mod manager;
mod memory;
mod pids;
pub mod systemd_manager;
pub mod util;
pub use systemd_manager::SystemDCGroupManager;
Loading

0 comments on commit e50e73d

Please sign in to comment.