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 namespace information to info command #258

Merged
merged 1 commit into from
Sep 3, 2021
Merged
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
46 changes: 44 additions & 2 deletions src/commands/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ impl Info {
print_os();
print_hardware();
print_cgroups();
print_namespaces();

Ok(())
}
Expand Down Expand Up @@ -107,10 +108,10 @@ pub fn print_hardware() {
pub fn print_cgroups() {
if let Ok(cgroup_fs) = cgroups::common::get_supported_cgroup_fs() {
let cgroup_fs: Vec<String> = cgroup_fs.into_iter().map(|c| c.to_string()).collect();
println!("{:<18}{}", "cgroup version", cgroup_fs.join(" and "));
println!("{:<18}{}", "Cgroup version", cgroup_fs.join(" and "));
}

println!("cgroup mounts");
println!("Cgroup mounts");
if let Ok(v1_mounts) = cgroups::v1::util::list_subsystem_mount_points() {
let mut v1_mounts: Vec<String> = v1_mounts
.iter()
Expand All @@ -128,3 +129,44 @@ pub fn print_cgroups() {
println!(" {:<16}{}", "unified", mount_point.display());
}
}

pub fn print_namespaces() {
let uname = nix::sys::utsname::uname();
let kernel_config = Path::new("/boot").join(format!("config-{}", uname.release()));
if !kernel_config.exists() {
return;
}

if let Ok(content) = fs::read_to_string(kernel_config) {
if let Some(ns_enabled) = find_parameter(&content, "CONFIG_NAMESPACES") {
if ns_enabled == "y" {
println!("{:<18}enabled", "Namespaces");
} else {
println!("{:<18}disabled", "Namespaces");
return;
}
}

// mount namespace is always enabled if namespaces are enabled
println!(" {:<16}enabled", "mount");
print_feature_status(&content, "CONFIG_UTS_NS", "uts");
print_feature_status(&content, "CONFIG_IPC_NS", "ipc");
print_feature_status(&content, "CONFIG_USER_NS", "user");
print_feature_status(&content, "CONFIG_PID_NS", "pid");
print_feature_status(&content, "CONFIG_NET_NS", "network");
}
}

fn print_feature_status(config: &str, feature: &str, display: &str) {
if let Some(status_flag) = find_parameter(config, feature) {
let status = if status_flag == "y" {
"enabled"
} else {
"disabled"
};

println!(" {:<16}{}", display, status);
} else {
println!(" {:<16}disabled", display);
}
}