-
Notifications
You must be signed in to change notification settings - Fork 11
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
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
0d6cf10
chore: bootstrap fs-gen project
Mathias-Boulay bc92ea8
feat(cli): basic argument handling
Mathias-Boulay 0cde345
feat(validation): verify image name
Mathias-Boulay d7352e6
Update Cargo.toml, image_builder.rs, and main.rs
Mathias-Boulay adb5c82
wip: fuse overlayfs test
Mathias-Boulay 778a141
feat: extract layers into a single folder
Mathias-Boulay 3b160a0
add(args): agent_host_path and target path, better error display
Mathias-Boulay 8a64316
Fix: disallow other users to access the overlay fs
Mathias-Boulay 25c5105
feat: download and unpack image layers from Docker Hub
sea-gull-diana d4b9ef8
feat: return paths to layers in download_image_fs function, improve l…
sea-gull-diana c44c054
wip: trying to combine fs download and fuse
sea-gull-diana b4ab148
Fix: disallow other users to access the overlay fs
Mathias-Boulay 0a6c6a5
feat: create init file
BioTheWolff d5b0196
feat: implement v0 with bash command
BioTheWolff 33cce94
feat: change cli arguments to be more modular
BioTheWolff bf7e50e
refactor: use subdirs for layers and overlay
BioTheWolff 6a4c87d
refactor: add flair to signal the initramfs comes from cloudlet
BioTheWolff 9026839
lint: correct linting for crate
BioTheWolff 38d9fb6
cleanup(image_builder): error handling with anyhow
Mathias-Boulay 290f456
cleanup: clippy compliance
Mathias-Boulay 0e619c2
fix: handle already existing temp folder
Mathias-Boulay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}); | ||
|
||
/// 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
use std::{ | ||
fs::{self}, | ||
path::{Path, PathBuf}, | ||
sync::Arc, | ||
thread, | ||
}; | ||
|
||
use anyhow::{Context, Result}; | ||
use fuse_backend_rs::{ | ||
api::{filesystem::Layer, server::Server}, | ||
overlayfs::{config::Config, OverlayFs}, | ||
passthrough::{self, PassthroughFs}, | ||
transport::{FuseChannel, FuseSession}, | ||
}; | ||
|
||
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<()> { | ||
fs::create_dir(output_folder).with_context(|| { | ||
format!( | ||
"Failed to ensure folder creation: {}", | ||
output_folder.to_string_lossy() | ||
) | ||
}) | ||
} | ||
|
||
/// 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(()) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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})?
There was a problem hiding this comment.
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