public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Christoph Heiss <c.heiss@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH installer v2 14/17] auto-installer: move `SystemDMI` struct to common crate
Date: Thu, 18 Jul 2024 15:48:59 +0200	[thread overview]
Message-ID: <20240718134905.1177775-15-c.heiss@proxmox.com> (raw)
In-Reply-To: <20240718134905.1177775-1-c.heiss@proxmox.com>

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@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



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


  parent reply	other threads:[~2024-07-18 13:49 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-18 13:48 [pve-devel] [PATCH installer v2 00/17] fix #5536: implement post-(auto-)installation notification mechanism Christoph Heiss
2024-07-18 13:48 ` [pve-devel] [PATCH installer v2 01/17] tree-wide: fix some typos Christoph Heiss
2024-07-18 13:48 ` [pve-devel] [PATCH installer v2 02/17] fetch-answer: partition: fix clippy warning Christoph Heiss
2024-07-18 13:48 ` [pve-devel] [PATCH installer v2 03/17] common: simplify filesystem type serializing & Display trait impl Christoph Heiss
2024-07-18 13:48 ` [pve-devel] [PATCH installer v2 04/17] common: setup: serialize `target_hd` as string explicitly Christoph Heiss
2024-07-18 13:48 ` [pve-devel] [PATCH installer v2 05/17] common: split out installer setup files loading functionality Christoph Heiss
2024-07-18 13:48 ` [pve-devel] [PATCH installer v2 06/17] common: setup: deserialize `secure_boot` property from runtime env Christoph Heiss
2024-07-23 14:31   ` Aaron Lauterer
2024-08-05 13:12     ` Christoph Heiss
2024-08-19 10:32     ` Christoph Heiss
2024-08-20 14:56       ` Christoph Heiss
2024-07-18 13:48 ` [pve-devel] [PATCH installer v2 07/17] debian: strip unused library dependencies Christoph Heiss
2024-07-18 13:48 ` [pve-devel] [PATCH installer v2 08/17] fetch-answer: move http-related code to gated module in installer-common Christoph Heiss
2024-07-18 13:48 ` [pve-devel] [PATCH installer v2 09/17] tree-wide: convert some more crates to use workspace dependencies Christoph Heiss
2024-07-18 13:48 ` [pve-devel] [PATCH installer v2 10/17] auto-install-assistant: replace `PathBuf` parameters with `AsRef<Path>` Christoph Heiss
2024-07-18 13:48 ` [pve-devel] [PATCH installer v2 11/17] auto-installer: tests: replace manual panic!() with assert_eq!() Christoph Heiss
2024-07-23 10:39   ` Aaron Lauterer
2024-07-23 10:40     ` Aaron Lauterer
2024-07-23 10:46     ` Christoph Heiss
2024-07-23 11:04       ` Aaron Lauterer
2024-07-23 11:37         ` Christoph Heiss
2024-07-24  7:54           ` Thomas Lamprecht
2024-07-18 13:48 ` [pve-devel] [PATCH installer v2 12/17] auto-installer: tests: simplify empty disks check Christoph Heiss
2024-07-18 13:48 ` [pve-devel] [PATCH installer v2 13/17] auto-installer: tests: replace `PathBuf` parameters with `AsRef<Path>` Christoph Heiss
2024-07-18 13:48 ` Christoph Heiss [this message]
2024-07-18 13:49 ` [pve-devel] [PATCH installer v2 15/17] fix #5536: auto-installer: answer: add `posthook` section Christoph Heiss
2024-07-18 13:49 ` [pve-devel] [PATCH installer v2 16/17] fix #5536: post-hook: add utility for sending notifications after auto-install Christoph Heiss
2024-07-23 14:57   ` Aaron Lauterer
2024-07-24  8:24     ` Thomas Lamprecht
2024-07-24 11:21   ` Aaron Lauterer
2024-08-05 13:17     ` Christoph Heiss
2024-07-18 13:49 ` [pve-devel] [PATCH installer v2 17/17] unconfigured.sh: run proxmox-post-hook after successful auto-install Christoph Heiss
2024-07-24 11:34 ` [pve-devel] [PATCH installer v2 00/17] fix #5536: implement post-(auto-)installation notification mechanism Aaron Lauterer
2024-08-21  9:41 ` Christoph Heiss

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240718134905.1177775-15-c.heiss@proxmox.com \
    --to=c.heiss@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal