37
src/main.rs
37
src/main.rs
@@ -1,16 +1,19 @@
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
extern crate failure;
|
||||
extern crate nix;
|
||||
extern crate simplelog;
|
||||
extern crate structopt;
|
||||
extern crate tempfile;
|
||||
extern crate which;
|
||||
|
||||
mod error;
|
||||
mod mountstack;
|
||||
mod tool;
|
||||
|
||||
use error::*;
|
||||
use failure::{Fail, ResultExt};
|
||||
use mountstack::{Filesystem, MountStack};
|
||||
use simplelog::*;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
@@ -90,10 +93,9 @@ fn create(disk: PathBuf) -> Result<(), Error> {
|
||||
let pacstrap = Tool::find("pacstrap")?;
|
||||
let arch_chroot = Tool::find("arch-chroot")?;
|
||||
let genfstab = Tool::find("genfstab")?;
|
||||
let mount = Tool::find("mount")?;
|
||||
let umount = Tool::find("umount")?;
|
||||
let mkfat = Tool::find("mkfs.fat")?;
|
||||
let mkbtrfs = Tool::find("mkfs.btrfs")?;
|
||||
let mut mount_stack = MountStack::new();
|
||||
|
||||
if !(disk.starts_with("/dev/disk/by-id")
|
||||
&& (disk
|
||||
@@ -137,20 +139,24 @@ fn create(disk: PathBuf) -> Result<(), Error> {
|
||||
.run(ErrorKind::Creation)?;
|
||||
|
||||
info!("Mounting filesystems to {}", mount_point.path().display());
|
||||
mount
|
||||
.execute()
|
||||
.arg(format!("{}-part3", disk.display()))
|
||||
.arg(mount_point.path())
|
||||
.run(ErrorKind::Creation)?;
|
||||
mount_stack
|
||||
.mount(
|
||||
&PathBuf::from(format!("{}-part3", disk.display())),
|
||||
&mount_point.path(),
|
||||
Filesystem::Btrfs,
|
||||
Some("compress=zstd"),
|
||||
).context(ErrorKind::Creation)?;
|
||||
|
||||
let boot_point = mount_point.path().join("boot");
|
||||
fs::create_dir(&boot_point).context(ErrorKind::Creation)?;
|
||||
|
||||
mount
|
||||
.execute()
|
||||
.arg(format!("{}-part2", disk.display()))
|
||||
.arg(&boot_point)
|
||||
.run(ErrorKind::Creation)?;
|
||||
mount_stack
|
||||
.mount(
|
||||
&PathBuf::from(format!("{}-part2", disk.display())),
|
||||
&boot_point,
|
||||
Filesystem::Vfat,
|
||||
None,
|
||||
).context(ErrorKind::Creation)?;
|
||||
|
||||
info!("Bootstrapping system");
|
||||
pacstrap
|
||||
@@ -199,12 +205,7 @@ fn create(disk: PathBuf) -> Result<(), Error> {
|
||||
.run(ErrorKind::Creation)?;
|
||||
|
||||
info!("Unmounting filesystems");
|
||||
umount
|
||||
.execute()
|
||||
.arg(boot_point)
|
||||
.arg(mount_point.path())
|
||||
.run(ErrorKind::Creation)?;
|
||||
|
||||
drop(mount_stack);
|
||||
sync.execute().run(ErrorKind::Creation)?;
|
||||
|
||||
Ok(())
|
||||
|
||||
60
src/mountstack.rs
Normal file
60
src/mountstack.rs
Normal file
@@ -0,0 +1,60 @@
|
||||
use nix;
|
||||
use nix::mount::{mount, umount, MsFlags};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Filesystem {
|
||||
Btrfs,
|
||||
Vfat,
|
||||
}
|
||||
|
||||
impl Filesystem {
|
||||
fn to_type(self) -> &'static str {
|
||||
match self {
|
||||
Filesystem::Btrfs => "btrfs",
|
||||
Filesystem::Vfat => "vfat",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MountStack {
|
||||
targets: Vec<PathBuf>,
|
||||
}
|
||||
|
||||
impl MountStack {
|
||||
pub fn new() -> Self {
|
||||
MountStack {
|
||||
targets: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn mount(
|
||||
&mut self,
|
||||
source: &Path,
|
||||
target: &Path,
|
||||
filesystem: Filesystem,
|
||||
options: Option<&str>,
|
||||
) -> nix::Result<()> {
|
||||
debug!("Mounting {:?} ({:?}) to {:?}", source, filesystem, target);
|
||||
mount(
|
||||
Some(source),
|
||||
target,
|
||||
Some(filesystem.to_type()),
|
||||
MsFlags::empty(),
|
||||
options,
|
||||
)?;
|
||||
self.targets.push(target.to_owned());
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for MountStack {
|
||||
fn drop(&mut self) {
|
||||
while let Some(target) = self.targets.pop() {
|
||||
if !umount(&target).is_ok() {
|
||||
warn!("Unable to mount {}", target.display());
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user