From 0ac815f9f4327b7d4c24cacff7d060d917321cff Mon Sep 17 00:00:00 2001 From: JMARyA Date: Fri, 4 Apr 2025 23:03:26 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20image=20creation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/args.rs | 3 ++- src/install/drives.rs | 33 +++++++++++++++++++++++++++++++++ src/install/mod.rs | 9 +++++++-- src/main.rs | 9 +++++---- 4 files changed, 47 insertions(+), 7 deletions(-) diff --git a/src/args.rs b/src/args.rs index b61c4a2..ee015cb 100644 --- a/src/args.rs +++ b/src/args.rs @@ -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() } diff --git a/src/install/drives.rs b/src/install/drives.rs index deb8019..99838dd 100644 --- a/src/install/drives.rs +++ b/src/install/drives.rs @@ -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); +} diff --git a/src/install/mod.rs b/src/install/mod.rs index d85a94c..0d1312f 100644 --- a/src/install/mod.rs +++ b/src/install/mod.rs @@ -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); diff --git a/src/main.rs b/src/main.rs index d3b44e2..d90dc42 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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); } _ => {} }