diff --git a/examples/root.rs b/examples/root.rs index 989a558..5fbf762 100644 --- a/examples/root.rs +++ b/examples/root.rs @@ -7,8 +7,8 @@ fn main() { container.host_bind_mount(); container.mount().unwrap(); - - // or just do + + // or just do // Container::new("chroot".into()) // .host_bind_mount() // .run(|| { diff --git a/src/lib.rs b/src/lib.rs index 8ffa59c..b31af56 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,4 @@ -use std::{ - collections::BTreeMap, - error::Error, - fs::File, - os::fd::AsRawFd, - path::PathBuf, -}; +use std::{collections::BTreeMap, error::Error, fs::File, os::fd::AsRawFd, path::PathBuf}; use sys_mount::{FilesystemType, Mount, MountFlags, Unmount, UnmountDrop, UnmountFlags}; /// Mount object struct #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)] @@ -177,8 +171,6 @@ pub struct Container { } impl Container { - - /// Enter chroot jail /// /// This makes use of the `chroot` syscall to enter the chroot jail. @@ -188,7 +180,7 @@ impl Container { if !self._initialized { // mount the tmpfs first, idiot proofing in case the // programmer forgets to mount it before chrooting - // + // // This should be fine as it's going to be dismounted after dropping regardless self.mount()?; } @@ -205,7 +197,7 @@ impl Container { /// to a raw file descriptor of the sysroot we saved earlier /// in `[Container::new]`, and then chrooting to the directory /// we just moved to. - /// + /// /// We then also take the pwd stored earlier and move back to it, /// for good measure. #[inline(always)] @@ -213,12 +205,12 @@ impl Container { nix::unistd::fchdir(self.sysroot.as_raw_fd())?; nix::unistd::chroot(".")?; self.chroot = false; - + // Let's return back to pwd nix::unistd::fchdir(self.pwd.as_raw_fd())?; Ok(()) } - + /// Create a new tiffin container /// /// To use it, you need to create a new container with `root` @@ -279,7 +271,7 @@ impl Container { self._initialized = false; Ok(()) } - + /// Adds a bind mount for the system's root filesystem to /// the container's root filesystem at `/run/host` pub fn host_bind_mount(&mut self) -> &mut Self { @@ -374,16 +366,17 @@ mod tests { fn test_container() { let mut container = Container::new(PathBuf::from("/tmp/tiffin")); container.host_bind_mount(); - container.run(|| { - let mut file = File::create("/run/host/test.txt").unwrap(); - file.write_all(b"Hello, world!").unwrap(); - Ok(()) - }) - .unwrap(); - + container + .run(|| { + let mut file = File::create("/run/host/test.txt").unwrap(); + file.write_all(b"Hello, world!").unwrap(); + Ok(()) + }) + .unwrap(); + let mut file = File::open("/tmp/tiffin/run/host/test.txt").unwrap(); let mut contents = String::new(); file.read_to_string(&mut contents).unwrap(); assert_eq!(contents, "Hello, world!"); } -} \ No newline at end of file +}