From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: <pve-devel-bounces@lists.proxmox.com> Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) by lore.proxmox.com (Postfix) with ESMTPS id 9CE501FF164 for <inbox@lore.proxmox.com>; Fri, 9 May 2025 14:11:50 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id A61633C740; Fri, 9 May 2025 14:12:06 +0200 (CEST) From: Christoph Heiss <c.heiss@proxmox.com> To: pve-devel@lists.proxmox.com Date: Fri, 9 May 2025 14:09:16 +0200 Message-ID: <20250509121007.1430080-5-c.heiss@proxmox.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250509121007.1430080-1-c.heiss@proxmox.com> References: <20250509121007.1430080-1-c.heiss@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.028 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 RCVD_IN_VALIDITY_CERTIFIED_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_RPBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_SAFE_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [main.rs, proxmox.com] Subject: [pve-devel] [PATCH installer 4/5] chroot: replace btrfs parsing regex with lsblk json parsing X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com> List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe> List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/> List-Post: <mailto:pve-devel@lists.proxmox.com> List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help> List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe> Reply-To: Proxmox VE development discussion <pve-devel@lists.proxmox.com> Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pve-devel-bounces@lists.proxmox.com Sender: "pve-devel" <pve-devel-bounces@lists.proxmox.com> Makes the whole routine a lot more robust by parsing the JSON output of `lsblk` instead of the human-readable output from `btrfs filesystem show`. The labels of Btrfs filesystems are pretty much arbitrary labels, e.g. the following is a valid filesystem and thus output from `btrfs filesystem show`: Label: 'uuid: 70361d98-7492-45d4-a0e4-bf8fee9203a3' uuid: ab158685-c0b7-4540-a739-72c43a773282 Total devices 1 FS bytes used 144.00KiB devid 1 size 8.00GiB used 536.00MiB path /dev/sda .. which would break the current parsing code. Also drops the usage of a regex, thus being able to eliminate the whole regex machinery from the final binary. This reduces (stripped) binary size significantly, from 2.4 MiB to ~727 KiB, a ~70%(!) decrease. text data bss dec hex filename 2213016 261208 408 2474632 25c288 proxmox-chroot-old 702481 27776 360 730617 b25f9 proxmox-chroot-new No functional changes. Signed-off-by: Christoph Heiss <c.heiss@proxmox.com> --- proxmox-chroot/Cargo.toml | 2 +- proxmox-chroot/src/main.rs | 48 +++++++++++++++++++++++++++----------- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/proxmox-chroot/Cargo.toml b/proxmox-chroot/Cargo.toml index c043938..a6a705d 100644 --- a/proxmox-chroot/Cargo.toml +++ b/proxmox-chroot/Cargo.toml @@ -9,6 +9,6 @@ homepage = "https://www.proxmox.com" [dependencies] anyhow.workspace = true -regex.workspace = true proxmox-installer-common = { workspace = true, features = [ "cli" ] } +serde = { workspace = true, features = [ "derive" ] } serde_json.workspace = true diff --git a/proxmox-chroot/src/main.rs b/proxmox-chroot/src/main.rs index 037f079..2cff630 100644 --- a/proxmox-chroot/src/main.rs +++ b/proxmox-chroot/src/main.rs @@ -18,7 +18,7 @@ use proxmox_installer_common::{ options::FsType, setup::{InstallConfig, SetupInfo}, }; -use regex::Regex; +use serde::Deserialize; const ANSWER_MP: &str = "answer"; static BINDMOUNTS: [&str; 4] = ["dev", "proc", "run", "sys"]; @@ -355,29 +355,49 @@ fn mount_btrfs(btrfs_uuid: Option<String>) -> Result<()> { Ok(()) } +/// Searches for all Btrfs filesystems present on the system. +/// +/// # Returns +/// +/// The UUID of that filesystem, if a single Btrfs filesystem is found. +/// If multiple are found, returns a (human-readable) error with all possible +/// filesystem UUIDs. fn get_btrfs_uuid() -> Result<String> { - let output = Command::new("btrfs") - .arg("filesystem") - .arg("show") + let output = Command::new("lsblk") + .args(["--json", "--fs", "--list", "--output", "fstype,uuid"]) .output()?; + if !output.status.success() { bail!( "Error checking for BTRFS file systems: {}", String::from_utf8(output.stderr)? ); } - let out = String::from_utf8(output.stdout)?; - let mut uuids = Vec::new(); - let re_uuid = - Regex::new(r"uuid: ([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$")?; - for line in out.lines() { - if let Some(cap) = re_uuid.captures(line) { - if let Some(uuid) = cap.get(1) { - uuids.push(uuid.as_str()); - } - } + #[derive(Deserialize)] + struct Entry { + fstype: Option<String>, + uuid: Option<String>, } + + #[derive(Deserialize)] + struct LsblkOutput { + blockdevices: Vec<Entry>, + } + + let out: LsblkOutput = serde_json::from_slice(&output.stdout)?; + let uuids = out + .blockdevices + .iter() + .filter_map(|entry| match entry { + Entry { + fstype: Some(fstype), + uuid: Some(uuid), + } if fstype == "btrfs" => Some(uuid), + _ => None, + }) + .collect::<Vec<&String>>(); + match uuids.len() { 0 => bail!("Could not find any BTRFS UUID"), i if i > 1 => { -- 2.49.0 _______________________________________________ pve-devel mailing list pve-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel