From 98dcd688025e8d8a0749f3c8b8e0e7c301e82aec Mon Sep 17 00:00:00 2001 From: Mathias-Boulay Date: Fri, 15 Mar 2024 14:56:59 +0100 Subject: [PATCH 1/6] chore: bootstrap fs-gen project --- Cargo.toml | 2 +- src/fs-gen/Cargo.toml | 8 ++++++++ src/fs-gen/src/main.rs | 3 +++ 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 src/fs-gen/Cargo.toml create mode 100644 src/fs-gen/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index 715dc56..c7b3753 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,3 +1,3 @@ [workspace] -members = [ "src/api","src/vmm", "src/cli"] +members = [ "src/api","src/vmm", "src/cli", "src/fs-gen"] resolver = "2" diff --git a/src/fs-gen/Cargo.toml b/src/fs-gen/Cargo.toml new file mode 100644 index 0000000..e6c8ef2 --- /dev/null +++ b/src/fs-gen/Cargo.toml @@ -0,0 +1,8 @@ +[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] diff --git a/src/fs-gen/src/main.rs b/src/fs-gen/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/src/fs-gen/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} From ab8c8f0aad43b2667e2b526fa6ebfe4f71d499b8 Mon Sep 17 00:00:00 2001 From: Mathias-Boulay Date: Mon, 18 Mar 2024 08:46:49 +0100 Subject: [PATCH 2/6] feat(cli): basic argument handling --- src/fs-gen/Cargo.toml | 1 + src/fs-gen/src/cli_args.rs | 23 +++++++++++++++++++++++ src/fs-gen/src/main.rs | 9 ++++++++- 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/fs-gen/src/cli_args.rs diff --git a/src/fs-gen/Cargo.toml b/src/fs-gen/Cargo.toml index e6c8ef2..23a50ac 100644 --- a/src/fs-gen/Cargo.toml +++ b/src/fs-gen/Cargo.toml @@ -6,3 +6,4 @@ 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"] } diff --git a/src/fs-gen/src/cli_args.rs b/src/fs-gen/src/cli_args.rs new file mode 100644 index 0000000..b7279aa --- /dev/null +++ b/src/fs-gen/src/cli_args.rs @@ -0,0 +1,23 @@ +use std::{env, path::PathBuf}; + +use clap::{command, Parser}; +/// 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 + #[arg(short, long)] + 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, +} + +/// 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 +} diff --git a/src/fs-gen/src/main.rs b/src/fs-gen/src/main.rs index e7a11a9..8623f70 100644 --- a/src/fs-gen/src/main.rs +++ b/src/fs-gen/src/main.rs @@ -1,3 +1,10 @@ +use clap::Parser; + +use crate::cli_args::CliArgs; + +mod cli_args; + fn main() { - println!("Hello, world!"); + let args = CliArgs::parse(); + println!("Hello, world!, {:?}", args); } From 8c394305d351c1a30f94b2f5b4cc2227c5856b9a Mon Sep 17 00:00:00 2001 From: Mathias-Boulay Date: Thu, 28 Mar 2024 12:26:51 +0100 Subject: [PATCH 3/6] feat(validation): verify image name --- src/fs-gen/Cargo.toml | 3 +++ src/fs-gen/src/cli_args.rs | 28 +++++++++++++++++++++++++++- src/fs-gen/src/main.rs | 2 +- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/fs-gen/Cargo.toml b/src/fs-gen/Cargo.toml index 23a50ac..0a0b17d 100644 --- a/src/fs-gen/Cargo.toml +++ b/src/fs-gen/Cargo.toml @@ -7,3 +7,6 @@ edition = "2021" [dependencies] clap = { version = "4.5.3", features = ["derive", "wrap_help", "string"] } +once_cell = "1.19.0" +regex = "1.10.4" +validator = { version = "0.17.0", features = ["derive"] } diff --git a/src/fs-gen/src/cli_args.rs b/src/fs-gen/src/cli_args.rs index b7279aa..350f6c7 100644 --- a/src/fs-gen/src/cli_args.rs +++ b/src/fs-gen/src/cli_args.rs @@ -1,12 +1,24 @@ use std::{env, path::PathBuf}; use clap::{command, Parser}; +use regex::Regex; + +use once_cell::sync::Lazy; +use validator::Validate; + +// 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 = 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)] +#[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 @@ -14,6 +26,20 @@ pub struct CliArgs { 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("wut")); + } + + args + } +} + /// Get the default output path for the cpio file. fn get_default_log_path() -> PathBuf { let mut path = env::current_exe().unwrap(); diff --git a/src/fs-gen/src/main.rs b/src/fs-gen/src/main.rs index 8623f70..da6e973 100644 --- a/src/fs-gen/src/main.rs +++ b/src/fs-gen/src/main.rs @@ -5,6 +5,6 @@ use crate::cli_args::CliArgs; mod cli_args; fn main() { - let args = CliArgs::parse(); + let args = CliArgs::get_args(); println!("Hello, world!, {:?}", args); } From a64a0dbcbc996c7a83c8fe3db1fcb56010045e1e Mon Sep 17 00:00:00 2001 From: Mathias-Boulay Date: Sat, 30 Mar 2024 17:43:14 +0100 Subject: [PATCH 4/6] Update Cargo.toml, image_builder.rs, and main.rs --- src/fs-gen/Cargo.toml | 3 +++ src/fs-gen/src/image_builder.rs | 22 ++++++++++++++++++++++ src/fs-gen/src/main.rs | 13 ++++++++++++- 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 src/fs-gen/src/image_builder.rs diff --git a/src/fs-gen/Cargo.toml b/src/fs-gen/Cargo.toml index 0a0b17d..9790eb3 100644 --- a/src/fs-gen/Cargo.toml +++ b/src/fs-gen/Cargo.toml @@ -9,4 +9,7 @@ edition = "2021" 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" diff --git a/src/fs-gen/src/image_builder.rs b/src/fs-gen/src/image_builder.rs new file mode 100644 index 0000000..8319048 --- /dev/null +++ b/src/fs-gen/src/image_builder.rs @@ -0,0 +1,22 @@ +use std::path::{Path, PathBuf}; + +use vfs::{FileSystem, OverlayFS, PhysicalFS, VfsPath}; + +pub fn build_new_image(blob_paths: &Vec, output_folder: &Path) { + let virtual_paths = blob_paths + .iter() + .map(|p| VfsPath::new(PhysicalFS::new(p))) + .collect::>(); + + let vfs = OverlayFS::new(&virtual_paths); + let toto: Vec = 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 !"); +} diff --git a/src/fs-gen/src/main.rs b/src/fs-gen/src/main.rs index da6e973..c827c2c 100644 --- a/src/fs-gen/src/main.rs +++ b/src/fs-gen/src/main.rs @@ -1,10 +1,21 @@ +use std::{ + path::{Path, PathBuf}, + str::FromStr, +}; + use clap::Parser; -use crate::cli_args::CliArgs; +use crate::{cli_args::CliArgs, image_builder::build_new_image}; mod cli_args; +mod image_builder; fn main() { let args = CliArgs::get_args(); println!("Hello, world!, {:?}", args); + + let paths: Vec = + vec![PathBuf::from_str("../../image-gen/blobs/sha256/layer_1").unwrap()]; + + build_new_image(&paths, &PathBuf::from_str("./titi").unwrap()); } From fccee09e35b8896bb92204a9f92c726a9e5502bd Mon Sep 17 00:00:00 2001 From: BioTheWolff <47079795+BioTheWolff@users.noreply.github.com> Date: Tue, 9 Apr 2024 20:33:44 +0200 Subject: [PATCH 5/6] cleanup: make it cleaner for a PR --- src/fs-gen/src/cli_args.rs | 9 ++++----- src/fs-gen/src/image_builder.rs | 4 +++- src/fs-gen/src/main.rs | 7 +++---- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/fs-gen/src/cli_args.rs b/src/fs-gen/src/cli_args.rs index 350f6c7..8a224a7 100644 --- a/src/fs-gen/src/cli_args.rs +++ b/src/fs-gen/src/cli_args.rs @@ -1,12 +1,12 @@ -use std::{env, path::PathBuf}; +//! 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; -// So, for any of you who may be scared, this is the regex from the OCI Distribution Sepcification for the image name + the tag +/// Official regex from the OCI Distribution Sepcification (image name and tag) static RE_IMAGE_NAME: Lazy = 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() }); @@ -16,7 +16,6 @@ static RE_IMAGE_NAME: Lazy = Lazy::new(|| { #[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, @@ -33,7 +32,7 @@ impl CliArgs { let validation = args.validate(); if validation.is_err() { - panic!("Invalid arguments: {}", validation.expect_err("wut")); + panic!("Invalid arguments: {}", validation.expect_err("Invalid arguments given")); } args diff --git a/src/fs-gen/src/image_builder.rs b/src/fs-gen/src/image_builder.rs index 8319048..60edc4a 100644 --- a/src/fs-gen/src/image_builder.rs +++ b/src/fs-gen/src/image_builder.rs @@ -1,7 +1,9 @@ -use std::path::{Path, PathBuf}; +//! 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, output_folder: &Path) { let virtual_paths = blob_paths .iter() diff --git a/src/fs-gen/src/main.rs b/src/fs-gen/src/main.rs index c827c2c..2656f93 100644 --- a/src/fs-gen/src/main.rs +++ b/src/fs-gen/src/main.rs @@ -1,10 +1,10 @@ +//! Main module for the initramfs tarball generation + use std::{ - path::{Path, PathBuf}, + path::PathBuf, str::FromStr, }; - use clap::Parser; - use crate::{cli_args::CliArgs, image_builder::build_new_image}; mod cli_args; @@ -12,7 +12,6 @@ mod image_builder; fn main() { let args = CliArgs::get_args(); - println!("Hello, world!, {:?}", args); let paths: Vec = vec![PathBuf::from_str("../../image-gen/blobs/sha256/layer_1").unwrap()]; From 27bf4352f0346d42eb3eaae14840740d9a58530e Mon Sep 17 00:00:00 2001 From: Mathias-Boulay Date: Wed, 17 Apr 2024 22:39:20 +0200 Subject: [PATCH 6/6] cut: remove unused stuff Signed-off-by: Mathias-Boulay --- src/fs-gen/src/main.rs | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/fs-gen/src/main.rs b/src/fs-gen/src/main.rs index 2656f93..1efec39 100644 --- a/src/fs-gen/src/main.rs +++ b/src/fs-gen/src/main.rs @@ -1,20 +1,12 @@ //! Main module for the initramfs tarball generation -use std::{ - path::PathBuf, - str::FromStr, -}; -use clap::Parser; 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(); - - let paths: Vec = - vec![PathBuf::from_str("../../image-gen/blobs/sha256/layer_1").unwrap()]; - - build_new_image(&paths, &PathBuf::from_str("./titi").unwrap()); }