src/tools/disks/lvm.rs: implement get_lvm_devices()

This commit is contained in:
Dietmar Maurer 2020-06-04 09:12:19 +02:00
parent 5c264c8d80
commit 620911b426
2 changed files with 57 additions and 0 deletions

View File

@ -18,6 +18,8 @@ use proxmox::{io_bail, io_format_err};
mod zfs; mod zfs;
pub use zfs::*; pub use zfs::*;
mod lvm;
pub use lvm::*;
bitflags! { bitflags! {
/// Ways a device is being used. /// Ways a device is being used.

55
src/tools/disks/lvm.rs Normal file
View File

@ -0,0 +1,55 @@
use std::collections::{HashSet, HashMap};
use anyhow::{format_err, Error};
use serde_json::Value;
use lazy_static::lazy_static;
lazy_static!{
static ref LVM_UUIDS: HashSet<&'static str> = {
let mut set = HashSet::new();
set.insert("e6d6d379-f507-44c2-a23c-238f2a3df928");
set
};
}
/// Get list of devices used by LVM (pvs).
pub fn get_lvm_devices(
partition_type_map: &HashMap<String, Vec<String>>,
) -> Result<HashSet<String>, Error> {
const PVS_BIN_PATH: &str = "/sbin/pvs";
let mut command = std::process::Command::new(PVS_BIN_PATH);
command.args(&["--reportformat", "json", "--noheadings", "--readonly", "-o", "pv_name"]);
let output = command.output()
.map_err(|err| format_err!("failed to execute '{}' - {}", PVS_BIN_PATH, err))?;
let output = crate::tools::command_output(output, None)
.map_err(|err| format_err!("pvs command failed: {}", err))?;
let mut device_set: HashSet<String> = HashSet::new();
for device_list in partition_type_map.iter()
.filter_map(|(uuid, list)| if LVM_UUIDS.contains(uuid.as_str()) { Some(list) } else { None })
{
for device in device_list {
device_set.insert(device.clone());
}
}
let output: Value = output.parse()?;
match output["report"][0]["pv"].as_array() {
Some(list) => {
for info in list {
if let Some(pv_name) = info["pv_name"].as_str() {
device_set.insert(pv_name.to_string());
}
}
}
None => return Ok(device_set),
}
Ok(device_set)
}