From: Christoph Heiss <c.heiss@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH installer v2 1/3] proxmox: add zfs module for retrieving importable zpool info
Date: Thu, 11 Jul 2024 11:56:38 +0200 [thread overview]
Message-ID: <20240711095641.369504-2-c.heiss@proxmox.com> (raw)
In-Reply-To: <20240711095641.369504-1-c.heiss@proxmox.com>
Signed-off-by: Christoph Heiss <c.heiss@proxmox.com>
---
Changes v1 -> v2:
* incorporated Aaron suggestion to use anonymous arrays instead
* added documentation
* renamed parsing function parse_pool_list -> zpool_import_parse_output
* split out tests into separate patch
Proxmox/Makefile | 1 +
Proxmox/Sys/ZFS.pm | 93 ++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 94 insertions(+)
create mode 100644 Proxmox/Sys/ZFS.pm
diff --git a/Proxmox/Makefile b/Proxmox/Makefile
index 9561d9b..035626b 100644
--- a/Proxmox/Makefile
+++ b/Proxmox/Makefile
@@ -17,6 +17,7 @@ PERL_MODULES=\
Sys/File.pm \
Sys/Net.pm \
Sys/Udev.pm \
+ Sys/ZFS.pm \
UI.pm \
UI/Base.pm \
UI/Gtk3.pm \
diff --git a/Proxmox/Sys/ZFS.pm b/Proxmox/Sys/ZFS.pm
new file mode 100644
index 0000000..9232d1a
--- /dev/null
+++ b/Proxmox/Sys/ZFS.pm
@@ -0,0 +1,93 @@
+package Proxmox::Sys::ZFS;
+
+use strict;
+use warnings;
+
+use Proxmox::Sys::Command qw(run_command);
+
+use base qw(Exporter);
+our @EXPORT_OK = qw(get_exported_pools);
+
+# Parses the output of running `zpool import`, which shows all importable
+# ZFS pools.
+# Unfortunately, `zpool` does not support JSON or any other machine-readable
+# format for output, so we have to do it the hard way.
+#
+# Example output of `zpool import` looks like this:
+#
+# root@proxmox:/# zpool import
+# pool: testpool
+# id: 4958685680270539150
+# state: ONLINE
+# action: The pool can be imported using its name or numeric identifier.
+# config:
+#
+# testpool ONLINE
+# vdc ONLINE
+# vdd ONLINE
+#
+# pool: rpool
+# id: 9412322616744093413
+# state: ONLINE
+# status: The pool was last accessed by another system.
+# action: The pool can be imported using its name or numeric identifier and
+# the '-f' flag.
+# see: https://openzfs.github.io/openzfs-docs/msg/ZFS-8000-EY
+# config:
+#
+# rpool ONLINE
+# mirror-0 ONLINE
+# vda3 ONLINE
+# vdb3 ONLINE
+#
+sub zpool_import_parse_output {
+ my ($fh) = @_;
+
+ my $pools = []; # all found pools
+ my $pool = {}; # last found pool in output
+
+ while (my $line = <$fh>) {
+ # first, if we find the start of a new pool, add it to the list with
+ # its name
+ if ($line =~ /^\s+pool: (.+)$/) {
+ # push the previous parsed pool to the result list
+ push @$pools, $pool if %$pool;
+ $pool = { name => $1 };
+ next;
+ }
+
+ # ignore any (garbage) output before the actual list, just in case
+ next if !%$pool;
+
+ # add any possibly-useful attribute to the last (aka. current) pool
+ if ($line =~ /^\s*(id|state|status|action): (.+)$/) {
+ chomp($pool->{$1} = $2);
+ next;
+ }
+ }
+
+ # add the final parsed pool to the list
+ push @$pools, $pool if %$pool;
+ return $pools;
+}
+
+# Returns an array of all importable ZFS pools on the system.
+# Each entry is a hash of the format:
+#
+# {
+# name => '<pool name>',
+# id => <numeric pool-id>,
+# /* see also zpool_state_to_name() in lib/libzfs/libzfs_pool.c /*
+# state => 'OFFLINE' | 'REMOVED' | 'FAULTED' | 'SPLIT' | 'UNAVAIL' \
+# | 'FAULTED' | 'DEGRADED' | 'ONLINE',
+# status => '<human-readable status string>', optional
+# action => '<human-readable action string>', optional
+# }
+sub get_exported_pools {
+ my $raw = run_command(['zpool', 'import']);
+ open (my $fh, '<', \$raw) or die 'failed to open in-memory stream';
+
+ return zpool_import_parse_output($fh);
+}
+
+1;
--
2.45.1
_______________________________________________
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:[~2024-07-11 9:59 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-11 9:56 [pve-devel] [PATCH installer v2 0/3] add check/rename for already-existing ZFS rpool Christoph Heiss
2024-07-11 9:56 ` Christoph Heiss [this message]
2024-07-11 9:56 ` [pve-devel] [PATCH installer v2 2/3] test: add test cases for new zfs module Christoph Heiss
2024-07-11 9:56 ` [pve-devel] [PATCH installer v2 3/3] low-level: install: check for already-existing `rpool` on install Christoph Heiss
2024-07-11 11:31 ` [pve-devel] [PATCH installer v2 0/3] add check/rename for already-existing ZFS rpool Aaron Lauterer
2024-07-11 12:00 ` 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=20240711095641.369504-2-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.