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 BC4169410F for ; Wed, 21 Feb 2024 15:10:20 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 95D3C18F26 for ; Wed, 21 Feb 2024 15:09:50 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (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 for ; Wed, 21 Feb 2024 15:09:49 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 92F63444AE for ; Wed, 21 Feb 2024 15:09:49 +0100 (CET) Date: Wed, 21 Feb 2024 15:09:48 +0100 From: Christoph Heiss To: Aaron Lauterer Cc: Proxmox VE development discussion Message-ID: <3bocddpb6wlnclu5frfje6qf6c6u5zu42ppxtczs2ocgnfwikn@4vfcgpnpwnj7> References: <20240221110805.931925-1-a.lauterer@proxmox.com> <20240221110805.931925-19-a.lauterer@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240221110805.931925-19-a.lauterer@proxmox.com> X-SPAM-LEVEL: Spam detection results: 0 AWL -0.121 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 KAM_LOTSOFHASH 0.25 Emails with lots of hash-like gibberish SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record T_SCC_BODY_TEXT_LINE -0.01 - Subject: Re: [pve-devel] [PATCH v2 18/22] auto-installer: fetch: add gathering of system identifiers and restructure code 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: , X-List-Received-Date: Wed, 21 Feb 2024 14:10:20 -0000 On Wed, Feb 21, 2024 at 12:08:01PM +0100, Aaron Lauterer wrote: > They will be used as payload when POSTing a request for an answer file. The > idea is, that with this information, it should be possible to identify > the system and generate a matching answer file on the fly. > Many of these properties can also be found on the machine or packaging > of the machine and could therefore be scanned into a database. > > Identifiers are the following properties from `dmidecode` sections 1, 2, > and 3: > * Asset Tag > * Product Name > * Serial Number > * SKU Number > * UUID > > As well as a list of the MAC addresses of all the NICs. [..] > diff --git a/proxmox-auto-installer/src/fetch_plugins/utils/sysinfo.rs b/proxmox-auto-installer/src/fetch_plugins/utils/sysinfo.rs > new file mode 100644 > index 0000000..74701cd > --- /dev/null > +++ b/proxmox-auto-installer/src/fetch_plugins/utils/sysinfo.rs > @@ -0,0 +1,200 @@ > +use anyhow::{bail, Result}; > +use serde::Serialize; > +use std::{collections::HashMap, fs, process::Command}; > + > +use super::get_nic_list; > + > +pub fn get_sysinfo(pretty: bool) -> Result { While looking at this and reading the `dmidecode` manpage a bit, it looks like everything collected here is also available under /sys/devices/virtual/dmi/id as separate files, e.g. # ls /sys/devices/virtual/dmi/id/ bios_date chassis_asset_tag product_name bios_release chassis_serial product_serial bios_vendor chassis_type product_sku bios_version chassis_vendor product_uuid board_asset_tag chassis_version product_version board_name ec_firmware_release subsystem board_serial modalias sys_vendor board_vendor power uevent board_version product_family Most of these file are even world-readable, just some (e.g. *_serial) need root to read. So it could be a simple list of filenames to read from there. So that could be a nice alternative to calling and parsing the `dmidecode` output. The names of the files would also serve as good keys for matching. Maybe you already considered it? Just wanted to throw this in there. Not everything seems to be available there, e.g. "Boot-up State", "Power Supply State" and "Thermal State", but at least everything which is interesting to identify machines uniquely seems to be there. > + let mut system = HashMap::new(); > + let mut baseboard = HashMap::new(); > + let mut chassis = HashMap::new(); > + for option in 1..=3 { > + let dmiresult = Command::new("dmidecode") > + .arg("-t") > + .arg(format!("{option}")) > + .output()?; > + > + if dmiresult.status.success() { > + let output = String::from_utf8(dmiresult.stdout)?; > + match option { > + 1 => system = parse_dmidecode(&output)?, > + 2 => baseboard = parse_dmidecode(&output)?, > + 3 => chassis = parse_dmidecode(&output)?, > + _ => (), > + } > + } else { > + let stderr = String::from_utf8(dmiresult.stderr)?; > + bail!("Failed to get dmidecode information. Are you running as root? '{stderr}'"); > + } > + } > + > + let mut mac_addresses: Vec = Vec::new(); > + let links = get_nic_list()?; > + for link in links { > + let address = fs::read_to_string(format!("/sys/class/net/{link}/address"))?; > + let address = String::from(address.trim()); > + mac_addresses.push(address); > + } > + > + let sysinfo = SysInfo { > + system, > + baseboard, > + chassis, > + mac_addresses, > + }; > + if pretty { > + return Ok(serde_json::to_string_pretty(&sysinfo)?); > + } > + Ok(serde_json::to_string(&sysinfo)?) > +} > + > +#[derive(Debug, Serialize)] > +struct SysInfo { > + system: HashMap, > + baseboard: HashMap, > + chassis: HashMap, > + mac_addresses: Vec, > +} > + > +fn parse_dmidecode(output: &str) -> Result> { > + let keywords = vec![ > + "Asset Tag", > + "Product Name", > + "Serial Number", > + "SKU Number", > + "UUID", > + ]; > + > + let mut res: HashMap = HashMap::new(); > + for mut line in output.lines() { > + line = line.trim(); > + if let Some((key, value)) = line.split_once(':') { > + if keywords.contains(&key) { > + res.insert(String::from(key), String::from(value.trim())); > + } > + } > + } > + > + Ok(res) > +} > + > +#[cfg(test)] > +mod tests { > + use std::collections::HashMap; > + > + use super::parse_dmidecode; > + > + #[test] > + fn dmidecode_parse() { > + let system1 = String::from( > + r#" > +# dmidecode 3.4 > +Getting SMBIOS data from sysfs. > +SMBIOS 3.2.0 present. > + > +Handle 0x0001, DMI type 1, 27 bytes > +System Information > + Manufacturer: GIGABYTE > + Product Name: MZ32-AR0-00 > + Version: 0100 > + Serial Number: 01234567890123456789AB > + UUID: 61df0000-9855-11ed-8000-b42e99acXXXX > + Wake-up Type: Power Switch > + SKU Number: 01234567890123456789AB > + Family: Server"#, > + ); > + > + let mut system1_check: HashMap = HashMap::new(); > + system1_check.insert( > + String::from("Serial Number"), > + String::from("01234567890123456789AB"), > + ); > + system1_check.insert( > + String::from("UUID"), > + String::from("61df0000-9855-11ed-8000-b42e99acXXXX"), > + ); > + system1_check.insert( > + String::from("SKU Number"), > + String::from("01234567890123456789AB"), > + ); > + system1_check.insert( > + String::from("Product Name"), > + String::from("MZ32-AR0-00"), > + ); > + > + let baseboard1 = String::from( > + r#" > +# dmidecode 3.4 > +Getting SMBIOS data from sysfs. > +SMBIOS 3.2.0 present. > + > +Handle 0x0002, DMI type 2, 15 bytes > +Base Board Information > + Manufacturer: GIGABYTE > + Product Name: MZ32-AR0-00 > + Version: 01000100 > + Serial Number: JGBNA600XXX > + Asset Tag: 01234567890123456789AB > + Features: > + Board is a hosting board > + Board is removable > + Board is replaceable > + Location In Chassis: 01234567890123456789AB > + Chassis Handle: 0x0003 > + Type: Motherboard > + Contained Object Handles: 0"#, > + ); > + let mut baseboard1_check: HashMap = HashMap::new(); > + baseboard1_check.insert(String::from("Serial Number"), String::from("JGBNA600XXX")); > + baseboard1_check.insert( > + String::from("Asset Tag"), > + String::from("01234567890123456789AB"), > + ); > + baseboard1_check.insert( > + String::from("Product Name"), > + String::from("MZ32-AR0-00"), > + ); > + > + let chassis1 = String::from( > + r#" > +# dmidecode 3.4 > +Getting SMBIOS data from sysfs. > +SMBIOS 3.2.0 present. > + > +Handle 0x0003, DMI type 3, 22 bytes > +Chassis Information > + Manufacturer: GIGABYTE > + Type: Main Server Chassis > + Lock: Not Present > + Version: 01234567 > + Serial Number: 01234567890123456789AB > + Asset Tag: 01234567890123456789AB > + Boot-up State: Safe > + Power Supply State: Safe > + Thermal State: Safe > + Security Status: None > + OEM Information: 0x00000000 > + Height: Unspecified > + Number Of Power Cords: 1 > + Contained Elements: 0 > + SKU Number: 01234567890123456789AB"#, > + ); > + let mut chassis1_check: HashMap = HashMap::new(); > + chassis1_check.insert( > + String::from("Serial Number"), > + String::from("01234567890123456789AB"), > + ); > + chassis1_check.insert( > + String::from("Asset Tag"), > + String::from("01234567890123456789AB"), > + ); > + chassis1_check.insert( > + String::from("SKU Number"), > + String::from("01234567890123456789AB"), > + ); > + > + assert_eq!(parse_dmidecode(&system1).unwrap(), system1_check); > + assert_eq!(parse_dmidecode(&baseboard1).unwrap(), baseboard1_check); > + assert_eq!(parse_dmidecode(&chassis1).unwrap(), chassis1_check); > + } > +} > -- > 2.39.2 > > > > _______________________________________________ > pve-devel mailing list > pve-devel@lists.proxmox.com > https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel > >