refactor prelude

This commit is contained in:
mii
2024-10-31 16:20:40 +09:00
parent 7d19f624dd
commit 2ff477d8e1

View File

@ -48,7 +48,7 @@ impl IzoliBox {
let mut callback = callback; let mut callback = callback;
let new_callback = Box::new(|| { let new_callback = Box::new(|| {
IzoliBox::prelude(self.id).unwrap(); self.prelude().unwrap();
callback(); callback();
@ -58,11 +58,28 @@ impl IzoliBox {
unsafe { sched::clone(new_callback, &mut stack, flags, Some(SIGCHLD)) } unsafe { sched::clone(new_callback, &mut stack, flags, Some(SIGCHLD)) }
} }
pub fn prelude(id: usize) -> Result<(), Box<dyn std::error::Error>> { fn prelude(&self) -> Result<(), Box<dyn std::error::Error>> {
info!("box prelude"); info!("box prelude");
let root = format!("/var/local/lib/izoli/{}", id); let root = self.get_root();
fs::create_dir_all(Path::new(&root))?; fs::create_dir_all(Path::new(&root))?;
self.prelude_mount()?;
info!("chroot to {}", root);
chroot(&root)?;
set_current_dir("/")?;
sethostname(format!("IzoliBox"))?;
Ok(())
}
fn get_root(&self) -> String {
format!("/var/local/lib/izoli/{}", self.id)
}
fn prelude_mount(&self) -> Result<(), Box<dyn std::error::Error>> {
let root = self.get_root();
Self::umount_mount( Self::umount_mount(
Some("none"), Some("none"),
"/", "/",
@ -118,12 +135,6 @@ impl IzoliBox {
None::<&str>, None::<&str>,
)?; )?;
} }
info!("chroot to {}", root);
chroot(&root)?;
set_current_dir("/")?;
sethostname(format!("IzoliBox"))?;
Ok(()) Ok(())
} }