Files
krata/hypha/src/container/init.rs

64 lines
1.8 KiB
Rust
Raw Normal View History

2024-01-22 02:15:53 -08:00
use crate::error::Result;
use oci_spec::image::ImageConfiguration;
2024-01-22 02:15:53 -08:00
use std::fs;
use std::path::Path;
use sys_mount::{FilesystemType, Mount, MountFlags};
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";
const IMAGE_CONFIG_JSON_PATH: &str = "/config/image/config.json";
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 {}
}
pub fn init(&mut self) -> Result<()> {
self.prepare_mounts()?;
let config = self.parse_image_config()?;
if let Some(cfg) = config.config() {
if let Some(cmd) = cfg.cmd() {
println!("image command: {:?}", cmd);
}
}
2024-01-22 02:15:53 -08:00
Ok(())
}
fn prepare_mounts(&mut self) -> Result<()> {
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 parse_image_config(&mut self) -> Result<ImageConfiguration> {
let image_config_path = Path::new(IMAGE_CONFIG_JSON_PATH);
let config = ImageConfiguration::from_file(image_config_path)?;
Ok(config)
}
2024-01-22 02:15:53 -08:00
fn mount_squashfs(&mut self, from: &Path, to: &Path) -> Result<()> {
if !to.is_dir() {
fs::create_dir(to)?;
}
Mount::builder()
.fstype(FilesystemType::Manual("squashfs"))
.flags(MountFlags::RDONLY)
.mount(from, to)?;
Ok(())
}
}