move command execution to src/internal/exec.rs and add first part of partitioning

axtloss/rework-partitioning
amy 3 years ago
parent 1015f96727
commit 6665868da7

@ -1,7 +1,7 @@
use crate::internal::*; use crate::internal::*;
pub fn choose_pkgs(desktop_setup: &str) { pub fn choose_pkgs(desktop_setup: &str) {
println!("Installing {}", desktop_setup); log(format!("Installing {}", desktop_setup));
match desktop_setup { match desktop_setup {
"onyx" => { "onyx" => {
install(vec![ install(vec![

@ -6,7 +6,7 @@ pub fn set_hostname(hostname: &str) {
let return_val = files::append_file("/etc/hostname", hostname); let return_val = files::append_file("/etc/hostname", hostname);
match return_val { match return_val {
Ok(_) => { Ok(_) => {
info(format!("Set hostname to {}", hostname)); log(format!("Set hostname to {}", hostname));
} }
Err(e) => { Err(e) => {
crash( crash(
@ -19,17 +19,16 @@ pub fn set_hostname(hostname: &str) {
pub fn enable_ipv6(ipv6: bool) { pub fn enable_ipv6(ipv6: bool) {
if ipv6 { if ipv6 {
println!("Enabling IPv6");
let return_val = files::append_file("/etc/hosts", "::1 localhost"); let return_val = files::append_file("/etc/hosts", "::1 localhost");
match return_val { match return_val {
Ok(_) => { Ok(_) => {
info("Enabled IPv6".to_string()); log("Enabled IPv6".to_string());
} }
Err(e) => { Err(e) => {
crash(format!("Failed to enable IPv6, Error: {}", e), 1); crash(format!("Failed to enable IPv6, Error: {}", e), 1);
} }
} }
} else { } else {
println!("Not enabling ipv6"); log("Not enabling ipv6".to_string());
} }
} }

@ -1,7 +1,135 @@
pub fn partition(device: &str, mode: &str) { use crate::internal::exec::*;
use crate::internal::*;
pub fn partition(device: &str, mode: &str, efi: bool) {
if mode == "manual" { if mode == "manual" {
println!("Manual partitioning"); log("Manual partitioning".to_string());
} else { } else {
println!("automatically partitioning {}", device); log(format!("automatically partitioning {}", device));
if efi {
let return_code = exec(
"parted",
vec![
String::from("-s"),
String::from(device),
String::from("mklabel"),
String::from("gpt"),
],
);
match return_code {
Ok(_) => {
log("Created GPT label".to_string());
}
Err(e) => {
crash(format!("Failed to create GPT label, Error: {}", e), 1);
}
}
let return_code = exec(
"parted",
vec![
String::from("-s"),
String::from(device),
String::from("mkpart"),
String::from("fat32"),
String::from("0"),
String::from("300"),
],
);
match return_code {
Ok(_) => {
log("Created fat32 EFI partition".to_string());
}
Err(e) => {
crash(
format!("Failed to create fat32 EFI partition, Error: {}", e),
1,
);
}
}
let return_code = exec(
"parted",
vec![
String::from("-s"),
String::from(device),
String::from("mkpart"),
String::from("btrfs"),
String::from("300"),
String::from("100%"),
],
);
match return_code {
Ok(_) => {
log("Created btrfs root partition".to_string());
}
Err(e) => {
crash(
format!("Failed to create btrfs root partition, Error: {}", e),
1,
);
}
}
} else {
let return_code = exec(
"parted",
vec![
String::from("-s"),
String::from(device),
String::from("mklabel"),
String::from("msdos"),
],
);
match return_code {
Ok(_) => {
log("Created MSDOS label".to_string());
}
Err(e) => {
crash(format!("Failed to create MSDOS label, Error: {}", e), 1);
}
}
let return_code = exec(
"parted",
vec![
String::from("-s"),
String::from(device),
String::from("mkpart"),
String::from("btrfs"),
String::from("512MIB"),
String::from("100&"),
],
);
match return_code {
Ok(_) => {
log("Created btrfs root partition".to_string());
}
Err(e) => {
crash(
format!("Failed to create btrfs root partition, Error: {}", e),
1,
);
}
}
let return_code = exec(
"parted",
vec![
String::from("-s"),
String::from(device),
String::from("mkpart"),
String::from("ext4"),
String::from("1MIB"),
String::from("512MIB"),
],
);
match return_code {
Ok(_) => {
log("Created ext4 boot partition".to_string());
}
Err(e) => {
crash(
format!("Failed to create ext4 boot partition, Error: {}", e),
1,
);
}
}
}
} }
} }

@ -1,16 +1,19 @@
use crate::internal::exec::*;
use crate::internal::*; use crate::internal::*;
use std::process::Command;
pub fn new_user(username: &str, hasroot: bool, password: &str) { pub fn new_user(username: &str, hasroot: bool, password: &str) {
let return_val = Command::new("useradd") let return_val = exec(
.arg("-m") "useradd",
.arg("-s") vec![
.arg("/bin/bash") String::from("-m"),
.arg(username) String::from("-s"),
.output(); String::from("/bin/bash"),
String::from(username),
],
);
match return_val { match return_val {
Ok(_) => { Ok(_) => {
info(format!("Created user {}", username)); log(format!("Created user {}", username));
} }
Err(e) => { Err(e) => {
crash( crash(
@ -20,37 +23,43 @@ pub fn new_user(username: &str, hasroot: bool, password: &str) {
} }
} }
if hasroot { if hasroot {
let return_val = Command::new("usermod") let return_val = exec(
.arg("-a") "usermod",
.arg("-G") vec![
.arg("wheel") String::from("-a"),
.arg(username) String::from("-G"),
.output(); String::from("wheel"),
String::from(username),
],
);
match return_val { match return_val {
Ok(_) => { Ok(_) => {
info(format!("Added user {} to group wheel", username)); log(format!("Added user {} to group wheel", username));
} }
Err(e) => { Err(e) => {
crash(format!("Failed to add user {}, Error: {}", username, e), 1); crash(format!("Failed to add user {}, Error: {}", username, e), 1);
} }
} }
} }
let return_val = Command::new("arch-chroot") let return_val = exec(
.arg("/mnt") "arch-chroot",
.arg("usermod") vec![
.arg("--password") String::from("/mnt"),
.arg("$(echo") String::from("usermod"),
.arg(format!("${{{}}}", password)) String::from("--password"),
.arg("|") String::from("$(echo"),
.arg("openssl") String::from(format!("${}", password)),
.arg("passwd") String::from("|"),
.arg("-1") String::from("openssl"),
.arg("-stdin)") String::from("passwd"),
.arg(username) String::from("-1"),
.output(); String::from("-stdin)"),
String::from(username),
],
);
match return_val { match return_val {
Ok(_) => { Ok(_) => {
info(format!("Set password for user {}", username)); log(format!("Set password for user {}", username));
} }
Err(e) => { Err(e) => {
crash( crash(
@ -63,22 +72,25 @@ pub fn new_user(username: &str, hasroot: bool, password: &str) {
pub fn root_pass(root_pass: &str) { pub fn root_pass(root_pass: &str) {
println!("Setting root password to '{}'", root_pass); println!("Setting root password to '{}'", root_pass);
let return_val = Command::new("arch-chroot") let return_val = exec(
.arg("/mnt") "arch-chroot",
.arg("usermod") vec![
.arg("--password") String::from("/mnt"),
.arg("$(echo") String::from("usermod"),
.arg(format!("${{{}}}", root_pass)) String::from("--password"),
.arg("|") String::from("$(echo"),
.arg("openssl") String::from(format!("${{{}}}", root_pass)),
.arg("passwd") String::from("|"),
.arg("-1") String::from("openssl"),
.arg("-stdin)") String::from("passwd"),
.arg("root") String::from("-1"),
.output(); String::from("-stdin)"),
String::from("root"),
],
);
match return_val { match return_val {
Ok(_) => { Ok(_) => {
info("Set root password".to_string()); log("Set root password".to_string());
} }
Err(e) => { Err(e) => {
crash(format!("Failed to set root password, Error: {}", e), 1); crash(format!("Failed to set root password, Error: {}", e), 1);

@ -0,0 +1,6 @@
use std::process::Command;
pub fn exec(command: &str, args: Vec<String>) -> Result<std::process::Output, std::io::Error> {
let returncode = Command::new(command).args(args).output();
returncode
}

@ -6,7 +6,7 @@ pub fn create_file(path: &str) {
let return_val = File::create(path); let return_val = File::create(path);
match return_val { match return_val {
Ok(_file) => { Ok(_file) => {
info(format!("Created file {}", path)); log(format!("Created file {}", path));
} }
Err(e) => { Err(e) => {
crash(format!("Failed to create file {}, Error: {}", path, e), 1); crash(format!("Failed to create file {}, Error: {}", path, e), 1);
@ -15,7 +15,7 @@ pub fn create_file(path: &str) {
} }
pub fn append_file(path: &str, content: &str) -> std::io::Result<()> { pub fn append_file(path: &str, content: &str) -> std::io::Result<()> {
info(format!("Appending {} to file {}", content, path)); log(format!("Appending {} to file {}", content, path));
let mut file = OpenOptions::new().append(true).open(path)?; let mut file = OpenOptions::new().append(true).open(path)?;
file.write(content.as_bytes())?; file.write(content.as_bytes())?;
Ok(()) Ok(())
@ -25,7 +25,7 @@ pub fn delete_file(path: &str) {
let return_val = std::fs::remove_file(path); let return_val = std::fs::remove_file(path);
match return_val { match return_val {
Ok(_) => { Ok(_) => {
info(format!("Deleted file {}", path)); log(format!("Deleted file {}", path));
} }
Err(e) => { Err(e) => {
crash(format!("Failed to delete file {}, Error: {}", path, e), 1); crash(format!("Failed to delete file {}, Error: {}", path, e), 1);
@ -37,7 +37,7 @@ pub fn create_directory(path: &str) {
let return_val = std::fs::create_dir(path); let return_val = std::fs::create_dir(path);
match return_val { match return_val {
Ok(_) => { Ok(_) => {
info(format!("Created directory {}", path)); log(format!("Created directory {}", path));
} }
Err(e) => { Err(e) => {
crash( crash(
@ -54,7 +54,7 @@ pub fn overwrite_file(path: &str, content: &str) {
let return_val = append_file(path, content); let return_val = append_file(path, content);
match return_val { match return_val {
Ok(_) => { Ok(_) => {
info(format!("Overwrote file {}", path)); log(format!("Overwrote file {}", path));
} }
Err(e) => { Err(e) => {
crash( crash(

@ -1,3 +1,4 @@
pub mod exec;
pub mod files; pub mod files;
pub mod install; pub mod install;
pub mod strings; pub mod strings;

@ -1,9 +1,8 @@
use crate::uwu; use crate::uwu;
use std::io::Write; use std::env;
use std::process::exit; use std::process::exit;
use std::str::FromStr; use std::str::FromStr;
use std::time::UNIX_EPOCH; use std::time::UNIX_EPOCH;
use std::{env, io};
pub fn info(a: String) { pub fn info(a: String) {
let a = if env::var("JADE_UWU").unwrap_or_else(|_| "".to_string()) == "true" { let a = if env::var("JADE_UWU").unwrap_or_else(|_| "".to_string()) == "true" {
uwu!(&a) uwu!(&a)

@ -22,6 +22,12 @@ fn main() {
.help("The device to partition") .help("The device to partition")
.required_if("mode", "auto"), .required_if("mode", "auto"),
) )
.arg(
Arg::with_name("efi")
.help("If the install destination should be partitioned with EFI")
.long("efi")
.takes_value(false),
),
) )
.subcommand( .subcommand(
SubCommand::with_name("locale") SubCommand::with_name("locale")
@ -104,6 +110,7 @@ fn main() {
partition::partition( partition::partition(
app.value_of("device").unwrap(), app.value_of("device").unwrap(),
app.value_of("mode").unwrap(), app.value_of("mode").unwrap(),
app.is_present("efi"),
); );
} else if let Some(app) = app.subcommand_matches("locale") { } else if let Some(app) = app.subcommand_matches("locale") {
let kbrlayout = app.value_of("keyboard").unwrap(); let kbrlayout = app.value_of("keyboard").unwrap();

Loading…
Cancel
Save