mirror of
https://github.com/philmmanjaro/alma.git
synced 2025-12-06 19:29:21 +01:00
Migrate ALMA to anyhow (#54)
This commit is contained in:
@@ -2,19 +2,18 @@ use super::mount;
|
||||
use super::Tool;
|
||||
use crate::args;
|
||||
use crate::constants::{BOOT_PARTITION_INDEX, ROOT_PARTITION_INDEX};
|
||||
use crate::error::{Error, ErrorKind};
|
||||
use crate::process::CommandExt;
|
||||
use crate::storage;
|
||||
use crate::storage::{is_encrypted_device, EncryptedDevice};
|
||||
use crate::storage::{BlockDevice, Filesystem, FilesystemType, LoopDevice};
|
||||
use anyhow::Context;
|
||||
use log::info;
|
||||
|
||||
use failure::ResultExt;
|
||||
use tempfile::tempdir;
|
||||
|
||||
/// Use arch-chroot to chroot to the given device
|
||||
/// Also handles encrypted root partitions (detected by checking for the LUKS magic header)
|
||||
pub fn chroot(command: args::ChrootCommand) -> Result<(), Error> {
|
||||
pub fn chroot(command: args::ChrootCommand) -> anyhow::Result<()> {
|
||||
let arch_chroot = Tool::find("arch-chroot")?;
|
||||
let cryptsetup;
|
||||
|
||||
@@ -26,12 +25,12 @@ pub fn chroot(command: args::ChrootCommand) -> Result<(), Error> {
|
||||
Err(_) => {
|
||||
loop_device = Some(LoopDevice::create(&command.block_device)?);
|
||||
storage::StorageDevice::from_path(
|
||||
loop_device.as_ref().unwrap().path(),
|
||||
loop_device.as_ref().expect("loop device not found").path(),
|
||||
command.allow_non_removable,
|
||||
)?
|
||||
}
|
||||
};
|
||||
let mount_point = tempdir().context(ErrorKind::TmpDirError)?;
|
||||
let mount_point = tempdir().context("Error creating a temporary directory")?;
|
||||
|
||||
let boot_partition = storage_device.get_partition(BOOT_PARTITION_INDEX)?;
|
||||
let boot_filesystem = Filesystem::from_partition(&boot_partition, FilesystemType::Vfat);
|
||||
@@ -40,7 +39,7 @@ pub fn chroot(command: args::ChrootCommand) -> Result<(), Error> {
|
||||
let encrypted_root = if is_encrypted_device(&root_partition_base)? {
|
||||
cryptsetup = Some(Tool::find("cryptsetup")?);
|
||||
Some(EncryptedDevice::open(
|
||||
cryptsetup.as_ref().unwrap(),
|
||||
cryptsetup.as_ref().expect("cryptsetup not found"),
|
||||
&root_partition_base,
|
||||
"alma_root".into(),
|
||||
)?)
|
||||
@@ -61,7 +60,13 @@ pub fn chroot(command: args::ChrootCommand) -> Result<(), Error> {
|
||||
.execute()
|
||||
.arg(mount_point.path())
|
||||
.args(&command.command)
|
||||
.run(ErrorKind::Interactive)?;
|
||||
.run()
|
||||
.with_context(|| {
|
||||
format!(
|
||||
"Error running command in chroot: {}",
|
||||
command.command.join(" "),
|
||||
)
|
||||
})?;
|
||||
|
||||
info!("Unmounting filesystems");
|
||||
mount_stack.umount()?;
|
||||
|
||||
@@ -6,8 +6,7 @@ pub use chroot::chroot;
|
||||
pub use mount::mount;
|
||||
pub use qemu::qemu;
|
||||
|
||||
use crate::error::*;
|
||||
use failure::ResultExt;
|
||||
use anyhow::anyhow;
|
||||
use std::path::PathBuf;
|
||||
use std::process::Command;
|
||||
use which::which;
|
||||
@@ -18,10 +17,17 @@ pub struct Tool {
|
||||
}
|
||||
|
||||
impl Tool {
|
||||
pub fn find(name: &'static str) -> Result<Self, Error> {
|
||||
Ok(Self {
|
||||
exec: which(name).context(ErrorKind::NoTool(name))?,
|
||||
})
|
||||
pub fn find(name: &'static str) -> anyhow::Result<Self> {
|
||||
// Note this conversion is only necessary until which releases their new version using
|
||||
// thiserror instead of failure - then we can just use .with_context() on the thiserror
|
||||
// Error
|
||||
// Commit pending release:
|
||||
// BLOCKED: https://github.com/harryfei/which-rs/commit/e6e839c4f6cdf8d3e33ec7eafdd50d34472740ea
|
||||
let which = match which(name) {
|
||||
Ok(x) => Ok(x),
|
||||
Err(_) => Err(anyhow!("Could not find tool: {}", name)),
|
||||
}?;
|
||||
Ok(Self { exec: which })
|
||||
}
|
||||
|
||||
pub fn execute(&self) -> Command {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use crate::error::{Error, ErrorKind};
|
||||
use crate::storage::{Filesystem, MountStack};
|
||||
use failure::ResultExt;
|
||||
use anyhow::Context;
|
||||
use log::{debug, info};
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
@@ -12,7 +11,7 @@ pub fn mount<'a>(
|
||||
mount_path: &Path,
|
||||
boot_filesystem: &'a Filesystem,
|
||||
root_filesystem: &'a Filesystem,
|
||||
) -> Result<MountStack<'a>, Error> {
|
||||
) -> anyhow::Result<MountStack<'a>> {
|
||||
let mut mount_stack = MountStack::new();
|
||||
debug!(
|
||||
"Root partition: {}",
|
||||
@@ -22,16 +21,16 @@ pub fn mount<'a>(
|
||||
info!("Mounting filesystems to {}", mount_path.display());
|
||||
mount_stack
|
||||
.mount(&root_filesystem, mount_path.into(), None)
|
||||
.context(ErrorKind::Mounting)?;
|
||||
.with_context(|| format!("Error mounting filesystem to {}", mount_path.display()))?;
|
||||
|
||||
let boot_point = mount_path.join("boot");
|
||||
if !boot_point.exists() {
|
||||
fs::create_dir(&boot_point).context(ErrorKind::CreateBoot)?;
|
||||
fs::create_dir(&boot_point).context("Error creating the boot directory")?;
|
||||
}
|
||||
|
||||
mount_stack
|
||||
.mount(&boot_filesystem, boot_point, None)
|
||||
.context(ErrorKind::Mounting)?;
|
||||
.context("Error mounting the boot point")?;
|
||||
|
||||
Ok(mount_stack)
|
||||
}
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
use super::Tool;
|
||||
use crate::args;
|
||||
use crate::error;
|
||||
use anyhow::Context;
|
||||
use log::debug;
|
||||
|
||||
use failure::ResultExt;
|
||||
use std::os::unix::process::CommandExt as UnixCommandExt;
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// Loads given block device in qemu
|
||||
/// Uses kvm if it is enabled
|
||||
pub fn qemu(command: args::QemuCommand) -> Result<(), error::Error> {
|
||||
pub fn qemu(command: args::QemuCommand) -> anyhow::Result<()> {
|
||||
let qemu = Tool::find("qemu-system-x86_64")?;
|
||||
|
||||
let mut run = qemu.execute();
|
||||
@@ -39,5 +38,5 @@ pub fn qemu(command: args::QemuCommand) -> Result<(), error::Error> {
|
||||
|
||||
let err = run.exec();
|
||||
|
||||
Err(err).context(error::ErrorKind::Qemu)?
|
||||
Err(err).context("Failed launching Qemu")?
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user