From: Christoph Heiss <c.heiss@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH installer 4/5] chroot: replace btrfs parsing regex with lsblk json parsing
Date: Fri, 9 May 2025 14:09:16 +0200 [thread overview]
Message-ID: <20250509121007.1430080-5-c.heiss@proxmox.com> (raw)
In-Reply-To: <20250509121007.1430080-1-c.heiss@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
next prev parent reply other threads:[~2025-05-09 12:11 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-09 12:09 [pve-devel] [PATCH installer 0/5] chroot, assistant: replace clap with pico-args Christoph Heiss
2025-05-09 12:09 ` [pve-devel] [PATCH installer 1/5] common: introduce simple cli subcommand parser based on pico-args Christoph Heiss
2025-05-09 12:09 ` [pve-devel] [PATCH installer 2/5] chroot: replace clap with pico-args for command argument parsing Christoph Heiss
2025-05-09 12:09 ` [pve-devel] [PATCH installer 3/5] assistant: " Christoph Heiss
2025-05-09 12:09 ` Christoph Heiss [this message]
2025-05-09 12:09 ` [pve-devel] [PATCH installer 5/5] tui: drop unused dependencies from manifest 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=20250509121007.1430080-5-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal