Skip to content

Commit

Permalink
start tiffin refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
korewaChino committed Oct 11, 2024
1 parent a1d7496 commit 411961c
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 23 deletions.
95 changes: 94 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ uuid = { version = "1.4.1", features = ["v4", "serde"] }
loopdev-fyra = { version = "0.5.0" }
bytesize = { version = "1.3.0", features = ["serde"] }
indexmap = "2.2.6"
tiffin = "0.3.2"
54 changes: 37 additions & 17 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,13 @@ impl RootBuilder for DnfRootBuilder {
// While grub2-mkconfig may not return 0 it should still work
// todo: figure out why it still wouldn't write the file to /boot/grub2/grub.cfg
// but works when run inside a post script
let res = crate::run_cmd_prep_chroot!(&chroot,
unshare -R $chroot grub2-mkconfig -o /boot/grub2/grub.cfg;
);
let res = crate::util::enter_chroot_run(&chroot, || {
std::process::Command::new("grub2-mkconfig")
.arg("-o")
.arg("/boot/grub2/grub.cfg")
.status()?;
Ok(())
});

if let Err(e) = res {
warn!(?e, "grub2-mkconfig not returning 0, continuing anyway");
Expand All @@ -407,7 +411,8 @@ pub fn run_script(script: Script, chroot: &Path, is_post: bool) -> Result<()> {
let id = script.id.as_ref().map_or("<NULL>", |s| s);
bail_let!(Some(mut data) = script.load() => "Cannot load script `{id}`");
let name = script.name.as_ref().map_or("<Untitled>", |s| s);
info!(id, name, "Running script");

info!(id, name, in_chroot = script.chroot, "Running script");

let name = format!("script-{}", script.id.as_ref().map_or("untitled", |s| s));
// check if data has shebang
Expand All @@ -416,13 +421,21 @@ pub fn run_script(script: Script, chroot: &Path, is_post: bool) -> Result<()> {
data.insert_str(0, "#!/bin/sh\n");
}

let mut tiffin = tiffin::Container::new(chroot.to_path_buf());

if script.chroot.unwrap_or(is_post) {
just_write(chroot.join("tmp").join(&name), data)?;
crate::run_cmd_prep_chroot!(chroot,
chmod +x $chroot/tmp/$name;
unshare -R $chroot /tmp/$name 2>&1;
rm -f $chroot/tmp/$name;
)?;
tiffin.run(|| -> Result<()> {
// just_write(chroot.join("tmp").join(&name), data)?;
just_write(PathBuf::from(format!("/tmp/{name}")), data)?;

cmd_lib::run_cmd!(
chmod +x /tmp/$name;
/tmp/$name 2>&1;
rm -f /tmp/$name;
)?;

Ok(())
})??;
} else {
just_write(PathBuf::from(format!("katsu-work/{name}")), data)?;
// export envar
Expand Down Expand Up @@ -534,9 +547,12 @@ impl ImageBuilder for DiskImageBuilder {
// Let's use grub2-install to bless the disk

info!("Blessing disk image with MBR");
cmd_lib::run_cmd!(
grub2-install --target=i386-pc --boot-directory=$chroot/boot $ldp 2>&1;
)?;
std::process::Command::new("grub2-install")
.arg("--target=i386-pc")
.arg(format!("--boot-directory={}", chroot.join("boot").display()))
.arg(ldp)
.output()
.map_err(|e| color_eyre::eyre::eyre!("Failed to execute grub2-install: {}", e))?;
}

disk.unmount_from_chroot(chroot)?;
Expand Down Expand Up @@ -649,10 +665,14 @@ impl IsoBuilder {
dr_args.push(&dr_omit);
}

crate::run_cmd_prep_chroot!(root,
unshare -R $root env - DRACUT_SYSTEMD=0 dracut $[dr_args]
/boot/initramfs-$kver.img --kver $kver 2>&1;
)?;


crate::util::enter_chroot_run(root, || -> Result<()> {
cmd_lib::run_cmd!(
env - DRACUT_SYSTEMD=0 dracut $[dr_args] /boot/initramfs-$kver.img --kver $kver 2>&1;
)?;
Ok(())
})?;
Ok(())
}

Expand Down
8 changes: 6 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{builder::Bootloader, cli::OutputFormat, run_cmd_prep_chroot};
use crate::{builder::Bootloader, cli::OutputFormat, util::enter_chroot_run};
use bytesize::ByteSize;
use color_eyre::Result;
use serde::Deserialize;
Expand Down Expand Up @@ -843,7 +843,11 @@ impl Auth {

trace!(?args, "useradd args");

run_cmd_prep_chroot!(chroot, unshare -R $chroot useradd $[args] 2>&1)?;
enter_chroot_run(chroot, || {
info!(?self, "Adding user to chroot");
std::process::Command::new("useradd").args(&args).status()?;
Ok(())
})?;

// add ssh keys
if !self.ssh_keys.is_empty() {
Expand Down
14 changes: 11 additions & 3 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ macro_rules! run {
}};
}

/// Enters a chroot environment using `tiffin`, then runs the function
pub fn enter_chroot_run<F>(root: &Path, f: F) -> Result<()>
where
F: FnOnce() -> Result<()>,
{
tiffin::Container::new(root.to_path_buf()).run(f)?
}

/// Macro for string feature flags.
/// Should return the value of the environment variable if defined, else None.
///
Expand Down Expand Up @@ -62,7 +70,7 @@ macro_rules! chroot_run {
}

/// Preps chroot, then wraps around cmd_lib::run_cmd!
///
///
/// # ***DOES NOT ACTUALLY CHROOT INTO THE ENVIRONMENT!!! YOU NEED TO RUN `unshare -R` YOURSELF***
/// Example:
///
Expand All @@ -85,9 +93,9 @@ macro_rules! run_cmd_prep_chroot {
}

/// Preps chroot, then wraps around cmd_lib::run_fun!
///
///
/// # ***DOES NOT ACTUALLY CHROOT INTO THE ENVIRONMENT!!! YOU NEED TO RUN `unshare -R` YOURSELF***
///
///
#[macro_export]
macro_rules! prep_chroot_run_fun {
($chroot:expr, $($cmd:tt)*) => {{
Expand Down

0 comments on commit 411961c

Please sign in to comment.