diff --git a/mkinitramfs.sh b/mkinitramfs.sh new file mode 100644 index 0000000..5cb3c0f --- /dev/null +++ b/mkinitramfs.sh @@ -0,0 +1,24 @@ +#!/usr/bin/bash +DEST=${1:-"alpine-minirootfs"} + +pushd "$DEST" +cat > init < ../initramfs.img + +popd \ No newline at end of file diff --git a/src/quardle/mod.rs b/src/quardle/mod.rs index 42e14ca..222d3bd 100644 --- a/src/quardle/mod.rs +++ b/src/quardle/mod.rs @@ -49,6 +49,7 @@ impl Quardle { .add_kernel() .add_initramfs() .add_config_file() + .clean_quardle_build_dir() .make_archive()?; // Deleting temporary files used to build the quardle @@ -91,6 +92,16 @@ impl Quardle { .expect("failed to setup quark"); } + // Install kaps sources + if !std::path::Path::new(&format!("{}kaps", QUARK_CONFIG_DIR)).exists() { + println!("Kaps not found, installing it !"); + Command::new("git") + .args(["clone", "https://github.com/virt-do/kaps.git"]) // Using https protocol because it seems not supporting ssh + .current_dir(format!("{}", QUARK_CONFIG_DIR)) + .output() + .expect("failed to fetch kaps"); + } + // Install a default kernel configuration file if !std::path::Path::new(&format!("{}linux-config-x86_64", QUARK_CONFIG_DIR)).exists() { println!("Kernel config file not found, installing it !"); @@ -151,6 +162,9 @@ impl Quardle { .current_dir(format!("{}", QUARK_CONFIG_DIR)) .output() .expect("failed to extract rootfs archive"); + + // Adding kaps binary to the rootfs + self.add_kaps_to_rootfs(); if !std::path::Path::new(&format!("{}mkinitramfs.sh", QUARK_CONFIG_DIR)).exists() { println!("InitramFS build script not found, installing it !"); @@ -168,11 +182,23 @@ impl Quardle { .expect("failed to make initramfs build script executable"); } - println!("InitramFS not builded, building it !"); - Command::new(format!("{}mkinitramfs.sh", QUARK_CONFIG_DIR)) - .current_dir(format!("{}", QUARK_CONFIG_DIR)) - .output() - .expect("failed to build initramfs"); + // Install a script to build kaps bundle + if !std::path::Path::new(&format!("{}mkbundle.sh", QUARK_CONFIG_DIR)).exists() { + println!("Kaps bundle build script not found, installing it !"); + Command::new("curl") + .arg("https://raw.githubusercontent.com/virt-do/lab/main/do-vmm/rootfs/mkbundle.sh") + .arg("-O") + .current_dir(format!("{}", QUARK_CONFIG_DIR)) + .output() + .expect("failed to fetch kaps bundle build script"); + + Command::new("chmod") + .arg("+x") + .arg(format!("{}mkbundle.sh", QUARK_CONFIG_DIR)) + .current_dir(format!("{}", QUARK_CONFIG_DIR)) + .output() + .expect("failed to make kernel build script executable"); + } } self } @@ -192,16 +218,67 @@ impl Quardle { /// Append basic rootfs /// Fetch automated script if isn't already installed, and use some bash script to build it fn add_initramfs(&self) -> &Quardle { - println!("Installing initRamFS image to quardle"); + println!("Installing rootfs !"); Command::new("cp") - .arg(format!("{}initramfs.img", QUARK_CONFIG_DIR)) + .arg("-r") + .arg(format!("{}alpine-minirootfs", QUARK_CONFIG_DIR)) .arg(format!("{}", self.clone().get_work_dir())) - .output() - .expect("failed to write initramfs"); + .spawn() + .expect("failed to copy rootfs"); + + Command::new(format!("{}mkbundle.sh", QUARK_CONFIG_DIR)) + .arg(format!("{}/alpine-minirootfs/ctr-bundle", self.clone().get_work_dir())) + .current_dir(format!("{}", QUARK_CONFIG_DIR)) + .output() + .expect("failed to build initramfs"); + + // add init file to it + println!("InitramFS not builded, building it !"); + Command::new(format!("{}mkinitramfs.sh", QUARK_CONFIG_DIR)) + .arg(format!("{}/alpine-minirootfs", self.clone().get_work_dir())) + .current_dir(format!("{}", QUARK_CONFIG_DIR)) + .output() + .expect("failed to build initramfs"); self } + fn clean_quardle_build_dir(&self) -> &Quardle { + println!("Cleaning quardle build directory !"); + Command::new("rm") + .arg("-rdf") + .arg("alpine-minirootfs") + .arg("mkbundle.sh") + .arg("mkinitramfs.sh") + .arg(format!("{}", self.clone().get_work_dir())) + .output() + .expect("failed to clean quardle build directory"); + self + } + + /// Append kaps binary to rootfs image + /// Fetch kaps source code if isn't already installed, build it from source and copy it to the working directory + fn add_kaps_to_rootfs(&self) -> &Quardle { + println!("Installing kaps to quardle"); + Command::new("cargo") + .current_dir(format!("{}kaps", QUARK_CONFIG_DIR)) + .arg("build") + .arg("--release") + .arg("--target=x86_64-unknown-linux-musl") // Build with all dependancies packed into the binary + // .arg("--out-dir") //TODO: outdir is only available on nightly for now, should be used later + // .arg(format!("{}/rootfs/usr/bin/kaps",self.clone().get_work_dir())) + .output() + .expect("failed to build kaps"); + + Command::new("cp") + .arg(format!("{}kaps/target/release/kaps", QUARK_CONFIG_DIR)) + .arg(format!("{}/alpine-minirootfs/usr/bin/kaps",self.clone().get_work_dir())) + .output() + .expect("failed to copy kaps"); + + self + } + /// Generate the config file of the quardle /// The config file is a JSON file containing a QuardleConfig struct fn add_config_file(&self) -> &Quardle { diff --git a/tools/mkinitramfs.sh b/tools/mkinitramfs.sh index 4bfe9f3..5cb3c0f 100644 --- a/tools/mkinitramfs.sh +++ b/tools/mkinitramfs.sh @@ -1,6 +1,7 @@ #!/usr/bin/bash +DEST=${1:-"alpine-minirootfs"} -pushd alpine-minirootfs +pushd "$DEST" cat > init <