Skip to content

Commit

Permalink
More descriptive macros, fix grub2-mkconfig call (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
korewaChino committed Oct 10, 2024
1 parent 5fdc1e6 commit a1d7496
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
10 changes: 5 additions & 5 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ impl RootBuilder for DnfRootBuilder {
options.append(&mut exclude.iter().map(|p| format!("--exclude={p}")).collect());

info!("Initializing system with dnf");
crate::chroot_run_cmd!(&chroot,
crate::run_cmd_prep_chroot!(&chroot,
$dnf install -y --releasever=$releasever --installroot=$chroot $[packages] $[options] 2>&1;
$dnf clean all --installroot=$chroot;
)?;
Expand All @@ -381,8 +381,8 @@ 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::chroot_run_cmd!(&chroot,
grub2-mkconfig -o /boot/grub2/grub.cfg;
let res = crate::run_cmd_prep_chroot!(&chroot,
unshare -R $chroot grub2-mkconfig -o /boot/grub2/grub.cfg;
);

if let Err(e) = res {
Expand Down Expand Up @@ -418,7 +418,7 @@ pub fn run_script(script: Script, chroot: &Path, is_post: bool) -> Result<()> {

if script.chroot.unwrap_or(is_post) {
just_write(chroot.join("tmp").join(&name), data)?;
crate::chroot_run_cmd!(chroot,
crate::run_cmd_prep_chroot!(chroot,
chmod +x $chroot/tmp/$name;
unshare -R $chroot /tmp/$name 2>&1;
rm -f $chroot/tmp/$name;
Expand Down Expand Up @@ -649,7 +649,7 @@ impl IsoBuilder {
dr_args.push(&dr_omit);
}

crate::chroot_run_cmd!(root,
crate::run_cmd_prep_chroot!(root,
unshare -R $root env - DRACUT_SYSTEMD=0 dracut $[dr_args]
/boot/initramfs-$kver.img --kver $kver 2>&1;
)?;
Expand Down
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{builder::Bootloader, chroot_run_cmd, cli::OutputFormat};
use crate::{builder::Bootloader, cli::OutputFormat, run_cmd_prep_chroot};
use bytesize::ByteSize;
use color_eyre::Result;
use serde::Deserialize;
Expand Down Expand Up @@ -843,7 +843,7 @@ impl Auth {

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

chroot_run_cmd!(chroot, unshare -R $chroot useradd $[args] 2>&1)?;
run_cmd_prep_chroot!(chroot, unshare -R $chroot useradd $[args] 2>&1)?;

// add ssh keys
if !self.ssh_keys.is_empty() {
Expand Down
17 changes: 13 additions & 4 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,20 @@ macro_rules! chroot_run {
}};
}

/// Wraps around cmd_lib::run_cmd!, but mounts the chroot
/// Preps chroot, then wraps around cmd_lib::run_cmd!
///
/// # ***DOES NOT ACTUALLY CHROOT INTO THE ENVIRONMENT!!! YOU NEED TO RUN `unshare -R` YOURSELF***
/// Example:
///
/// ```rs
/// chroot_run!(PathBuf::from("/path/to/chroot"), chroot /path/to/chroot echo "hello world" > /hello.txt);
/// ```
// The fact I misnamed this macro back in 2023 caused me so much trouble...
// I wasted 6 hours trying to debug this macro, only to find out it's not
// supposed to chroot into the environment for you.
// - @korewaChino, 2024-10-10
#[macro_export]
macro_rules! chroot_run_cmd {
macro_rules! run_cmd_prep_chroot {
($chroot:expr, $($cmd:tt)*) => {{
$crate::util::run_with_chroot(&PathBuf::from($chroot), || {
tracing::debug!("Running command: {}", stringify!($($cmd)*) );
Expand All @@ -78,9 +84,12 @@ macro_rules! chroot_run_cmd {
}};
}

/// Runs in chroot, returns stdout
/// 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! chroot_run_fun {
macro_rules! prep_chroot_run_fun {
($chroot:expr, $($cmd:tt)*) => {{
$crate::util::run_with_chroot(&PathBuf::from($chroot), || {
cmd_lib::run_fun!($($cmd)*)?;
Expand Down
6 changes: 3 additions & 3 deletions tests/ng/katsu-bios.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ scripts:
# Generate GRUB configuration
# While Katsu tries to actually generate one for you before running scripts,
# That process is not perfect and usually does not actually write the configuration to the disk at least for now.
- id: grub-install
name: Install GRUB
file: modules/scripts/grub-confgen.sh
# - id: grub-install
# name: Install GRUB
# file: modules/scripts/grub-confgen.sh



Expand Down

0 comments on commit a1d7496

Please sign in to comment.