[pve-devel] [PATCH installer v4 1/4] proxmox: add zfs module for retrieving importable zpool info

Christoph Heiss c.heiss at proxmox.com
Tue Jul 16 10:18:07 CEST 2024


Signed-off-by: Christoph Heiss <c.heiss at proxmox.com>
---
Changes v3 -> v4:
  * no changes

Changes v2 -> v3:
  * no changes

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 at 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





More information about the pve-devel mailing list