[pve-devel] [PATCH installer v2 14/17] auto-installer: move `SystemDMI` struct to common crate

Christoph Heiss c.heiss at proxmox.com
Thu Jul 18 15:48:59 CEST 2024


This functionality will be reused by the post-hook, which sends this
data as part of its information set.

Signed-off-by: Christoph Heiss <c.heiss at proxmox.com>
---
Changes v1 -> v2:
  * new patch
---
 proxmox-auto-installer/src/sysinfo.rs   | 51 +-----------------------
 proxmox-installer-common/src/lib.rs     |  1 +
 proxmox-installer-common/src/sysinfo.rs | 52 +++++++++++++++++++++++++
 3 files changed, 55 insertions(+), 49 deletions(-)
 create mode 100644 proxmox-installer-common/src/sysinfo.rs

diff --git a/proxmox-auto-installer/src/sysinfo.rs b/proxmox-auto-installer/src/sysinfo.rs
index 112e898..0a6aaf2 100644
--- a/proxmox-auto-installer/src/sysinfo.rs
+++ b/proxmox-auto-installer/src/sysinfo.rs
@@ -1,15 +1,14 @@
 use anyhow::{bail, Result};
 use proxmox_installer_common::{
     setup::{IsoInfo, ProductConfig, SetupInfo},
+    sysinfo::SystemDMI,
     RUNTIME_DIR,
 };
 use serde::Serialize;
-use std::{collections::HashMap, fs, io, path::PathBuf};
+use std::{fs, io, path::PathBuf};
 
 use crate::utils::get_nic_list;
 
-const DMI_PATH: &str = "/sys/devices/virtual/dmi/id";
-
 #[derive(Debug, Serialize)]
 pub struct SysInfo {
     product: ProductConfig,
@@ -70,49 +69,3 @@ impl NetdevWithMac {
         Ok(result)
     }
 }
-
-#[derive(Debug, Serialize)]
-struct SystemDMI {
-    system: HashMap<String, String>,
-    baseboard: HashMap<String, String>,
-    chassis: HashMap<String, String>,
-}
-
-impl SystemDMI {
-    pub(crate) fn get() -> Result<Self> {
-        let system_files = [
-            "product_serial",
-            "product_sku",
-            "product_uuid",
-            "product_name",
-        ];
-        let baseboard_files = ["board_asset_tag", "board_serial", "board_name"];
-        let chassis_files = ["chassis_serial", "chassis_sku", "chassis_asset_tag"];
-
-        Ok(Self {
-            system: Self::get_dmi_infos(&system_files)?,
-            baseboard: Self::get_dmi_infos(&baseboard_files)?,
-            chassis: Self::get_dmi_infos(&chassis_files)?,
-        })
-    }
-
-    fn get_dmi_infos(files: &[&str]) -> Result<HashMap<String, String>> {
-        let mut res: HashMap<String, String> = HashMap::new();
-
-        for file in files {
-            let path = format!("{DMI_PATH}/{file}");
-            let content = match fs::read_to_string(&path) {
-                Err(ref err) if err.kind() == std::io::ErrorKind::NotFound => continue,
-                Err(ref err) if err.kind() == std::io::ErrorKind::PermissionDenied => {
-                    bail!("Could not read data. Are you running as root or with sudo?")
-                }
-                Err(err) => bail!("Error: '{err}' on '{path}'"),
-                Ok(content) => content.trim().into(),
-            };
-            let key = file.splitn(2, '_').last().unwrap();
-            res.insert(key.into(), content);
-        }
-
-        Ok(res)
-    }
-}
diff --git a/proxmox-installer-common/src/lib.rs b/proxmox-installer-common/src/lib.rs
index ee9f2a8..59f1ab1 100644
--- a/proxmox-installer-common/src/lib.rs
+++ b/proxmox-installer-common/src/lib.rs
@@ -1,6 +1,7 @@
 pub mod disk_checks;
 pub mod options;
 pub mod setup;
+pub mod sysinfo;
 pub mod utils;
 
 #[cfg(feature = "http")]
diff --git a/proxmox-installer-common/src/sysinfo.rs b/proxmox-installer-common/src/sysinfo.rs
new file mode 100644
index 0000000..9746cb2
--- /dev/null
+++ b/proxmox-installer-common/src/sysinfo.rs
@@ -0,0 +1,52 @@
+use std::{collections::HashMap, fs};
+
+use anyhow::{bail, Result};
+use serde::Serialize;
+
+const DMI_PATH: &str = "/sys/devices/virtual/dmi/id";
+
+#[derive(Debug, Serialize)]
+pub struct SystemDMI {
+    system: HashMap<String, String>,
+    baseboard: HashMap<String, String>,
+    chassis: HashMap<String, String>,
+}
+
+impl SystemDMI {
+    pub fn get() -> Result<Self> {
+        let system_files = [
+            "product_serial",
+            "product_sku",
+            "product_uuid",
+            "product_name",
+        ];
+        let baseboard_files = ["board_asset_tag", "board_serial", "board_name"];
+        let chassis_files = ["chassis_serial", "chassis_sku", "chassis_asset_tag"];
+
+        Ok(Self {
+            system: Self::get_dmi_infos(&system_files)?,
+            baseboard: Self::get_dmi_infos(&baseboard_files)?,
+            chassis: Self::get_dmi_infos(&chassis_files)?,
+        })
+    }
+
+    fn get_dmi_infos(files: &[&str]) -> Result<HashMap<String, String>> {
+        let mut res: HashMap<String, String> = HashMap::new();
+
+        for file in files {
+            let path = format!("{DMI_PATH}/{file}");
+            let content = match fs::read_to_string(&path) {
+                Err(ref err) if err.kind() == std::io::ErrorKind::NotFound => continue,
+                Err(ref err) if err.kind() == std::io::ErrorKind::PermissionDenied => {
+                    bail!("Could not read data. Are you running as root or with sudo?")
+                }
+                Err(err) => bail!("Error: '{err}' on '{path}'"),
+                Ok(content) => content.trim().into(),
+            };
+            let key = file.splitn(2, '_').last().unwrap();
+            res.insert(key.into(), content);
+        }
+
+        Ok(res)
+    }
+}
-- 
2.45.1





More information about the pve-devel mailing list