Generate fstab (fix #8 #12)

This commit is contained in:
Roey Darwish Dror 2018-11-04 16:00:52 +02:00
parent c48ea40c98
commit c7f7f9b533
2 changed files with 43 additions and 6 deletions

View File

@ -14,7 +14,7 @@ This tool should be ran from an exiting Arch Linux installations. It depends on
* sgdisk * sgdisk
* partprobe * partprobe
* pacstrap and arch-chroot * Arch install scripts
* mkfs.fat * mkfs.fat
* mkfs.btrfs * mkfs.btrfs

View File

@ -14,7 +14,8 @@ use failure::{Fail, ResultExt};
use simplelog::*; use simplelog::*;
use std::fs; use std::fs;
use std::path::PathBuf; use std::path::PathBuf;
use std::process::{exit, Command}; use std::process::{exit, Command, ExitStatus};
use std::str;
use std::thread; use std::thread;
use std::time::Duration; use std::time::Duration;
use structopt::StructOpt; use structopt::StructOpt;
@ -26,12 +27,18 @@ BINARIES=()
FILES=() FILES=()
HOOKS=(base udev block filesystems keyboard fsck)"; HOOKS=(base udev block filesystems keyboard fsck)";
#[derive(Fail, Debug)] #[derive(Debug, Fail)]
#[fail(display = "Process failed")] enum ProcessError {
pub struct ProcessFailed; #[fail(display = "Bad exit code: {}", _0)]
BadExitCode(ExitStatus),
#[fail(display = "Process output isn't valid UTF-8")]
InvalidUtf8,
}
trait CommandExt { trait CommandExt {
fn run(&mut self, context: ErrorKind) -> Result<(), Error>; fn run(&mut self, context: ErrorKind) -> Result<(), Error>;
fn run_text_output(&mut self, context: ErrorKind) -> Result<String, Error>;
} }
impl CommandExt for Command { impl CommandExt for Command {
@ -39,11 +46,31 @@ impl CommandExt for Command {
let exit_status = self.spawn().context(context)?.wait().context(context)?; let exit_status = self.spawn().context(context)?.wait().context(context)?;
if !exit_status.success() { if !exit_status.success() {
return Err(ProcessFailed {}.context(context).into()); return Err(ProcessError::BadExitCode(exit_status)
.context(context)
.into());
} }
Ok(()) Ok(())
} }
fn run_text_output(&mut self, context: ErrorKind) -> Result<String, Error> {
let output = self.output().context(context)?;
if !output.status.success() {
let error = str::from_utf8(&output.stderr).unwrap_or("[INVALID UTF8]");
error!("{}", error);
return Err(ProcessError::BadExitCode(output.status)
.context(context)
.into());
}
Ok(String::from(
str::from_utf8(&output.stdout)
.map_err(|_| ProcessError::InvalidUtf8)
.context(context)?,
))
}
} }
#[derive(StructOpt)] #[derive(StructOpt)]
@ -62,6 +89,7 @@ fn create(disk: PathBuf) -> Result<(), Error> {
let partprobe = Tool::find("partprobe")?; let partprobe = Tool::find("partprobe")?;
let pacstrap = Tool::find("pacstrap")?; let pacstrap = Tool::find("pacstrap")?;
let arch_chroot = Tool::find("arch-chroot")?; let arch_chroot = Tool::find("arch-chroot")?;
let genfstab = Tool::find("genfstab")?;
let mount = Tool::find("mount")?; let mount = Tool::find("mount")?;
let umount = Tool::find("umount")?; let umount = Tool::find("umount")?;
let mkfat = Tool::find("mkfs.fat")?; let mkfat = Tool::find("mkfs.fat")?;
@ -138,6 +166,15 @@ fn create(disk: PathBuf) -> Result<(), Error> {
"btrfs-progs", "btrfs-progs",
]).run(ErrorKind::Creation)?; ]).run(ErrorKind::Creation)?;
let fstab = genfstab
.execute()
.arg("-U")
.arg(mount_point.path())
.run_text_output(ErrorKind::Creation)?
.replace("relatime", "noatime");
debug!("fstab:\n{}", fstab);
fs::write(mount_point.path().join("etc/fstab"), fstab).context(ErrorKind::Creation)?;
arch_chroot arch_chroot
.execute() .execute()
.arg(mount_point.path()) .arg(mount_point.path())