From 2dfe2b187dcd545931528b8af7d1ea394e3d96b2 Mon Sep 17 00:00:00 2001 From: leofvo Date: Tue, 26 Apr 2022 18:54:01 +0200 Subject: [PATCH] implemented initrd build feature - setup: fetch all files needed to build inird image - add_init_rd: adding initramfs image to quardle Signed-off-by: leofvo --- src/quardle/mod.rs | 67 ++++++++++++++++++++++++++++++++++++++++++++ tools/mkinitramfs.sh | 23 +++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 tools/mkinitramfs.sh diff --git a/src/quardle/mod.rs b/src/quardle/mod.rs index 4fb942f..a94ea2e 100644 --- a/src/quardle/mod.rs +++ b/src/quardle/mod.rs @@ -48,6 +48,7 @@ impl Quardle { self .setup() .add_kernel() + .add_initramfs() .add_config_file() .make_archive()?; @@ -129,6 +130,51 @@ impl Quardle { .expect("failed to build kernel"); } + // Install a script to build initramfs + if !std::path::Path::new(&format!("{}alpine-minirootfs", QUARK_CONFIG_DIR)).exists() { + warn!("Rootfs not builded, building it !"); + Command::new("curl") + .arg("https://dl-cdn.alpinelinux.org/alpine/v3.14/releases/x86_64/alpine-minirootfs-3.14.2-x86_64.tar.gz") + .arg("-O") + .current_dir(format!("{}", QUARK_CONFIG_DIR)) + .output() + .expect("failed to download rootfs archive"); + Command::new("mkdir") + .arg("alpine-minirootfs") + .current_dir(format!("{}", QUARK_CONFIG_DIR)) + .output() + .expect("failed to download initramfs build script"); + Command::new("tar") + .arg("-xzf") + .arg("alpine-minirootfs-3.14.2-x86_64.tar.gz") + .arg("-C") + .arg("alpine-minirootfs") + .current_dir(format!("{}", QUARK_CONFIG_DIR)) + .output() + .expect("failed to extract rootfs archive"); + + if !std::path::Path::new(&format!("{}mkinitramfs.sh", QUARK_CONFIG_DIR)).exists() { + warn!("InitramFS build script not found, installing it !"); + Command::new("curl") + .arg("https://raw.githubusercontent.com/virt-do/quark/main/tools/mkinitramfs.sh") + .arg("-O") + .current_dir(format!("{}", QUARK_CONFIG_DIR)) + .output() + .expect("failed to download initramfs build script"); + Command::new("chmod") + .arg("+x") + .arg(format!("{}mkinitramfs.sh", QUARK_CONFIG_DIR)) + .current_dir(format!("{}", QUARK_CONFIG_DIR)) + .output() + .expect("failed to make initramfs build script executable"); + } + + warn!("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"); + } self } @@ -144,6 +190,19 @@ impl Quardle { self } + /// Append basic rootfs + /// Fetch automated script if isn't already installed, and use some bash script to build it + fn add_initramfs(&self) -> &Quardle { + info!("Installing initRamFS image to quardle"); + Command::new("cp") + .arg(format!("{}initramfs.img", QUARK_CONFIG_DIR)) + .arg(format!("{}", self.clone().get_work_dir())) + .output() + .expect("failed to write initramfs"); + + 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 { @@ -210,4 +269,12 @@ mod tests { assert_eq!(quardle.as_ref().unwrap().name, "test3"); assert_eq!(quardle.as_ref().unwrap().container_image_url, "container3"); } + + #[test] + fn quardle_add_initramfs() { + let quardle = Quardle::new("test4".to_string(), "container4".to_string(), false); + quardle.as_ref().unwrap().add_initramfs(); + assert_eq!(quardle.as_ref().unwrap().name, "test4"); + assert_eq!(quardle.as_ref().unwrap().container_image_url, "container4"); + } } \ No newline at end of file diff --git a/tools/mkinitramfs.sh b/tools/mkinitramfs.sh new file mode 100644 index 0000000..4bfe9f3 --- /dev/null +++ b/tools/mkinitramfs.sh @@ -0,0 +1,23 @@ +#!/usr/bin/bash + +pushd alpine-minirootfs +cat > init < ../initramfs.img + +popd \ No newline at end of file