2024-01-30 02:15:03 -08:00
|
|
|
use anyhow::{anyhow, Result};
|
2024-02-05 12:45:45 +00:00
|
|
|
use futures::stream::TryStreamExt;
|
2024-02-06 09:07:18 +00:00
|
|
|
use hypha::{LaunchInfo, LaunchNetwork};
|
2024-02-05 12:45:45 +00:00
|
|
|
use ipnetwork::IpNetwork;
|
2024-02-01 11:50:33 +00:00
|
|
|
use log::{trace, warn};
|
2024-01-31 01:40:42 -08:00
|
|
|
use nix::libc::{c_int, dup2, wait};
|
|
|
|
use nix::unistd::{execve, fork, ForkResult, Pid};
|
2024-01-22 05:08:14 -08:00
|
|
|
use oci_spec::image::{Config, ImageConfiguration};
|
2024-01-31 01:40:42 -08:00
|
|
|
use std::ffi::{CStr, CString};
|
2024-01-22 02:15:53 -08:00
|
|
|
use std::fs;
|
2024-01-22 16:05:47 -08:00
|
|
|
use std::fs::{File, OpenOptions, Permissions};
|
2024-01-22 05:08:14 -08:00
|
|
|
use std::os::fd::AsRawFd;
|
|
|
|
use std::os::linux::fs::MetadataExt;
|
2024-01-22 16:05:47 -08:00
|
|
|
use std::os::unix::fs::{chroot, PermissionsExt};
|
2024-01-22 02:15:53 -08:00
|
|
|
use std::path::Path;
|
2024-01-31 01:40:42 -08:00
|
|
|
use std::ptr::addr_of_mut;
|
|
|
|
use std::thread::sleep;
|
|
|
|
use std::time::Duration;
|
2024-01-22 02:15:53 -08:00
|
|
|
use sys_mount::{FilesystemType, Mount, MountFlags};
|
2024-01-22 05:08:14 -08:00
|
|
|
use walkdir::WalkDir;
|
2024-01-22 02:15:53 -08:00
|
|
|
|
|
|
|
const IMAGE_BLOCK_DEVICE_PATH: &str = "/dev/xvda";
|
|
|
|
const CONFIG_BLOCK_DEVICE_PATH: &str = "/dev/xvdb";
|
|
|
|
|
|
|
|
const IMAGE_MOUNT_PATH: &str = "/image";
|
|
|
|
const CONFIG_MOUNT_PATH: &str = "/config";
|
2024-01-22 03:00:58 -08:00
|
|
|
const OVERLAY_MOUNT_PATH: &str = "/overlay";
|
|
|
|
|
|
|
|
const OVERLAY_IMAGE_BIND_PATH: &str = "/overlay/image";
|
|
|
|
const OVERLAY_WORK_PATH: &str = "/overlay/work";
|
|
|
|
const OVERLAY_UPPER_PATH: &str = "/overlay/upper";
|
|
|
|
|
2024-01-22 16:05:47 -08:00
|
|
|
const SYS_PATH: &str = "/sys";
|
|
|
|
const PROC_PATH: &str = "/proc";
|
|
|
|
const DEV_PATH: &str = "/dev";
|
|
|
|
|
2024-01-22 05:08:14 -08:00
|
|
|
const NEW_ROOT_PATH: &str = "/newroot";
|
2024-01-22 16:05:47 -08:00
|
|
|
const NEW_ROOT_SYS_PATH: &str = "/newroot/sys";
|
|
|
|
const NEW_ROOT_PROC_PATH: &str = "/newroot/proc";
|
|
|
|
const NEW_ROOT_DEV_PATH: &str = "/newroot/dev";
|
|
|
|
|
2024-01-22 02:26:43 -08:00
|
|
|
const IMAGE_CONFIG_JSON_PATH: &str = "/config/image/config.json";
|
2024-01-22 05:30:02 -08:00
|
|
|
const LAUNCH_CONFIG_JSON_PATH: &str = "/config/launch.json";
|
2024-01-22 02:26:43 -08:00
|
|
|
|
2024-01-22 02:15:53 -08:00
|
|
|
pub struct ContainerInit {}
|
|
|
|
|
|
|
|
impl Default for ContainerInit {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ContainerInit {
|
|
|
|
pub fn new() -> ContainerInit {
|
|
|
|
ContainerInit {}
|
|
|
|
}
|
|
|
|
|
2024-02-05 12:45:45 +00:00
|
|
|
pub async fn init(&mut self) -> Result<()> {
|
2024-01-22 16:05:47 -08:00
|
|
|
self.early_init()?;
|
|
|
|
|
2024-01-30 20:22:48 -08:00
|
|
|
trace!("opening console descriptor");
|
2024-02-01 11:50:33 +00:00
|
|
|
match OpenOptions::new()
|
2024-01-22 05:08:14 -08:00
|
|
|
.read(true)
|
|
|
|
.write(true)
|
2024-02-01 11:50:33 +00:00
|
|
|
.open("/dev/console")
|
|
|
|
{
|
|
|
|
Ok(console) => self.map_console(console)?,
|
|
|
|
Err(error) => warn!("failed to open console: {}", error),
|
|
|
|
}
|
2024-01-22 03:00:58 -08:00
|
|
|
|
2024-01-22 05:08:14 -08:00
|
|
|
self.mount_squashfs_images()?;
|
|
|
|
let config = self.parse_image_config()?;
|
2024-01-22 05:30:02 -08:00
|
|
|
let launch = self.parse_launch_config()?;
|
2024-01-22 05:08:14 -08:00
|
|
|
self.mount_new_root()?;
|
|
|
|
self.nuke_initrd()?;
|
|
|
|
self.bind_new_root()?;
|
2024-02-05 12:45:45 +00:00
|
|
|
|
|
|
|
if let Some(network) = &launch.network {
|
|
|
|
if let Err(error) = self.network_setup(network).await {
|
|
|
|
warn!("failed to initialize network: {}", error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-22 02:26:43 -08:00
|
|
|
if let Some(cfg) = config.config() {
|
2024-02-01 11:50:33 +00:00
|
|
|
self.run(cfg, &launch)?;
|
2024-01-22 05:08:14 -08:00
|
|
|
} else {
|
2024-01-30 02:15:03 -08:00
|
|
|
return Err(anyhow!(
|
|
|
|
"unable to determine what to execute, image config doesn't tell us"
|
|
|
|
));
|
2024-01-22 02:26:43 -08:00
|
|
|
}
|
2024-01-22 02:15:53 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-01-22 16:05:47 -08:00
|
|
|
fn early_init(&mut self) -> Result<()> {
|
|
|
|
trace!("early init");
|
|
|
|
self.create_dir("/dev", Some(0o0755))?;
|
|
|
|
self.create_dir("/proc", None)?;
|
|
|
|
self.create_dir("/sys", None)?;
|
|
|
|
self.create_dir("/root", Some(0o0700))?;
|
|
|
|
self.create_dir("/tmp", None)?;
|
2024-02-01 11:50:33 +00:00
|
|
|
self.mount_kernel_fs("devtmpfs", "/dev", "mode=0755")?;
|
|
|
|
self.mount_kernel_fs("proc", "/proc", "")?;
|
|
|
|
self.mount_kernel_fs("sysfs", "/sys", "")?;
|
2024-01-22 16:05:47 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_dir(&mut self, path: &str, mode: Option<u32>) -> Result<()> {
|
|
|
|
let path = Path::new(path);
|
|
|
|
if !path.is_dir() {
|
|
|
|
trace!("creating directory {:?}", path);
|
|
|
|
fs::create_dir(path)?;
|
|
|
|
}
|
|
|
|
if let Some(mode) = mode {
|
|
|
|
let permissions = Permissions::from_mode(mode);
|
2024-01-30 20:22:48 -08:00
|
|
|
trace!("setting directory {:?} permissions to {:?}", path, mode);
|
2024-01-22 16:05:47 -08:00
|
|
|
fs::set_permissions(path, permissions)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-02-01 11:50:33 +00:00
|
|
|
fn mount_kernel_fs(&mut self, fstype: &str, path: &str, data: &str) -> Result<()> {
|
2024-01-22 16:05:47 -08:00
|
|
|
let metadata = fs::metadata(path)?;
|
|
|
|
if metadata.st_dev() == fs::metadata("/")?.st_dev() {
|
|
|
|
trace!("mounting kernel fs {} to {}", fstype, path);
|
|
|
|
Mount::builder()
|
|
|
|
.fstype(FilesystemType::Manual(fstype))
|
2024-02-01 12:20:13 +00:00
|
|
|
.flags(MountFlags::NOEXEC | MountFlags::NOSUID)
|
2024-02-01 11:50:33 +00:00
|
|
|
.data(data)
|
2024-01-22 16:05:47 -08:00
|
|
|
.mount(fstype, path)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-02-01 11:50:33 +00:00
|
|
|
fn map_console(&mut self, console: File) -> Result<()> {
|
|
|
|
trace!("mapping console");
|
|
|
|
unsafe {
|
|
|
|
dup2(console.as_raw_fd(), 0);
|
|
|
|
dup2(console.as_raw_fd(), 1);
|
|
|
|
dup2(console.as_raw_fd(), 2);
|
|
|
|
}
|
|
|
|
drop(console);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-01-22 05:08:14 -08:00
|
|
|
fn mount_squashfs_images(&mut self) -> Result<()> {
|
|
|
|
trace!("mounting squashfs images");
|
2024-01-22 02:15:53 -08:00
|
|
|
let image_mount_path = Path::new(IMAGE_MOUNT_PATH);
|
|
|
|
let config_mount_path = Path::new(CONFIG_MOUNT_PATH);
|
|
|
|
self.mount_squashfs(Path::new(IMAGE_BLOCK_DEVICE_PATH), image_mount_path)?;
|
|
|
|
self.mount_squashfs(Path::new(CONFIG_BLOCK_DEVICE_PATH), config_mount_path)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mount_squashfs(&mut self, from: &Path, to: &Path) -> Result<()> {
|
2024-01-22 05:08:14 -08:00
|
|
|
trace!("mounting squashfs image {:?} to {:?}", from, to);
|
2024-01-22 02:15:53 -08:00
|
|
|
if !to.is_dir() {
|
|
|
|
fs::create_dir(to)?;
|
|
|
|
}
|
|
|
|
Mount::builder()
|
|
|
|
.fstype(FilesystemType::Manual("squashfs"))
|
|
|
|
.flags(MountFlags::RDONLY)
|
|
|
|
.mount(from, to)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2024-01-22 03:00:58 -08:00
|
|
|
|
2024-01-22 16:05:47 -08:00
|
|
|
fn mount_move_subtree(&mut self, from: &Path, to: &Path) -> Result<()> {
|
|
|
|
trace!("moving subtree {:?} to {:?}", from, to);
|
|
|
|
if !to.is_dir() {
|
|
|
|
fs::create_dir(to)?;
|
|
|
|
}
|
|
|
|
Mount::builder()
|
|
|
|
.fstype(FilesystemType::Manual("none"))
|
|
|
|
.flags(MountFlags::MOVE)
|
|
|
|
.mount(from, to)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-01-22 05:08:14 -08:00
|
|
|
fn mount_new_root(&mut self) -> Result<()> {
|
|
|
|
trace!("mounting new root");
|
2024-01-22 03:00:58 -08:00
|
|
|
self.mount_overlay_tmpfs()?;
|
|
|
|
self.bind_image_to_overlay_tmpfs()?;
|
2024-01-22 05:08:14 -08:00
|
|
|
self.mount_overlay_to_new_root()?;
|
|
|
|
std::env::set_current_dir(NEW_ROOT_PATH)?;
|
|
|
|
trace!("mounted new root");
|
2024-01-22 03:00:58 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mount_overlay_tmpfs(&mut self) -> Result<()> {
|
|
|
|
fs::create_dir(OVERLAY_MOUNT_PATH)?;
|
|
|
|
Mount::builder()
|
|
|
|
.fstype(FilesystemType::Manual("tmpfs"))
|
|
|
|
.mount("tmpfs", OVERLAY_MOUNT_PATH)?;
|
|
|
|
fs::create_dir(OVERLAY_UPPER_PATH)?;
|
|
|
|
fs::create_dir(OVERLAY_WORK_PATH)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bind_image_to_overlay_tmpfs(&mut self) -> Result<()> {
|
|
|
|
fs::create_dir(OVERLAY_IMAGE_BIND_PATH)?;
|
|
|
|
Mount::builder()
|
|
|
|
.fstype(FilesystemType::Manual("none"))
|
|
|
|
.flags(MountFlags::BIND | MountFlags::RDONLY)
|
|
|
|
.mount(IMAGE_MOUNT_PATH, OVERLAY_IMAGE_BIND_PATH)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-01-22 05:08:14 -08:00
|
|
|
fn mount_overlay_to_new_root(&mut self) -> Result<()> {
|
|
|
|
fs::create_dir(NEW_ROOT_PATH)?;
|
2024-01-22 03:00:58 -08:00
|
|
|
Mount::builder()
|
|
|
|
.fstype(FilesystemType::Manual("overlay"))
|
|
|
|
.flags(MountFlags::NOATIME)
|
|
|
|
.data(&format!(
|
|
|
|
"lowerdir={},upperdir={},workdir={}",
|
|
|
|
OVERLAY_IMAGE_BIND_PATH, OVERLAY_UPPER_PATH, OVERLAY_WORK_PATH
|
|
|
|
))
|
2024-01-22 05:08:14 -08:00
|
|
|
.mount(format!("overlayfs:{}", OVERLAY_MOUNT_PATH), NEW_ROOT_PATH)?;
|
2024-01-22 03:00:58 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_image_config(&mut self) -> Result<ImageConfiguration> {
|
2024-01-22 05:08:14 -08:00
|
|
|
trace!("parsing image config");
|
2024-01-22 03:00:58 -08:00
|
|
|
let image_config_path = Path::new(IMAGE_CONFIG_JSON_PATH);
|
|
|
|
let config = ImageConfiguration::from_file(image_config_path)?;
|
|
|
|
Ok(config)
|
|
|
|
}
|
2024-01-22 05:08:14 -08:00
|
|
|
|
2024-01-22 05:30:02 -08:00
|
|
|
fn parse_launch_config(&mut self) -> Result<LaunchInfo> {
|
|
|
|
trace!("parsing launch config");
|
|
|
|
let launch_config = Path::new(LAUNCH_CONFIG_JSON_PATH);
|
|
|
|
Ok(serde_json::from_str(&fs::read_to_string(launch_config)?)?)
|
|
|
|
}
|
|
|
|
|
2024-01-22 05:08:14 -08:00
|
|
|
fn nuke_initrd(&mut self) -> Result<()> {
|
|
|
|
trace!("nuking initrd");
|
|
|
|
let initrd_dev = fs::metadata("/")?.st_dev();
|
|
|
|
for item in WalkDir::new("/")
|
|
|
|
.same_file_system(true)
|
|
|
|
.follow_links(false)
|
|
|
|
.contents_first(true)
|
|
|
|
{
|
|
|
|
if item.is_err() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let item = item?;
|
|
|
|
let metadata = match item.metadata() {
|
|
|
|
Ok(value) => value,
|
|
|
|
Err(_) => continue,
|
|
|
|
};
|
|
|
|
|
|
|
|
if metadata.st_dev() != initrd_dev {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if metadata.is_symlink() || metadata.is_file() {
|
|
|
|
let _ = fs::remove_file(item.path());
|
|
|
|
trace!("deleting file {:?}", item.path());
|
|
|
|
} else if metadata.is_dir() {
|
|
|
|
let _ = fs::remove_dir(item.path());
|
|
|
|
trace!("deleting directory {:?}", item.path());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
trace!("nuked initrd");
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bind_new_root(&mut self) -> Result<()> {
|
2024-01-22 16:05:47 -08:00
|
|
|
self.mount_move_subtree(Path::new(SYS_PATH), Path::new(NEW_ROOT_SYS_PATH))?;
|
|
|
|
self.mount_move_subtree(Path::new(PROC_PATH), Path::new(NEW_ROOT_PROC_PATH))?;
|
|
|
|
self.mount_move_subtree(Path::new(DEV_PATH), Path::new(NEW_ROOT_DEV_PATH))?;
|
2024-01-22 05:08:14 -08:00
|
|
|
trace!("binding new root");
|
|
|
|
Mount::builder()
|
|
|
|
.fstype(FilesystemType::Manual("none"))
|
|
|
|
.flags(MountFlags::BIND)
|
|
|
|
.mount(".", "/")?;
|
|
|
|
trace!("chrooting into new root");
|
|
|
|
chroot(".")?;
|
|
|
|
trace!("setting root as current directory");
|
|
|
|
std::env::set_current_dir("/")?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-02-05 12:45:45 +00:00
|
|
|
async fn network_setup(&mut self, network: &LaunchNetwork) -> Result<()> {
|
|
|
|
trace!(
|
|
|
|
"setting up network with link {} and ipv4 {}",
|
|
|
|
network.link,
|
|
|
|
network.ipv4
|
|
|
|
);
|
|
|
|
|
|
|
|
let (connection, handle, _) = rtnetlink::new_connection()?;
|
|
|
|
tokio::spawn(connection);
|
|
|
|
|
|
|
|
let ip: IpNetwork = network.ipv4.parse()?;
|
|
|
|
|
|
|
|
let mut links = handle
|
|
|
|
.link()
|
|
|
|
.get()
|
|
|
|
.match_name(network.link.clone())
|
|
|
|
.execute();
|
|
|
|
if let Some(link) = links.try_next().await? {
|
|
|
|
handle
|
|
|
|
.address()
|
|
|
|
.add(link.header.index, ip.ip(), ip.prefix())
|
|
|
|
.execute()
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
handle.link().set(link.header.index).up().execute().await?;
|
|
|
|
} else {
|
|
|
|
warn!("unable to find link named {}", network.link);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-02-01 11:50:33 +00:00
|
|
|
fn run(&mut self, config: &Config, launch: &LaunchInfo) -> Result<()> {
|
2024-01-22 05:08:14 -08:00
|
|
|
let mut cmd = match config.cmd() {
|
|
|
|
None => vec![],
|
|
|
|
Some(value) => value.clone(),
|
|
|
|
};
|
2024-01-22 05:30:02 -08:00
|
|
|
|
|
|
|
if launch.run.is_some() {
|
|
|
|
cmd = launch.run.as_ref().unwrap().clone();
|
|
|
|
}
|
|
|
|
|
2024-01-22 05:08:14 -08:00
|
|
|
if cmd.is_empty() {
|
|
|
|
cmd.push("/bin/sh".to_string());
|
|
|
|
}
|
2024-01-22 05:30:02 -08:00
|
|
|
|
2024-01-22 05:08:14 -08:00
|
|
|
let path = cmd.remove(0);
|
|
|
|
let mut env = match config.env() {
|
|
|
|
None => vec![],
|
|
|
|
Some(value) => value.clone(),
|
|
|
|
};
|
|
|
|
env.push("HYPHA_CONTAINER=1".to_string());
|
2024-01-30 18:34:47 -08:00
|
|
|
if let Some(extra_env) = &launch.env {
|
|
|
|
env.extend_from_slice(extra_env.as_slice());
|
|
|
|
}
|
2024-01-31 01:40:42 -08:00
|
|
|
|
|
|
|
trace!("running container command: {}", cmd.join(" "));
|
|
|
|
|
2024-01-22 05:08:14 -08:00
|
|
|
let path_cstr = CString::new(path)?;
|
2024-01-22 05:30:02 -08:00
|
|
|
let cmd_cstr = ContainerInit::strings_as_cstrings(cmd)?;
|
|
|
|
let env_cstr = ContainerInit::strings_as_cstrings(env)?;
|
2024-01-22 05:08:14 -08:00
|
|
|
let mut working_dir = config
|
|
|
|
.working_dir()
|
|
|
|
.as_ref()
|
|
|
|
.map(|x| x.to_string())
|
|
|
|
.unwrap_or("/".to_string());
|
|
|
|
|
|
|
|
if working_dir.is_empty() {
|
|
|
|
working_dir = "/".to_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::env::set_current_dir(&working_dir)?;
|
2024-02-01 11:50:33 +00:00
|
|
|
self.fork_and_exec(&path_cstr, cmd_cstr, env_cstr)?;
|
2024-01-22 05:08:14 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn strings_as_cstrings(values: Vec<String>) -> Result<Vec<CString>> {
|
|
|
|
let mut results: Vec<CString> = vec![];
|
|
|
|
for value in values {
|
|
|
|
results.push(CString::new(value.as_bytes().to_vec())?);
|
|
|
|
}
|
|
|
|
Ok(results)
|
|
|
|
}
|
2024-01-31 01:40:42 -08:00
|
|
|
|
2024-02-01 11:50:33 +00:00
|
|
|
fn fork_and_exec(&mut self, path: &CStr, cmd: Vec<CString>, env: Vec<CString>) -> Result<()> {
|
2024-01-31 01:40:42 -08:00
|
|
|
match unsafe { fork()? } {
|
|
|
|
ForkResult::Parent { child } => self.background(child),
|
|
|
|
ForkResult::Child => {
|
|
|
|
execve(path, &cmd, &env)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn background(&mut self, executed: Pid) -> Result<()> {
|
|
|
|
loop {
|
|
|
|
let mut status: c_int = 0;
|
|
|
|
let pid = unsafe { wait(addr_of_mut!(status)) };
|
|
|
|
if executed.as_raw() == pid {
|
|
|
|
return self.death(status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn death(&mut self, code: c_int) -> Result<()> {
|
|
|
|
println!("[hypha] container process exited: status = {}", code);
|
|
|
|
println!("[hypha] looping forever");
|
|
|
|
loop {
|
|
|
|
sleep(Duration::from_secs(1));
|
|
|
|
}
|
|
|
|
}
|
2024-01-22 02:15:53 -08:00
|
|
|
}
|