From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 8E63870AC4 for ; Wed, 15 Jun 2022 08:18:10 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 76B5A26467 for ; Wed, 15 Jun 2022 08:17:40 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 937582645E for ; Wed, 15 Jun 2022 08:17:39 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 5FE4A43B47 for ; Wed, 15 Jun 2022 08:17:39 +0200 (CEST) From: Hannes Laimer To: pbs-devel@lists.proxmox.com Date: Wed, 15 Jun 2022 06:17:14 +0000 Message-Id: <20220615061714.4870-1-h.laimer@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220609103849.g4rxdo2mpz35froa@wobu-vie.proxmox.com> References: <20220609103849.g4rxdo2mpz35froa@wobu-vie.proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.044 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record T_SCC_BODY_TEXT_LINE -0.01 - URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [directory.rs, mod.rs, zfs.rs, self.smart] Subject: [pbs-devel] [PATCH proxmox-backup v3 2/3] disks: use builder pattern for querying disk usage X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 15 Jun 2022 06:18:10 -0000 Signed-off-by: Hannes Laimer --- previous patch was missing the removal of the not needed lifetimes, sorry for the noise v2, thanks @Wolfgang Bumiller * use just one struct for querying * remove not needed lifetimes the two 'find' functions arguments are for filtering the data and the builder functions are for defining what data the (maybe filtered) resulting elements should contain. src/api2/node/disks/directory.rs | 6 ++-- src/api2/node/disks/mod.rs | 12 +++++--- src/api2/node/disks/zfs.rs | 2 +- src/tools/disks/mod.rs | 53 +++++++++++++++++++++++--------- 4 files changed, 51 insertions(+), 22 deletions(-) diff --git a/src/api2/node/disks/directory.rs b/src/api2/node/disks/directory.rs index 123a8d7b..cada95cd 100644 --- a/src/api2/node/disks/directory.rs +++ b/src/api2/node/disks/directory.rs @@ -13,8 +13,8 @@ use pbs_api_types::{ }; use crate::tools::disks::{ - create_file_system, create_single_linux_partition, get_disk_usage_info, get_fs_uuid, - DiskManage, DiskUsageType, FileSystemType, + create_file_system, create_single_linux_partition, get_fs_uuid, DiskManage, DiskUsageQuery, + DiskUsageType, FileSystemType, }; use crate::tools::systemd::{self, types::*}; @@ -147,7 +147,7 @@ pub fn create_datastore_disk( 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 { bail!("disk '{}' is already in use.", disk); diff --git a/src/api2/node/disks/mod.rs b/src/api2/node/disks/mod.rs index 478829fb..c16be46a 100644 --- a/src/api2/node/disks/mod.rs +++ b/src/api2/node/disks/mod.rs @@ -12,8 +12,8 @@ use pbs_api_types::{ }; use crate::tools::disks::{ - get_disk_usage_info, get_disks, get_smart_data, inititialize_gpt_disk, DiskManage, - DiskUsageInfo, DiskUsageType, SmartData, + get_smart_data, inititialize_gpt_disk, DiskManage, DiskUsageInfo, DiskUsageQuery, + DiskUsageType, SmartData, }; use proxmox_rest_server::WorkerTask; @@ -64,7 +64,11 @@ pub fn list_disks( ) -> Result, Error> { 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 info.used == *usage_type { list.push(info); @@ -147,7 +151,7 @@ pub fn initialize_disk( 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 { bail!("disk '{}' is already in use.", disk); diff --git a/src/api2/node/disks/zfs.rs b/src/api2/node/disks/zfs.rs index 5cb23e70..3efc8a05 100644 --- a/src/api2/node/disks/zfs.rs +++ b/src/api2/node/disks/zfs.rs @@ -174,7 +174,7 @@ pub fn create_zpool( .map(|v| v.as_str().unwrap().to_string()) .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() { match disk_map.get(disk) { Some(info) => { diff --git a/src/tools/disks/mod.rs b/src/tools/disks/mod.rs index 61e0f17a..35ec9996 100644 --- a/src/tools/disks/mod.rs +++ b/src/tools/disks/mod.rs @@ -763,19 +763,44 @@ fn scan_partitions( Ok(used) } -/// Get disk usage information for a single disk -pub fn get_disk_usage_info( - disk: &str, - no_smart: bool, - include_partitions: bool, -) -> Result { - let mut filter = Vec::new(); - filter.push(disk.to_string()); - let mut map = get_disks(Some(filter), no_smart, include_partitions)?; - if let Some(info) = map.remove(disk) { - Ok(info) - } else { - bail!("failed to get disk usage info - internal error"); // should not happen +pub struct DiskUsageQuery { + smart: bool, + partitions: bool, +} + +impl DiskUsageQuery { + pub fn new() -> Self { + Self { + smart: true, + partitions: false, + } + } + + 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, Error> { + get_disks(None, !self.smart, self.partitions) + } + + pub fn find(&self, disk: &str) -> Result { + 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) -> Result, Error> { + get_disks(Some(disks), !self.smart, self.partitions) } } @@ -838,7 +863,7 @@ fn get_partitions_info( } /// Get disk usage information for multiple disks -pub fn get_disks( +fn get_disks( // filter - list of device names (without leading /dev) disks: Option>, // do no include data from smartctl -- 2.30.2