entire disk
All checks were successful
ci/woodpecker/push/build Pipeline was successful

This commit is contained in:
JMARyA 2025-04-09 13:52:33 +02:00
parent 57e06dcc99
commit b352a7bc43
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
6 changed files with 75 additions and 20 deletions

View file

@ -2,6 +2,8 @@ use std::fmt::Display;
use serde::Deserialize; use serde::Deserialize;
use crate::install::drives::partition_disk;
/// Declarative install configuration /// Declarative install configuration
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
pub struct InstallConfig { pub struct InstallConfig {
@ -61,14 +63,28 @@ pub struct PackageConfig {
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
pub struct DriveConfig { pub struct DriveConfig {
/// Disk Path (using entire disk)
pub disk: Option<String>,
/// Boot Drive Path /// Boot Drive Path
pub boot: String, pub boot: Option<String>,
/// Root Drive Path /// Root Drive Path
pub root: String, pub root: Option<String>,
/// Enable encryption on root /// Enable encryption on root
pub encryption: Option<String>, pub encryption: Option<String>,
} }
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)] #[derive(Debug, Clone, Deserialize)]
pub struct GeneralConfig { pub struct GeneralConfig {
/// Presets /// Presets

View file

@ -10,29 +10,52 @@ use crate::{
/// Format the drives with the given config /// Format the drives with the given config
pub fn format_drives(conf: &DriveConfig) { pub fn format_drives(conf: &DriveConfig) {
disk_safe_check(&conf.root); disk_safe_check(&conf.root.as_ref().unwrap());
disk_safe_check(&conf.root); disk_safe_check(&conf.root.as_ref().unwrap());
// EFI (BOOT) // 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 // ROOT
if let Some(pass) = &conf.encryption { if let Some(pass) = &conf.encryption {
run_command( run_command(
&["cryptsetup", "-q", "luksFormat", conf.root.as_str()], &[
"cryptsetup",
"-q",
"luksFormat",
conf.root.as_ref().unwrap().as_str(),
],
Some(&format!("{pass}\n")), Some(&format!("{pass}\n")),
false, false,
); );
run_command( run_command(
&["cryptsetup", "open", conf.root.as_str(), "root"], &[
"cryptsetup",
"open",
conf.root.as_ref().unwrap().as_str(),
"root",
],
Some(&format!("{pass}\n")), Some(&format!("{pass}\n")),
false, false,
); );
run_command(&["mkfs.ext4", "/dev/mapper/root"], None, false); run_command(&["mkfs.ext4", "/dev/mapper/root"], None, false);
} else { } 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() { if conf.encryption.is_some() {
run_command(&["mount", "/dev/mapper/root", "/mnt"], None, false); run_command(&["mount", "/dev/mapper/root", "/mnt"], None, false);
} else { } 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( run_command(
&[ &[
"mount", "mount",
"--mkdir", "--mkdir",
conf.boot.as_str(), conf.boot.as_ref().unwrap().as_str(),
"/mnt/boot", "/mnt/boot",
"-o", "-o",
"rw,nosuid,nodev,noatime,fmask=0137,dmask=0027", "rw,nosuid,nodev,noatime,fmask=0137,dmask=0027",
@ -98,6 +125,7 @@ pub fn has_filesystem(dev: &str) -> Option<String> {
.unwrap() .unwrap()
.iter() .iter()
.find(|x| { .find(|x| {
// TODO : go through all children too
x.as_object() x.as_object()
.unwrap() .unwrap()
.get("name") .get("name")
@ -145,8 +173,9 @@ pub fn setup_disk_image(img_file: &str) {
let loop_dev = stdout.trim(); let loop_dev = stdout.trim();
let drive = DriveConfig { let drive = DriveConfig {
boot: format!("{loop_dev}p1"), disk: None,
root: format!("{loop_dev}p2"), boot: Some(format!("{loop_dev}p1")),
root: Some(format!("{loop_dev}p2")),
encryption: None, encryption: None,
}; };

View file

@ -22,12 +22,16 @@ pub fn setup_mkinitcpio(conf: &DriveConfig) {
std::fs::create_dir_all("/mnt/etc/kernel").unwrap(); std::fs::create_dir_all("/mnt/etc/kernel").unwrap();
if conf.encryption.is_some() { 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", 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") format!("rd.luks.options=timeout=30s,discard rd.luks.name={block_uuid}=root root=/dev/mapper/root rw")
).unwrap(); ).unwrap();
} else { } 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"); print_status("Writing /etc/mkinitcpio.conf");

View file

@ -113,7 +113,13 @@ pub fn uncomment_tag(tag: &str, file: &str) {
} }
/// Install a config on a new system /// 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 // Drive Setup
format_drives(&conf.drive); format_drives(&conf.drive);
mount_drives(&conf.drive); mount_drives(&conf.drive);

View file

@ -4,7 +4,7 @@ use yansi::{Color, Paint};
use crate::{ use crate::{
config::DriveConfig, 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, pkg::install_pkgs,
}; };
@ -28,7 +28,7 @@ pub fn setup_tpm_unlock(conf: &DriveConfig) {
.as_str(), .as_str(),
"systemd-cryptenroll", "systemd-cryptenroll",
"--recovery-key", "--recovery-key",
&conf.root, &conf.root.as_ref().unwrap(),
], ],
None, None,
false, false,
@ -41,7 +41,7 @@ pub fn setup_tpm_unlock(conf: &DriveConfig) {
"/mnt/etc/systemd/system/tpm-enroll.service", "/mnt/etc/systemd/system/tpm-enroll.service",
&include_str!("../root/tpm-enroll.service") &include_str!("../root/tpm-enroll.service")
.replace("<PASSPHRASE>", conf.encryption.as_ref().unwrap()) .replace("<PASSPHRASE>", conf.encryption.as_ref().unwrap())
.replace("<ROOT>", &conf.root), .replace("<ROOT>", &conf.root.as_ref().unwrap()),
0o644, 0o644,
); );
systemd_service_enable("tpm-enroll.service"); systemd_service_enable("tpm-enroll.service");

View file

@ -10,7 +10,7 @@ pub fn print_config(conf: &InstallConfig) {
let mut drive_info = Tree::new(); let mut drive_info = Tree::new();
drive_info.add_str(format!( drive_info.add_str(format!(
"💾 {} {}", "💾 {} {}",
conf.drive.boot.paint(Color::Red), conf.drive.boot.as_ref().unwrap().paint(Color::Red),
"[EFI]".paint(Color::Blue) "[EFI]".paint(Color::Blue)
)); ));
drive_info.add_str(format!( drive_info.add_str(format!(
@ -20,7 +20,7 @@ pub fn print_config(conf: &InstallConfig) {
} else { } else {
"💾" "💾"
}, },
conf.drive.root.paint(Color::Red), conf.drive.root.as_ref().unwrap().paint(Color::Red),
"[ROOT]".paint(Color::Blue) "[ROOT]".paint(Color::Blue)
)); ));