From a94f6a713634471ea96aa46995e3a8336a1751f5 Mon Sep 17 00:00:00 2001 From: amy Date: Sat, 22 Jan 2022 20:23:41 +0100 Subject: [PATCH] finish up partitioning --- src/functions/partition.rs | 146 +++++++++++++++++++++++++++++++++++++ src/internal/exec.rs | 5 ++ 2 files changed, 151 insertions(+) diff --git a/src/functions/partition.rs b/src/functions/partition.rs index 4dad15f..707df24 100755 --- a/src/functions/partition.rs +++ b/src/functions/partition.rs @@ -132,4 +132,150 @@ pub fn partition(device: &str, mode: &str, efi: bool) { } } } + if device.contains("nvme") { + part_nvme(device, efi); + } else { + part_disk(device, efi); + } +} + +fn part_nvme(device: &str, efi: bool) { + if efi { + let devicep1 = format!("{}p1", device).as_str(); + let devicep2 = format!("{}p2", device).as_str(); + returncode_eval(exec("mkfs.vfat", vec![ + String::from(devicep1), + ])); + returncode_eval(exec("mkfs.btrfs", vec![ + String::from(devicep2), + ])); + mount(devicep2, "/mnt", ""); + returncode_eval(exec_workdir("btrfs", "/mnt", vec![ + String::from("subvolume"), + String::from("create"), + String::from("@"), + ])); + returncode_eval(exec_workdir("btrfs", "/mnt", vec![ + String::from("subvolume"), + String::from("create"), + String::from("@home"), + ])); + umount("/mnt"); + mount(devicep2, "/mnt/", "subvol=@"); + files::create_directory("/mnt/boot"); + files::create_directory("/mnt/boot/efi"); + files::create_directory("/mnt/home"); + mount(devicep2, "/mnt/home", "subvol=@home"); + mount(devicep1, "/mnt/boot/efi", ""); + } else { + let devicep1 = format!("{}p1", device).as_str(); + let devicep2 = format!("{}p2", device).as_str(); + returncode_eval(exec("mkfs.ext4", vec![ + String::from(devicep1), + ])); + returncode_eval(exec("mkfs.btrfs", vec![ + String::from(devicep2), + ])); + mount(devicep2, "/mnt/", ""); + returncode_eval(exec_workdir("btrfs", "/mnt", vec![ + String::from("subvolume"), + String::from("create"), + String::from("@"), + ])); + returncode_eval(exec_workdir("btrfs", "/mnt", vec![ + String::from("subvolume"), + String::from("create"), + String::from("@home"), + ])); + umount("/mnt"); + mount(devicep2, "/mnt/", "subvol=@"); + files::create_directory("/mnt/boot"); + files::create_directory("/mnt/home"); + mount(devicep2, "/mnt/home", "subvol=@home"); + mount(devicep1, "/mnt/boot", ""); + } +} + +fn part_disk(device: &str, efi: bool) { + if efi { + let device1 = format!("{}1", device).as_str(); + let device2 = format!("{}2", device).as_str(); + returncode_eval(exec("mkfs.vfat", vec![ + String::from(device1), + ])); + returncode_eval(exec("mkfs.btrfs", vec![ + String::from(device2), + ])); + mount(device2, "/mnt", ""); + returncode_eval(exec_workdir("btrfs", "/mnt", vec![ + String::from("subvolume"), + String::from("create"), + String::from("@"), + ])); + returncode_eval(exec_workdir("btrfs", "/mnt", vec![ + String::from("subvolume"), + String::from("create"), + String::from("@home"), + ])); + umount("/mnt"); + mount(device2, "/mnt/", "subvol=@"); + files::create_directory("/mnt/boot"); + files::create_directory("/mnt/boot/efi"); + files::create_directory("/mnt/home"); + mount(device2, "/mnt/home", "subvol=@home"); + mount(device1, "/mnt/boot/efi", ""); + } else { + let device1 = format!("{}1", device).as_str(); + let device2 = format!("{}2", device).as_str(); + returncode_eval(exec("mkfs.ext4", vec![ + String::from(device1), + ])); + returncode_eval(exec("mkfs.btrfs", vec![ + String::from(device2), + ])); + mount(device2, "/mnt/", ""); + returncode_eval(exec_workdir("btrfs", "/mnt", vec![ + String::from("subvolume"), + String::from("create"), + String::from("@"), + ])); + returncode_eval(exec_workdir("btrfs", "/mnt", vec![ + String::from("subvolume"), + String::from("create"), + String::from("@home"), + ])); + umount("/mnt"); + mount(device2, "/mnt/", "subvol=@"); + files::create_directory("/mnt/boot"); + files::create_directory("/mnt/home"); + mount(device2, "/mnt/home", "subvol=@home"); + mount(device1, "/mnt/boot", ""); + } +} + +fn mount(partition: &str, mountpoint: &str, options: &str) { + let options = if options.is_empty() { "\"\"" } else { options }; + returncode_eval(exec("mount", vec![ + String::from(partition), + String::from(mountpoint), + String::from("-o"), + String::from(options), + ])); +} + +fn umount(mountpoint: &str) { + returncode_eval(exec("umount", vec![ + String::from(mountpoint), + ])); } + +fn returncode_eval(return_code: std::result::Result) { + match return_code { + Ok(_) => { + log("Success".to_string()); + } + Err(e) => { + crash(format!("Failed with error: {}", e), 1); + } + } +} \ No newline at end of file diff --git a/src/internal/exec.rs b/src/internal/exec.rs index 035a625..3728edf 100755 --- a/src/internal/exec.rs +++ b/src/internal/exec.rs @@ -4,3 +4,8 @@ pub fn exec(command: &str, args: Vec) -> Result,) -> Result { + let returncode = Command::new(command).args(args).current_dir(workdir).output(); + returncode +} \ No newline at end of file