Skip to content

Commit

Permalink
refactor: docs and stuff and dead-code round 2
Browse files Browse the repository at this point in the history
  • Loading branch information
madonuko committed Jul 19, 2024
1 parent 56746c9 commit b6df6a6
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 157 deletions.
26 changes: 22 additions & 4 deletions Cargo.lock

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

28 changes: 14 additions & 14 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,46 @@ keywords = ["build", "image", "os", "linux", "chroot", "container"]

[dependencies]
sudo = "0.6.0"
tracing = "0.1.37"
color-eyre = "0.6.2"
tracing-subscriber = { version = "0.3.16", features = [
tracing = "0.1.40"
color-eyre = "0.6.3"
tracing-subscriber = { version = "0.3.18", features = [
"env-filter",
"tracing-log",
] }
serde_derive = "1"
serde = "1.0.152"
serde = "1.0.204"
tracing-error = "0.2"
glob = "0.3"
serde_yaml = "0.9"
dotenvy = "0.15"
tera = "1"
merge-struct = "0.1.0"
clap = { version = "4.4", features = ["derive", "env"] }
nix = { version = "0.27", features = ["mount", "hostname", "dir"] }
uuid = { version = "1.4.1", features = ["v4", "serde"] }
clap = { version = "4.5", features = ["derive", "env"] }
nix = { version = "0.29", features = ["mount", "hostname", "dir"] }
uuid = { version = "1.10.0", features = ["v4", "serde"] }
loopdev-fyra = { version = "0.5.0" }
bytesize = { version = "1.3.0", features = ["serde"] }
indexmap = "2.2.6"
ensan = "0.2.1"
hcl-rs = "0.18.0"
const_format = "0.2.32"
tiffin = "0.3.0"
tiffin = "0.3.1"
tempfile = "3"
lazy_format = "2.0.3"
itertools = "0.13.0"
sys-mount = "3.0.1"
thiserror = "1.0.61"
thiserror = "1.0.63"
ensan-proc-macro = "0.1.0"
passwd-rs = "0.2.0"
serde_json = "1.0.120"
dyn-clone = "1.0.17"

[lints.clippy]
cargo = "warn"
complexity = "warn"
nursery = "warn"
pedantic = "warn"
suspicious = "warn"
cargo = { level = "warn", priority = -1 }
complexity = { level = "warn", priority = -1 }
nursery = { level = "warn", priority = -1 }
pedantic = { level = "warn", priority = -1 }
suspicious = { level = "warn", priority = -1 }

disallowed-macros = "deny"
excessive-nesting = "warn"
Expand Down
48 changes: 31 additions & 17 deletions src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::module_name_repetitions)]
use crate::{
bail_let,
cfg::{
Expand All @@ -23,10 +24,9 @@ use std::{
use tracing::{debug, info, trace, warn};

const WORKDIR: &str = "katsu-work";
crate::prepend_comment!(GRUB_PREPEND_COMMENT: "/boot/grub/grub.cfg", "Grub configurations", katsu::builder::Bootloader::cp_grub);
crate::prepend_comment!(LIMINE_PREPEND_COMMENT: "/boot/limine.cfg", "Limine configurations", katsu::builder::Bootloader::cp_limine);

pub trait RootBuilder {
#[allow(clippy::missing_errors_doc)]
fn build(&self, chroot: &Path, manifest: &Manifest) -> Result<()>;
}

Expand Down Expand Up @@ -164,7 +164,7 @@ pub fn run_scripts(
}

// Find needs
let id = scr.id.clone().unwrap_or("<NULL>".into());
let id = scr.id.as_deref().unwrap_or("<NULL>").to_owned();
let mut needs = IndexMap::new();
let scr_needs_vec = &scr.needs.clone();
for need in scr_needs_vec {
Expand All @@ -191,6 +191,7 @@ pub fn run_scripts(
}

pub trait ImageBuilder {
#[allow(clippy::missing_errors_doc)]
fn build(
&self, chroot: &Path, image: &Path, manifest: &Manifest, skip_phases: &SkipPhases,
) -> Result<()>;
Expand Down Expand Up @@ -291,7 +292,12 @@ const KATSU_DRACUT_OMIT: &str = "";
const KATSU_DRACUT_ARGS: &str = "--xz --no-early-microcode";

impl IsoBuilder {
fn dracut(&self, root: &Path) -> Result<()> {
/// Install dracut.
///
/// # Errors
///
/// This function will return an error if `dracut` fails.
fn dracut(root: &Path) -> Result<()> {
info!(?root, "Generating initramfs");
bail_let!(
Some(kver) = fs::read_dir(root.join("boot"))?.find_map(|f| {
Expand All @@ -305,10 +311,7 @@ impl IsoBuilder {
return None;
}
debug!(?kver, "Kernel version");
Some(kver.to_string())
// Some(
// f.file_name().to_str()?.rsplit_once('/')?.1.strip_prefix("initramfs-")?.strip_suffix(".img")?.to_string()
// )
Some(kver.to_owned())
})
}) => "Can't find initramfs in /boot."
);
Expand Down Expand Up @@ -355,7 +358,7 @@ impl IsoBuilder {
// Extra configurable options, for now we use envars
// todo: document these

let sqfs_comp = env_flag!("KATSU_SQUASHFS_ARGS").unwrap_or_else(|| "zstd".to_string());
let sqfs_comp = env_flag!("KATSU_SQUASHFS_ARGS").unwrap_or_else(|| "zstd".to_owned());

info!("Determining squashfs options");

Expand All @@ -371,8 +374,8 @@ impl IsoBuilder {
.split(' ')
.collect::<Vec<_>>();

let binding = env_flag!("KATSU_SQUASHFS_ARGS").unwrap_or_default();
let sqfs_extra_args = binding.split(' ').collect::<Vec<_>>();
let sqfs_extra_args = env_flag!("KATSU_SQUASHFS_ARGS").unwrap_or_default();
let sqfs_extra_args = sqfs_extra_args.split(' ').collect::<Vec<_>>();

info!("Squashing file system (mksquashfs)");
let res = cmd!(
Expand All @@ -393,11 +396,20 @@ impl IsoBuilder {
.note(format!("sqfs_extra_args: {sqfs_extra_args:?}"))
})
}
/// # Errors
/// - fail to run `mkfs.erofs`
#[allow(dead_code)]
pub fn erofs(&self, chroot: &Path, image: &Path) -> Result<()> {
cmd!(? "mkfs.erofs" "-d" {{chroot.display()}} "-o" {{image.display()}})
}
/// # Errors
/// - fail to run `xorriso`
///
/// # Panics
/// - fail to parse `chroot` path (not UTF-8)
/// - `chroot` has no parent dir
// TODO: add mac support
#[allow(clippy::unwrap_in_result)]
pub fn xorriso(&self, chroot: &Path, image: &Path, manifest: &Manifest) -> Result<()> {
info!("Generating ISO image");
let volid = manifest.get_volid();
Expand All @@ -424,7 +436,7 @@ impl IsoBuilder {
// Hybrid mode is only supported on x86_64
"x86_64" => vec!["--grub2-mbr", grub2_mbr_hybrid.to_str().unwrap()],
"aarch64" => vec![],
_ => unimplemented!(),
_ => unreachable!(),
};

// todo: move to partition::Xorriso and Iso9660Table
Expand Down Expand Up @@ -473,6 +485,7 @@ impl IsoBuilder {
const ISO_TREE: &str = "iso-tree";

impl ImageBuilder for IsoBuilder {
#![allow(clippy::unwrap_in_result)]
fn build(
&self, chroot: &Path, _: &Path, manifest: &Manifest, skip_phases: &SkipPhases,
) -> Result<()> {
Expand All @@ -488,7 +501,7 @@ impl ImageBuilder for IsoBuilder {
phase!("root": self.root_builder.build(chroot, manifest));
// self.root_builder.build(chroot.canonicalize()?.as_path(), manifest)?;

phase!("dracut": self.dracut(chroot));
phase!("dracut": Self::dracut(chroot));

// temporarily store content of iso
let image_dir = workspace.join(ISO_TREE).join("LiveOS");
Expand Down Expand Up @@ -522,9 +535,7 @@ pub struct KatsuBuilder {
}

impl KatsuBuilder {
pub fn new(
manifest: Manifest, output_format: OutputFormat, skip_phases: SkipPhases,
) -> Result<Self> {
pub fn new(manifest: Manifest, output_format: OutputFormat, skip_phases: SkipPhases) -> Self {
let root_builder = manifest.builder.clone().into();

let bootloader = manifest.bootloader.clone();
Expand All @@ -544,9 +555,12 @@ impl KatsuBuilder {
_ => todo!(),
};

Ok(Self { image_builder, manifest, skip_phases })
Self { image_builder, manifest, skip_phases }
}

/// # Errors
/// - IO-errors
/// - `image_builder` failure
pub fn build(&self) -> Result<()> {
let workdir = PathBuf::from(WORKDIR);

Expand Down
Loading

0 comments on commit b6df6a6

Please sign in to comment.