From b352a7bc43f71c9beea82f78752ea5463659c0d6 Mon Sep 17 00:00:00 2001 From: JMARyA Date: Wed, 9 Apr 2025 13:52:33 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20entire=20disk?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config.rs | 20 +++++++++++++++-- src/install/drives.rs | 49 ++++++++++++++++++++++++++++++++--------- src/install/kernel.rs | 8 +++++-- src/install/mod.rs | 8 ++++++- src/install/security.rs | 6 ++--- src/print.rs | 4 ++-- 6 files changed, 75 insertions(+), 20 deletions(-) diff --git a/src/config.rs b/src/config.rs index 95372c2..1854a0f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,6 +2,8 @@ use std::fmt::Display; use serde::Deserialize; +use crate::install::drives::partition_disk; + /// Declarative install configuration #[derive(Debug, Clone, Deserialize)] pub struct InstallConfig { @@ -61,14 +63,28 @@ pub struct PackageConfig { #[derive(Debug, Clone, Deserialize)] pub struct DriveConfig { + /// Disk Path (using entire disk) + pub disk: Option, /// Boot Drive Path - pub boot: String, + pub boot: Option, /// Root Drive Path - pub root: String, + pub root: Option, /// Enable encryption on root pub encryption: Option, } +impl DriveConfig { + pub fn use_entire_disk(&mut self) { + if self.disk.is_some() { + partition_disk(&self.disk.as_ref().unwrap()); + + // TODO : assign two children to root / boot the right way + self.boot = Some(format!("{}1", self.disk.as_ref().unwrap())); + self.root = Some(format!("{}2", self.disk.as_ref().unwrap())); + } + } +} + #[derive(Debug, Clone, Deserialize)] pub struct GeneralConfig { /// Presets diff --git a/src/install/drives.rs b/src/install/drives.rs index 99838dd..85ce385 100644 --- a/src/install/drives.rs +++ b/src/install/drives.rs @@ -10,29 +10,52 @@ use crate::{ /// Format the drives with the given config pub fn format_drives(conf: &DriveConfig) { - disk_safe_check(&conf.root); - disk_safe_check(&conf.root); + disk_safe_check(&conf.root.as_ref().unwrap()); + disk_safe_check(&conf.root.as_ref().unwrap()); // EFI (BOOT) - run_command(&["mkfs.vfat", "-F", "32", conf.boot.as_str()], None, false); + run_command( + &[ + "mkfs.vfat", + "-F", + "32", + conf.boot.as_ref().unwrap().as_str(), + ], + None, + false, + ); // ROOT if let Some(pass) = &conf.encryption { run_command( - &["cryptsetup", "-q", "luksFormat", conf.root.as_str()], + &[ + "cryptsetup", + "-q", + "luksFormat", + conf.root.as_ref().unwrap().as_str(), + ], Some(&format!("{pass}\n")), false, ); run_command( - &["cryptsetup", "open", conf.root.as_str(), "root"], + &[ + "cryptsetup", + "open", + conf.root.as_ref().unwrap().as_str(), + "root", + ], Some(&format!("{pass}\n")), false, ); run_command(&["mkfs.ext4", "/dev/mapper/root"], None, false); } else { - run_command(&["mkfs.ext4", conf.root.as_str()], None, false); + run_command( + &["mkfs.ext4", conf.root.as_ref().unwrap().as_str()], + None, + false, + ); } } @@ -43,14 +66,18 @@ pub fn mount_drives(conf: &DriveConfig) { if conf.encryption.is_some() { run_command(&["mount", "/dev/mapper/root", "/mnt"], None, false); } else { - run_command(&["mount", conf.root.as_str(), "/mnt"], None, false); + run_command( + &["mount", conf.root.as_ref().unwrap().as_str(), "/mnt"], + None, + false, + ); } run_command( &[ "mount", "--mkdir", - conf.boot.as_str(), + conf.boot.as_ref().unwrap().as_str(), "/mnt/boot", "-o", "rw,nosuid,nodev,noatime,fmask=0137,dmask=0027", @@ -98,6 +125,7 @@ pub fn has_filesystem(dev: &str) -> Option { .unwrap() .iter() .find(|x| { + // TODO : go through all children too x.as_object() .unwrap() .get("name") @@ -145,8 +173,9 @@ pub fn setup_disk_image(img_file: &str) { let loop_dev = stdout.trim(); let drive = DriveConfig { - boot: format!("{loop_dev}p1"), - root: format!("{loop_dev}p2"), + disk: None, + boot: Some(format!("{loop_dev}p1")), + root: Some(format!("{loop_dev}p2")), encryption: None, }; diff --git a/src/install/kernel.rs b/src/install/kernel.rs index 601ee0f..e31084d 100644 --- a/src/install/kernel.rs +++ b/src/install/kernel.rs @@ -22,12 +22,16 @@ pub fn setup_mkinitcpio(conf: &DriveConfig) { std::fs::create_dir_all("/mnt/etc/kernel").unwrap(); if conf.encryption.is_some() { - let block_uuid = find_uuid_by_dev(&conf.root).unwrap(); + let block_uuid = find_uuid_by_dev(&conf.root.as_ref().unwrap()).unwrap(); std::fs::write("/mnt/etc/kernel/cmdline", format!("rd.luks.options=timeout=30s,discard rd.luks.name={block_uuid}=root root=/dev/mapper/root rw") ).unwrap(); } else { - std::fs::write("/mnt/etc/kernel/cmdline", format!("root={}", conf.root)).unwrap(); + std::fs::write( + "/mnt/etc/kernel/cmdline", + format!("root={}", conf.root.as_ref().unwrap()), + ) + .unwrap(); } print_status("Writing /etc/mkinitcpio.conf"); diff --git a/src/install/mod.rs b/src/install/mod.rs index d9f8980..fa406bd 100644 --- a/src/install/mod.rs +++ b/src/install/mod.rs @@ -113,7 +113,13 @@ pub fn uncomment_tag(tag: &str, file: &str) { } /// Install a config on a new system -pub fn install(conf: InstallConfig, bare: bool) { +pub fn install(mut conf: InstallConfig, bare: bool) { + if conf.drive.disk.is_some() && (conf.drive.boot.is_some() || conf.drive.root.is_some()) { + println!("The `drive` option cannot be used with `root` / `boot` options") + } + + conf.drive.use_entire_disk(); + // Drive Setup format_drives(&conf.drive); mount_drives(&conf.drive); diff --git a/src/install/security.rs b/src/install/security.rs index ef88881..b4da29b 100644 --- a/src/install/security.rs +++ b/src/install/security.rs @@ -4,7 +4,7 @@ use yansi::{Color, Paint}; use crate::{ config::DriveConfig, - linux::{arch_chroot, install_file, run_command, run_command_noerr, systemd_service_enable}, + linux::{arch_chroot, install_file, run_command_noerr, systemd_service_enable}, pkg::install_pkgs, }; @@ -28,7 +28,7 @@ pub fn setup_tpm_unlock(conf: &DriveConfig) { .as_str(), "systemd-cryptenroll", "--recovery-key", - &conf.root, + &conf.root.as_ref().unwrap(), ], None, false, @@ -41,7 +41,7 @@ pub fn setup_tpm_unlock(conf: &DriveConfig) { "/mnt/etc/systemd/system/tpm-enroll.service", &include_str!("../root/tpm-enroll.service") .replace("", conf.encryption.as_ref().unwrap()) - .replace("", &conf.root), + .replace("", &conf.root.as_ref().unwrap()), 0o644, ); systemd_service_enable("tpm-enroll.service"); diff --git a/src/print.rs b/src/print.rs index 1719aa1..549594f 100644 --- a/src/print.rs +++ b/src/print.rs @@ -10,7 +10,7 @@ pub fn print_config(conf: &InstallConfig) { let mut drive_info = Tree::new(); drive_info.add_str(format!( "💾 {} {}", - conf.drive.boot.paint(Color::Red), + conf.drive.boot.as_ref().unwrap().paint(Color::Red), "[EFI]".paint(Color::Blue) )); drive_info.add_str(format!( @@ -20,7 +20,7 @@ pub fn print_config(conf: &InstallConfig) { } else { "💾" }, - conf.drive.root.paint(Color::Red), + conf.drive.root.as_ref().unwrap().paint(Color::Red), "[ROOT]".paint(Color::Blue) ));