rbd plugin: improve volume exists helper

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@proxmox.com>
This commit is contained in:
Fiona Ebner
2024-12-19 11:43:09 +01:00
committed by Fabian Grünbichler
parent b6f4b89640
commit e2cf3cea96

View File

@ -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