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)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 90EEAA02B for ; Mon, 4 Apr 2022 11:51:40 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 8883CB49D for ; Mon, 4 Apr 2022 11:51:10 +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 9A192B494 for ; Mon, 4 Apr 2022 11:51:09 +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 6DBF44595C for ; Mon, 4 Apr 2022 11:51:09 +0200 (CEST) From: Hannes Laimer To: pbs-devel@lists.proxmox.com Date: Mon, 4 Apr 2022 09:50:46 +0000 Message-Id: <20220404095048.25443-2-h.laimer@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220404095048.25443-1-h.laimer@proxmox.com> References: <20220404095048.25443-1-h.laimer@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.037 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 - Subject: [pbs-devel] [PATCH proxmox-backup 1/3] api2: disks endpoint return partitions 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: Mon, 04 Apr 2022 09:51:40 -0000 Signed-off-by: Hannes Laimer --- src/api2/node/disks/directory.rs | 2 +- src/api2/node/disks/mod.rs | 11 ++- src/api2/node/disks/zfs.rs | 2 +- src/tools/disks/mod.rs | 121 ++++++++++++++++++++++++++++++- 4 files changed, 130 insertions(+), 6 deletions(-) diff --git a/src/api2/node/disks/directory.rs b/src/api2/node/disks/directory.rs index bf1a1be6..79fe2624 100644 --- a/src/api2/node/disks/directory.rs +++ b/src/api2/node/disks/directory.rs @@ -149,7 +149,7 @@ pub fn create_datastore_disk( let auth_id = rpcenv.get_auth_id().unwrap(); - let info = get_disk_usage_info(&disk, true)?; + let info = get_disk_usage_info(&disk, true, false)?; 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 c44ccfee..478829fb 100644 --- a/src/api2/node/disks/mod.rs +++ b/src/api2/node/disks/mod.rs @@ -33,6 +33,12 @@ pub mod zfs; optional: true, default: false, }, + "include-partitions": { + description: "Include partitions.", + type: bool, + optional: true, + default: false, + }, "usage-type": { type: DiskUsageType, optional: true, @@ -53,11 +59,12 @@ pub mod zfs; /// List local disks pub fn list_disks( skipsmart: bool, + include_partitions: bool, usage_type: Option, ) -> Result, Error> { let mut list = Vec::new(); - for (_, info) in get_disks(None, skipsmart)? { + for (_, info) in get_disks(None, skipsmart, include_partitions)? { if let Some(ref usage_type) = usage_type { if info.used == *usage_type { list.push(info); @@ -140,7 +147,7 @@ pub fn initialize_disk( let auth_id = rpcenv.get_auth_id().unwrap(); - let info = get_disk_usage_info(&disk, true)?; + let info = get_disk_usage_info(&disk, true, false)?; 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 a542f9e0..5329f44f 100644 --- a/src/api2/node/disks/zfs.rs +++ b/src/api2/node/disks/zfs.rs @@ -177,7 +177,7 @@ pub fn create_zpool( let devices: Vec = devices.as_array().unwrap().iter() .map(|v| v.as_str().unwrap().to_string()).collect(); - let disk_map = crate::tools::disks::get_disks(None, true)?; + let disk_map = crate::tools::disks::get_disks(None, true, false)?; 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 94da7b3a..fb9c78a9 100644 --- a/src/tools/disks/mod.rs +++ b/src/tools/disks/mod.rs @@ -602,6 +602,26 @@ fn get_file_system_devices(lsblk_info: &[LsblkInfo]) -> Result, Err Ok(device_set) } +#[api()] +#[derive(Debug, Serialize, Deserialize, PartialEq)] +#[serde(rename_all = "lowercase")] +pub enum PartitionUsageType { + /// Partition is not used (as far we can tell) + Unused, + /// Partition is mounted + Mounted, + /// Partition is used by LVM + LVM, + /// Partition is used by ZFS + ZFS, + /// Partition is an EFI partition + EFI, + /// Partition is a BIOS partition + BIOS, + /// Partition contains a file system label + FileSystem, +} + #[api()] #[derive(Debug, Serialize, Deserialize, PartialEq)] #[serde(rename_all = "lowercase")] @@ -622,6 +642,23 @@ pub enum DiskUsageType { FileSystem, } +#[api()] +#[derive(Debug, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +/// Baic information about a partition +pub struct PartitionInfo { + /// The partition name + pub name: String, + /// What the partition is used for + pub used: PartitionUsageType, + /// The partition devpath + pub devpath: Option, + /// Size in bytes + pub size: Option, + /// Size in bytes + pub gpt: bool, +} + #[api( properties: { used: { @@ -632,6 +669,12 @@ pub enum DiskUsageType { }, status: { type: SmartStatus, + }, + partitions: { + optional: true, + items: { + type: PartitionInfo + } } } )] @@ -656,6 +699,8 @@ pub struct DiskUsageInfo { pub size: u64, /// Serisal number pub serial: Option, + /// Partitions on the device + pub partitions: Option>, /// Linux device path (/dev/xxx) pub devpath: Option, /// Set if disk contains a GPT partition table @@ -733,10 +778,14 @@ fn scan_partitions( } /// Get disk usage information for a single disk -pub fn get_disk_usage_info(disk: &str, no_smart: bool) -> Result { +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)?; + let mut map = get_disks(Some(filter), no_smart, include_partitions)?; if let Some(info) = map.remove(disk) { Ok(info) } else { @@ -750,6 +799,8 @@ pub fn get_disks( disks: Option>, // do no include data from smartctl no_smart: bool, + // include partitions + include_partitions: bool, ) -> Result, Error> { let disk_manager = DiskManage::new(); @@ -837,6 +888,71 @@ pub fn get_disks( let wwn = disk.wwn().map(|s| s.to_string_lossy().into_owned()); + let partitions: Option> = if include_partitions { + let lsblk_infos = get_lsblk_info(); + disk.partitions().map_or(None, |parts| { + Some( + parts + .iter() + .map(|(nr, disk)| { + let devpath = disk + .device_path() + .map(|p| p.to_owned()) + .map(|p| p.to_string_lossy().to_string()); + + let mut used = PartitionUsageType::Unused; + + if let Some(devnum) = disk.devnum().ok() { + if lvm_devices.contains(&devnum) { + used = PartitionUsageType::LVM; + } + if zfs_devices.contains(&devnum) { + used = PartitionUsageType::ZFS; + } + } + match disk.is_mounted() { + Ok(true) => used = PartitionUsageType::Mounted, + Ok(false) => {} + Err(_) => {} // skip devices with undetectable mount status + } + + if let Some(devpath) = devpath.as_ref() { + if let Ok(infos) = lsblk_infos.as_ref() { + for info in 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 + } + _ => used, + } + } + } + } + + if used == PartitionUsageType::Unused + && file_system_devices.contains(&devnum) + { + used = PartitionUsageType::FileSystem; + } + + PartitionInfo { + name: format!("{}{}", name, nr), + devpath, + used, + size: disk.size().ok(), + gpt: disk.has_gpt(), + } + }) + .collect(), + ) + }) + } else { + None + }; + if usage != DiskUsageType::Mounted { match scan_partitions(disk_manager.clone(), &lvm_devices, &zfs_devices, &name) { Ok(part_usage) => { @@ -870,6 +986,7 @@ pub fn get_disks( name: name.clone(), vendor, model, + partitions, serial, devpath, size, -- 2.30.2