[pve-devel] [RFC pve-storage 33/36] plugin: rbd: update private sub signatures and make helpers private

Max Carrara m.carrara at proxmox.com
Wed Jul 17 11:40:31 CEST 2024


Instead of assigning anonymous subs to a reference, make them "proper"
private subs instead. Thus, signatures like

  my $foo = sub { ... };

are changed to

  my sub foo { ... }

and their call sites are updated correspondingly.

The remaining helpers are also made private, because they're not used
anywhere else in our code.

Signed-off-by: Max Carrara <m.carrara at proxmox.com>
---
 src/PVE/Storage/RBDPlugin.pm | 84 ++++++++++++++++++------------------
 1 file changed, 42 insertions(+), 42 deletions(-)

diff --git a/src/PVE/Storage/RBDPlugin.pm b/src/PVE/Storage/RBDPlugin.pm
index f45ad3f..919b9f9 100644
--- a/src/PVE/Storage/RBDPlugin.pm
+++ b/src/PVE/Storage/RBDPlugin.pm
@@ -20,13 +20,13 @@ use PVE::Tools qw(run_command trim file_read_firstline);
 
 use base qw(PVE::Storage::Plugin);
 
-my $get_parent_image_name = sub {
+my sub get_parent_image_name {
     my ($parent) = @_;
     return undef if !$parent;
     return $parent->{image} . "@" . $parent->{snapshot};
 };
 
-my $librados_connect = sub {
+my sub librados_connect {
     my ($scfg, $storeid, $options) = @_;
 
     $options->{timeout} = 60
@@ -55,7 +55,7 @@ my sub get_rbd_dev_path {
 	# NOTE: the config doesn't support this currently (but it could!), hack for qemu-server tests
 	$cluster_id = $scfg->{fsid};
     } elsif ($scfg->{monhost}) {
-	my $rados = $librados_connect->($scfg, $storeid);
+	my $rados = librados_connect($scfg, $storeid);
 	$cluster_id = $rados->mon_command({ prefix => 'fsid', format => 'json' })->{fsid};
     } else {
 	$cluster_id = cfs_read_file('ceph.conf')->{global}->{fsid};
@@ -82,7 +82,7 @@ my sub get_rbd_dev_path {
     return $pve_path;
 }
 
-my $build_cmd = sub {
+my sub build_cmd {
     my ($binary, $scfg, $storeid, $op, @options) = @_;
 
     my $cmd_option = PVE::CephConfig::ceph_connect_option($scfg, $storeid);
@@ -110,20 +110,20 @@ my $build_cmd = sub {
     return $cmd;
 };
 
-my $rbd_cmd = sub {
+my sub rbd_cmd {
     my ($scfg, $storeid, $op, @options) = @_;
 
-    return $build_cmd->('/usr/bin/rbd', $scfg, $storeid, $op, @options);
+    return build_cmd('/usr/bin/rbd', $scfg, $storeid, $op, @options);
 };
 
-my $rados_cmd = sub {
+my sub rados_cmd {
     my ($scfg, $storeid, $op, @options) = @_;
 
-    return $build_cmd->('/usr/bin/rados', $scfg, $storeid, $op, @options);
+    return build_cmd('/usr/bin/rados', $scfg, $storeid, $op, @options);
 };
 
 # needed for volumes created using ceph jewel (or higher)
-my $krbd_feature_update = sub {
+my sub krbd_feature_update {
     my ($scfg, $storeid, $name) = @_;
 
     my (@disable, @enable);
@@ -150,7 +150,7 @@ my $krbd_feature_update = sub {
 
     if ($to_disable) {
 	print "disable RBD image features this kernel RBD drivers is not compatible with: $to_disable\n";
-	my $cmd = $rbd_cmd->($scfg, $storeid, 'feature', 'disable', $name, $to_disable);
+	my $cmd = rbd_cmd($scfg, $storeid, 'feature', 'disable', $name, $to_disable);
 	run_rbd_command(
 	    $cmd,
 	    errmsg => "could not disable krbd-incompatible image features '$to_disable' for rbd image: $name",
@@ -159,7 +159,7 @@ my $krbd_feature_update = sub {
     if ($to_enable) {
 	print "enable RBD image features this kernel RBD drivers supports: $to_enable\n";
 	eval {
-	    my $cmd = $rbd_cmd->($scfg, $storeid, 'feature', 'enable', $name, $to_enable);
+	    my $cmd = rbd_cmd($scfg, $storeid, 'feature', 'enable', $name, $to_enable);
 	    run_rbd_command(
 		$cmd,
 		errmsg => "could not enable krbd-compatible image features '$to_enable' for rbd image: $name",
@@ -169,7 +169,7 @@ my $krbd_feature_update = sub {
     }
 };
 
-sub run_rbd_command {
+my sub run_rbd_command {
     my ($cmd, %args) = @_;
 
     my $lasterr;
@@ -198,7 +198,7 @@ sub run_rbd_command {
     return undef;
 }
 
-sub rbd_ls {
+my sub rbd_ls {
     my ($scfg, $storeid) = @_;
 
     my $pool =  $scfg->{pool} ? $scfg->{pool} : 'rbd';
@@ -207,7 +207,7 @@ sub rbd_ls {
     my $raw = '';
     my $parser = sub { $raw .= shift };
 
-    my $cmd = $rbd_cmd->($scfg, $storeid, 'ls', '-l', '--format', 'json');
+    my $cmd = rbd_cmd($scfg, $storeid, 'ls', '-l', '--format', 'json');
     eval {
 	run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
     };
@@ -237,7 +237,7 @@ sub rbd_ls {
 	$list->{$pool}->{$image} = {
 	    name => $image,
 	    size => $el->{size},
-	    parent => $get_parent_image_name->($el->{parent}),
+	    parent => get_parent_image_name($el->{parent}),
 	    vmid => $owner
 	};
     }
@@ -245,10 +245,10 @@ sub rbd_ls {
     return $list;
 }
 
-sub rbd_ls_snap {
+my sub rbd_ls_snap {
     my ($scfg, $storeid, $name) = @_;
 
-    my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'ls', $name, '--format', 'json');
+    my $cmd = rbd_cmd($scfg, $storeid, 'snap', 'ls', $name, '--format', 'json');
 
     my $raw = '';
     run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => sub { $raw .= shift; });
@@ -277,7 +277,7 @@ sub rbd_ls_snap {
     return $res;
 }
 
-sub rbd_volume_info {
+my sub rbd_volume_info {
     my ($scfg, $storeid, $volname, $snap) = @_;
 
     my $cmd = undef;
@@ -287,7 +287,7 @@ sub rbd_volume_info {
 	push @options, '--snap', $snap;
     }
 
-    $cmd = $rbd_cmd->($scfg, $storeid, @options);
+    $cmd = rbd_cmd($scfg, $storeid, @options);
 
     my $raw = '';
     my $parser = sub { $raw .= shift };
@@ -303,17 +303,17 @@ sub rbd_volume_info {
 	die "got unexpected data from rbd info: '$raw'\n";
     }
 
-    $volume->{parent} = $get_parent_image_name->($volume->{parent});
+    $volume->{parent} = get_parent_image_name($volume->{parent});
     $volume->{protected} = defined($volume->{protected}) && $volume->{protected} eq "true" ? 1 : undef;
 
     return $volume->@{qw(size parent format protected features)};
 }
 
-sub rbd_volume_du {
+my sub rbd_volume_du {
     my ($scfg, $storeid, $volname) = @_;
 
     my @options = ('du', $volname, '--format', 'json');
-    my $cmd = $rbd_cmd->($scfg, $storeid, @options);
+    my $cmd = rbd_cmd($scfg, $storeid, @options);
 
     my $raw = '';
     my $parser = sub { $raw .= shift };
@@ -484,7 +484,7 @@ sub path {
 sub find_free_diskname {
     my ($class, $storeid, $scfg, $vmid, $fmt, $add_fmt_suffix) = @_;
 
-    my $cmd = $rbd_cmd->($scfg, $storeid, 'ls');
+    my $cmd = rbd_cmd($scfg, $storeid, 'ls');
 
     my $disk_list = [];
 
@@ -528,7 +528,7 @@ sub create_base {
 
     my $newvolname = $basename ? "$basename/$newname" : "$newname";
 
-    my $cmd = $rbd_cmd->(
+    my $cmd = rbd_cmd(
 	$scfg,
 	$storeid,
 	'rename',
@@ -547,7 +547,7 @@ sub create_base {
     my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $newname, $snap);
 
     if (!$protected){
-	my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'protect', $newname, '--snap', $snap);
+	my $cmd = rbd_cmd($scfg, $storeid, 'snap', 'protect', $newname, '--snap', $snap);
 	run_rbd_command($cmd, errmsg => "rbd protect $newname snap '$snap' error");
     }
 
@@ -575,7 +575,7 @@ sub clone_image {
 	my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $volname, $snapname);
 
 	if (!$protected) {
-	    my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'protect', $volname, '--snap', $snapname);
+	    my $cmd = rbd_cmd($scfg, $storeid, 'snap', 'protect', $volname, '--snap', $snapname);
 	    run_rbd_command($cmd, errmsg => "rbd protect $volname snap $snapname error");
 	}
     }
@@ -589,7 +589,7 @@ sub clone_image {
     );
     push @options, ('--data-pool', $scfg->{'data-pool'}) if $scfg->{'data-pool'};
 
-    my $cmd = $rbd_cmd->($scfg, $storeid, 'clone', @options, get_rbd_path($scfg, $name));
+    my $cmd = rbd_cmd($scfg, $storeid, 'clone', @options, get_rbd_path($scfg, $name));
     run_rbd_command($cmd, errmsg => "rbd clone '$basename' error");
 
     return $newvol;
@@ -610,7 +610,7 @@ sub alloc_image {
     );
     push @options, ('--data-pool', $scfg->{'data-pool'}) if $scfg->{'data-pool'};
 
-    my $cmd = $rbd_cmd->($scfg, $storeid, 'create', @options, $name);
+    my $cmd = rbd_cmd($scfg, $storeid, 'create', @options, $name);
     run_rbd_command($cmd, errmsg => "rbd create '$name' error");
 
     return $name;
@@ -626,17 +626,17 @@ sub free_image {
     my $snaps = rbd_ls_snap($scfg, $storeid, $name);
     foreach my $snap (keys %$snaps) {
 	if ($snaps->{$snap}->{protected}) {
-	    my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
+	    my $cmd = rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
 	    run_rbd_command($cmd, errmsg => "rbd unprotect $name snap '$snap' error");
 	}
     }
 
     $class->deactivate_volume($storeid, $scfg, $volname);
 
-    my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'purge',  $name);
+    my $cmd = rbd_cmd($scfg, $storeid, 'snap', 'purge',  $name);
     run_rbd_command($cmd, errmsg => "rbd snap purge '$name' error");
 
-    $cmd = $rbd_cmd->($scfg, $storeid, 'rm', $name);
+    $cmd = rbd_cmd($scfg, $storeid, 'rm', $name);
     run_rbd_command($cmd, errmsg => "rbd rm '$name' error");
 
     return undef;
@@ -679,7 +679,7 @@ sub list_images {
 sub status {
     my ($class, $storeid, $scfg, $cache) = @_;
 
-    my $rados = $librados_connect->($scfg, $storeid);
+    my $rados = librados_connect($scfg, $storeid);
     my $df = $rados->mon_command({ prefix => 'df', format => 'json' });
 
     my $pool = $scfg->{'data-pool'} // $scfg->{pool} // 'rbd';
@@ -724,9 +724,9 @@ sub map_volume {
     return $kerneldev if -b $kerneldev; # already mapped
 
     # features can only be enabled/disabled for image, not for snapshot!
-    $krbd_feature_update->($scfg, $storeid, $img_name);
+    krbd_feature_update($scfg, $storeid, $img_name);
 
-    my $cmd = $rbd_cmd->($scfg, $storeid, 'map', $name);
+    my $cmd = rbd_cmd($scfg, $storeid, 'map', $name);
     run_rbd_command($cmd, errmsg => "can't map rbd volume $name");
 
     return $kerneldev;
@@ -741,7 +741,7 @@ sub unmap_volume {
     my $kerneldev = get_rbd_dev_path($scfg, $storeid, $name);
 
     if (-b $kerneldev) {
-	my $cmd = $rbd_cmd->($scfg, $storeid, 'unmap', $kerneldev);
+	my $cmd = rbd_cmd($scfg, $storeid, 'unmap', $kerneldev);
 	run_rbd_command($cmd, errmsg => "can't unmap rbd device $kerneldev");
     }
 
@@ -780,7 +780,7 @@ sub volume_resize {
 
     my ($vtype, $name, $vmid) = $class->parse_volname($volname);
 
-    my $cmd = $rbd_cmd->($scfg, $storeid, 'resize', '--size', int(ceil($size/1024/1024)), $name);
+    my $cmd = rbd_cmd($scfg, $storeid, 'resize', '--size', int(ceil($size/1024/1024)), $name);
     run_rbd_command($cmd, errmsg => "rbd resize '$volname' error");
     return undef;
 }
@@ -790,7 +790,7 @@ sub volume_snapshot {
 
     my ($vtype, $name, $vmid) = $class->parse_volname($volname);
 
-    my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'create', '--snap', $snap, $name);
+    my $cmd = rbd_cmd($scfg, $storeid, 'snap', 'create', '--snap', $snap, $name);
     run_rbd_command($cmd, errmsg => "rbd snapshot '$volname' error");
     return undef;
 }
@@ -800,7 +800,7 @@ sub volume_snapshot_rollback {
 
     my ($vtype, $name, $vmid) = $class->parse_volname($volname);
 
-    my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $name);
+    my $cmd = rbd_cmd($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $name);
     run_rbd_command($cmd, errmsg => "rbd snapshot $volname to '$snap' error");
 }
 
@@ -813,11 +813,11 @@ sub volume_snapshot_delete {
 
     my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $name, $snap);
     if ($protected){
-	my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
+	my $cmd = rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
 	run_rbd_command($cmd, errmsg => "rbd unprotect $name snap '$snap' error");
     }
 
-    my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name);
+    my $cmd = rbd_cmd($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name);
 
     run_rbd_command($cmd, errmsg => "rbd snapshot '$volname' error");
 
@@ -869,12 +869,12 @@ sub rename_volume {
 	if !$target_volname;
 
     eval {
-	my $cmd = $rbd_cmd->($scfg, $storeid, 'info', $target_volname);
+	my $cmd = rbd_cmd($scfg, $storeid, 'info', $target_volname);
 	run_rbd_command($cmd, errmsg => "exist check",  quiet => 1);
     };
     die "target volume '${target_volname}' already exists\n" if !$@;
 
-    my $cmd = $rbd_cmd->($scfg, $storeid, 'rename', $source_image, $target_volname);
+    my $cmd = rbd_cmd($scfg, $storeid, 'rename', $source_image, $target_volname);
 
     run_rbd_command(
 	$cmd,
-- 
2.39.2





More information about the pve-devel mailing list