From: Christoph Heiss <c.heiss@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH installer v3 4/4] low-level: install: check for already-existing `rpool` on install
Date: Thu, 11 Jul 2024 13:57:56 +0200 [thread overview]
Message-ID: <20240711115804.706227-5-c.heiss@proxmox.com> (raw)
In-Reply-To: <20240711115804.706227-1-c.heiss@proxmox.com>
.. much in the same manner as the detection for LVM works.
zpools can only be renamed by importing them with a new name, so
unfortunately the import-export dance is needed.
Signed-off-by: Christoph Heiss <c.heiss@proxmox.com>
---
Changes v2 -> v3:
* skip rename prompt in auto-install mode
Changes v1 -> v2:
* use unique pool id for renaming instead of generating random id
ourselves
* moved renaming functionality to own subroutine in new
Proxmox::Sys::ZFS module
Proxmox/Install.pm | 33 +++++++++++++++++++++++++++++++++
Proxmox/Sys/ZFS.pm | 20 ++++++++++++++++++--
2 files changed, 51 insertions(+), 2 deletions(-)
diff --git a/Proxmox/Install.pm b/Proxmox/Install.pm
index 7342e4b..ba699fa 100644
--- a/Proxmox/Install.pm
+++ b/Proxmox/Install.pm
@@ -16,6 +16,7 @@ use Proxmox::Install::StorageConfig;
use Proxmox::Sys::Block qw(get_cached_disks wipe_disk partition_bootable_disk);
use Proxmox::Sys::Command qw(run_command syscmd);
use Proxmox::Sys::File qw(file_read_firstline file_write_all);
+use Proxmox::Sys::ZFS;
use Proxmox::UI;
# TODO: move somewhere better?
@@ -169,9 +170,41 @@ sub btrfs_create {
syscmd($cmd);
}
+sub zfs_ask_existing_zpool_rename {
+ my ($pool_name) = @_;
+
+ # At this point, no pools should be imported/active
+ my $exported_pools = Proxmox::Sys::ZFS::get_exported_pools();
+
+ foreach (@$exported_pools) {
+ next if $_->{name} ne $pool_name || $_->{state} ne 'ONLINE';
+ my $renamed_pool = "$_->{name}-OLD-$_->{id}";
+
+ my $response_ok = Proxmox::Install::Config::get_existing_storage_auto_rename();
+ if (!$response_ok) {
+ $response_ok = Proxmox::UI::prompt(
+ "A ZFS pool named '$_->{name}' (id $_->{id}) already exists on the system.\n\n" .
+ "Do you want to rename the pool to '$renamed_pool' before continuing " .
+ "or cancel the installation?"
+ );
+ }
+
+ # Import the pool using its id, as that is unique and works even if there are
+ # multiple zpools with the same name.
+ if ($response_ok) {
+ Proxmox::Sys::ZFS::rename_pool($_->{id}, $renamed_pool);
+ } else {
+ warn "Canceled installation as requested by user, due to already existing ZFS pool '$pool_name'\n";
+ die "\n"; # causes abort without re-showing an error dialogue
+ }
+ }
+}
+
sub zfs_create_rpool {
my ($vdev, $pool_name, $root_volume_name) = @_;
+ zfs_ask_existing_zpool_rename($pool_name);
+
my $iso_env = Proxmox::Install::ISOEnv::get();
my $zfs_opts = Proxmox::Install::Config::get_zfs_opt();
diff --git a/Proxmox/Sys/ZFS.pm b/Proxmox/Sys/ZFS.pm
index 9232d1a..c5a807e 100644
--- a/Proxmox/Sys/ZFS.pm
+++ b/Proxmox/Sys/ZFS.pm
@@ -3,10 +3,10 @@ package Proxmox::Sys::ZFS;
use strict;
use warnings;
-use Proxmox::Sys::Command qw(run_command);
+use Proxmox::Sys::Command qw(run_command syscmd);
use base qw(Exporter);
-our @EXPORT_OK = qw(get_exported_pools);
+our @EXPORT_OK = qw(get_exported_pools rename_pool);
# Parses the output of running `zpool import`, which shows all importable
# ZFS pools.
@@ -90,4 +90,20 @@ sub get_exported_pools {
return zpool_import_parse_output($fh);
}
+# Renames a importable ZFS pool by importing it with a new name and then
+# exporting again.
+#
+# Arguments:
+#
+# $poolid - Unique, numeric identifier of the pool to rename
+# $new_name - New name for the pool
+sub rename_pool {
+ my ($poolid, $new_name) = @_;
+
+ syscmd("zpool import -f $poolid $new_name") == 0 ||
+ die "failed to import zfs pool with id '$poolid' with new name '$new_name'\n";
+ syscmd("zpool export $new_name") == 0 ||
+ warn "failed to export renamed zfs pool '$new_name'\n";
+}
+
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 11:58 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-11 11:57 [pve-devel] [PATCH installer v3 0/4] add check/rename for already-existing ZFS rpool Christoph Heiss
2024-07-11 11:57 ` [pve-devel] [PATCH installer v3 1/4] proxmox: add zfs module for retrieving importable zpool info Christoph Heiss
2024-07-11 11:57 ` [pve-devel] [PATCH installer v3 2/4] test: add test cases for new zfs module Christoph Heiss
2024-07-11 11:57 ` [pve-devel] [PATCH installer v3 3/4] install: config: rename option lvm_auto_rename -> existing_storage_auto_rename Christoph Heiss
2024-07-11 11:57 ` Christoph Heiss [this message]
2024-07-12 9:36 ` [pve-devel] [PATCH installer v3 4/4] low-level: install: check for already-existing `rpool` on install Aaron Lauterer
2024-07-15 14:49 ` Christoph Heiss
2024-07-12 9:42 ` [pve-devel] [PATCH installer v3 0/4] add check/rename for already-existing ZFS rpool Aaron Lauterer
2024-07-15 14:52 ` Christoph Heiss
2024-07-16 8:33 ` 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=20240711115804.706227-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.