Skip to content

Commit

Permalink
Fix emulated cgroups v1 subsystem when running docker-in-docker (#2532)
Browse files Browse the repository at this point in the history
* Fix issues when running with docker-in-docker

Signed-off-by: Jorge Prendes <[email protected]>

* Add the comment to make it understand ease

Signed-off-by: utam0k <[email protected]>

---------

Signed-off-by: Jorge Prendes <[email protected]>
Signed-off-by: utam0k <[email protected]>
Co-authored-by: Toru Komatsu <[email protected]>
  • Loading branch information
jprendes and utam0k authored Dec 19, 2023
1 parent 035b0dc commit 4c6fa31
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
23 changes: 21 additions & 2 deletions crates/libcontainer/src/rootfs/mount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl Mount {
panic!("libcontainer can't run in a Legacy or Hybrid cgroup setup without the v1 feature");
#[cfg(feature = "v1")]
self.mount_cgroup_v1(mount, options).map_err(|err| {
tracing::error!("failed to mount cgroup v2: {}", err);
tracing::error!("failed to mount cgroup v1: {}", err);
err
})?
}
Expand Down Expand Up @@ -171,10 +171,29 @@ impl Mount {
tracing::debug!("cgroup mounts: {:?}", host_mounts);

// get process cgroups
let ppid = std::os::unix::process::parent_id();
// The non-zero ppid means that the PID Namespace is not separated.
let ppid = if ppid == 0 { std::process::id() } else { ppid };
let root_cgroups = Process::new(ppid as i32)?.cgroups()?.0;
let process_cgroups: HashMap<String, String> = Process::myself()?
.cgroups()?
.into_iter()
.map(|c| (c.controllers.join(","), c.pathname))
.map(|c| {
let hierarchy = c.hierarchy;
// When youki itself is running inside a container, the cgroup path
// will include the path of pid-1, which needs to be stripped before
// mounting.
let root_pathname = root_cgroups
.iter()
.find(|c| c.hierarchy == hierarchy)
.map(|c| c.pathname.as_ref())
.unwrap_or("");
let path = c
.pathname
.strip_prefix(root_pathname)
.unwrap_or(&c.pathname);
(c.controllers.join(","), path.to_owned())
})
.collect();
tracing::debug!("Process cgroups: {:?}", process_cgroups);

Expand Down
10 changes: 9 additions & 1 deletion crates/youki/src/observability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,15 @@ where
let journald = config.systemd_log;

let systemd_journald = if journald {
Some(tracing_journald::layer()?.with_syslog_identifier("youki".to_string()))
match tracing_journald::layer() {
Ok(layer) => Some(layer.with_syslog_identifier("youki".to_string())),
Err(err) => {
// Do not fail if we can't open syslog, just print a warning.
// This is the case in, e.g., docker-in-docker.
eprintln!("failed to initialize syslog logging: {:?}", err);
None
}
}
} else {
None
};
Expand Down

0 comments on commit 4c6fa31

Please sign in to comment.