From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id 1E4461FF173 for ; Mon, 11 Nov 2024 14:16:31 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 9E04CB4BA; Mon, 11 Nov 2024 14:16:03 +0100 (CET) From: Christoph Heiss To: pve-devel@lists.proxmox.com Date: Mon, 11 Nov 2024 14:15:03 +0100 Message-ID: <20241111131519.867887-8-c.heiss@proxmox.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241111131519.867887-1-c.heiss@proxmox.com> References: <20241111131519.867887-1-c.heiss@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.030 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy 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 Subject: [pve-devel] [PATCH installer v4 07/12] auto-installer: move `SystemDMI` struct to common crate X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox VE development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pve-devel-bounces@lists.proxmox.com Sender: "pve-devel" This functionality will be reused by the post-hook, which sends this data as part of its information set. Signed-off-by: Christoph Heiss --- Changes v3 -> v4: * no changes Changes v2 -> v3: * no changes 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, - baseboard: HashMap, - chassis: HashMap, -} - -impl SystemDMI { - pub(crate) fn get() -> Result { - 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> { - let mut res: HashMap = 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 85fc399..10b5940 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, + baseboard: HashMap, + chassis: HashMap, +} + +impl SystemDMI { + pub fn get() -> Result { + 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> { + let mut res: HashMap = 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.47.0 _______________________________________________ pve-devel mailing list pve-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel