Skip to content

Commit

Permalink
implemented initrd build feature
Browse files Browse the repository at this point in the history
- setup: fetch all files needed to build inird image
- add_init_rd: adding initramfs image to quardle

Signed-off-by: leofvo <[email protected]>
  • Loading branch information
leofvo committed Apr 27, 2022
1 parent 77a4713 commit f3d6f39
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
61 changes: 60 additions & 1 deletion src/quardle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -49,6 +49,7 @@ impl Quardle {
self
.setup()
.add_kernel()
.add_initramfs()
.add_config_file()
.make_archive()?;

Expand Down Expand Up @@ -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
}

Expand All @@ -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 {
Expand Down
23 changes: 23 additions & 0 deletions tools/mkinitramfs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/bash

pushd alpine-minirootfs
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

0 comments on commit f3d6f39

Please sign in to comment.