mirror of
https://github.com/mii443/izoli.git
synced 2025-08-22 16:05:40 +00:00
add tracing
This commit is contained in:
@ -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"]
|
||||
|
@ -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,
|
||||
}),
|
||||
}),
|
||||
|
@ -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<Self, std::io::Error> {
|
||||
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<String, std::io::Error> {
|
||||
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())?;
|
||||
|
@ -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<Pid, nix::errno::Errno> {
|
||||
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<dyn std::error::Error>> {
|
||||
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("/")?;
|
||||
|
||||
|
Reference in New Issue
Block a user