Skip to content

Commit

Permalink
Remove udev settle and use canonicalized path for crypt activated dev…
Browse files Browse the repository at this point in the history
…ice path
  • Loading branch information
jbaublitz committed Apr 8, 2022
1 parent 722c046 commit 485e54b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 42 deletions.
26 changes: 21 additions & 5 deletions src/engine/strat_engine/backstore/crypt/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use std::{fmt::Debug, path::Path};
use std::{
fmt::Debug,
iter::once,
path::{Path, PathBuf},
};

use either::Either;
use serde_json::Value;
Expand All @@ -23,6 +27,7 @@ use crate::{
},
},
cmd::{clevis_decrypt, clevis_luks_bind, clevis_luks_regen, clevis_luks_unbind},
dm::DEVICEMAPPER_PATH,
keys::MemoryPrivateFilesystem,
metadata::StratisIdentifiers,
},
Expand All @@ -43,6 +48,7 @@ use crate::{
/// a next crypt device context for each operation.
#[derive(Debug, Clone)]
pub struct CryptHandle {
activated_path: DevicePath,
metadata_handle: CryptMetadataHandle,
}

Expand All @@ -52,7 +58,7 @@ impl CryptHandle {
identifiers: StratisIdentifiers,
encryption_info: EncryptionInfo,
name: String,
) -> CryptHandle {
) -> StratisResult<CryptHandle> {
CryptHandle::new_with_metadata_handle(CryptMetadataHandle::new(
physical_path,
identifiers,
Expand All @@ -61,8 +67,18 @@ impl CryptHandle {
))
}

pub(super) fn new_with_metadata_handle(metadata_handle: CryptMetadataHandle) -> CryptHandle {
CryptHandle { metadata_handle }
pub(super) fn new_with_metadata_handle(
metadata_handle: CryptMetadataHandle,
) -> StratisResult<CryptHandle> {
let activated_path = DevicePath::new(
&once(DEVICEMAPPER_PATH)
.chain(once(metadata_handle.name()))
.collect::<PathBuf>(),
)?;
Ok(CryptHandle {
activated_path,
metadata_handle,
})
}

/// Acquire the crypt device handle for the physical path in this `CryptHandle`.
Expand Down Expand Up @@ -107,7 +123,7 @@ impl CryptHandle {
/// storage device. In an encrypted pool, this is the path that can be used to read
/// the Stratis blockdev metatdata.
pub fn activated_device_path(&self) -> &Path {
self.metadata_handle.activated_path()
&*self.activated_path
}

/// Return the name of the activated devicemapper device.
Expand Down
31 changes: 14 additions & 17 deletions src/engine/strat_engine/backstore/crypt/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,34 +82,31 @@ impl CryptInitializer {
MetadataSize::try_from(DEFAULT_CRYPT_METADATA_SIZE)?,
KeyslotsSize::try_from(DEFAULT_CRYPT_KEYSLOTS_SIZE)?,
)?;
let result = self
self
.initialize_with_err(&mut device, key_description, clevis_parsed)
.and_then(|path| clevis_info_from_metadata(&mut device).map(|ci| (path, ci)));

match result {
Ok((_, clevis_info)) => {
.and_then(|path| clevis_info_from_metadata(&mut device).map(|ci| (path, ci)))
.and_then(|(_, clevis_info)| {
let encryption_info =
EncryptionInfo::from_options((key_description.cloned(), clevis_info))
.expect("Encrypted device must be provided encryption parameters");
Ok(CryptHandle::new(
self.physical_path,
CryptHandle::new(
self.physical_path.clone(),
self.identifiers,
encryption_info,
self.activation_name,
))
}
Err(e) => {
self.activation_name.clone(),
)
})
.map_err(|e| {
if let Err(err) =
Self::rollback(&mut device, &self.physical_path, self.activation_name)
Self::rollback(&mut device, &self.physical_path, &self.activation_name)
{
warn!(
"Failed to roll back crypt device initialization; you may need to manually wipe this device: {}",
err
);
}
Err(e)
}
}
e
})
}

/// Initialize with a passphrase in the kernel keyring only.
Expand Down Expand Up @@ -255,8 +252,8 @@ impl CryptInitializer {
pub fn rollback(
device: &mut CryptDevice,
physical_path: &Path,
name: String,
name: &str,
) -> StratisResult<()> {
ensure_wiped(device, physical_path, &name)
ensure_wiped(device, physical_path, name)
}
}
17 changes: 2 additions & 15 deletions src/engine/strat_engine/backstore/crypt/metadata_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use std::{
iter::once,
path::{Path, PathBuf},
};
use std::path::Path;

use crate::{
engine::{
strat_engine::{
backstore::crypt::shared::{setup_crypt_device, setup_crypt_metadata_handle},
dm::DEVICEMAPPER_PATH,
metadata::StratisIdentifiers,
},
types::{DevicePath, EncryptionInfo},
Expand All @@ -26,7 +22,6 @@ pub struct CryptMetadataHandle {
pub(super) identifiers: StratisIdentifiers,
pub(super) encryption_info: EncryptionInfo,
pub(super) name: String,
pub(super) activated_path: PathBuf,
}

impl CryptMetadataHandle {
Expand All @@ -40,10 +35,7 @@ impl CryptMetadataHandle {
physical_path,
identifiers,
encryption_info,
name: name.clone(),
activated_path: once(DEVICEMAPPER_PATH.to_string())
.chain(once(name))
.collect::<PathBuf>(),
name,
}
}

Expand Down Expand Up @@ -75,9 +67,4 @@ impl CryptMetadataHandle {
pub fn name(&self) -> &str {
&self.name
}

/// Get the path of the activated device.
pub fn activated_path(&self) -> &Path {
&self.activated_path
}
}
14 changes: 13 additions & 1 deletion src/engine/strat_engine/backstore/crypt/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,19 @@ pub fn setup_crypt_handle(
},
};

Ok(Some(CryptHandle::new_with_metadata_handle(metadata_handle)))
match CryptHandle::new_with_metadata_handle(metadata_handle) {
Ok(h) => Ok(Some(h)),
Err(e) => {
if let Err(err) = ensure_inactive(device, &name) {
Err(StratisError::NoActionRollbackError {
causal_error: Box::new(e),
rollback_error: Box::new(err),
})
} else {
Err(e)
}
}
}
}

/// Create a device handle and load the LUKS2 header into memory from
Expand Down
4 changes: 0 additions & 4 deletions src/engine/strat_engine/liminal/liminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use crate::{
engine::Pool,
strat_engine::{
backstore::{CryptActivationHandle, CryptHandle},
cmd::udev_settle,
liminal::{
device_info::{DeviceSet, LInfo, LLuksInfo, LStratisInfo},
identify::{
Expand Down Expand Up @@ -192,9 +191,6 @@ impl LiminalDevices {
.stopped_pools
.remove(&pool_uuid)
.expect("Checked above");
if let Err(e) = udev_settle() {
warn!("Failed to wait for udev processing to complete: {}", e);
}
match find_pool_stratis_devices(
unlocked_devices
.iter()
Expand Down

0 comments on commit 485e54b

Please sign in to comment.