From f3d6f398d6605c83aadb148a096a44026dcf7fc5 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 | 61 +++++++++++++++++++++++++++++++++++++++++++- tools/mkinitramfs.sh | 23 +++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 tools/mkinitramfs.sh diff --git a/src/quardle/mod.rs b/src/quardle/mod.rs index ab0eecd..413e6fd 100644 --- a/src/quardle/mod.rs +++ b/src/quardle/mod.rs @@ -9,8 +9,8 @@ use std::process::Command; const QUARK_BUILD_DIR: &str = "/tmp/quark/builds/"; const QUARK_CONFIG_DIR: &str = "/opt/quark/"; +const QUARDLE_INITRD: &str = "initramfs.img"; const QUARDLE_KERNEL: &str = "bzImage"; -const QUARDLE_INITRD: &str = "rootfs/"; const QUARDLE_KERNEL_CMDLINE: &str = "/proc/cmdline"; /// Containers related errors @@ -49,6 +49,7 @@ impl Quardle { self .setup() .add_kernel() + .add_initramfs() .add_config_file() .make_archive()?; @@ -130,6 +131,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() { + println!("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() { + println!("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"); + } + + 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"); + } self } @@ -145,6 +191,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 { + println!("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 { 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