add bootloader and base package installation and make crash/log msesages all the same format

axtloss/rework-partitioning
amy 3 years ago
parent 719acfa617
commit dce3a65210

@ -0,0 +1,54 @@
use crate::internal::exec::*;
use crate::internal::*;
pub fn install_base_packages() {
install::install(vec![
"base",
"linux",
"linux-firmware",
"systemd-sysvcompat",
"networkmanager",
"man-db",
"man-pages",
"texinfo",
"micro",
"sudo",
"curl",
"archlinux-keyring",
"neofetch",
"btrfs-progs",
"timeshift",
"timeshift-autosnap",
"which",
]);
}
pub fn create_hosts() {
files::create_file("/mnt/etc/hosts");
files_eval(files::append_file("/mnt/etc/hosts", "127.0.0.1 localhost"), "create /etc/hosts");
}
pub fn install_bootloader_efi(efidir: &str) {
install::install(vec!["grub", "efibootmgr",]);
exec_eval(exec_chroot("grub-install", vec![
String::from("--target=x86_64-efi"),
format!("--efi-directory={}", efidir),
String::from("--bootloader-id=crystal"),
]), "install grub as efi");
exec_eval(exec_chroot("grub-mkconfig", vec![
String::from("-o"),
String::from("/boot/grub/grub.cfg"),
]), "create grub.cfg");
}
pub fn install_bootloader_legacy(device: &str) {
install::install(vec!["grub"]);
exec_eval(exec_chroot("grub-install", vec![
String::from("--target=i386-pc"),
String::from(device),
]), "install grub as legacy");
exec_eval(exec_chroot("grub-mkconfig", vec![
String::from("-o"),
String::from("/boot/grub/grub.cfg"),
]), "create grub.cfg");
}

@ -9,16 +9,16 @@ pub fn set_timezone(timezone: &str) {
format!("/usr/share/zoneinfo/{}", timezone),
"/etc/localtime".to_string(),
],
));
exec_eval(exec_chroot("hwclock", vec!["--systohc".to_string()]));
), "Set timezone");
exec_eval(exec_chroot("hwclock", vec!["--systohc".to_string()]), "Set system clock");
}
pub fn set_locale(locale: String) {
files_eval(files::append_file("/etc/locale.gen", "en_US.UTF-8 UTF-8"));
files_eval(files::append_file("/etc/locale.gen", locale.as_str()));
exec_eval(exec_chroot("locale-gen", vec!["".to_string()]));
files_eval(files::append_file("/etc/locale.gen", "en_US.UTF-8 UTF-8"), "add en_US.UTF-8 UTF-8 to locale.gen");
files_eval(files::append_file("/etc/locale.gen", locale.as_str()), "add locales to locale.gen");
exec_eval(exec_chroot("locale-gen", vec!["".to_string()]), "generate locales");
files::create_file("/etc/locale.conf");
files_eval(files::append_file("/etc/locale.conf", "LANG=en_US.UTF-8"));
files_eval(files::append_file("/etc/locale.conf", "LANG=en_US.UTF-8"), "edit locale.conf");
}
pub fn set_keyboard(keyboard: &str) {
@ -26,27 +26,5 @@ pub fn set_keyboard(keyboard: &str) {
files_eval(files::append_file(
"/etc/vconsole.conf",
format!("KEYMAP={}", keyboard).as_str(),
));
}
fn files_eval(return_code: std::result::Result<(), std::io::Error>) {
match return_code {
Ok(_) => {
log("Success".to_string());
}
Err(e) => {
crash(format!("Failed to create file, Error: {}", e), 1);
}
}
}
fn exec_eval(return_code: std::result::Result<std::process::Output, std::io::Error>) {
match return_code {
Ok(_) => {
log("Success".to_string());
}
Err(e) => {
crash(format!("Failed with error: {}", e), 1);
}
}
), "set keyboard layout");
}

@ -3,3 +3,4 @@ pub mod locale;
pub mod network;
pub mod partition;
pub mod users;
pub mod base;

@ -3,32 +3,14 @@ use crate::internal::*;
pub fn set_hostname(hostname: &str) {
println!("Setting hostname to {}", hostname);
files::create_file("/mnt/etc/hostname");
let return_val = files::append_file("/mnt/etc/hostname", hostname);
match return_val {
Ok(_) => {
log(format!("Set hostname to {}", hostname));
}
Err(e) => {
crash(
format!("Failed to set hostname to {}, Error: {}", hostname, e),
1,
);
}
}
files_eval(files::append_file("/mnt/etc/hostname", hostname), "set hostname");
}
pub fn enable_ipv6(ipv6: bool) {
if ipv6 {
let return_val = files::append_file("/mnt/etc/hosts", "::1 localhost");
match return_val {
Ok(_) => {
log("Enabled IPv6".to_string());
}
Err(e) => {
crash(format!("Failed to enable IPv6, Error: {}", e), 1);
}
}
} else {
log("Not enabling ipv6".to_string());
}
pub fn create_hosts() {
files::create_file("/mnt/etc/hosts");
files_eval(files::append_file("/mnt/etc/hosts", "127.0.0.1 localhost"), "create /etc/hosts");
}
pub fn enable_ipv6() {
files_eval(files::append_file("/mnt/etc/hosts", "::1 localhost"), "add ipv6 localhost");
}

@ -7,7 +7,7 @@ pub fn partition(device: &str, mode: &str, efi: bool) {
} else {
log(format!("automatically partitioning {}", device));
if efi {
returncode_eval(exec(
exec_eval(exec(
"parted",
vec![
String::from("-s"),
@ -15,8 +15,8 @@ pub fn partition(device: &str, mode: &str, efi: bool) {
String::from("mklabel"),
String::from("gpt"),
],
));
returncode_eval(exec(
), format!("create gpt label on {}", device).as_str());
exec_eval(exec(
"parted",
vec![
String::from("-s"),
@ -26,8 +26,8 @@ pub fn partition(device: &str, mode: &str, efi: bool) {
String::from("0"),
String::from("300"),
],
));
returncode_eval(exec(
), "create EFI partition");
exec_eval(exec(
"parted",
vec![
String::from("-s"),
@ -37,9 +37,9 @@ pub fn partition(device: &str, mode: &str, efi: bool) {
String::from("300"),
String::from("100%"),
],
));
), "Create btrfs root partition");
} else {
returncode_eval(exec(
exec_eval(exec(
"parted",
vec![
String::from("-s"),
@ -47,8 +47,8 @@ pub fn partition(device: &str, mode: &str, efi: bool) {
String::from("mklabel"),
String::from("msdos"),
],
));
returncode_eval(exec(
), format!("Create msdos label on {}", device).as_str());
exec_eval(exec(
"parted",
vec![
String::from("-s"),
@ -58,8 +58,8 @@ pub fn partition(device: &str, mode: &str, efi: bool) {
String::from("512MIB"),
String::from("100&"),
],
));
returncode_eval(exec(
), "create btrfs root partition");
exec_eval(exec(
"parted",
vec![
String::from("-s"),
@ -69,7 +69,7 @@ pub fn partition(device: &str, mode: &str, efi: bool) {
String::from("1MIB"),
String::from("512MIB"),
],
));
), "create bios boot partition");
}
}
if device.contains("nvme") {
@ -81,16 +81,10 @@ pub fn partition(device: &str, mode: &str, efi: bool) {
fn part_nvme(device: &str, efi: bool) {
if efi {
returncode_eval(exec(
"mkfs.vfat",
vec![format!("{}p1", device)],
));
returncode_eval(exec(
"mkfs.btrfs",
vec![format!("{}p2", device)],
));
mount(format!("{}p2", device), "/mnt", "");
returncode_eval(exec_workdir(
exec_eval(exec("mkfs.vfat", vec![format!("{}p1", device)]), format!("format {}p1 as fat32", device).as_str());
exec_eval(exec("mkfs.btrfs", vec![format!("{}p2", device)]), format!("format {}p2 as btrfs", device).as_str());
mount(format!("{}p2", device).as_str(), "/mnt", "");
exec_eval(exec_workdir(
"btrfs",
"/mnt",
vec![
@ -98,8 +92,8 @@ fn part_nvme(device: &str, efi: bool) {
String::from("create"),
String::from("@"),
],
));
returncode_eval(exec_workdir(
), "Create btrfs subvolume @");
exec_eval(exec_workdir(
"btrfs",
"/mnt",
vec![
@ -107,25 +101,19 @@ fn part_nvme(device: &str, efi: bool) {
String::from("create"),
String::from("@home"),
],
));
), "Create btrfs subvolume @home");
umount("/mnt");
mount(format!("{}p2", device), "/mnt/", "subvol=@");
files::create_directory("/mnt/boot");
files::create_directory("/mnt/boot/efi");
files::create_directory("/mnt/home");
mount(format!("{}p2", device), "/mnt/home", "subvol=@home");
mount(format!("{}p1", device), "/mnt/boot/efi", "");
mount(format!("{}p2", device).as_str(), "/mnt/", "subvol=@");
files_eval(files::create_directory("/mnt/boot"), "create /mnt/boot");
files_eval(files::create_directory("/mnt/boot/efi"), "create /mnt/boot/efi");
files_eval(files::create_directory("/mnt/home"), "create /mnt/home");
mount(format!("{}p2", device).as_str(), "/mnt/home", "subvol=@home");
mount(format!("{}p1", device).as_str(), "/mnt/boot/efi", "");
} else {
returncode_eval(exec(
"mkfs.ext4",
vec![format!("{}p1", device)],
));
returncode_eval(exec(
"mkfs.btrfs",
vec![format!("{}p2", device)],
));
mount(format!("{}p2", device), "/mnt/", "");
returncode_eval(exec_workdir(
exec_eval(exec("mkfs.ext4", vec![format!("{}p1", device)]), format!("format {}p1 as ext4", device).as_str());
exec_eval(exec("mkfs.btrfs", vec![format!("{}p2", device)]), format!("format {}p2 as btrfs", device).as_str());
mount(format!("{}p2", device).as_str(), "/mnt/", "");
exec_eval(exec_workdir(
"btrfs",
"/mnt",
vec![
@ -133,8 +121,8 @@ fn part_nvme(device: &str, efi: bool) {
String::from("create"),
String::from("@"),
],
));
returncode_eval(exec_workdir(
), "Create btrfs subvolume @");
exec_eval(exec_workdir(
"btrfs",
"/mnt",
vec![
@ -142,28 +130,22 @@ fn part_nvme(device: &str, efi: bool) {
String::from("create"),
String::from("@home"),
],
));
), "Create btrfs subvolume @home");
umount("/mnt");
mount(format!("{}p2", device), "/mnt/", "subvol=@");
files::create_directory("/mnt/boot");
files::create_directory("/mnt/home");
mount(format!("{}p2", device), "/mnt/home", "subvol=@home");
mount(format!("{}p1", device), "/mnt/boot", "");
mount(format!("{}p2", device).as_str(), "/mnt/", "subvol=@");
files_eval(files::create_directory("/mnt/boot"), "create /mnt/boot");
files_eval(files::create_directory("/mnt/home"), "create /mnt/home");
mount(format!("{}p2", device).as_str(), "/mnt/home", "subvol=@home");
mount(format!("{}p1", device).as_str(), "/mnt/boot", "");
}
}
fn part_disk(device: &str, efi: bool) {
if efi {
returncode_eval(exec(
"mkfs.vfat",
vec![format!("{}1", device)],
));
returncode_eval(exec(
"mkfs.btrfs",
vec![format!("{}2", device)],
));
mount(format!("{}2", device), "/mnt", "");
returncode_eval(exec_workdir(
exec_eval(exec("mkfs.vfat", vec![format!("{}1", device)]), format!("format {}1 as fat32", device).as_str());
exec_eval(exec("mkfs.btrfs", vec![format!("{}2", device)]), format!("format {}2 as btrfs", device).as_str());
mount(format!("{}2", device).as_str(), "/mnt", "");
exec_eval(exec_workdir(
"btrfs",
"/mnt",
vec![
@ -171,8 +153,8 @@ fn part_disk(device: &str, efi: bool) {
String::from("create"),
String::from("@"),
],
));
returncode_eval(exec_workdir(
), "Create btrfs subvolume @");
exec_eval(exec_workdir(
"btrfs",
"/mnt",
vec![
@ -180,25 +162,19 @@ fn part_disk(device: &str, efi: bool) {
String::from("create"),
String::from("@home"),
],
));
), "Create btrfs subvolume @home");
umount("/mnt");
mount(format!("{}2", device), "/mnt/", "subvol=@");
files::create_directory("/mnt/boot");
files::create_directory("/mnt/boot/efi");
files::create_directory("/mnt/home");
mount(format!("{}2", device), "/mnt/home", "subvol=@home");
mount(format!("{}1", device), "/mnt/boot/efi", "");
mount(format!("{}2", device).as_str(), "/mnt/", "subvol=@");
files_eval(files::create_directory("/mnt/boot"), "create /mnt/boot");
files_eval(files::create_directory("/mnt/boot/efi"), "create /mnt/boot/efi");
files_eval(files::create_directory("/mnt/home"), "create /mnt/home");
mount(format!("{}2", device).as_str(), "/mnt/home", "subvol=@home");
mount(format!("{}1", device).as_str(), "/mnt/boot/efi", "");
} else {
returncode_eval(exec(
"mkfs.ext4",
vec![format!("{}1", device)],
));
returncode_eval(exec(
"mkfs.btrfs",
vec![format!("{}2", device)],
));
mount(format!("{}2", device), "/mnt/", "");
returncode_eval(exec_workdir(
exec_eval(exec("mkfs.ext4", vec![format!("{}1", device)]), format!("format {}1 as ext4", device).as_str());
exec_eval(exec("mkfs.btrfs", vec![format!("{}2", device)]), format!("format {}2 as btrfs", device).as_str());
mount(format!("{}2", device).as_str(), "/mnt/", "");
exec_eval(exec_workdir(
"btrfs",
"/mnt",
vec![
@ -206,8 +182,8 @@ fn part_disk(device: &str, efi: bool) {
String::from("create"),
String::from("@"),
],
));
returncode_eval(exec_workdir(
), "Create btrfs subvolume @");
exec_eval(exec_workdir(
"btrfs",
"/mnt",
vec![
@ -215,40 +191,29 @@ fn part_disk(device: &str, efi: bool) {
String::from("create"),
String::from("@home"),
],
));
), "create btrfs subvolume @home");
umount("/mnt");
mount(format!("{}2", device), "/mnt/", "subvol=@");
files::create_directory("/mnt/boot");
files::create_directory("/mnt/home");
mount(format!("{}2", device), "/mnt/home", "subvol=@home");
mount(format!("{}1", device), "/mnt/boot", "");
mount(format!("{}2", device).as_str(), "/mnt/", "subvol=@");
files_eval(files::create_directory("/mnt/boot"), "create directory /mnt/boot");
files_eval(files::create_directory("/mnt/home"), "create directory /mnt/home");
mount(format!("{}2", device).as_str(), "/mnt/home", "subvol=@home");
mount(format!("{}1", device).as_str(), "/mnt/boot", "");
}
}
fn mount(partition: String, mountpoint: &str, options: &str) {
fn mount(partition: &str, mountpoint: &str, options: &str) {
let options = if options.is_empty() { "\"\"" } else { options };
returncode_eval(exec(
exec_eval(exec(
"mount",
vec![
partition,
String::from(partition),
String::from(mountpoint),
String::from("-o"),
String::from(options),
],
));
), format!("mount {} with options {} at {}", partition, options, mountpoint).as_str());
}
fn umount(mountpoint: &str) {
returncode_eval(exec("umount", vec![String::from(mountpoint)]));
}
fn returncode_eval(return_code: std::result::Result<std::process::Output, std::io::Error>) {
match return_code {
Ok(_) => {
log("Success".to_string());
}
Err(e) => {
crash(format!("Failed with error: {}", e), 1);
}
}
exec_eval(exec("umount", vec![String::from(mountpoint)]), format!("unmount {}", mountpoint).as_str());
}

@ -2,7 +2,7 @@ use crate::internal::exec::*;
use crate::internal::*;
pub fn new_user(username: &str, hasroot: bool, password: &str) {
let return_val = exec_chroot(
exec_eval(exec_chroot(
"useradd",
vec![
String::from("-m"),
@ -10,20 +10,9 @@ pub fn new_user(username: &str, hasroot: bool, password: &str) {
String::from("/bin/bash"),
String::from(username),
],
);
match return_val {
Ok(_) => {
log(format!("Created user {}", username));
}
Err(e) => {
crash(
format!("Failed to create user {}, Error: {}", username, e),
1,
);
}
}
), format!("Create user {}", username).as_str());
if hasroot {
let return_val = exec_chroot(
exec_eval(exec_chroot(
"usermod",
vec![
String::from("-a"),
@ -31,17 +20,9 @@ pub fn new_user(username: &str, hasroot: bool, password: &str) {
String::from("wheel"),
String::from(username),
],
);
match return_val {
Ok(_) => {
log(format!("Added user {} to group wheel", username));
}
Err(e) => {
crash(format!("Failed to add user {}, Error: {}", username, e), 1);
}
}
), format!("Add user {} to wheel group", username).as_str());
}
let return_val = exec_chroot(
exec_eval(exec_chroot(
"usermod",
vec![
String::from("--password"),
@ -54,23 +35,12 @@ pub fn new_user(username: &str, hasroot: bool, password: &str) {
String::from("-stdin)"),
String::from(username),
],
);
match return_val {
Ok(_) => {
log(format!("Set password for user {}", username));
}
Err(e) => {
crash(
format!("Failed to set password for user {}, Error: {}", username, e),
1,
);
}
}
), format!("Set password for user {}", username).as_str());
}
pub fn root_pass(root_pass: &str) {
println!("Setting root password to '{}'", root_pass);
let return_val = exec_chroot(
exec_eval(exec_chroot(
"usermod",
vec![
String::from("--password"),
@ -83,13 +53,5 @@ pub fn root_pass(root_pass: &str) {
String::from("-stdin)"),
String::from("root"),
],
);
match return_val {
Ok(_) => {
log("Set root password".to_string());
}
Err(e) => {
crash(format!("Failed to set root password, Error: {}", e), 1);
}
}
), "set root password");
}

@ -1,18 +1,18 @@
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();
pub fn exec(command: &str, args: Vec<String>) -> Result<std::process::ExitStatus, std::io::Error> {
let returncode = Command::new(command).args(args).status();
returncode
}
pub fn exec_chroot(
command: &str,
args: Vec<String>,
) -> Result<std::process::Output, std::io::Error> {
args: Vec<String>
) -> Result<std::process::ExitStatus, std::io::Error> {
let returncode = Command::new("arch-chroot")
.args(&["/mnt", command])
.args(args)
.output();
.status();
returncode
}
@ -20,10 +20,10 @@ pub fn exec_workdir(
command: &str,
workdir: &str,
args: Vec<String>,
) -> Result<std::process::Output, std::io::Error> {
) -> Result<std::process::ExitStatus, std::io::Error> {
let returncode = Command::new(command)
.args(args)
.current_dir(workdir)
.output();
.status();
returncode
}

@ -3,13 +3,13 @@ use std::fs::{File, OpenOptions};
use std::io::prelude::*;
pub fn create_file(path: &str) {
let return_val = File::create(path);
match return_val {
Ok(_file) => {
log(format!("Created file {}", path));
let returncode = File::create(path);
match returncode {
Ok(_) => {
log(format!("Create {}: Sucess", path));
}
Err(e) => {
crash(format!("Failed to create file {}, Error: {}", path, e), 1);
crash(format!("Create {}: Failed with error {}", path, e), 1);
}
}
}
@ -21,18 +21,6 @@ pub fn append_file(path: &str, content: &str) -> std::io::Result<()> {
Ok(())
}
pub fn create_directory(path: &str) {
let return_val = std::fs::create_dir(path);
match return_val {
Ok(_) => {
log(format!("Created directory {}", path));
}
Err(e) => {
crash(
format!("Failed to create directory {}, Error: {}", path, e),
1,
);
}
}
pub fn create_directory(path: &str) -> std::io::Result<()> {
std::fs::create_dir(path)
}

@ -2,6 +2,7 @@ pub mod exec;
pub mod files;
pub mod install;
pub mod strings;
pub mod returncode_eval;
pub fn install(pkgs: Vec<&str>) {
install::install(pkgs);
@ -15,6 +16,14 @@ pub fn log(a: String) {
strings::log(a);
}
pub fn files_eval(returncode: std::result::Result<(), std::io::Error>, logmsg: &str) {
returncode_eval::files_eval(returncode, logmsg);
}
pub fn exec_eval(returncode: std::result::Result<std::process::ExitStatus, std::io::Error>, logmsg: &str) {
returncode_eval::exec_eval(returncode, logmsg);
}
#[macro_export]
macro_rules! uwu {
($x:expr) => {{

@ -0,0 +1,23 @@
use crate::internal::*;
pub fn exec_eval(return_code: std::result::Result<std::process::ExitStatus, std::io::Error>, logmsg: &str) {
match &return_code {
Ok(_) => {
log(format!("{}: Success", logmsg));
}
Err(e) => {
crash(format!("{}: Failed with error: {}", logmsg, e), return_code.unwrap_err().raw_os_error().unwrap());
}
}
}
pub fn files_eval(return_code: std::result::Result<(), std::io::Error>, logmsg: &str) {
match &return_code {
Ok(_) => {
log(format!("[ \x1b[2;1;32mOK\x1b[0m ] {}", logmsg));
}
Err(e) => {
crash(format!("[ \x1b[2;1;31mFAILED\x1b[0m ] {} ERROR: {}", logmsg, e), return_code.unwrap_err().raw_os_error().unwrap());
}
}
}

@ -8,6 +8,7 @@ fn main() {
let app = App::new("jade")
.version(env!("CARGO_PKG_VERSION"))
.about(env!("CARGO_PKG_DESCRIPTION"))
.author(env!("CARGO_PKG_AUTHORS"))
.subcommand(
SubCommand::with_name("partition")
.about("Partition the install destination")
@ -29,6 +30,32 @@ fn main() {
.takes_value(false),
),
)
.subcommand(
SubCommand::with_name("install-base")
.about("Install base packages")
)
.subcommand(
SubCommand::with_name("bootloader")
.about("Install bootloader")
.subcommand(
SubCommand::with_name("grub-efi")
.about("Install grub-efi")
.arg(
Arg::with_name("efidir")
.help("The directory to install the EFI bootloader to")
.required(true),
),
)
.subcommand(
SubCommand::with_name("grub-legacy")
.about("Install grub-legacy")
.arg(
Arg::with_name("device")
.help("The device to install the bootloader to")
.required(true),
),
),
)
.subcommand(
SubCommand::with_name("locale")
.about("Set locale stuff")
@ -58,6 +85,12 @@ fn main() {
.help("The hostname to use")
.required(true),
)
.arg(
Arg::with_name("create-hosts")
.help("create /etc/hosts")
.long("hosts")
.takes_value(false),
)
.arg(
Arg::with_name("ipv6")
.help("Wether ipv6 should be enabled")
@ -65,7 +98,7 @@ fn main() {
.long("ipv6")
.required(true)
.takes_value(false),
)
),
)
.subcommand(
SubCommand::with_name("users")
@ -128,7 +161,8 @@ fn main() {
locale::set_keyboard(kbrlayout);
locale::set_timezone(timezn);
} else if let Some(app) = app.subcommand_matches("networking") {
network::enable_ipv6(app.is_present("ipv6"));
if app.is_present("ipv6") { network::enable_ipv6() }
if app.is_present("create-hosts") { network::create_hosts() }
network::set_hostname(app.value_of("hostname").unwrap())
} else if let Some(app) = app.subcommand_matches("users") {
if let Some(app) = app.subcommand_matches("newUser") {
@ -144,6 +178,14 @@ fn main() {
}
} else if let Some(app) = app.subcommand_matches("desktops") {
desktops::choose_pkgs(app.value_of("desktopsetup").unwrap());
} else if let Some(app) = app.subcommand_matches("bootloader") {
if let Some(app) = app.subcommand_matches("grub-efi") {
base::install_bootloader_efi(app.value_of("efidir").unwrap());
} else if let Some(app) = app.subcommand_matches("grub-legacy") {
base::install_bootloader_legacy(app.value_of("device").unwrap());
}
} else if let Some(_) = app.subcommand_matches("install-base") {
base::install_base_packages();
} else {
println!("Running TUI installer");
}

Loading…
Cancel
Save