disks: use builder pattern for querying disk usage
Signed-off-by: Hannes Laimer <h.laimer@proxmox.com>
This commit is contained in:
parent
6a6ba4cdac
commit
be2604109d
@ -13,8 +13,8 @@ use pbs_api_types::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use crate::tools::disks::{
|
use crate::tools::disks::{
|
||||||
create_file_system, create_single_linux_partition, get_disk_usage_info, get_fs_uuid,
|
create_file_system, create_single_linux_partition, get_fs_uuid, DiskManage, DiskUsageQuery,
|
||||||
DiskManage, DiskUsageType, FileSystemType,
|
DiskUsageType, FileSystemType,
|
||||||
};
|
};
|
||||||
use crate::tools::systemd::{self, types::*};
|
use crate::tools::systemd::{self, types::*};
|
||||||
|
|
||||||
@ -147,7 +147,7 @@ pub fn create_datastore_disk(
|
|||||||
|
|
||||||
let auth_id = rpcenv.get_auth_id().unwrap();
|
let auth_id = rpcenv.get_auth_id().unwrap();
|
||||||
|
|
||||||
let info = get_disk_usage_info(&disk, true, false)?;
|
let info = DiskUsageQuery::new().smart(false).find(&disk)?;
|
||||||
|
|
||||||
if info.used != DiskUsageType::Unused {
|
if info.used != DiskUsageType::Unused {
|
||||||
bail!("disk '{}' is already in use.", disk);
|
bail!("disk '{}' is already in use.", disk);
|
||||||
|
@ -12,8 +12,8 @@ use pbs_api_types::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use crate::tools::disks::{
|
use crate::tools::disks::{
|
||||||
get_disk_usage_info, get_disks, get_smart_data, inititialize_gpt_disk, DiskManage,
|
get_smart_data, inititialize_gpt_disk, DiskManage, DiskUsageInfo, DiskUsageQuery,
|
||||||
DiskUsageInfo, DiskUsageType, SmartData,
|
DiskUsageType, SmartData,
|
||||||
};
|
};
|
||||||
use proxmox_rest_server::WorkerTask;
|
use proxmox_rest_server::WorkerTask;
|
||||||
|
|
||||||
@ -64,7 +64,11 @@ pub fn list_disks(
|
|||||||
) -> Result<Vec<DiskUsageInfo>, Error> {
|
) -> Result<Vec<DiskUsageInfo>, Error> {
|
||||||
let mut list = Vec::new();
|
let mut list = Vec::new();
|
||||||
|
|
||||||
for (_, info) in get_disks(None, skipsmart, include_partitions)? {
|
for (_, info) in DiskUsageQuery::new()
|
||||||
|
.smart(!skipsmart)
|
||||||
|
.partitions(include_partitions)
|
||||||
|
.query()?
|
||||||
|
{
|
||||||
if let Some(ref usage_type) = usage_type {
|
if let Some(ref usage_type) = usage_type {
|
||||||
if info.used == *usage_type {
|
if info.used == *usage_type {
|
||||||
list.push(info);
|
list.push(info);
|
||||||
@ -147,7 +151,7 @@ pub fn initialize_disk(
|
|||||||
|
|
||||||
let auth_id = rpcenv.get_auth_id().unwrap();
|
let auth_id = rpcenv.get_auth_id().unwrap();
|
||||||
|
|
||||||
let info = get_disk_usage_info(&disk, true, false)?;
|
let info = DiskUsageQuery::new().find(&disk)?;
|
||||||
|
|
||||||
if info.used != DiskUsageType::Unused {
|
if info.used != DiskUsageType::Unused {
|
||||||
bail!("disk '{}' is already in use.", disk);
|
bail!("disk '{}' is already in use.", disk);
|
||||||
|
@ -174,7 +174,7 @@ pub fn create_zpool(
|
|||||||
.map(|v| v.as_str().unwrap().to_string())
|
.map(|v| v.as_str().unwrap().to_string())
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let disk_map = crate::tools::disks::get_disks(None, true, false)?;
|
let disk_map = crate::tools::disks::DiskUsageQuery::new().query()?;
|
||||||
for disk in devices.iter() {
|
for disk in devices.iter() {
|
||||||
match disk_map.get(disk) {
|
match disk_map.get(disk) {
|
||||||
Some(info) => {
|
Some(info) => {
|
||||||
|
@ -763,19 +763,44 @@ fn scan_partitions(
|
|||||||
Ok(used)
|
Ok(used)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get disk usage information for a single disk
|
pub struct DiskUsageQuery {
|
||||||
pub fn get_disk_usage_info(
|
smart: bool,
|
||||||
disk: &str,
|
partitions: bool,
|
||||||
no_smart: bool,
|
}
|
||||||
include_partitions: bool,
|
|
||||||
) -> Result<DiskUsageInfo, Error> {
|
impl DiskUsageQuery {
|
||||||
let mut filter = Vec::new();
|
pub fn new() -> Self {
|
||||||
filter.push(disk.to_string());
|
Self {
|
||||||
let mut map = get_disks(Some(filter), no_smart, include_partitions)?;
|
smart: true,
|
||||||
if let Some(info) = map.remove(disk) {
|
partitions: false,
|
||||||
Ok(info)
|
}
|
||||||
} else {
|
}
|
||||||
bail!("failed to get disk usage info - internal error"); // should not happen
|
|
||||||
|
pub fn smart(&mut self, smart: bool) -> &mut Self {
|
||||||
|
self.smart = smart;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn partitions(&mut self, partitions: bool) -> &mut Self {
|
||||||
|
self.partitions = partitions;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn query(&self) -> Result<HashMap<String, DiskUsageInfo>, Error> {
|
||||||
|
get_disks(None, !self.smart, self.partitions)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn find(&self, disk: &str) -> Result<DiskUsageInfo, Error> {
|
||||||
|
let mut map = get_disks(Some(vec![disk.to_string()]), !self.smart, self.partitions)?;
|
||||||
|
if let Some(info) = map.remove(disk) {
|
||||||
|
Ok(info)
|
||||||
|
} else {
|
||||||
|
bail!("failed to get disk usage info - internal error"); // should not happen
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn find_all(&self, disks: Vec<String>) -> Result<HashMap<String, DiskUsageInfo>, Error> {
|
||||||
|
get_disks(Some(disks), !self.smart, self.partitions)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -838,7 +863,7 @@ fn get_partitions_info(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Get disk usage information for multiple disks
|
/// Get disk usage information for multiple disks
|
||||||
pub fn get_disks(
|
fn get_disks(
|
||||||
// filter - list of device names (without leading /dev)
|
// filter - list of device names (without leading /dev)
|
||||||
disks: Option<Vec<String>>,
|
disks: Option<Vec<String>>,
|
||||||
// do no include data from smartctl
|
// do no include data from smartctl
|
||||||
|
Loading…
Reference in New Issue
Block a user