image creation
All checks were successful
ci/woodpecker/push/build Pipeline was successful

This commit is contained in:
JMARyA 2025-04-04 23:03:26 +02:00
parent 8d046e5852
commit 0ac815f9f4
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
4 changed files with 47 additions and 7 deletions

View file

@ -28,7 +28,8 @@ pub fn get_args() -> clap::ArgMatches {
command!()
.name("create-img")
.about("Create an install on a disk image for VMs or embedded devices")
.arg(arg!([config] "Config file").required(true)),
.arg(arg!([config] "Config file").required(true))
.arg(arg!([image] "Image file").required(true))
)
.get_matches()
}

View file

@ -120,3 +120,36 @@ pub fn setup_fstrim() {
print_status("Setting up FsTrim");
systemd_service_enable("fstrim.service");
}
pub fn setup_disk_image(img_file: &str) {
print_status(&format!("Allocating disk image"));
run_command(&["fallocate", "-l", "8G", img_file], None, false);
print_status(&format!("Partitioning disk image"));
run_command(
&[
"parted", img_file, "--script", "--", "mklabel", "gpt", "mkpart", "ESP", "fat32",
"1MiB", "1GiB", "set", "1", "esp", "on", "mkpart", "primary", "ext4", "1GiB", "100%",
],
None,
true,
);
print_status(&format!("Setting up disk image"));
let (stdout, _) = run_command(
&["losetup", "--show", "--find", "--partscan", img_file],
None,
false,
);
let loop_dev = stdout.trim();
let drive = DriveConfig {
boot: format!("{loop_dev}p1"),
root: format!("{loop_dev}p2"),
encryption: None,
};
format_drives(&drive);
mount_drives(&drive);
}

View file

@ -113,11 +113,14 @@ pub fn uncomment_tag(tag: &str, file: &str) {
}
/// Install a config on a new system
pub fn install(conf: InstallConfig) {
pub fn install(conf: InstallConfig, bare: bool) {
// Drive Setup
format_drives(&conf.drive);
mount_drives(&conf.drive);
install_mnt(conf, bare);
}
pub fn install_mnt(conf: InstallConfig, bare: bool) {
// Base Install
pacstrap(&conf.pkg);
genfstab();
@ -183,7 +186,9 @@ pub fn install(conf: InstallConfig) {
setup_fstrim();
setup_bootloader();
setup_mkinitcpio(&conf.drive);
setup_secure_boot();
if bare {
setup_secure_boot();
}
if conf.drive.encryption.is_some() {
setup_tpm_unlock(&conf.drive);

View file

@ -10,7 +10,7 @@ mod linux;
mod pkg;
mod print;
use create_iso::create_iso;
use install::{install, security::ensure_secure_boot};
use install::{drives::setup_disk_image, install, install_mnt, security::ensure_secure_boot};
use linux::is_root;
use print::print_config;
use yansi::{Color, Paint};
@ -58,9 +58,10 @@ fn main() {
Some(("create-img", install_args)) => {
let config_file: &String = install_args.get_one("config").unwrap();
let conf = read_conf(config_file);
let img_file: &String = install_args.get_one("image").unwrap();
println!("Installing to a disk image is not yet supported");
unimplemented!()
setup_disk_image(img_file.as_str());
install_mnt(conf, false);
}
Some(("install", install_args)) => {
if !is_root() {
@ -81,7 +82,7 @@ fn main() {
}
// Run the
install(conf);
install(conf, true);
}
_ => {}
}