Skip to content

Commit

Permalink
refactor: build chroot now done?
Browse files Browse the repository at this point in the history
  • Loading branch information
korewaChino committed Sep 29, 2023
1 parent 9b9b90a commit 2416e3c
Show file tree
Hide file tree
Showing 9 changed files with 350 additions and 19 deletions.
116 changes: 113 additions & 3 deletions Cargo.lock

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

21 changes: 17 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,30 @@
name = "katsu"
version = "0.1.0"
edition = "2021"

authors = [
"Fyra Labs",
"Pornpipat Popum <[email protected]>",
"Madomado <[email protected]>",
"Lleyton Gray <[email protected]>",
]
description = "A rusty tool to build OS images"
license = "MIT"
categories = ["system", "utilities"]
keywords = ["build", "image", "os", "linux", "chroot", "container"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
in-container = "1.1.0"
sudo = "0.6.0"
tracing = "0.1.37"
color-eyre = "0.6.2"
tracing-subscriber = { version = "0.3.16", features = ["env-filter", "tracing-log"] }
tracing-subscriber = { version = "0.3.16", features = [
"env-filter",
"tracing-log",
] }
serde_derive = "1.0.152"
serde = "1.0.152"
smartstring = { version = "1.0.1", features = ["serde"]}
smartstring = { version = "1.0.1", features = ["serde"] }
tracing-error = "0.2.0"
regex = { version = "1.8.4", features = ["unicode-case"] }
glob = "0.3.1"
Expand All @@ -23,4 +35,5 @@ dotenvy = "0.15.7"
gpt = "3.1.0"
block-utils = "0.11.1"
tera = "1"
merge-struct = "0.1.0"
merge-struct = "0.1.0"
clap = { version = "4.4.6", features = ["derive", "env"] }
67 changes: 58 additions & 9 deletions src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
use color_eyre::Result;
use serde_derive::{Deserialize, Serialize};
use std::{collections::BTreeMap, path::PathBuf};
use std::{collections::BTreeMap, fs, path::PathBuf};

use crate::{cli::OutputFormat, config::Manifest, util};
const WORKDIR: &str = "katsu-work";

pub enum Bootloader {
Limine,
Grub,
SystemdBoot,
}

pub trait RootBuilder {
fn build(&self, chroot: PathBuf) -> Result<()>;
}
#[derive(Deserialize, Debug, Clone, Serialize, Default)]

pub struct DnfRootBuilder {
#[serde(default)]
pub packages: Vec<String>,
Expand All @@ -38,7 +41,7 @@ impl RootBuilder for DnfRootBuilder {
options.push(format!("--forcearch={}", self.arch.as_ref().unwrap()));
}

// 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);
Expand All @@ -51,10 +54,14 @@ impl RootBuilder for DnfRootBuilder {
options.push(format!("--exclude={}", package));
}

cmd_lib::run_cmd!(
dnf -y --releasever=${releasever} --installroot=${chroot} $[packages] $[options];
dnf clean all --installroot=${chroot};
)?;
// todo: maybe not unwrap?
util::run_with_chroot(chroot.to_str().unwrap(), || -> color_eyre::Result<()> {
cmd_lib::run_cmd!(
dnf install -y --releasever=${releasever} --installroot=${chroot} $[packages] $[options];
dnf clean all --installroot=${chroot};
)?;
Ok(())
})??;

Ok(())
}
Expand Down Expand Up @@ -98,6 +105,7 @@ impl ImageBuilder for DeviceInstaller {

pub struct IsoBuilder {
pub bootloader: Bootloader,
pub root_builder: Box<dyn RootBuilder>,
}

impl IsoBuilder {
Expand All @@ -113,8 +121,49 @@ impl IsoBuilder {

impl ImageBuilder for IsoBuilder {
fn build(&self, chroot: PathBuf, image: PathBuf) -> Result<()> {
// Tree file?
todo!();
// Create workspace directory
self.root_builder.build(chroot)?;
Ok(())
}
}

// todo: proper builder struct

pub struct KatsuBuilder {
pub image_builder: Box<dyn ImageBuilder>,
pub manifest: Manifest,
}

impl KatsuBuilder {
pub fn new(manifest: Manifest, output_format: OutputFormat) -> Result<Self> {
let root_builder = match manifest.builder.as_str() {
"dnf" => Box::new(manifest.dnf.clone()) as Box<dyn RootBuilder>,
_ => todo!("builder not implemented"),
};

let image_builder = match output_format {
OutputFormat::Iso => {
Box::new(IsoBuilder { bootloader: Bootloader::Limine, root_builder })
as Box<dyn ImageBuilder>
},
_ => todo!(),
};

Ok(Self { image_builder, manifest })
}

pub fn build(&self) -> Result<()> {
let workdir = PathBuf::from(WORKDIR);
fs::create_dir_all(workdir.clone())?;

let chroot = workdir.join("chroot");
fs::create_dir_all(chroot.clone())?;

let image = workdir.join("image");
fs::create_dir_all(image.clone())?;

self.image_builder.build(chroot.canonicalize()?, image)?;

Ok(())
}
}
Loading

0 comments on commit 2416e3c

Please sign in to comment.