Skip to content

Commit

Permalink
Detect group mount points by parsing /proc/mounts
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Moessmer authored and flxo committed Sep 24, 2020
1 parent 89009a8 commit 8ec5dbe
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 6 deletions.
45 changes: 45 additions & 0 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions north.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ data_dir = "target/north/data"
key_dir = "target/north/keys"

[cgroups]
memory = "/sys/fs/cgroup/memory/north"
cpu = "/sys/fs/cgroup/cpu/north"
memory = "north"
cpu = "north"

[devices]
unshare_root = "/"
Expand Down
1 change: 1 addition & 0 deletions north/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ minijail = { path = "../minijail/minijail" }
nix = "0.17.0"
north_common = { path = "../north_common" }
prettytable-rs = { version = "0.8.0", default-features = false }
proc-mounts = "0.2.4"
regex = "1.3.9"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.56"
Expand Down
19 changes: 17 additions & 2 deletions north/src/linux/cgroups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use async_std::{
use futures::{future::FutureExt, select};
use log::{debug, warn};
use north_common::manifest;
use proc_mounts::MountIter;
use std::time::Duration;

const LIMIT_IN_BYTES: &str = "memory.limit_in_bytes";
Expand Down Expand Up @@ -76,7 +77,9 @@ impl CGroup for CGroupMem {

impl CGroupMem {
pub async fn new(name: &str, cgroup: &manifest::CGroupMem, tx: EventTx) -> Result<CGroupMem> {
let path = SETTINGS.cgroups.memory.join(name);
let mount_point =
get_mount_point("memory").context("Failed to detect cgroups memory mount point")?;
let path = mount_point.join(SETTINGS.cgroups.memory.join(name));
create(&path).await?;

// Configure memory limit
Expand Down Expand Up @@ -151,7 +154,9 @@ impl CGroup for CGroupCpu {

impl CGroupCpu {
pub async fn new(name: &str, cgroup: &manifest::CGroupCpu) -> Result<CGroupCpu> {
let path = SETTINGS.cgroups.cpu.join(name);
let mount_point =
get_mount_point("cpu").context("Failed to detect cgroups cpu mount point")?;
let path = mount_point.join(SETTINGS.cgroups.cpu.join(name));
create(&path).await?;

// Configure cpu shares
Expand Down Expand Up @@ -247,3 +252,13 @@ async fn write(path: &Path, value: &str) -> Result<()> {
.with_context(|| format!("Failed sync {}", path.display()))?;
Ok(())
}

fn get_mount_point(cgroup: &str) -> Result<PathBuf> {
let cgroup = String::from(cgroup);
MountIter::new()
.context("Cannot access mount points")?
.filter_map(|m| m.ok())
.find(|m| m.fstype == "cgroup" && m.options.contains(&cgroup))
.map(|m| m.dest.into())
.ok_or_else(|| anyhow!("No mount point for cgroup {}", &cgroup))
}
4 changes: 2 additions & 2 deletions north/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,13 @@ struct CliOptions {
#[structopt(long)]
pub unshare_fstype: Option<String>,

/// CGroups Memory dir. This can be a subdir of the root memory cgroup.
/// CGroups Memory dir. This is the subdir added to the root memory cgroup.
/// The directory is created if it does not exist.
/// This groups acts as the toplevel north memory cgroup.
#[structopt(long)]
pub cgroup_memory: Option<PathBuf>,

/// CGroups CPU dir. This can be a subdir of the root memory cgroup.
/// CGroups CPU dir. This is the subdir added to the root cpu cgroup.
/// The directory is created if it does not exist.
/// This groups acts as the toplevel north cpu cgroup.
#[structopt(long)]
Expand Down

0 comments on commit 8ec5dbe

Please sign in to comment.