Skip to content

Commit

Permalink
Fix for case where metadata needs to be read but device is not activated
Browse files Browse the repository at this point in the history
  • Loading branch information
jbaublitz committed Nov 13, 2023
1 parent d011666 commit ba62ef1
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions src/engine/strat_engine/crypt/handle/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,12 @@ pub fn load_crypt_metadata(
.into_iter()
.collect::<PathBuf>();
let activated_path = path.canonicalize().unwrap_or(path);
let devno = get_devno_from_path(&activated_path)?;
let size = blkdev_size(&File::open(&activated_path)?)?.sectors();
Ok(Some(CryptMetadata {
physical_path: physical,
pool_uuid,
encryption_info,
activation_name,
activated_path,
device: devno,
size,
}))
}

Expand All @@ -120,8 +116,6 @@ pub struct CryptMetadata {
pub encryption_info: EncryptionInfo,
pub activation_name: DmNameBuf,
pub activated_path: PathBuf,
pub device: Device,
pub size: Sectors,
}

/// Check whether the physical device path corresponds to an encrypted
Expand Down Expand Up @@ -204,12 +198,15 @@ pub fn setup_crypt_handle(
)?
}

let device = get_devno_from_path(&metadata.activated_path)?;
let size = blkdev_size(&File::open(&metadata.activated_path)?)?.sectors();

Ok(Some(CryptHandle::new(
metadata.physical_path,
metadata.pool_uuid,
metadata.encryption_info,
metadata.device,
metadata.size,
device,
size,
)))
}

Expand All @@ -220,6 +217,8 @@ pub fn setup_crypt_handle(
#[derive(Debug, Clone)]
pub struct CryptHandle {
metadata: CryptMetadata,
device: Device,
size: Sectors,
}

impl CryptHandle {
Expand All @@ -241,10 +240,10 @@ impl CryptHandle {
pool_uuid,
encryption_info,
activation_name,
device: devno,
activated_path,
size,
},
device: devno,
size,
}
}

Expand Down Expand Up @@ -504,7 +503,7 @@ impl CryptHandle {
/// Get the device size for this encrypted device.
#[cfg(test)]
pub fn size(&self) -> Sectors {
self.metadata.size
self.size
}

/// Get the encryption info for this encrypted device.
Expand All @@ -531,7 +530,7 @@ impl CryptHandle {

/// Device number for the LUKS2 encrypted device.
pub fn device(&self) -> Device {
self.metadata.device
self.device
}

/// Get the keyslot associated with the given token ID.
Expand Down Expand Up @@ -774,7 +773,7 @@ impl CryptHandle {
.context_handle()
.resize(&self.activation_name().to_string(), processed_size)
.map_err(StratisError::Crypt)?;
self.metadata.size = blkdev_size(&File::open(&self.metadata.activated_path)?)?.sectors();
self.size = blkdev_size(&File::open(&self.metadata.activated_path)?)?.sectors();
Ok(())
}
}
Expand Down Expand Up @@ -1037,8 +1036,6 @@ mod tests {

fn test_both_initialize(paths: &[&Path]) {
fn both_initialize(paths: &[&Path], key_desc: &KeyDescription, pool_uuid: PoolUuid) {
unshare_mount_namespace().unwrap();
let _memfs = MemoryFilesystem::new().unwrap();
let path = paths.get(0).copied().expect("Expected exactly one path");
let handle = CryptHandle::initialize(
path,
Expand Down

0 comments on commit ba62ef1

Please sign in to comment.