Skip to content

Commit

Permalink
implemented kaps in quardle feature
Browse files Browse the repository at this point in the history
- setup: install kaps sources
- add_kaps: build kaps from sources and add it to quardle

Signed-off-by: leofvo <[email protected]>
  • Loading branch information
leofvo committed May 2, 2022
1 parent 6a3efe6 commit 4a9abcb
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 10 deletions.
24 changes: 24 additions & 0 deletions mkinitramfs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/bash
DEST=${1:-"alpine-minirootfs"}

pushd "$DEST"
cat > init <<EOF
#! /bin/sh
#
# /init executable file in the initramfs
#
mount -t devtmpfs dev /dev
mount -t proc proc /proc
mount -t sysfs sysfs /sys
ip link set up dev lo
exec /sbin/getty -n -l /bin/sh 115200 /dev/console
poweroff -f
EOF

chmod +x init

find . -print0 |
cpio --null --create --verbose --owner root:root --format=newc |
xz -9 --format=lzma > ../initramfs.img

popd
95 changes: 86 additions & 9 deletions src/quardle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 !");
Expand Down Expand Up @@ -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 !");
Expand All @@ -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
}
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion tools/mkinitramfs.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/bash
DEST=${1:-"alpine-minirootfs"}

pushd alpine-minirootfs
pushd "$DEST"
cat > init <<EOF
#! /bin/sh
#
Expand Down

0 comments on commit 4a9abcb

Please sign in to comment.