From: Gabriel Goller Date: Tue, 17 Sep 2024 08:05:49 +0000 (+0200) Subject: api: avoid retrieving lsblk result twice X-Git-Tag: v3.2.9~8 X-Git-Url: https://git.proxmox.com/?a=commitdiff_plain;h=b3d9b6d5f157b4bbb75f760e9b48d583b57bd656;p=proxmox-backup.git api: avoid retrieving lsblk result twice Avoid running `lsblk` twice when executing the `list_disk` endpoint/command. This and the various other small nits improve the performance of the endpoint. Does not really fix, but is related to: #4961. Signed-off-by: Gabriel Goller --- diff --git a/src/api2/node/disks/mod.rs b/src/api2/node/disks/mod.rs index 4ef4ee2b..abcb8ee4 100644 --- a/src/api2/node/disks/mod.rs +++ b/src/api2/node/disks/mod.rs @@ -115,7 +115,11 @@ pub fn smart_status(disk: String, healthonly: Option) -> Result, zfs_devices: &HashSet, file_system_devices: &HashSet, + lsblk_infos: &[LsblkInfo], ) -> Vec { - let lsblk_infos = get_lsblk_info().ok(); partitions .values() .map(|disk| { @@ -889,8 +889,8 @@ fn get_partitions_info( let mounted = disk.is_mounted().unwrap_or(false); let mut filesystem = None; - if let (Some(devpath), Some(infos)) = (devpath.as_ref(), lsblk_infos.as_ref()) { - for info in infos.iter().filter(|i| i.path.eq(devpath)) { + if let Some(devpath) = devpath.as_ref() { + for info in lsblk_infos.iter().filter(|i| i.path.eq(devpath)) { used = match info.partition_type.as_deref() { Some("21686148-6449-6e6f-744e-656564454649") => PartitionUsageType::BIOS, Some("c12a7328-f81f-11d2-ba4b-00a0c93ec93b") => PartitionUsageType::EFI, @@ -944,6 +944,7 @@ fn get_disks( // fixme: ceph journals/volumes let mut result = HashMap::new(); + let mut device_paths = Vec::new(); for item in proxmox_sys::fs::scan_subdir(libc::AT_FDCWD, "/sys/block", &BLOCKDEVICE_NAME_REGEX)? { @@ -1011,6 +1012,8 @@ fn get_disks( .map(|p| p.to_owned()) .map(|p| p.to_string_lossy().to_string()); + device_paths.push((name.clone(), devpath.clone())); + let wwn = disk.wwn().map(|s| s.to_string_lossy().into_owned()); let partitions: Option> = if include_partitions { @@ -1020,6 +1023,7 @@ fn get_disks( &lvm_devices, &zfs_devices, &file_system_devices, + &lsblk_info, )) }) } else { diff --git a/src/tools/disks/smart.rs b/src/tools/disks/smart.rs index 3ad782b7..4868815f 100644 --- a/src/tools/disks/smart.rs +++ b/src/tools/disks/smart.rs @@ -1,8 +1,8 @@ -use std::collections::{HashMap, HashSet}; use std::sync::LazyLock; +use std::{collections::{HashMap, HashSet}, path::Path}; use ::serde::{Deserialize, Serialize}; -use anyhow::{bail, Error}; +use anyhow::Error; use proxmox_schema::api; @@ -76,7 +76,7 @@ pub struct SmartData { } /// Read smartctl data for a disk (/dev/XXX). -pub fn get_smart_data(disk: &super::Disk, health_only: bool) -> Result { +pub fn get_smart_data(disk_path: &Path, health_only: bool) -> Result { const SMARTCTL_BIN_PATH: &str = "smartctl"; let mut command = std::process::Command::new(SMARTCTL_BIN_PATH); @@ -85,10 +85,6 @@ pub fn get_smart_data(disk: &super::Disk, health_only: bool) -> Result path, - None => bail!("disk {:?} has no node in /dev", disk.syspath()), - }; command.arg(disk_path); let output = proxmox_sys::command::run_command( diff --git a/src/tools/disks/zfs.rs b/src/tools/disks/zfs.rs index 2abb5176..3b7da154 100644 --- a/src/tools/disks/zfs.rs +++ b/src/tools/disks/zfs.rs @@ -70,7 +70,7 @@ pub fn zfs_pool_stats(pool: &OsStr) -> Result, Error> { /// /// The set is indexed by using the unix raw device number (dev_t is u64) pub fn zfs_devices(lsblk_info: &[LsblkInfo], pool: Option) -> Result, Error> { - let list = zpool_list(pool, true)?; + let list = zpool_list(pool.as_ref(), true)?; let mut device_set = HashSet::new(); for entry in list { @@ -79,12 +79,13 @@ pub fn zfs_devices(lsblk_info: &[LsblkInfo], pool: Option) -> Result Result, Error> { /// /// Devices are only included when run with verbose flags /// set. Without, device lists are empty. -pub fn zpool_list(pool: Option, verbose: bool) -> Result, Error> { +pub fn zpool_list(pool: Option<&String>, verbose: bool) -> Result, Error> { // Note: zpools list verbose output can include entries for 'special', 'cache' and 'logs' // and maybe other things.