Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for systemd managed cgroups #46

Merged
merged 1 commit into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ jobs:
steps:
- uses: actions/checkout@v2
- run: rustup component add clippy
- run: sudo apt-get -y update
- 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 +32,8 @@ jobs:
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
- run: sudo apt-get -y update
- 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
124 changes: 116 additions & 8 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 [email protected]: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()? {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be the only place where the systemd crate is used. You do not need to do this now, but think about if we can check the availability of systemd another way and avoid another dependency. I

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
5 changes: 2 additions & 3 deletions src/cgroups/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

use anyhow::Result;
use std::{
io::Write,
io::Write,
path::{Path, PathBuf},
};

use oci_spec::LinuxCpu;

use crate::utils::{create_temp_dir, TempDir};

use crate::utils::{create_temp_dir, TempDir};

pub fn setup(testname: &str, cgroup_file: &str) -> (TempDir, PathBuf) {
let tmp = create_temp_dir(testname).expect("create temp directory for test");
Expand Down
2 changes: 1 addition & 1 deletion src/cgroups/v1/freezer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl Freezer {
mod tests {
use super::*;
use crate::cgroups::test::set_fixture;
use crate::utils::create_temp_dir;
use crate::utils::create_temp_dir;
use oci_spec::FreezerState;

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/cgroups/v1/hugetlb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl Hugetlb {
mod tests {
use super::*;
use crate::cgroups::test::set_fixture;
use crate::utils::create_temp_dir;
use crate::utils::create_temp_dir;
use oci_spec::LinuxHugepageLimit;
use std::fs::read_to_string;

Expand Down
Loading