[pve-devel] [PATCH storage v3 06/13] rbd plugin: improve volume exists helper

Fiona Ebner f.ebner at proxmox.com
Thu Dec 19 11:43:09 CET 2024


Currently, the helper would not distinguish between different kinds
of errors. Instead of relying on an error, list the images and check
there.

Signed-off-by: Fiona Ebner <f.ebner at proxmox.com>
---

Changes in v3:
* do not match error message, list images instead

 src/PVE/Storage/RBDPlugin.pm | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/src/PVE/Storage/RBDPlugin.pm b/src/PVE/Storage/RBDPlugin.pm
index 44770b0..1d1cfad 100644
--- a/src/PVE/Storage/RBDPlugin.pm
+++ b/src/PVE/Storage/RBDPlugin.pm
@@ -357,11 +357,24 @@ sub rbd_volume_du {
 my sub rbd_volume_exists {
     my ($scfg, $storeid, $volname) = @_;
 
-    eval {
-	my $cmd = $rbd_cmd->($scfg, $storeid, 'info', $volname);
-	run_rbd_command($cmd, errmsg => "exist check",  quiet => 1);
-    };
-    return $@ ? undef : 1;
+    my $cmd = $rbd_cmd->($scfg, $storeid, 'ls', '--format', 'json');
+    my $raw = '';
+    run_rbd_command(
+	$cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => sub { $raw .= shift; });
+
+    my $list;
+    if ($raw =~ m/^(\[.*\])$/s) { # untaint
+	$list = eval { JSON::decode_json($1); };
+	die "invalid JSON output from 'rbd ls': $@\n" if $@;
+    } else {
+	die "got unexpected data from 'rbd ls': '$raw'\n";
+    }
+
+    for my $name ($list->@*) {
+	return 1 if $name eq $volname;
+    }
+
+    return 0;
 }
 
 # Configuration
-- 
2.39.5





More information about the pve-devel mailing list