Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fs-gen): implement initramfs building and archiving #24

Merged
merged 21 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
0d6cf10
chore: bootstrap fs-gen project
Mathias-Boulay Mar 15, 2024
bc92ea8
feat(cli): basic argument handling
Mathias-Boulay Mar 18, 2024
0cde345
feat(validation): verify image name
Mathias-Boulay Mar 28, 2024
d7352e6
Update Cargo.toml, image_builder.rs, and main.rs
Mathias-Boulay Mar 30, 2024
adb5c82
wip: fuse overlayfs test
Mathias-Boulay Apr 3, 2024
778a141
feat: extract layers into a single folder
Mathias-Boulay Apr 4, 2024
3b160a0
add(args): agent_host_path and target path, better error display
Mathias-Boulay Apr 6, 2024
8a64316
Fix: disallow other users to access the overlay fs
Mathias-Boulay Apr 15, 2024
25c5105
feat: download and unpack image layers from Docker Hub
sea-gull-diana Apr 13, 2024
d4b9ef8
feat: return paths to layers in download_image_fs function, improve l…
sea-gull-diana Apr 14, 2024
c44c054
wip: trying to combine fs download and fuse
sea-gull-diana Apr 15, 2024
b4ab148
Fix: disallow other users to access the overlay fs
Mathias-Boulay Apr 15, 2024
0a6c6a5
feat: create init file
BioTheWolff Apr 15, 2024
d5b0196
feat: implement v0 with bash command
BioTheWolff Apr 15, 2024
33cce94
feat: change cli arguments to be more modular
BioTheWolff Apr 17, 2024
bf7e50e
refactor: use subdirs for layers and overlay
BioTheWolff Apr 17, 2024
6a4c87d
refactor: add flair to signal the initramfs comes from cloudlet
BioTheWolff Apr 22, 2024
9026839
lint: correct linting for crate
BioTheWolff Apr 22, 2024
38d9fb6
cleanup(image_builder): error handling with anyhow
Mathias-Boulay Apr 22, 2024
290f456
cleanup: clippy compliance
Mathias-Boulay Apr 22, 2024
0e619c2
fix: handle already existing temp folder
Mathias-Boulay Apr 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[workspace]
members = ["src/api", "src/vmm", "src/cli"]
members = ["src/api", "src/vmm", "src/cli", "src/fs-gen"]
resolver = "2"
21 changes: 21 additions & 0 deletions src/fs-gen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "fs-gen"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "4.5.3", features = ["derive", "wrap_help", "string"] }
dircpy = "0.3.16"
fuse-backend-rs = "0.12.0"
flate2 = "1.0.28"
once_cell = "1.19.0"
regex = "1.10.4"
reqwest = { version = "0.12.3", features = ["blocking", "json"] }
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.115"
signal-hook = "0.3.17"
tar = "0.4.40"
validator = { version = "0.17.0", features = ["derive"] }
anyhow = "1.0.82"
80 changes: 80 additions & 0 deletions src/fs-gen/src/cli_args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use std::{env, path::PathBuf};

use clap::{command, error::ErrorKind, CommandFactory, Parser};
use regex::Regex;

use once_cell::sync::Lazy;

// So, for any of you who may be scared, this is the regex from the OCI Distribution Sepcification for the image name + the tag
static RE_IMAGE_NAME: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"[a-z0-9]+((\.|_|__|-+)[a-z0-9]+)*(\/[a-z0-9]+((\.|_|__|-+)[a-z0-9]+)*)*:[a-zA-Z0-9_][a-zA-Z0-9._-]{0,127}").unwrap()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This regex pattern expects an image name in the form image:tag and not just . But our function that downloads layers doesn't have a problem with receiving an image name without tag (it will just use 'latest' as tag by default). Maybe we should change the regex to make tag part optional?
[a-z0-9]+((\.|_|__|-+)[a-z0-9]+)*(\/[a-z0-9]+((\.|_|__|-+)[a-z0-9]+)*)*(?::[a-zA-Z0-9_][a-zA-Z0-9._-]{0,127})?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could do that for a later iteration, sure

});

/// Convert an OCI image into a CPIO file
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct CliArgs {
/// The name of the image to download
pub image_name: String,

/// The path to the output file
#[arg(short='o', long="output", default_value=get_default_output_file().into_os_string())]
pub output_file: PathBuf,

/// The path to the temporary folder
#[arg(short='t', long="tempdir", default_value=get_default_temp_directory().into_os_string())]
pub temp_directory: PathBuf,

/// The host path to the guest agent binary
pub agent_host_path: PathBuf,
}

impl CliArgs {
/// Get the cli arguments with additional validation
pub fn get_args() -> Self {
let args = CliArgs::parse();

args.validate_image();
args.validate_host_path();

args
}

fn validate_image(&self) {
if !RE_IMAGE_NAME.is_match(&self.image_name) {
let mut cmd = CliArgs::command();
cmd.error(
ErrorKind::InvalidValue,
format!("Invalid image name: \"{}\"", self.image_name),
)
.exit();
}
}

fn validate_host_path(&self) {
if !self.agent_host_path.exists() {
let mut cmd = CliArgs::command();
cmd.error(
ErrorKind::InvalidValue,
format!(
"File not found for agent binary: \"{}\"",
self.agent_host_path.to_string_lossy()
),
)
.exit();
}
}
}

/// Get the default output path for the cpio file.
fn get_default_temp_directory() -> PathBuf {
let mut path = env::current_dir().unwrap();
path.push(".cloudlet_temp/");
path
}

fn get_default_output_file() -> PathBuf {
let mut path = env::current_dir().unwrap();
path.push("initramfs.img");
path
}
169 changes: 169 additions & 0 deletions src/fs-gen/src/image_builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
use std::{
fs,
path::{Path, PathBuf},
sync::Arc,
thread,
};

use anyhow::anyhow;
use anyhow::{Context, Ok, Result};
use fuse_backend_rs::{
api::{filesystem::Layer, server::Server},
overlayfs::{config::Config, OverlayFs},
passthrough::{self, PassthroughFs},
transport::{FuseChannel, FuseSession},
};

static FILE_EXISTS_ERROR: i32 = 17;

pub struct FuseServer {
server: Arc<Server<Arc<OverlayFs>>>,
ch: FuseChannel,
}

type BoxedLayer = Box<dyn Layer<Inode = u64, Handle = u64> + Send + Sync>;

/// Initialiazes a passthrough fs for a given layer
/// a passthrough fs is just a dummy implementation to map to the physical disk
/// # Usage
/// ```
/// let passthrough_layer = new_passthroughfs_layer("/path/to/layer")
/// ```
fn new_passthroughfs_layer(rootdir: &str) -> Result<BoxedLayer> {
let config = passthrough::Config {
root_dir: String::from(rootdir),
xattr: true,
do_import: true,
..Default::default()
};
let fs = Box::new(PassthroughFs::<()>::new(config)?);
fs.import()
.with_context(|| format!("Failed to create the passthrough layer: {}", rootdir))?;
Ok(fs as BoxedLayer)
}

/// Ensure a destination folder is created
fn ensure_folder_created(output_folder: &Path) -> Result<()> {
let result = fs::create_dir(output_folder);

// If the file already exists, we're fine
if result.is_err()
&& result
.unwrap_err()
.raw_os_error()
.is_some_and(|err_val| err_val != FILE_EXISTS_ERROR)
{
return Err(anyhow!("Failed to create folder"));
}

Ok(())
}

/// Merges all the layers into a single folder for further manipulation
/// It works by instantiating an overlay fs via FUSE then copying the files to the desired target
/// # Usage
/// ```
/// merge_layer(vec!["source/layer_1", "source/layer_2"], "/tmp/fused_layers")
/// ```
pub fn merge_layer(blob_paths: &[PathBuf], output_folder: &Path) -> Result<()> {
// Stack all lower layers
let mut lower_layers = Vec::new();
for lower in blob_paths {
lower_layers.push(Arc::new(new_passthroughfs_layer(&lower.to_string_lossy())?));
}

let mountpoint = Path::new("/tmp/cloudlet_internal");
let fs_name = "cloudlet_overlay";

ensure_folder_created(mountpoint)?;
ensure_folder_created(output_folder)?;

// Setup the overlay fs config
let config = Config {
work: "/work".into(),
mountpoint: output_folder.to_string_lossy().into(),
do_import: true,
..Default::default()
};

let fs = OverlayFs::new(None, lower_layers, config)
.with_context(|| "Failed to construct the Overlay fs struct !".to_string())?;
fs.import()
.with_context(|| "Failed to initialize the overlay fs".to_string())?;

// Enable a fuse session to make the fs available
let mut se = FuseSession::new(mountpoint, fs_name, "", true)
.with_context(|| "Failed to construct the Fuse session")?;
se.set_allow_other(false);
se.mount()
.with_context(|| "Failed to mount the overlay fs".to_string())?;

// Fuse session
let mut server = FuseServer {
server: Arc::new(Server::new(Arc::new(fs))),
ch: se
.new_channel()
.with_context(|| "Failed to create a new channel".to_string())?,
};

let handle = thread::spawn(move || {
let _ = server.svc_loop();
});

println!("copy starting !");
//So now we need to copy the files
dircpy::copy_dir(mountpoint, output_folder).with_context(|| {
format!(
"Failed to copy directories into the output folder: {}",
output_folder.to_string_lossy()
)
})?;
println!("copy finished");

// Unmount sessions so it can be re-used in later executions of the program
se.wake()
.with_context(|| "Failed to exit the fuse session".to_string())?;
se.umount()
.with_context(|| "Failed to unmount the fuse session".to_string())?;

let _ = handle.join();
Ok(())
}

impl FuseServer {
/// Run a loop to execute requests from the FUSE session
///
pub fn svc_loop(&mut self) -> Result<()> {
println!("entering server loop");
loop {
let value = self
.ch
.get_request()
.with_context(|| "Failed to get message from fuse session".to_string())?;

if value.is_none() {
println!("fuse server exits");
break;
}

// Technically the unwrap is safe
let (reader, writer) = value.unwrap();

if let Err(e) = self
.server
.handle_message(reader, writer.into(), None, None)
{
match e {
fuse_backend_rs::Error::EncodeMessage(_ebadf) => {
break;
}
_ => {
print!("Handling fuse message failed");
continue;
}
}
}
}
Ok(())
}
}
Loading