src/tools/disks.rs: use dev_t to index zfs/lvm device sets

This commit is contained in:
Dietmar Maurer
2020-06-08 09:01:34 +02:00
parent dfb31de8f0
commit d406de299b
3 changed files with 30 additions and 20 deletions

View File

@ -1,4 +1,5 @@
use std::collections::{HashSet, HashMap};
use std::os::unix::fs::MetadataExt;
use anyhow::{format_err, Error};
use serde_json::Value;
@ -12,10 +13,10 @@ lazy_static!{
};
}
/// Get list of devices used by LVM (pvs).
/// Get set of devices used by LVM (pvs).
pub fn get_lvm_devices(
partition_type_map: &HashMap<String, Vec<String>>,
) -> Result<HashSet<String>, Error> {
) -> Result<HashSet<u64>, Error> {
const PVS_BIN_PATH: &str = "/sbin/pvs";
@ -28,13 +29,14 @@ pub fn get_lvm_devices(
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();
let mut device_set: HashSet<u64> = 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 meta = std::fs::metadata(device)?;
device_set.insert(meta.rdev());
}
}
@ -44,7 +46,8 @@ pub fn get_lvm_devices(
Some(list) => {
for info in list {
if let Some(pv_name) = info["pv_name"].as_str() {
device_set.insert(pv_name.to_string());
let meta = std::fs::metadata(pv_name)?;
device_set.insert(meta.rdev());
}
}
}

View File

@ -1,5 +1,6 @@
use std::path::PathBuf;
use std::collections::{HashMap, HashSet};
use std::os::unix::fs::MetadataExt;
use anyhow::{bail, Error};
use lazy_static::lazy_static;
@ -168,11 +169,13 @@ pub fn parse_zfs_list(i: &str) -> Result<Vec<ZFSPoolStatus>, Error> {
}
}
/// List devices used by zfs (or a specific zfs pool)
/// Get set of devices used by zfs (or a specific zfs pool)
///
/// The set is indexed by using the unix raw device number (dev_t is u64)
pub fn zfs_devices(
partition_type_map: &HashMap<String, Vec<String>>,
pool: Option<&OsStr>,
) -> Result<HashSet<String>, Error> {
) -> Result<HashSet<u64>, Error> {
// Note: zpools list output can include entries for 'special', 'cache' and 'logs'
// and maybe other things.
@ -193,7 +196,8 @@ pub fn zfs_devices(
let mut device_set = HashSet::new();
for entry in list {
for device in entry.devices {
device_set.insert(device.clone());
let meta = std::fs::metadata(device)?;
device_set.insert(meta.rdev());
}
}
@ -201,7 +205,8 @@ pub fn zfs_devices(
.filter_map(|(uuid, list)| if ZFS_UUIDS.contains(uuid.as_str()) { Some(list) } else { None })
{
for device in device_list {
device_set.insert(device.clone());
let meta = std::fs::metadata(device)?;
device_set.insert(meta.rdev());
}
}