src/tools/disks/zfs.rs: make zfs list parser private

This commit is contained in:
Dietmar Maurer 2020-06-17 07:00:54 +02:00
parent c72ccd4e33
commit 66af7f51bc
2 changed files with 24 additions and 17 deletions

View File

@ -17,6 +17,7 @@ use proxmox::api::router::Router;
use crate::config::acl::{PRIV_SYS_AUDIT, PRIV_SYS_MODIFY}; use crate::config::acl::{PRIV_SYS_AUDIT, PRIV_SYS_MODIFY};
use crate::tools::disks::{ use crate::tools::disks::{
zpool_list,
DiskUsageType, DiskUsageType,
}; };
@ -127,12 +128,7 @@ pub struct ZpoolListItem {
/// List zfs pools. /// List zfs pools.
pub fn list_zpools() -> Result<Vec<ZpoolListItem>, Error> { pub fn list_zpools() -> Result<Vec<ZpoolListItem>, Error> {
let mut command = std::process::Command::new("/sbin/zpool"); let data = zpool_list(None, false)?;
command.args(&["list", "-H", "-p", "-P"]);
let output = crate::tools::run_command(command, None)?;
let data = crate::tools::disks::parse_zfs_list(&output)?;
let mut list = Vec::new(); let mut list = Vec::new();

View File

@ -184,7 +184,7 @@ fn parse_pool_status(i: &str) -> IResult<&str, ZFSPoolStatus> {
/// ///
/// Note: This does not reveal any details on how the pool uses the devices, because /// Note: This does not reveal any details on how the pool uses the devices, because
/// the zpool list output format is not really defined... /// the zpool list output format is not really defined...
pub fn parse_zfs_list(i: &str) -> Result<Vec<ZFSPoolStatus>, Error> { fn parse_zpool_list(i: &str) -> Result<Vec<ZFSPoolStatus>, Error> {
match all_consuming(many0(parse_pool_status))(i) { match all_consuming(many0(parse_pool_status))(i) {
Err(nom::Err::Error(err)) | Err(nom::Err::Error(err)) |
Err(nom::Err::Failure(err)) => { Err(nom::Err::Failure(err)) => {
@ -197,25 +197,36 @@ pub fn parse_zfs_list(i: &str) -> Result<Vec<ZFSPoolStatus>, Error> {
} }
} }
/// Get set of devices used by zfs (or a specific zfs pool) /// Run zpool list and return parsed output
/// ///
/// The set is indexed by using the unix raw device number (dev_t is u64) /// Devices are only included when run with verbose flags
pub fn zfs_devices( /// set. Without, device lists are empty.
partition_type_map: &HashMap<String, Vec<String>>, pub fn zpool_list(pool: Option<String>, verbose: bool) -> Result<Vec<ZFSPoolStatus>, Error> {
pool: Option<&OsStr>,
) -> Result<HashSet<u64>, Error> {
// Note: zpools list output can include entries for 'special', 'cache' and 'logs' // Note: zpools list verbose output can include entries for 'special', 'cache' and 'logs'
// and maybe other things. // and maybe other things.
let mut command = std::process::Command::new("/sbin/zpool"); let mut command = std::process::Command::new("zpool");
command.args(&["list", "-H", "-v", "-p", "-P"]); command.args(&["list", "-H", "-p", "-P"]);
if verbose { command.arg("-v"); }
if let Some(pool) = pool { command.arg(pool); } if let Some(pool) = pool { command.arg(pool); }
let output = crate::tools::run_command(command, None)?; let output = crate::tools::run_command(command, None)?;
let list = parse_zfs_list(&output)?; parse_zpool_list(&output)
}
/// 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<String>,
) -> Result<HashSet<u64>, Error> {
let list = zpool_list(pool, true)?;
let mut device_set = HashSet::new(); let mut device_set = HashSet::new();
for entry in list { for entry in list {