Container

Struct Container 

Source
pub struct Container { /* private fields */ }
Expand description

Safe and isolated environment for executing command.

A default environment can be generated using Container::new, which will unshare necessary namespaces. Then use bindmount_ro or bindmount_rw to mount directories to the container root.

use hakoniwa::Container;

let mut container = Container::new();
container.bindmount_ro("/bin", "/bin")
    .bindmount_ro("/lib", "/lib")
    .bindmount_ro("/lib64", "/lib64")
    .bindmount_ro("/usr", "/usr");

And now, we can execute Command in the container.

let mut command = container.command("/bin/echo");
let output = command.arg("hello")
    .output()
    .expect("failed to execute process witnin container");

Implementations§

Source§

impl Container

Source

pub fn new() -> Self

Constructs a new Container with following steps:

  • Create a new MOUNT namespace
  • Create a new USER namespace and map current user to itself
  • Create a new PID namespace and mount a new procfs on /proc
Source

pub fn empty() -> Self

Constructs a new Container with a completely empty environment.

Source

pub fn unshare(&mut self, namespace: Namespace) -> &mut Self

Create a new namespace.

Source

pub fn rootdir<P: AsRef<Path>>(&mut self, host_path: P) -> &mut Self

Use host_path as the mount point for the container root fs.

By default the mount point is a tmpdir, and will be automatically cleaned up when the last process exits.

This method is mainly useful if you set it to a directory that contains a file system hierarchy, and want chroot into it.

§Caveats

Some empty directories/files that were used as mount point targets may be left behind even when the last process exits.

Source

pub fn rootfs<P: AsRef<Path>>(&mut self, host_path: P) -> Result<&mut Self>

Bind mount all subdirectories in host_path to the container with read-only access in new MOUNT namespace.

§Caveats

When use / as rootfs, it only mount following subdirectories: /bin, /etc, /lib, /lib64, /lib32, /sbin, /usr.

Source

pub fn bindmount_ro( &mut self, host_path: &str, container_path: &str, ) -> &mut Self

Bind mount the host_path on container_path with read-only access in new MOUNT namespace.

Source

pub fn bindmount_rw( &mut self, host_path: &str, container_path: &str, ) -> &mut Self

Bind mount the host_path on container_path with read-write access in new MOUNT namespace.

Source

pub fn devfsmount(&mut self, container_path: &str) -> &mut Self

Mount new devfs on container_path in new MOUNT namespace.

§Caveats

This is not a real linux filesystem type. It just bind mount a minimal set of device files in container_path, such as /dev/null.

Source

pub fn tmpfsmount(&mut self, container_path: &str) -> &mut Self

Mount new tmpfs on container_path in new MOUNT namespace.

Source

pub fn procfsmount(&mut self, container_path: &str) -> &mut Self

Mount new procfs on container_path in new MOUNT namespace.

Source

pub fn file(&mut self, target: &str, contents: &str) -> &mut Self

Creates a new file with contents on the filesystem in new MOUNT namespace.

Source

pub fn dir(&mut self, target: &str, mode: u32) -> &mut Self

Creates a new dir with mode in new MOUNT namespace.

Creates a new symbolic link on the filesystem in new MOUNT namespace.

Source

pub fn uidmap(&mut self, uid: u32) -> &mut Self

Map current user to uid in new USER namespace.

This is a shorthand for uidmaps(&[(uid, Uid::current().as_raw(), 1)])

Source

pub fn gidmap(&mut self, gid: u32) -> &mut Self

Map current group to gid in new USER namespace.

This is a shorthand for gidmaps(&[(gid, Gid::current().as_raw(), 1)])

Source

pub fn uidmaps(&mut self, idmaps: &[(u32, u32, u32)]) -> &Self

Create new UID maps in new USER namespace.

Source

pub fn gidmaps(&mut self, idmaps: &[(u32, u32, u32)]) -> &Self

Create new GID maps in new USER namespace.

Source

pub fn user( &mut self, user: &str, group: Option<&str>, supplementary_groups: &[&str], ) -> &mut Self

Changes the user in the new USER namespace.

§Caveats

It uses the /etc/passwd and /etc/group files in the container to check and determine the user and group.

Source

pub fn hostname(&mut self, hostname: &str) -> &mut Self

Changes the hostname in the new UTS namespace.

Source

pub fn network<T: Into<Network>>(&mut self, network: T) -> &mut Self

Change the network mode in new NETWORK namespace.

Source

pub fn setrlimit( &mut self, resource: Rlimit, soft_limit: u64, hard_limit: u64, ) -> &mut Self

Set resource limit.

Source

pub fn landlock_ruleset(&mut self, ruleset: Ruleset) -> &mut Self

Set landlock ruleset.

Source

pub fn seccomp_filter(&mut self, filter: Filter) -> &mut Self

Set seccomp filter.

Source

pub fn runctl(&mut self, ctl: Runctl) -> &mut Self

Manipulates various aspects of the behavior of the container.

Source

pub fn command(&self, program: &str) -> Command

Constructs a new Command for launching the program at path program within container.

Trait Implementations§

Source§

impl Clone for Container

Source§

fn clone(&self) -> Container

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Container

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.