From 44324b71270118a7645ebee5c9f0264ddfa4c1aa Mon Sep 17 00:00:00 2001 From: Aiden McClelland Date: Wed, 3 Nov 2021 15:05:27 -0600 Subject: [PATCH] allow setup of disk previously used with lvm --- appmgr/src/disk/main.rs | 24 ++++++++++++++++++++---- appmgr/src/disk/util.rs | 10 +++++++--- appmgr/src/setup.rs | 11 ++++++++--- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/appmgr/src/disk/main.rs b/appmgr/src/disk/main.rs index ddbd1aff9..00b862482 100644 --- a/appmgr/src/disk/main.rs +++ b/appmgr/src/disk/main.rs @@ -1,4 +1,5 @@ -use std::path::Path; +use std::collections::BTreeMap; +use std::path::{Path, PathBuf}; use tokio::process::Command; use tracing::instrument; @@ -17,6 +18,7 @@ pub const SWAP_SIZE: FsSize = FsSize::Gigabytes(8); #[instrument(skip(disks, datadir, password))] pub async fn create( disks: &I, + pvscan: &BTreeMap>, datadir: impl AsRef, password: &str, ) -> Result @@ -24,20 +26,34 @@ where for<'a> &'a I: IntoIterator, P: AsRef, { - let guid = create_pool(disks).await?; + let guid = create_pool(disks, pvscan).await?; create_all_fs(&guid, &datadir, password).await?; export(&guid, datadir).await?; Ok(guid) } #[instrument(skip(disks))] -pub async fn create_pool(disks: &I) -> Result +pub async fn create_pool( + disks: &I, + pvscan: &BTreeMap>, +) -> Result where for<'a> &'a I: IntoIterator, P: AsRef, { + Command::new("dmsetup") + .arg("remove_all") // TODO: find a higher finesse way to do this for portability reasons + .invoke(crate::ErrorKind::DiskManagement) + .await?; for disk in disks { - tokio::fs::write(disk.as_ref(), &[0; 2048]).await?; // wipe partition table and lvm2 metadata + if pvscan.contains_key(disk.as_ref()) { + Command::new("pvremove") + .arg("-yff") + .arg(disk.as_ref()) + .invoke(crate::ErrorKind::DiskManagement) + .await?; + } + tokio::fs::write(disk.as_ref(), &[0; 2048]).await?; // wipe partition table Command::new("pvcreate") .arg("-yff") .arg(disk.as_ref()) diff --git a/appmgr/src/disk/util.rs b/appmgr/src/disk/util.rs index a0eb4ac3b..429dee2c9 100644 --- a/appmgr/src/disk/util.rs +++ b/appmgr/src/disk/util.rs @@ -153,13 +153,17 @@ pub async fn get_used>(path: P) -> Result { .parse()?) } -#[instrument] -pub async fn list() -> Result, Error> { +pub async fn pvscan() -> Result>, Error> { let pvscan_out = Command::new("pvscan") .invoke(crate::ErrorKind::DiskManagement) .await?; let pvscan_out_str = std::str::from_utf8(&pvscan_out)?; - let disk_guids = parse_pvscan_output(pvscan_out_str); + Ok(parse_pvscan_output(pvscan_out_str)) +} + +#[instrument] +pub async fn list() -> Result, Error> { + let disk_guids = pvscan().await?; let disks = tokio_stream::wrappers::ReadDirStream::new( tokio::fs::read_dir(DISK_PATH) .await diff --git a/appmgr/src/setup.rs b/appmgr/src/setup.rs index b53f0b5bf..b69b220cd 100644 --- a/appmgr/src/setup.rs +++ b/appmgr/src/setup.rs @@ -18,7 +18,7 @@ use tracing::instrument; use crate::context::SetupContext; use crate::db::model::RecoveredPackageInfo; use crate::disk::main::DEFAULT_PASSWORD; -use crate::disk::util::{DiskInfo, PartitionInfo, TmpMountGuard}; +use crate::disk::util::{pvscan, DiskInfo, PartitionInfo, TmpMountGuard}; use crate::id::Id; use crate::install::PKG_PUBLIC_DIR; use crate::net::ssl::SslManager; @@ -144,8 +144,13 @@ pub async fn execute_inner( crate::ErrorKind::InvalidRequest, )); } - let guid = - crate::disk::main::create(&[embassy_logicalname], &ctx.datadir, DEFAULT_PASSWORD).await?; + let guid = crate::disk::main::create( + &[embassy_logicalname], + &pvscan().await?, + &ctx.datadir, + DEFAULT_PASSWORD, + ) + .await?; crate::disk::main::import(&guid, &ctx.datadir, DEFAULT_PASSWORD).await?; let password = argon2::hash_encoded( embassy_password.as_bytes(),