Skip to content

Commit

Permalink
more refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
madonuko committed Oct 1, 2023
1 parent 682d831 commit 9a60962
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 119 deletions.
15 changes: 6 additions & 9 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,19 @@ impl RootBuilder for DnfRootBuilder {
let exclude = self.exclude.clone();
let releasever = &self.releasever;

if self.arch.is_some() {
options.push(format!("--forcearch={}", self.arch.as_ref().unwrap()));
if let Some(a) = &self.arch {
options.push(format!("--forcearch={a}"));
}

// Get host architecture using uname
// Get host architecture using uname
let host_arch = cmd_lib::run_fun!(uname -m;)?;

let arch_string = self.arch.as_ref().unwrap_or(&host_arch);

if self.arch_packages.contains_key(arch_string) {
packages.append(&mut self.arch_packages.get(arch_string).unwrap().clone());
}

for package in exclude {
options.push(format!("--exclude={}", package));
if let Some(pkg) = self.arch_packages.get(arch_string) {
packages.append(&mut pkg.clone());
}
options.append(&mut exclude.iter().map(|p| format!("--exclude={p}")).collect());

cmd_lib::run_cmd!(
dnf -y --releasever=${releasever} --installroot=${chroot} $[packages] $[options];
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ fn test_recurse() {

let manifest = Manifest::load_all(PathBuf::from("tests/ng/recurse/manifest.yaml")).unwrap();

println!("{:#?}", manifest);
println!("{manifest:#?}");

// let ass: Manifest = Manifest { import: vec!["recurse1.yaml", "recurse2.yaml"], distro: Some("RecursiveOS"), out_file: None, dnf: (), scripts: () }
}
192 changes: 83 additions & 109 deletions src/creator.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use color_eyre::{eyre::eyre, Help, Result};
use std::{
fs,
io::Write,
path::{Path, PathBuf},
};
use std::{io::Write, path::Path};
use tracing::{debug, error, info, instrument, trace, warn};

use crate::{
Expand Down Expand Up @@ -430,139 +426,120 @@ pub trait ImageCreator {
fn prep_disk(&self) -> Result<()> {
let cfg = self.get_cfg();
if let Some(layout) = &cfg.disk {
// Now let's create a disk file called {out}.raw
let out_file = format!("{}.raw", cfg.out);
let layout = cfg.disk.as_ref().ok_or_else(|| eyre!("No disk layout specified"))?;
// Now let's create a disk file called {out}.raw
let out_file = format!("{}.raw", cfg.out);
// Create the disk file
// Create the disk file
let disk_size = &layout.disk_size;
info!(out_file, "Creating disk file");
let disk_size = &layout.disk_size;
info!(out_file, "Creating disk file");
cmd_lib::run_cmd!(
truncate -s $disk_size $out_file;
)?;
cmd_lib::run_cmd!(
truncate -s $disk_size $out_file;
)?;
// Mount disk image to loop device, and return the loop device name
// Mount disk image to loop device, and return the loop device name
info!("Mounting disk image to loop device");
info!("Mounting disk image to loop device");
let loop_dev = cmd_lib::run_fun!(losetup -f;)?;
debug!("Found loop device: {loop_dev:?}");
// The reason we run this command instead of just losetup -f is
// because rustfmt messes up the formatting of the command
let loop_dev = cmd_lib::run_fun!(bash -c "losetup -f")?;
cmd_lib::run_cmd!(losetup $loop_dev $out_file --show;)?;
debug!("Found loop device: {loop_dev:?}");
info!("Partitioning disk");
// Create partition table, GPT
cmd_lib::run_cmd!(parted -s $loop_dev mklabel gpt;)?;
cmd_lib::run_cmd!(
losetup $loop_dev $out_file --show;
)?;
// number to track partition number
let mut part_num = 1;
let mut efi_num: Option<i32> = None;
let boot_num: i32;
let root_num: i32;
// Partition disk
info!("Partitioning disk");
// Create partition table, GPT
if layout.bootloader {
// create EFI partition with ESP flag for the first 250MiB
// label it as EFI
cmd_lib::run_cmd!(
parted -s $loop_dev mklabel gpt;
parted -s $loop_dev mkpart primary fat32 1MiB 250MiB;
parted -s $loop_dev set $part_num esp on;
parted -s $loop_dev name $part_num EFI;
)?;
// number to track partition number
let mut part_num = 1;
let mut efi_num: Option<i32> = None;
let boot_num: i32;
let root_num: i32;
if layout.bootloader {
// create EFI partition with ESP flag for the first 250MiB
// label it as EFI
cmd_lib::run_cmd!(
parted -s $loop_dev mkpart primary fat32 1MiB 250MiB;
parted -s $loop_dev set $part_num esp on;
parted -s $loop_dev name $part_num EFI;
)?;
// debug lsblk
// debug lsblk
cmd_lib::run_cmd!(
lsblk;
partprobe $loop_dev;
ls -l /dev;
)?;
cmd_lib::run_cmd!(
lsblk;
partprobe $loop_dev;
ls -l /dev;
)?;
// format EFI partition
// format EFI partition
cmd_lib::run_cmd!(mkfs.fat -F32 ${loop_dev}p$part_num -n EFI;)?;
efi_num = Some(part_num);
cmd_lib::run_cmd!(
mkfs.fat -F32 ${loop_dev}p$part_num -n EFI 2>&1;
)?;
efi_num = Some(part_num);
// increment partition number
part_num += 1;
}
// increment partition number
part_num += 1;
}
// create boot partition for installing kernels with the next 1GiB
// label as BOOT
// ext4
cmd_lib::run_cmd!(
parted -s $loop_dev mkpart primary ext4 250MiB 1.25GiB;
parted -s $loop_dev name $part_num BOOT;
)?;
// create boot partition for installing kernels with the next 1GiB
// label as BOOT
// ext4
cmd_lib::run_cmd!(
parted -s $loop_dev mkpart primary ext4 250MiB 1.25GiB;
parted -s $loop_dev name $part_num BOOT;
)?;
cmd_lib::run_cmd!(
mkfs.ext4 -F ${loop_dev}p$part_num -L BOOT;
)?;
cmd_lib::run_cmd!(
mkfs.ext4 -F ${loop_dev}p$part_num -L BOOT;
)?;
boot_num = part_num;
boot_num = part_num;
part_num += 1;
part_num += 1;
// Create blank partition with the rest of the free space
// Create blank partition with the rest of the free space
let volid = &cfg.volid;
cmd_lib::run_cmd!(
parted -s $loop_dev mkpart primary ext4 1.25GiB 100%;
parted -s $loop_dev name $part_num $volid;
)?;
let volid = &cfg.volid;
cmd_lib::run_cmd!(
parted -s $loop_dev mkpart primary ext4 1.25GiB 100%;
parted -s $loop_dev name $part_num $volid;
)?;
root_num = part_num;
root_num = part_num;
// now format the partition
// now format the partition
let root_format = &layout.root_format;
let root_format = &layout.root_format;
cmd_lib::run_cmd!(
mkfs.${root_format} ${loop_dev}p$part_num -L $volid;
)?;
cmd_lib::run_cmd!(
mkfs.${root_format} ${loop_dev}p$part_num -L $volid;
)?;
// Now, mount them all
// Now, mount them all
info!("Mounting partitions");
info!("Mounting partitions");
let instroot = &cfg.instroot.to_str().unwrap_or_default();
let instroot = &cfg.instroot.to_str().unwrap_or_default();
cmd_lib::run_cmd!(
mkdir -p $instroot;
mount ${loop_dev}p$root_num $instroot;
mkdir -p $instroot/boot;
mount ${loop_dev}p$boot_num $instroot/boot;
)?;
if layout.bootloader {
let efi_num = efi_num.unwrap();
cmd_lib::run_cmd!(
mkdir -p $instroot;
mount ${loop_dev}p$root_num $instroot;
mkdir -p $instroot/boot;
mount ${loop_dev}p$boot_num $instroot/boot;
mkdir -p $instroot/boot/efi;
mount ${loop_dev}p$efi_num $instroot/boot/efi;
)?;
if layout.bootloader {
let efi_num = efi_num.unwrap();
cmd_lib::run_cmd!(
mkdir -p $instroot/boot/efi;
mount ${loop_dev}p$efi_num $instroot/boot/efi;
)?;
}
Ok(())
} else {
// error out
return Err(eyre!("No disk layout specified"));
}
Ok(())
}
#[instrument(skip(self))]
Expand Down Expand Up @@ -610,9 +587,9 @@ pub trait ImageCreator {
// }
let mut extra_args = vec![];
if cfg.arch.is_some() {
if let Some(arch) = &cfg.arch {
extra_args.push("--forcearch");
extra_args.push(cfg.arch.as_ref().unwrap());
extra_args.push(arch);
}
prepare_chroot(root).unwrap_or_else(|e| {
error!(?e, "Failed to prepare chroot");
Expand All @@ -624,10 +601,7 @@ pub trait ImageCreator {
)
.unwrap_or_else(|e| {
error!(?e, "Failed to install packages");
unmount_chroot(root).unwrap_or_else(|e| {
error!(?e, "Failed to unmount chroot");
std::process::exit(1);
});
unmount_chroot(root).unwrap_or_else(|e| error!(?e, "Failed to unmount chroot"));
std::process::exit(1);
});
unmount_chroot(root)?;
Expand Down

0 comments on commit 9a60962

Please sign in to comment.