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]
|
[dependencies]
|
||||||
nix = { version = "0.29.0", features = ["sched", "hostname", "mount"] }
|
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 std::ffi::CString;
|
||||||
|
|
||||||
use izolilib::{
|
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,
|
izolibox::IzoliBox,
|
||||||
};
|
};
|
||||||
use nix::{sys::wait::waitpid, unistd::execvp};
|
use nix::{sys::wait::waitpid, unistd::execvp};
|
||||||
|
use tracing::Level;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
tracing_subscriber::fmt()
|
||||||
|
.with_max_level(Level::TRACE)
|
||||||
|
.init();
|
||||||
|
|
||||||
let cgroup = CGroup::new("izoli").unwrap();
|
let cgroup = CGroup::new("izoli").unwrap();
|
||||||
|
|
||||||
cgroup
|
cgroup
|
||||||
@ -17,7 +25,7 @@ fn main() {
|
|||||||
1,
|
1,
|
||||||
Some(CGroupOption {
|
Some(CGroupOption {
|
||||||
cpu_max: Some(CpuLimit {
|
cpu_max: Some(CpuLimit {
|
||||||
max: izolilib::cgroup::limit_value::CGroupLimitValue::Max,
|
max: CGroupLimitValue::Value(10000),
|
||||||
period: 100000,
|
period: 100000,
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
|
@ -6,6 +6,8 @@ use std::{
|
|||||||
str::FromStr,
|
str::FromStr,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use tracing::info;
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
cgroup_option::CGroupOption, cgroup_stat::CGroupStat, controller::Controller,
|
cgroup_option::CGroupOption, cgroup_stat::CGroupStat, controller::Controller,
|
||||||
cpu_limit::CpuLimit, limit_value::CGroupLimitValue,
|
cpu_limit::CpuLimit, limit_value::CGroupLimitValue,
|
||||||
@ -17,11 +19,13 @@ pub struct CGroup {
|
|||||||
|
|
||||||
impl CGroup {
|
impl CGroup {
|
||||||
pub fn new(path: &str) -> Result<Self, std::io::Error> {
|
pub fn new(path: &str) -> Result<Self, std::io::Error> {
|
||||||
|
info!("creating new cgroup");
|
||||||
let cgroup = CGroup {
|
let cgroup = CGroup {
|
||||||
path: PathBuf::from(path),
|
path: PathBuf::from(path),
|
||||||
};
|
};
|
||||||
|
|
||||||
if !cgroup.check_status() {
|
if !cgroup.check_status() {
|
||||||
|
info!("cgroup not exists. creating");
|
||||||
cgroup.create()?;
|
cgroup.create()?;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,6 +36,7 @@ impl CGroup {
|
|||||||
let mut file = std::fs::File::open("/proc/self/cgroup")?;
|
let mut file = std::fs::File::open("/proc/self/cgroup")?;
|
||||||
let mut buf = String::default();
|
let mut buf = String::default();
|
||||||
file.read_to_string(&mut buf)?;
|
file.read_to_string(&mut buf)?;
|
||||||
|
info!("self cgroup: {}", buf);
|
||||||
|
|
||||||
Ok(buf.trim().to_string())
|
Ok(buf.trim().to_string())
|
||||||
}
|
}
|
||||||
@ -42,7 +47,9 @@ impl CGroup {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn apply_options(&self, option: &CGroupOption) -> Result<(), std::io::Error> {
|
pub fn apply_options(&self, option: &CGroupOption) -> Result<(), std::io::Error> {
|
||||||
|
info!("applying cgroup options");
|
||||||
if let Some(cpu_max) = &option.cpu_max {
|
if let Some(cpu_max) = &option.cpu_max {
|
||||||
|
info!("setting cpu.max");
|
||||||
self.set_cpu_max(cpu_max)?;
|
self.set_cpu_max(cpu_max)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,11 +58,13 @@ impl CGroup {
|
|||||||
|
|
||||||
pub fn enter(&self) -> Result<(), std::io::Error> {
|
pub fn enter(&self) -> Result<(), std::io::Error> {
|
||||||
let pid = std::process::id();
|
let pid = std::process::id();
|
||||||
|
info!("cgroup enter: {}", pid);
|
||||||
|
|
||||||
self.add_procs(vec![pid])
|
self.add_procs(vec![pid])
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read(&self, name: &str) -> Result<String, std::io::Error> {
|
pub fn read(&self, name: &str) -> Result<String, std::io::Error> {
|
||||||
|
info!("reading {}", name);
|
||||||
let path = self.get_file_path(name);
|
let path = self.get_file_path(name);
|
||||||
let mut file = File::open(path)?;
|
let mut file = File::open(path)?;
|
||||||
let mut buf = String::default();
|
let mut buf = String::default();
|
||||||
@ -65,6 +74,7 @@ impl CGroup {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn write(&self, name: &str, data: &str) -> Result<(), std::io::Error> {
|
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 path = self.get_file_path(name);
|
||||||
let mut file = File::options().append(true).open(path)?;
|
let mut file = File::options().append(true).open(path)?;
|
||||||
file.write_all(data.as_bytes())?;
|
file.write_all(data.as_bytes())?;
|
||||||
|
@ -6,6 +6,7 @@ use nix::{
|
|||||||
sched::{self, CloneCb, CloneFlags},
|
sched::{self, CloneCb, CloneFlags},
|
||||||
unistd::{sethostname, Pid},
|
unistd::{sethostname, Pid},
|
||||||
};
|
};
|
||||||
|
use tracing::info;
|
||||||
|
|
||||||
use crate::cgroup::{cgroup::CGroup, cgroup_option::CGroupOption};
|
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> {
|
pub fn enter(&self, callback: CloneCb<'_>) -> Result<Pid, nix::errno::Errno> {
|
||||||
|
info!("box enter");
|
||||||
let mut stack = [0u8; STACK_SIZE];
|
let mut stack = [0u8; STACK_SIZE];
|
||||||
let flags = CloneFlags::CLONE_NEWNS
|
let flags = CloneFlags::CLONE_NEWNS
|
||||||
| CloneFlags::CLONE_NEWUTS
|
| CloneFlags::CLONE_NEWUTS
|
||||||
| CloneFlags::CLONE_NEWIPC
|
| CloneFlags::CLONE_NEWIPC
|
||||||
| CloneFlags::CLONE_NEWPID;
|
| CloneFlags::CLONE_NEWPID
|
||||||
|
| CloneFlags::CLONE_NEWNET;
|
||||||
|
|
||||||
if let Some(cgroup_option) = &self.cgroup_option {
|
if let Some(cgroup_option) = &self.cgroup_option {
|
||||||
let cgroup = CGroup::new(&format!("izoli/box_{}", self.id)).unwrap();
|
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>> {
|
pub fn prelude(id: usize) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
info!("box prelude");
|
||||||
let root = format!("/var/local/lib/izoli/{}", id);
|
let root = format!("/var/local/lib/izoli/{}", id);
|
||||||
fs::create_dir_all(Path::new(&root))?;
|
fs::create_dir_all(Path::new(&root))?;
|
||||||
|
|
||||||
@ -52,6 +56,7 @@ impl IzoliBox {
|
|||||||
for dir in &[
|
for dir in &[
|
||||||
"/proc", "/dev", "/tmp", "/lib", "/usr", "/bin", "/lib64", "/usr/lib", "/usr/bin",
|
"/proc", "/dev", "/tmp", "/lib", "/usr", "/bin", "/lib64", "/usr/lib", "/usr/bin",
|
||||||
] {
|
] {
|
||||||
|
info!("creating {}", dir);
|
||||||
fs::create_dir_all(format!("{}{}", root, dir))?;
|
fs::create_dir_all(format!("{}{}", root, dir))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,6 +72,7 @@ impl IzoliBox {
|
|||||||
];
|
];
|
||||||
|
|
||||||
for (target, source, flags) in mounts.iter() {
|
for (target, source, flags) in mounts.iter() {
|
||||||
|
info!("mounting {} {} {:?}", target, source, flags);
|
||||||
let full_target = format!("{}/{}", root, target);
|
let full_target = format!("{}/{}", root, target);
|
||||||
Self::umount_mount(
|
Self::umount_mount(
|
||||||
Some(source),
|
Some(source),
|
||||||
@ -77,6 +83,7 @@ impl IzoliBox {
|
|||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
info!("chroot to {}", root);
|
||||||
chroot(&root)?;
|
chroot(&root)?;
|
||||||
set_current_dir("/")?;
|
set_current_dir("/")?;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user