[pve-devel] [PATCH pve-storage v2 4/5] vdisk_alloc: factor out optional parameters in options hash argument

Daniel Kral d.kral at proxmox.com
Tue Feb 11 17:07:58 CET 2025


Moves the optional parameter `name` into an optional hash argument at
the end of the function.

This allows to add more optional arguments in the future and makes the
function call easier to follow when a name is not required.

Signed-off-by: Daniel Kral <d.kral at proxmox.com>
---
changes since v1:
- new!

 src/PVE/API2/Storage/Content.pm    |  6 +++---
 src/PVE/GuestImport.pm             |  2 +-
 src/PVE/Storage.pm                 | 30 ++++++++++++++++++++++++++++--
 src/test/run_test_zfspoolplugin.pl |  8 ++++----
 4 files changed, 36 insertions(+), 10 deletions(-)

diff --git a/src/PVE/API2/Storage/Content.pm b/src/PVE/API2/Storage/Content.pm
index fe0ad4a..77924a6 100644
--- a/src/PVE/API2/Storage/Content.pm
+++ b/src/PVE/API2/Storage/Content.pm
@@ -220,9 +220,9 @@ __PACKAGE__->register_method ({
 
 	my $cfg = PVE::Storage::config();
 
-	my $volid = PVE::Storage::vdisk_alloc ($cfg, $storeid, $param->{vmid},
-					       $param->{format},
-					       $name, $size);
+	my $volid = PVE::Storage::vdisk_alloc($cfg, $storeid, $param->{vmid}, $param->{format}, $size, {
+	    name => $name,
+	});
 
 	return $volid;
     }});
diff --git a/src/PVE/GuestImport.pm b/src/PVE/GuestImport.pm
index 16099ca..76ebc9d 100644
--- a/src/PVE/GuestImport.pm
+++ b/src/PVE/GuestImport.pm
@@ -69,7 +69,7 @@ sub extract_disk_from_import_file {
 
 	# create temporary 1M image that will get overwritten by the rename
 	# to reserve the filename and take care of locking
-	$target_volid = PVE::Storage::vdisk_alloc($cfg, $target_storeid, $vmid, $inner_fmt, undef, 1024);
+	$target_volid = PVE::Storage::vdisk_alloc($cfg, $target_storeid, $vmid, $inner_fmt, 1024);
 	$target_path = PVE::Storage::path($cfg, $target_volid);
 
 	print "renaming $source_path to $target_path\n";
diff --git a/src/PVE/Storage.pm b/src/PVE/Storage.pm
index 0134893..3776565 100755
--- a/src/PVE/Storage.pm
+++ b/src/PVE/Storage.pm
@@ -1041,8 +1041,34 @@ sub unmap_volume {
     return $plugin->unmap_volume($storeid, $scfg, $volname, $snapname);
 }
 
-sub vdisk_alloc {
-    my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
+=head3 vdisk_alloc($cfg, $storeid, $vmid, $size, %opts)
+
+Allocates a volume on the storage with the identifier C<$storeid> (defined in the storage config
+C<$cfg>) for the VM with the id C<$vmid> with format C<$fmt> and a size of C<$size> kilobytes.
+
+The format C<$fmt> can be C<'raw'>, C<'qcow2'>, C<'subvol'> or C<'vmdk'>. If C<$fmt> is left
+undefined, it will fallback on the default format of the storage type of C<$storeid>.
+
+Optionally, the following key-value pairs are available for C<%opts>:
+
+=over
+
+=item * C<< $name => $string >>
+
+Specifies the name of the new volume.
+
+If undefined, the name will be generated with C<PVE::Storage::Plugin::find_free_diskname>.
+
+=back
+
+Returns the identifier for the new volume in the format C<"$storeid:$volname">.
+
+=cut
+
+sub vdisk_alloc : prototype($$$$$;%) {
+    my ($cfg, $storeid, $vmid, $fmt, $size, $opts) = @_;
+
+    my $name = $opts->{name};
 
     die "no storage ID specified\n" if !$storeid;
 
diff --git a/src/test/run_test_zfspoolplugin.pl b/src/test/run_test_zfspoolplugin.pl
index 095ccb3..7dd0a2b 100755
--- a/src/test/run_test_zfspoolplugin.pl
+++ b/src/test/run_test_zfspoolplugin.pl
@@ -565,7 +565,7 @@ my $test13 = sub {
     print "\nrun test13 \"vdisk_alloc\"\n";
 
     eval {
-	my $tmp_volid = PVE::Storage::vdisk_alloc($cfg, $storagename, "112", "raw", undef ,1024 * 1024);
+	my $tmp_volid = PVE::Storage::vdisk_alloc($cfg, $storagename, "112", "raw", 1024 * 1024);
 
 	if ($tmp_volid ne "$storagename:vm-112-disk-0") {
 	    die "volname:$tmp_volid don't match\n";
@@ -589,7 +589,7 @@ my $test13 = sub {
     }
 
     eval {
-	my $tmp_volid = PVE::Storage::vdisk_alloc($cfg, $storagename, "112", "raw", undef ,2048 * 1024);
+	my $tmp_volid = PVE::Storage::vdisk_alloc($cfg, $storagename, "112", "raw", 2048 * 1024);
 
 	if ($tmp_volid ne "$storagename:vm-112-disk-1") {
 	    die "volname:$tmp_volid don't match\n";
@@ -613,7 +613,7 @@ my $test13 = sub {
     }
 
     eval {
-	my $tmp_volid = PVE::Storage::vdisk_alloc($cfg, $storagename, "113", "subvol", undef ,1024 * 1024);
+	my $tmp_volid = PVE::Storage::vdisk_alloc($cfg, $storagename, "113", "subvol", 1024 * 1024);
 
 	if ($tmp_volid ne "$storagename:subvol-113-disk-0") {
 	    die "volname:$tmp_volid  don't match\n";
@@ -637,7 +637,7 @@ my $test13 = sub {
     }
 
     eval {
-	my $tmp_volid = PVE::Storage::vdisk_alloc($cfg, $storagename, "113", "subvol", undef ,2048 * 1024);
+	my $tmp_volid = PVE::Storage::vdisk_alloc($cfg, $storagename, "113", "subvol", 2048 * 1024);
 
 	if ($tmp_volid ne "$storagename:subvol-113-disk-1") {
 	    die "volname:$tmp_volid  don't match\n";
-- 
2.39.5





More information about the pve-devel mailing list