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 A025274DA2 for ; Tue, 20 Apr 2021 11:53:54 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 9032E21537 for ; Tue, 20 Apr 2021 11:53:54 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (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 5B7EA21526 for ; Tue, 20 Apr 2021 11:53:50 +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 1A2C944D89 for ; Tue, 20 Apr 2021 11:53:50 +0200 (CEST) From: Fabian Ebner To: pbs-devel@lists.proxmox.com Date: Tue, 20 Apr 2021 11:53:45 +0200 Message-Id: <20210420095345.7775-2-f.ebner@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210420095345.7775-1-f.ebner@proxmox.com> References: <20210420095345.7775-1-f.ebner@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.007 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [disks.rs] Subject: [pbs-devel] [PATCH/RFC proxmox-backup 2/2] disks: also check for file systems with lsblk 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: Tue, 20 Apr 2021 09:53:54 -0000 so that whole disks with a file system that are not mounted are still detected as in-use. Reported-by: Thomas Lamprecht Signed-off-by: Fabian Ebner --- Ideally, we'd also show the type in the result, but the api macro complained when I tried using FileSystem(String) Mounted(String) // showing the fs type here would also be nice in the enum. Is there a good way to do this? An alternative would be replacing the DiskUsageType with a String in the DiskUsage struct, but that's not very nice... src/tools/disks.rs | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/tools/disks.rs b/src/tools/disks.rs index d374df0e..5eed723b 100644 --- a/src/tools/disks.rs +++ b/src/tools/disks.rs @@ -52,6 +52,8 @@ pub struct LsblkInfo { path: String, /// Partition type GUID. partition_type: Option, + /// File system label. + file_system_type: Option, } impl DiskManage { @@ -563,11 +565,11 @@ pub struct BlockDevStat { pub io_ticks: u64, // milliseconds } -/// Use lsblk to read partition type uuids. +/// Use lsblk to read partition type uuids and file system types. pub fn get_lsblk_info() -> Result, Error> { let mut command = std::process::Command::new("lsblk"); - command.args(&["--json", "-o", "path,parttype"]); + command.args(&["--json", "-o", "path,parttype,fstype"]); let output = crate::tools::run_command(command, None)?; @@ -582,9 +584,11 @@ pub fn get_lsblk_info() -> Result, Error> { None => continue, }; let partition_type = info["parttype"].as_str().map(|t| t.to_string()); + let file_system_type = info["fstype"].as_str().map(|t| t.to_string()); res.push(LsblkInfo { path, partition_type, + file_system_type, }); } } @@ -592,6 +596,25 @@ pub fn get_lsblk_info() -> Result, Error> { Ok(res) } +/// Get set of devices with a file system label. +/// +/// The set is indexed by using the unix raw device number (dev_t is u64) +fn get_file_system_devices( + lsblk_info: &[LsblkInfo], +) -> Result, Error> { + + let mut device_set: HashSet = HashSet::new(); + + for info in lsblk_info.iter() { + if info.file_system_type.is_some() { + let meta = std::fs::metadata(&info.path)?; + device_set.insert(meta.rdev()); + } + } + + Ok(device_set) +} + #[api()] #[derive(Debug, Serialize, Deserialize, PartialEq)] #[serde(rename_all="lowercase")] @@ -608,6 +631,8 @@ pub enum DiskUsageType { DeviceMapper, /// Disk has partitions Partitions, + /// Disk contains a file system label + FileSystem, } #[api( @@ -754,6 +779,8 @@ pub fn get_disks( let lvm_devices = get_lvm_devices(&lsblk_info)?; + let file_system_devices = get_file_system_devices(&lsblk_info)?; + // fixme: ceph journals/volumes let mut result = HashMap::new(); @@ -829,6 +856,10 @@ pub fn get_disks( }; } + if usage == DiskUsageType::Unused && file_system_devices.contains(&devnum) { + usage = DiskUsageType::FileSystem; + } + if usage == DiskUsageType::Unused && disk.has_holders()? { usage = DiskUsageType::DeviceMapper; } -- 2.20.1