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

chore(fs-gen): bootstrap project #17

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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"
15 changes: 15 additions & 0 deletions src/fs-gen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[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"] }
once_cell = "1.19.0"
regex = "1.10.4"
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.115"
validator = { version = "0.17.0", features = ["derive"] }
vfs = "0.12.0"
48 changes: 48 additions & 0 deletions src/fs-gen/src/cli_args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//! Command-line interface arguments and their management

use std::{env, path::PathBuf};
use clap::{command, Parser};
use regex::Regex;
use once_cell::sync::Lazy;
use validator::Validate;

/// Official regex from the OCI Distribution Sepcification (image name and 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()
});

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

/// The path to the output file
#[arg(short, long, default_value=get_default_log_path().into_os_string())]
pub ouput_file: PathBuf,
}

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

let validation = args.validate();
if validation.is_err() {
panic!("Invalid arguments: {}", validation.expect_err("Invalid arguments given"));
}

args
}
}

/// Get the default output path for the cpio file.
fn get_default_log_path() -> PathBuf {
let mut path = env::current_exe().unwrap();
path.pop();
path.push("output.cpio");
path
}
24 changes: 24 additions & 0 deletions src/fs-gen/src/image_builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//! Image builder module

use std::path::{Path, PathBuf};
use vfs::{FileSystem, OverlayFS, PhysicalFS, VfsPath};

/// Builds a new initramfs from path blobs and places it into a given destination folder
pub fn build_new_image(blob_paths: &Vec<PathBuf>, output_folder: &Path) {
let virtual_paths = blob_paths
.iter()
.map(|p| VfsPath::new(PhysicalFS::new(p)))
.collect::<Vec<VfsPath>>();

let vfs = OverlayFS::new(&virtual_paths);
let toto: Vec<String> = vfs.read_dir("/home").unwrap().collect();
println!("{:?}", toto);
println!("{:?}", vfs);
let overlay_root: VfsPath = vfs.into();

let output_vpath = VfsPath::new(PhysicalFS::new(output_folder));

overlay_root
.copy_dir(&output_vpath)
.expect("Failed to copy the blobs !");
}
12 changes: 12 additions & 0 deletions src/fs-gen/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//! Main module for the initramfs tarball generation

use crate::{cli_args::CliArgs, image_builder::build_new_image};
use clap::Parser;
use std::{path::PathBuf, str::FromStr};

mod cli_args;
mod image_builder;

fn main() {
let args = CliArgs::get_args();
}
Loading