From 3ef40c0c4a7750bec7bd8dc405be92af20834ab4 Mon Sep 17 00:00:00 2001 From: Masato Imai Date: Mon, 21 Oct 2024 07:35:55 +0000 Subject: [PATCH] add tracing --- Cargo.toml | 6 ++++++ src/bin/izoli.rs | 12 ++++++++++-- src/cgroup/cgroup.rs | 10 ++++++++++ src/izolibox.rs | 9 ++++++++- 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5d663aa..2aaf389 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,9 @@ path = "src/bin/izoli.rs" [dependencies] nix = { version = "0.29.0", features = ["sched", "hostname", "mount"] } +tracing = "0.1" +tracing-appender = "0.2" + +[dependencies.tracing-subscriber] +version = "0.3.16" +features = ["env-filter", "fmt", "json", "local-time", "time"] diff --git a/src/bin/izoli.rs b/src/bin/izoli.rs index faf6875..6115f26 100644 --- a/src/bin/izoli.rs +++ b/src/bin/izoli.rs @@ -1,12 +1,20 @@ use std::ffi::CString; use izolilib::{ - cgroup::{cgroup::CGroup, cgroup_option::CGroupOption, cpu_limit::CpuLimit}, + cgroup::{ + cgroup::CGroup, cgroup_option::CGroupOption, cpu_limit::CpuLimit, + limit_value::CGroupLimitValue, + }, izolibox::IzoliBox, }; use nix::{sys::wait::waitpid, unistd::execvp}; +use tracing::Level; fn main() { + tracing_subscriber::fmt() + .with_max_level(Level::TRACE) + .init(); + let cgroup = CGroup::new("izoli").unwrap(); cgroup @@ -17,7 +25,7 @@ fn main() { 1, Some(CGroupOption { cpu_max: Some(CpuLimit { - max: izolilib::cgroup::limit_value::CGroupLimitValue::Max, + max: CGroupLimitValue::Value(10000), period: 100000, }), }), diff --git a/src/cgroup/cgroup.rs b/src/cgroup/cgroup.rs index cdb4ac4..50202b6 100644 --- a/src/cgroup/cgroup.rs +++ b/src/cgroup/cgroup.rs @@ -6,6 +6,8 @@ use std::{ str::FromStr, }; +use tracing::info; + use super::{ cgroup_option::CGroupOption, cgroup_stat::CGroupStat, controller::Controller, cpu_limit::CpuLimit, limit_value::CGroupLimitValue, @@ -17,11 +19,13 @@ pub struct CGroup { impl CGroup { pub fn new(path: &str) -> Result { + info!("creating new cgroup"); let cgroup = CGroup { path: PathBuf::from(path), }; if !cgroup.check_status() { + info!("cgroup not exists. creating"); cgroup.create()?; } @@ -32,6 +36,7 @@ impl CGroup { let mut file = std::fs::File::open("/proc/self/cgroup")?; let mut buf = String::default(); file.read_to_string(&mut buf)?; + info!("self cgroup: {}", buf); Ok(buf.trim().to_string()) } @@ -42,7 +47,9 @@ impl CGroup { } pub fn apply_options(&self, option: &CGroupOption) -> Result<(), std::io::Error> { + info!("applying cgroup options"); if let Some(cpu_max) = &option.cpu_max { + info!("setting cpu.max"); self.set_cpu_max(cpu_max)?; } @@ -51,11 +58,13 @@ impl CGroup { pub fn enter(&self) -> Result<(), std::io::Error> { let pid = std::process::id(); + info!("cgroup enter: {}", pid); self.add_procs(vec![pid]) } pub fn read(&self, name: &str) -> Result { + info!("reading {}", name); let path = self.get_file_path(name); let mut file = File::open(path)?; let mut buf = String::default(); @@ -65,6 +74,7 @@ impl CGroup { } pub fn write(&self, name: &str, data: &str) -> Result<(), std::io::Error> { + info!("writing {} to {}", data, name); let path = self.get_file_path(name); let mut file = File::options().append(true).open(path)?; file.write_all(data.as_bytes())?; diff --git a/src/izolibox.rs b/src/izolibox.rs index cffc54f..f48f3b2 100644 --- a/src/izolibox.rs +++ b/src/izolibox.rs @@ -6,6 +6,7 @@ use nix::{ sched::{self, CloneCb, CloneFlags}, unistd::{sethostname, Pid}, }; +use tracing::info; use crate::cgroup::{cgroup::CGroup, cgroup_option::CGroupOption}; @@ -22,11 +23,13 @@ impl IzoliBox { } pub fn enter(&self, callback: CloneCb<'_>) -> Result { + info!("box enter"); let mut stack = [0u8; STACK_SIZE]; let flags = CloneFlags::CLONE_NEWNS | CloneFlags::CLONE_NEWUTS | CloneFlags::CLONE_NEWIPC - | CloneFlags::CLONE_NEWPID; + | CloneFlags::CLONE_NEWPID + | CloneFlags::CLONE_NEWNET; if let Some(cgroup_option) = &self.cgroup_option { let cgroup = CGroup::new(&format!("izoli/box_{}", self.id)).unwrap(); @@ -38,6 +41,7 @@ impl IzoliBox { } pub fn prelude(id: usize) -> Result<(), Box> { + info!("box prelude"); let root = format!("/var/local/lib/izoli/{}", id); fs::create_dir_all(Path::new(&root))?; @@ -52,6 +56,7 @@ impl IzoliBox { for dir in &[ "/proc", "/dev", "/tmp", "/lib", "/usr", "/bin", "/lib64", "/usr/lib", "/usr/bin", ] { + info!("creating {}", dir); fs::create_dir_all(format!("{}{}", root, dir))?; } @@ -67,6 +72,7 @@ impl IzoliBox { ]; for (target, source, flags) in mounts.iter() { + info!("mounting {} {} {:?}", target, source, flags); let full_target = format!("{}/{}", root, target); Self::umount_mount( Some(source), @@ -77,6 +83,7 @@ impl IzoliBox { )?; } + info!("chroot to {}", root); chroot(&root)?; set_current_dir("/")?;