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 May 3, 2022
1 parent b10a7ff commit 2dfe2b1
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/quardle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ impl Quardle {
self
.setup()
.add_kernel()
.add_initramfs()
.add_config_file()
.make_archive()?;

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

Expand All @@ -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 {
Expand Down Expand Up @@ -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");
}
}
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 2dfe2b1

Please sign in to comment.