summaryrefslogtreecommitdiffstats
path: root/src/runner/runc/init.rs
blob: f686e3e2f5490b5b27ca26af7b2e1e83a213f785 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use std::{
	fs::{DirBuilder, File},
	io,
};

use nix::{
	mount::{self, MsFlags},
	sched::{self, CloneFlags},
};
use serde::{Deserialize, Serialize};

use crate::util::{self, ToIOResult};

fn prepare_buildtmp() -> io::Result<()> {
	mount::mount::<_, _, _, str>(
		Some("buildtmp"),
		"build/tmp",
		Some("tmpfs"),
		MsFlags::empty(),
		None,
	)
	.to_io_result()?;

	util::tar::unpack(File::open("build/rootfs.tar")?, "build/tmp/rootfs")?;
	DirBuilder::new().create("build/tmp/runc")?;

	Ok(())
}

#[derive(Debug, Deserialize, Serialize)]
pub enum Error {
	Code(i32),
	String(String),
}

impl From<io::Error> for Error {
	fn from(error: io::Error) -> Self {
		match error.raw_os_error() {
			Some(code) => Error::Code(code),
			None => Error::String(error.to_string()),
		}
	}
}

impl From<Error> for io::Error {
	fn from(error: Error) -> Self {
		match error {
			Error::Code(code) => io::Error::from_raw_os_error(code),
			Error::String(string) => io::Error::new(io::ErrorKind::Other, string),
		}
	}
}

pub fn runc_preinit() -> Result<(), Error> {
	DirBuilder::new().recursive(true).create("build/state")?;
	sched::unshare(CloneFlags::CLONE_NEWUSER | CloneFlags::CLONE_NEWNS).to_io_result()?;
	Ok(())
}

pub fn runc_init() -> Result<(), Error> {
	prepare_buildtmp()?;
	Ok(())
}