]> git.proxmox.com Git - proxmox-backup.git/blob - src/tools/disks/lvm.rs
update to proxmox-sys 0.2 crate
[proxmox-backup.git] / src / tools / disks / lvm.rs
1 use std::collections::HashSet;
2 use std::os::unix::fs::MetadataExt;
3
4 use anyhow::{Error};
5 use serde_json::Value;
6 use lazy_static::lazy_static;
7
8 use super::LsblkInfo;
9
10 lazy_static!{
11 static ref LVM_UUIDS: HashSet<&'static str> = {
12 let mut set = HashSet::new();
13 set.insert("e6d6d379-f507-44c2-a23c-238f2a3df928");
14 set
15 };
16 }
17
18 /// Get set of devices used by LVM (pvs).
19 ///
20 /// The set is indexed by using the unix raw device number (dev_t is u64)
21 pub fn get_lvm_devices(
22 lsblk_info: &[LsblkInfo],
23 ) -> Result<HashSet<u64>, Error> {
24
25 const PVS_BIN_PATH: &str = "pvs";
26
27 let mut command = std::process::Command::new(PVS_BIN_PATH);
28 command.args(&["--reportformat", "json", "--noheadings", "--readonly", "-o", "pv_name"]);
29
30 let output = proxmox_sys::command::run_command(command, None)?;
31
32 let mut device_set: HashSet<u64> = HashSet::new();
33
34 for info in lsblk_info.iter() {
35 if let Some(partition_type) = &info.partition_type {
36 if LVM_UUIDS.contains(partition_type.as_str()) {
37 let meta = std::fs::metadata(&info.path)?;
38 device_set.insert(meta.rdev());
39 }
40 }
41 }
42
43 let output: Value = output.parse()?;
44
45 match output["report"][0]["pv"].as_array() {
46 Some(list) => {
47 for info in list {
48 if let Some(pv_name) = info["pv_name"].as_str() {
49 let meta = std::fs::metadata(pv_name)?;
50 device_set.insert(meta.rdev());
51 }
52 }
53 }
54 None => return Ok(device_set),
55 }
56
57 Ok(device_set)
58 }