]> git.proxmox.com Git - pve-storage.git/commitdiff
api: fix get content call response type for RBD/ZFS/iSCSI volumes
authorChristian Ebner <c.ebner@proxmox.com>
Thu, 9 Mar 2023 09:41:23 +0000 (10:41 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Mon, 20 Mar 2023 15:35:58 +0000 (16:35 +0100)
`pvesh get /nodes/{node}/storage/{storage}/content/{volume}` failed for
several storage types, because the respective storage plugins returned
only the volumes `size` on `volume_size_info` calls, while also the format
is required.

This patch fixes the issue by returning also `format` and where possible `used`.

The issue was reported in the forum:
https://forum.proxmox.com/threads/pvesh-get-nodes-node-storage-storage-content-volume-returns-error.123747/

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
 [ T: fixup white space error ]
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
PVE/Storage/ISCSIDirectPlugin.pm
PVE/Storage/RBDPlugin.pm
PVE/Storage/ZFSPoolPlugin.pm

index 9777969b39e02ee03c44aca84da332b8bb56a316..eb329d4c2968927d07834501df78feb9620ec1ba 100644 (file)
@@ -208,7 +208,7 @@ sub volume_size_info {
     my $vollist = iscsi_ls($scfg,$storeid);
     my $info = $vollist->{$storeid}->{$volname};
 
-    return $info->{size};
+    return wantarray ? ($info->{size}, 'raw', 0, undef) : $info->{size};
 }
 
 sub volume_resize {
index 9047504f002e7278b6d6d9abbfd570cf5b3452f1..35b2372b704ba6dcf53fd7d87b46140c6b30421b 100644 (file)
@@ -308,6 +308,45 @@ sub rbd_volume_info {
     return $volume->@{qw(size parent format protected features)};
 }
 
+sub rbd_volume_du {
+    my ($scfg, $storeid, $volname) = @_;
+
+    my @options = ('du', $volname, '--format', 'json');
+    my $cmd = $rbd_cmd->($scfg, $storeid, @options);
+
+    my $raw = '';
+    my $parser = sub { $raw .= shift };
+
+    run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
+
+    my $volume;
+    if ($raw eq '') {
+       $volume = {};
+    } elsif ($raw =~ m/^(\{.*\})$/s) { # untaint
+       $volume = JSON::decode_json($1);
+    } else {
+       die "got unexpected data from rbd du: '$raw'\n";
+    }
+
+    if (!defined($volume->{images})) {
+       die "got no images from rbd du\n";
+    }
+
+    # `rbd du` returns array of images for name matching `volname`,
+    # including snapshots.
+    my $images = $volume->{images};
+    foreach my $image (@$images) {
+       next if defined($image->{snapshot});
+       next if !defined($image->{used_size}) || !defined($image->{name});
+
+       # Return `used_size` of first volume with matching name which
+       # is not a snapshot.
+       return $image->{used_size} if $image->{name} eq $volname;
+    }
+
+    die "got no matching image from rbd du\n";
+}
+
 # Configuration
 
 sub type {
@@ -729,8 +768,9 @@ sub volume_size_info {
     my ($class, $scfg, $storeid, $volname, $timeout) = @_;
 
     my ($vtype, $name, $vmid) = $class->parse_volname($volname);
-    my ($size, undef) = rbd_volume_info($scfg, $storeid, $name);
-    return $size;
+    my ($size, $parent) = rbd_volume_info($scfg, $storeid, $name);
+    my $used = wantarray ? rbd_volume_du($scfg, $storeid, $name) : 0;
+    return wantarray ? ($size, 'raw', $used, $parent) : $size;
 }
 
 sub volume_resize {
index 9fbd1497fc49090de1aa6a9fdda9738f7fd6ff71..951d027c4aa47a6dde15d1985f496ec34c3fffdb 100644 (file)
@@ -446,13 +446,16 @@ sub status {
 sub volume_size_info {
     my ($class, $scfg, $storeid, $volname, $timeout) = @_;
 
-    my (undef, $vname, undef, undef, undef, undef, $format) =
+    my (undef, $vname, undef, $parent, undef, undef, $format) =
         $class->parse_volname($volname);
 
     my $attr = $format eq 'subvol' ? 'refquota' : 'volsize';
-    my $value = $class->zfs_get_properties($scfg, $attr, "$scfg->{pool}/$vname");
-    if ($value =~ /^(\d+)$/) {
-       return $1;
+    my ($size, $used) = $class->zfs_get_properties($scfg, "$attr,usedbydataset", "$scfg->{pool}/$vname");
+
+    $used = ($used =~ /^(\d+)$/) ? $1 : 0;
+
+    if ($size =~ /^(\d+)$/) {
+       return wantarray ? ($1, $format, $used, $parent) : $1;
     }
 
     die "Could not get zfs volume size\n";