use super::boot::BootMode;
use super::cmd;
use super::error::StorageError;
use super::partition::PartitionInfo;
use std::path::Path;
pub const MOSBOOT_LABEL: &str = "MOSBOOT";
pub const MOSDATA_LABEL: &str = "mosdata";
pub fn format_partitions(
partitions: &PartitionInfo,
boot_mode: BootMode,
) -> Result<(), StorageError> {
if boot_mode.is_efi() {
format_esp(&partitions.boot.path)?;
} else {
log::info!("skipping BIOS boot partition format (not needed)");
}
format_btrfs(&partitions.data.path)?;
Ok(())
}
fn format_esp(device: &Path) -> Result<(), StorageError> {
let dev_path = device.to_string_lossy();
log::info!("formatting ESP as FAT32: {}", dev_path);
cmd::run("mkfs.vfat", &["-F", "32", "-n", MOSBOOT_LABEL, &dev_path]).map_err(|e| {
StorageError::FormatFailed {
device: device.to_path_buf(),
error: format!("mkfs.vfat failed: {}", e),
}
})?;
Ok(())
}
fn format_btrfs(device: &Path) -> Result<(), StorageError> {
let dev_path = device.to_string_lossy();
log::info!("formatting data partition as btrfs: {}", dev_path);
cmd::run("mkfs.btrfs", &["-L", MOSDATA_LABEL, &dev_path]).map_err(|e| {
StorageError::FormatFailed {
device: device.to_path_buf(),
error: format!("mkfs.btrfs failed: {}", e),
}
})?;
Ok(())
}
pub fn find_by_label(label: &str) -> Option<std::path::PathBuf> {
let output = cmd::run_allow_fail("blkid", &["-L", label])?;
if output.status.success() {
let path = String::from_utf8_lossy(&output.stdout).trim().to_string();
if !path.is_empty() {
return Some(std::path::PathBuf::from(path));
}
}
None
}
pub fn mosdata_exists() -> bool {
find_by_label(MOSDATA_LABEL).is_some()
}
pub fn mosboot_exists() -> bool {
find_by_label(MOSBOOT_LABEL).is_some()
}