Skip to content

Commit

Permalink
feat(vmm): implemented initramfs
Browse files Browse the repository at this point in the history
Signed-off-by: Muriel Paraire <[email protected]>
  • Loading branch information
MurielParaire committed Apr 29, 2024
1 parent 2e63838 commit 83a26dc
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
5 changes: 5 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ build-agent args = "":
-w /volume \
-t clux/muslrust \
cargo build --release --bin agent {{args}}

build-musl-agent args = "":
#!/bin/bash
rustup target add x86_64-unknown-linux-musl
cargo build --release --bin agent --target=x86_64-unknown-linux-musl

build-rootfs mode = "dev":
#!/bin/bash
Expand Down
2 changes: 1 addition & 1 deletion src/vmm/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::args::{CliArgs, Commands};
use clap::Parser;
use tonic::transport::Server;
use tracing::{info, level_filters::LevelFilter};
use tracing::info;
use vmm::{
core::vmm::VMM,
service::{vmmorchestrator, VmmService},
Expand Down
48 changes: 34 additions & 14 deletions src/vmm/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ use self::vmmorchestrator::{
use crate::core::vmm::VMM;
use crate::VmmErrors;
use std::{
convert::From,
env::current_dir,
net::Ipv4Addr,
path::{Path, PathBuf},
process::{Command, Stdio},
convert::From, env::current_dir, net::Ipv4Addr, path::{Path, PathBuf}, process::{Command, Stdio}
};
use tonic::{Request, Response, Status};
use tracing::{error, info};
Expand Down Expand Up @@ -44,7 +40,7 @@ impl VmmServiceTrait for VmmService {
const HOST_NETMASK: Ipv4Addr = Ipv4Addr::new(255, 255, 0, 0);

// get current directory
let mut curr_dir = current_dir()?;
let mut curr_dir = current_dir().expect("Need to be able to access current directory path.");

// define kernel path
let mut kernel_entire_path = curr_dir.as_os_str().to_owned();
Expand All @@ -70,7 +66,7 @@ impl VmmServiceTrait for VmmService {
let kernel_path = Path::new(&kernel_entire_path);

// define initramfs file placement
let initramfs_entire_file_path = curr_dir.as_mut_os_string();
let mut initramfs_entire_file_path = curr_dir.as_os_str().to_owned();
initramfs_entire_file_path.push("/tools/rootfs/");

// get request with the language
Expand All @@ -91,16 +87,39 @@ impl VmmServiceTrait for VmmService {
"node:alpine"
},
};

// Check if the initramfs is on the system, else build it
let initramfs_exists = Path::new(&initramfs_entire_file_path).try_exists().expect(&format!("Could not access folder {:?}", &initramfs_entire_file_path));
if !initramfs_exists
{
info!("Initramfs not found, building initramfs");


let rootfs_exists = Path::new(&initramfs_entire_file_path).try_exists().expect(&format!("Could not access file {:?}", &initramfs_entire_file_path));
if !rootfs_exists {
// check if agent binary exists
let agent_file_name = curr_dir.as_mut_os_string();
agent_file_name.push("/target/x86_64-unknown-linux-musl/release/agent");

// if agent hasn't been build, build it
let agent_exists = Path::new(&agent_file_name).try_exists().expect(&format!("Could not access file {:?}", &agent_file_name));
if !agent_exists {
//build agent
info!("Building agent binary");
// Execute the script using sh and capture output and error streams
let output = Command::new("just")
.arg("build-musl-agent")
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
.expect("Failed to execute the just build script for the agent");

// Print output and error streams
info!("Script output: {}", String::from_utf8_lossy(&output.stdout));
error!("Script errors: {}", String::from_utf8_lossy(&output.stderr));
info!("Agent binary successfully built.")
}

info!("Building initramfs");
// Execute the script using sh and capture output and error streams
let output = Command::new("sh")
.arg("./tools/rootfs/mkrootfs.sh")
.arg(image)
.arg(&agent_file_name)
.arg(&initramfs_entire_file_path)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
Expand All @@ -110,7 +129,8 @@ impl VmmServiceTrait for VmmService {
// Print output and error streams
info!("Script output: {}", String::from_utf8_lossy(&output.stdout));
error!("Script errors: {}", String::from_utf8_lossy(&output.stderr));
};
info!("Initramfs successfully built.")
}
let initramfs_path = PathBuf::from(&initramfs_entire_file_path);

// Create a new VMM
Expand Down
2 changes: 1 addition & 1 deletion tools/rootfs/mkrootfs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ulimit -Sn 8192

if [ -d fs-gen ]
then
cargo run --bin fs-gen -- $1 ./fs-gen/test -o $2
cargo run --bin fs-gen -- $1 $2 -o $3
else
echo "Module fs-gen not found"
fi

0 comments on commit 83a26dc

Please sign in to comment.