From 6d894dcebf98b829f8a8499191124936004534ff Mon Sep 17 00:00:00 2001 From: axtlos Date: Sat, 23 Apr 2022 19:12:37 +0200 Subject: [PATCH] allow manual partitioning --- Cargo.toml | 1 + src/args.rs | 32 +++++++- src/functions/partition.rs | 163 ++++++++++++++++++++++++++++++++++++- src/internal/config.rs | 11 +++ src/main.rs | 2 +- 5 files changed, 206 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 42ee9d7..8422f06 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + [dependencies] clap = {version = "3.1.10", features = ["derive"] } serde_json = "1.0.59" diff --git a/src/args.rs b/src/args.rs index 17f8447..fcfb8f1 100644 --- a/src/args.rs +++ b/src/args.rs @@ -80,14 +80,44 @@ pub struct PartitionArgs { pub mode: PartitionMode, /// The device to partition - #[clap(required_if_eq("mode", "PartitionMode::Manual"))] + #[clap(required_if_eq("mode", "PartitionMode::Auto"))] pub device: PathBuf, /// If the install destination should be partitioned with EFI #[clap(long)] pub efi: bool, + + /// The partitions to use for manual partitioning + #[clap(required_if_eq("mode", "Partition::Manual"), parse(try_from_str = parse_partitions))] + pub partitions: Vec, +} + +#[derive(Debug)] +pub struct Partition { + pub mountpoint: String, + pub blockdevice: String, + pub filesystem: String, +} + +impl Partition { + pub fn new(mountpoint: String, blockdevice: String, filesystem: String) -> Self { + Self { + mountpoint, + blockdevice, + filesystem, + } + } +} + +pub fn parse_partitions(s: &str) -> Result { + Ok(Partition::new( + s.split(' ').collect::>()[0].to_string(), + s.split(' ').collect::>()[1].to_string(), + s.split(' ').collect::>()[2].to_string(), + )) } + #[derive(Debug, ArgEnum, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Serialize, Deserialize)] pub enum PartitionMode { #[clap(name = "auto")] diff --git a/src/functions/partition.rs b/src/functions/partition.rs index 7f24fad..3225067 100755 --- a/src/functions/partition.rs +++ b/src/functions/partition.rs @@ -1,9 +1,163 @@ use crate::args::PartitionMode; +use crate::args; use crate::internal::exec::*; use crate::internal::*; use std::path::{Path, PathBuf}; -pub fn partition(device: PathBuf, mode: PartitionMode, efi: bool) { +/*mkfs.bfs mkfs.cramfs mkfs.ext3 mkfs.fat mkfs.msdos mkfs.xfs +mkfs.btrfs mkfs.ext2 mkfs.extd mkfs.minix mkfs.vfat */ + +pub fn fmt_mount(mountpoint: &String, filesystem: &String, blockdevice: &String) { + match filesystem.as_str() { + "vfat" => { + exec_eval( + exec( + "mkfs.vfat", + vec![ + String::from("-F"), + String::from("32"), + String::from(blockdevice), + ], + ), + "Formatting {blockdevice} as vfat", + ) + } + "bfs" => { + exec_eval( + exec( + "mkfs.bfs", + vec![ + String::from(blockdevice), + ], + ), + "Formatting {blockdevice} as bfs", + ) + } + "cramfs" => { + exec_eval( + exec( + "mkfs.cramfs", + vec![ + String::from(blockdevice), + ], + ), + "Formatting {blockdevice} as cramfs", + ) + } + "ext3" => { + exec_eval( + exec( + "mkfs.ext3", + vec![ + String::from(blockdevice), + ], + ), + "Formatting {blockdevice} as ext3", + ) + } + "fat" => { + exec_eval( + exec( + "mkfs.fat", + vec![ + String::from(blockdevice), + ], + ), + "Formatting {blockdevice} as fat", + ) + } + "msdos" => { + exec_eval( + exec( + "mkfs.msdos", + vec![ + String::from(blockdevice), + ], + ), + "Formatting {blockdevice} as msdos", + ) + } + "xfs" => { + exec_eval( + exec( + "mkfs.xfs", + vec![ + String::from(blockdevice), + ], + ), + "Formatting {blockdevice} as xfs", + ) + } + "btrfs" => { + exec_eval( + exec( + "mkfs.btrfs", + vec![ + String::from(blockdevice), + ], + ), + "Formatting {blockdevice} as btrfs", + ) + } + "ext2" => { + exec_eval( + exec( + "mkfs.ext2", + vec![ + String::from(blockdevice), + ], + ), + "Formatting {blockdevice} as ext2", + ) + } + "extd" => { + exec_eval( + exec( + "mkfs.extd", + vec![ + String::from(blockdevice), + ], + ), + "Formatting {blockdevice} as extd", + ) + } + "minix" => { + exec_eval( + exec( + "mkfs.minix", + vec![ + String::from(blockdevice), + ], + ), + "Formatting {blockdevice} as minix", + ) + } + "vfat" => { + exec_eval( + exec( + "mkfs.vfat", + vec![ + String::from(blockdevice), + ], + ), + "Formatting {blockdevice} as vfat", + ) + } + _ => { + crash( + "Unknown filesystem {filesystem}, used in partition {blockdevice}", + 1, + ); + } + } + files_eval( + files::create_directory(&mountpoint), + "Creating mountpoint {mountpoint} for {blockdevice}", + ); + mount(&blockdevice, &mountpoint, ""); +} + +pub fn partition(device: PathBuf, mode: PartitionMode, efi: bool, partitions: Vec) { if !device.exists() { crash(format!("The device {device:?} doesn't exist"), 1); } @@ -18,6 +172,13 @@ pub fn partition(device: PathBuf, mode: PartitionMode, efi: bool) { } PartitionMode::Manual => { log::debug!("Manual partitioning"); + for i in 0..partitions.len() { + fmt_mount( + &partitions[i].mountpoint, + &partitions[i].filesystem, + &partitions[i].blockdevice, + ); + } } } if device.to_string_lossy().contains("nvme") { diff --git a/src/internal/config.rs b/src/internal/config.rs index e9d813b..eb5a207 100755 --- a/src/internal/config.rs +++ b/src/internal/config.rs @@ -1,4 +1,5 @@ use crate::args::{DesktopSetup, PartitionMode}; +use crate::args; use crate::functions::*; use crate::internal::*; use serde::{Deserialize, Serialize}; @@ -22,6 +23,7 @@ struct Partition { device: String, mode: PartitionMode, efi: bool, + partitions: Vec, } #[derive(Serialize, Deserialize)] @@ -77,10 +79,19 @@ pub fn read_config(configpath: PathBuf) { log::info!("Block device to use : /dev/{}", config.partition.device); log::info!("Partitioning mode : {:?}", config.partition.mode); log::info!("Partitioning for EFI : {}", config.partition.efi); + let mut partitions: Vec = Vec::new(); + for partition in config.partition.partitions { + partitions.push(args::Partition::new( + partition.split(':').collect::>()[0].to_string(), + partition.split(':').collect::>()[1].to_string(), + partition.split(':').collect::>()[2].to_string(), + )); + } partition::partition( PathBuf::from("/dev/").join(config.partition.device), config.partition.mode, config.partition.efi, + partitions, ); base::install_base_packages(); base::genfstab(); diff --git a/src/main.rs b/src/main.rs index b4420d6..f80d285 100755 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ fn main() { logging::init(opt.verbose); match opt.command { Command::Partition(args) => { - partition::partition(args.device, args.mode, args.efi); + partition::partition(args.device, args.mode, args.efi, args.partitions); } Command::InstallBase => { base::install_base_packages();