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 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<String>,
/// Boot Drive Path
pub boot: String,
pub boot: Option<String>,
/// Root Drive Path
pub root: String,
pub root: Option<String>,
/// Enable encryption on root
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)]
pub struct GeneralConfig {
/// Presets

View file

@ -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<String> {
.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,
};

View file

@ -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");

View file

@ -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);

View file

@ -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("<PASSPHRASE>", conf.encryption.as_ref().unwrap())
.replace("<ROOT>", &conf.root),
.replace("<ROOT>", &conf.root.as_ref().unwrap()),
0o644,
);
systemd_service_enable("tpm-enroll.service");

View file

@ -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)
));