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

[WIP] Allow youki to run with podman in rootless #1171

Closed
wants to merge 1 commit into from
Closed
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
18 changes: 17 additions & 1 deletion crates/libcgroups/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,22 @@ fn create_v2_cgroup_manager(_cgroup_path: PathBuf) -> Result<Box<dyn CgroupManag
bail!("cgroup v2 feature is required, but was not enabled during compile time");
}

/// Checks if rootless mode should be used
pub fn rootless_required() -> bool {
Copy link
Member

Choose a reason for hiding this comment

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

Can I ask you to write a unit test? The path(/proc/self/uid_map) should be tested by creating a function that returns the path and separation it from the test version, like that.
https://github.com/containers/youki/blob/95d85c510f94b701c3266728edcd68d4471e80e0/crates/youki/src/main.rs#L180-L188

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure thing, I'll add a unit test in the next update.

Copy link

Choose a reason for hiding this comment

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

FYI: you missed the doc comment when moving the function here. 😉

if !nix::unistd::geteuid().is_root() {
return true;
}

let uid_map_path = "/proc/self/uid_map";
let content = fs::read_to_string(uid_map_path)
.unwrap_or_else(|_| panic!("failed to read {}", uid_map_path));
if !content.contains("4294967295") {
Copy link

Choose a reason for hiding this comment

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

Maybe we should add a comment explaining the meaning of this ... like in https://github.com/opencontainers/runc/blob/v1.1.3/libcontainer/userns/userns_linux.go#L30

return true;
}

matches!(std::env::var("YOUKI_USE_ROOTLESS").as_deref(), Ok("true"))
}

#[cfg(feature = "systemd")]
fn create_systemd_cgroup_manager(
cgroup_path: PathBuf,
Expand All @@ -228,7 +244,7 @@ fn create_systemd_cgroup_manager(
);
}

let use_system = nix::unistd::geteuid().is_root();
let use_system = !rootless_required();

log::info!(
"systemd cgroup manager with system bus {} will be used",
Expand Down
10 changes: 1 addition & 9 deletions crates/libcontainer/src/rootless.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{namespaces::Namespaces, utils};
use anyhow::{bail, Context, Result};
use libcgroups::common::rootless_required;
use nix::unistd::Pid;
use oci_spec::runtime::{Linux, LinuxIdMapping, LinuxNamespace, LinuxNamespaceType, Mount, Spec};
use std::fs;
Expand Down Expand Up @@ -117,15 +118,6 @@ pub fn get_gid_path(pid: &Pid) -> PathBuf {
utils::get_temp_dir_path(format!("{pid}_mapping_path").as_str()).join("gid_map")
}

/// Checks if rootless mode should be used
pub fn rootless_required() -> bool {
if !nix::unistd::geteuid().is_root() {
return true;
}

matches!(std::env::var("YOUKI_USE_ROOTLESS").as_deref(), Ok("true"))
}

pub fn unprivileged_user_ns_enabled() -> Result<bool> {
let user_ns_sysctl = Path::new("/proc/sys/kernel/unprivileged_userns_clone");
if !user_ns_sysctl.exists() {
Expand Down
2 changes: 1 addition & 1 deletion crates/youki/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::fs;
use std::path::{Path, PathBuf};

use crate::commands::info;
use libcontainer::rootless::rootless_required;
use libcgroups::common::rootless_required;
use libcontainer::utils::create_dir_all_with_mode;
use nix::sys::stat::Mode;
use nix::unistd::getuid;
Expand Down