Implement auto detection of encrypted root devices (fix #21)

This commit is contained in:
Roey Darwish Dror
2019-06-18 22:53:57 +03:00
parent 8551a815c4
commit 4f7b834ce3
5 changed files with 28 additions and 12 deletions

View File

@@ -2,10 +2,16 @@ use super::markers::BlockDevice;
use crate::error::{Error, ErrorKind};
use crate::process::CommandExt;
use crate::tool::Tool;
use failure::ResultExt;
use log::{debug, warn};
use std::fs;
use std::io::Read;
use std::marker::PhantomData;
use std::path::{Path, PathBuf};
static LUKS_MAGIC_1: &'static [u8] = &[0x4c, 0x55, 0x4b, 0x53, 0xba, 0xbe];
static LUKS_MAGIC_2: &'static [u8] = &[0x53, 0x4b, 0x55, 0x4c, 0xba, 0xbe];
#[derive(Debug)]
pub struct EncryptedDevice<'t, 'o> {
cryptsetup: &'t Tool,
@@ -78,3 +84,17 @@ impl<'t, 'o> BlockDevice for EncryptedDevice<'t, 'o> {
&self.path
}
}
pub fn is_encrypted_device(device: &BlockDevice) -> Result<bool, Error> {
let mut f = fs::OpenOptions::new()
.read(true)
.write(false)
.open(device.path())
.context(ErrorKind::LuksDetection)?;
let mut buffer = [0; 6];
f.read_exact(&mut buffer)
.context(ErrorKind::LuksDetection)?;
Ok(buffer == LUKS_MAGIC_1 || buffer == LUKS_MAGIC_2)
}

View File

@@ -6,7 +6,7 @@ mod mount_stack;
mod partition;
mod storage_device;
pub use crypt::EncryptedDevice;
pub use crypt::{is_encrypted_device, EncryptedDevice};
pub use filesystem::{Filesystem, FilesystemType};
pub use loop_device::LoopDevice;
pub use markers::BlockDevice;