[pve-devel] [PATCH storage 20/26] plugins: update rename_volumes methods

Wolfgang Bumiller
2025-07-29T11:23:53Z

src/PVE/Storage/BTRFSPlugin.pm | 12 ++++++++++--
src/PVE/Storage/LVMPlugin.pm | 11 +++++++++--
src/PVE/Storage/Plugin.pm | 26 ++++++++++++++++++++++----
src/PVE/Storage/RBDPlugin.pm | 11 +++++++++--
src/PVE/Storage/ZFSPoolPlugin.pm | 11 +++++++++--
5 files changed, 59 insertions(+), 12 deletions(-)

diff --git a/src/PVE/Storage/BTRFSPlugin.pm b/src/PVE/Storage/BTRFSPlugin.pm
index 8417dd6..a0145bb 100644
--- a/src/PVE/Storage/BTRFSPlugin.pm
+++ b/src/PVE/Storage/BTRFSPlugin.pm
@@ -1031,7 +1031,7 @@ sub rename_volume {
my ($class, $scfg, $storeid, $source_volname, $target_vmid, $target_volname) = @_;
die "no path found\n" if !$scfg->{path};

- my $format = ($class->parse_volname($source_volname))[6];
+ my ($source_vtype, $format) = ($class->parse_volname($source_volname))[0, 6];

if ($format ne 'raw' && $format ne 'subvol') {
return $class->SUPER::rename_volume(
@@ -1039,8 +1039,16 @@ sub rename_volume {
);
}

- $target_volname = $class->find_free_diskname($storeid, $scfg, $target_vmid, $format, 1)
+ $target_volname =
+ $class->find_free_diskname($storeid, $scfg, $target_vmid, $format, 1, $source_vtype)
if !$target_volname;
+
+ my ($target_vtype) = $class->parse_volname($target_volname);
+ if (!PVE::Storage::Common::is_type_change_allowed($source_vtype, $target_vtype)) {
+ die "refusing to rename volume '$source_volname' to '$target_volname':"
+ . " source has vtype '$source_vtype', target name has vtype '$target_vtype'\n";
+ }
+
$target_volname = "$target_vmid/$target_volname";

my $basedir = $class->get_subdir($scfg, 'images');
diff --git a/src/PVE/Storage/LVMPlugin.pm b/src/PVE/Storage/LVMPlugin.pm
index 304d411..b578ff4 100644
--- a/src/PVE/Storage/LVMPlugin.pm
+++ b/src/PVE/Storage/LVMPlugin.pm
@@ -1332,7 +1332,7 @@ sub rename_volume {
my ($class, $scfg, $storeid, $source_volname, $target_vmid, $target_volname) = @_;

my (
- undef, $source_image, $source_vmid, $base_name, $base_vmid, undef, $format,
+ $source_vtype, $source_image, $source_vmid, $base_name, $base_vmid, undef, $format,
) = $class->parse_volname($source_volname);

if ($format eq 'qcow2') {
@@ -1340,9 +1340,16 @@ sub rename_volume {
die "we can't rename volume if external snapshot exists" if $snapshots->{current}->{parent};
}

- $target_volname = $class->find_free_diskname($storeid, $scfg, $target_vmid, $format)
+ $target_volname =
+ $class->find_free_diskname($storeid, $scfg, $target_vmid, $format, 0, $source_vtype)
if !$target_volname;

+ my ($target_vtype) = $class->parse_volname($target_volname);
+ if (!PVE::Storage::Common::is_type_change_allowed($source_vtype, $target_vtype)) {
+ die "refusing to rename volume '$source_volname' to '$target_volname':"
+ . " source has vtype '$source_vtype', target name has vtype '$target_vtype'\n";
+ }
+
my $vg = $scfg->{vgname};
my $lvs = lvm_list_volumes($vg);
die "target volume '${target_volname}' already exists\n"
diff --git a/src/PVE/Storage/Plugin.pm b/src/PVE/Storage/Plugin.pm
index 1b72a17..e418042 100644
--- a/src/PVE/Storage/Plugin.pm
+++ b/src/PVE/Storage/Plugin.pm
@@ -2367,7 +2367,7 @@ sub volume_import_formats {
}

sub rename_volume {
- my ($class, $scfg, $storeid, $source_volname, $target_vmid, $target_volname) = @_;
+ my ($class, $scfg, $storeid, $source_volname, $target_vmid, $target_volname, $vtype) = @_;
die "not implemented in storage plugin '$class'\n" if $class->can('api') && $class->api() < 10;
die "no path found\n" if !$scfg->{path};

@@ -2378,11 +2378,29 @@ sub rename_volume {
}

my (
- undef, $source_image, $source_vmid, $base_name, $base_vmid, undef, $format,
+ $source_vtype, $source_image, $source_vmid, $base_name, $base_vmid, undef, $format,
) = $class->parse_volname($source_volname);

- $target_volname = $class->find_free_diskname($storeid, $scfg, $target_vmid, $format, 1)
- if !$target_volname;
+ $target_volname = $class->find_free_diskname(
+ $storeid,
+ $scfg,
+ $target_vmid,
+ $format,
+ 1,
+ $vtype // $source_vtype,
+ ) if !$target_volname;
+
+ my ($target_vtype) = $class->parse_volname($target_volname);
+
+ if (!PVE::Storage::Common::is_type_change_allowed($source_vtype, $target_vtype)) {
+ die "refusing to rename volume '$source_volname' to '$target_volname':"
+ . " source has vtype '$source_vtype', target name has vtype '$target_vtype'\n";
+ }
+
+ if (defined($vtype) && !PVE::Storage::Common::is_type_change_allowed($vtype, $target_vtype)) {
+ die "refusing to rename volume '$source_volname' to '$target_volname':"
+ . " target name has vtype '$target_vtype', but vtype '$vtype' was requested\n";
+ }

my $basedir = $class->get_subdir($scfg, 'images');

diff --git a/src/PVE/Storage/RBDPlugin.pm b/src/PVE/Storage/RBDPlugin.pm
index fb97c94..437dd07 100644
--- a/src/PVE/Storage/RBDPlugin.pm
+++ b/src/PVE/Storage/RBDPlugin.pm
@@ -1111,11 +1111,18 @@ sub rename_volume {
my ($class, $scfg, $storeid, $source_volname, $target_vmid, $target_volname) = @_;

my (
- undef, $source_image, $source_vmid, $base_name, $base_vmid, undef, $format,
+ $source_vtype, $source_image, $source_vmid, $base_name, $base_vmid, undef, $format,
) = $class->parse_volname($source_volname);
- $target_volname = $class->find_free_diskname($storeid, $scfg, $target_vmid, $format)
+ $target_volname =
+ $class->find_free_diskname($storeid, $scfg, $target_vmid, $format, 0, $source_vtype)
if !$target_volname;

+ my ($target_vtype) = $class->parse_volname($target_volname);
+ if (!PVE::Storage::Common::is_type_change_allowed($source_vtype, $target_vtype)) {
+ die "refusing to rename volume '$source_volname' to '$target_volname':"
+ . " source has vtype '$source_vtype', target name has vtype '$target_vtype'\n";
+ }
+
die "target volume '${target_volname}' already exists\n"
if rbd_volume_exists($scfg, $storeid, $target_volname);

diff --git a/src/PVE/Storage/ZFSPoolPlugin.pm b/src/PVE/Storage/ZFSPoolPlugin.pm
index 1571310..34768d2 100644
--- a/src/PVE/Storage/ZFSPoolPlugin.pm
+++ b/src/PVE/Storage/ZFSPoolPlugin.pm
@@ -943,11 +943,18 @@ sub rename_volume {
my ($class, $scfg, $storeid, $source_volname, $target_vmid, $target_volname) = @_;

my (
- undef, $source_image, $source_vmid, $base_name, $base_vmid, undef, $format,
+ $source_vtype, $source_image, $source_vmid, $base_name, $base_vmid, undef, $format,
) = $class->parse_volname($source_volname);
- $target_volname = $class->find_free_diskname($storeid, $scfg, $target_vmid, $format)
+ $target_volname =
+ $class->find_free_diskname($storeid, $scfg, $target_vmid, $format, 0, $source_vtype)
if !$target_volname;

+ my ($target_vtype) = $class->parse_volname($target_volname);
+ if (!PVE::Storage::Common::is_type_change_allowed($source_vtype, $target_vtype)) {
+ die "refusing to rename volume '$source_volname' to '$target_volname':"
+ . " source has vtype '$source_vtype', target name has vtype '$target_vtype'\n";
+ }
+
my $pool = $scfg->{pool};
my $source_zfspath = "${pool}/${source_image}";
my $target_zfspath = "${pool}/${target_volname}";