]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/Storage/RBDPlugin.pm
api: fix get content call response type for RBD/ZFS/iSCSI volumes
[pve-storage.git] / PVE / Storage / RBDPlugin.pm
index 3511914dea9466de35f4e3ea952d2c4f806d44a9..35b2372b704ba6dcf53fd7d87b46140c6b30421b 100644 (file)
@@ -3,6 +3,7 @@ package PVE::Storage::RBDPlugin;
 use strict;
 use warnings;
 
+use Cwd qw(abs_path);
 use IO::File;
 use JSON;
 use Net::IP;
@@ -12,8 +13,9 @@ use PVE::Cluster qw(cfs_read_file);;
 use PVE::JSONSchema qw(get_standard_option);
 use PVE::ProcFSTools;
 use PVE::RADOS;
+use PVE::RPCEnvironment;
 use PVE::Storage::Plugin;
-use PVE::Tools qw(run_command trim);
+use PVE::Tools qw(run_command trim file_read_firstline);
 
 use base qw(PVE::Storage::Plugin);
 
@@ -26,7 +28,10 @@ my $get_parent_image_name = sub {
 my $librados_connect = sub {
     my ($scfg, $storeid, $options) = @_;
 
-    my $librados_config = PVE::CephConfig::ceph_connect_option($scfg, $storeid);
+    $options->{timeout} = 60
+       if !defined($options->{timeout}) && PVE::RPCEnvironment->is_worker();
+
+    my $librados_config = PVE::CephConfig::ceph_connect_option($scfg, $storeid, $options->%*);
 
     my $rados = PVE::RADOS->new(%$librados_config);
 
@@ -66,7 +71,13 @@ my sub get_rbd_dev_path {
     my $pve_path = "/dev/rbd-pve/${cluster_id}/${rbd_path}";
     my $path = "/dev/rbd/${rbd_path}";
 
-    return $path if !-e $pve_path && -e $path; # mapped before rbd-pve udev rule existed
+    if (!-e $pve_path && -e $path) {
+       # possibly mapped before rbd-pve rule existed
+       my $real_dev = abs_path($path);
+       my ($rbd_id) = ($real_dev =~ m|/dev/rbd([0-9]+)$|);
+       my $dev_cluster_id = file_read_firstline("/sys/devices/rbd/${rbd_id}/cluster_fsid");
+       return $path if $cluster_id eq $dev_cluster_id;
+    }
     return $pve_path;
 }
 
@@ -297,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 {
@@ -410,8 +460,10 @@ sub path {
     my ($vtype, $name, $vmid) = $class->parse_volname($volname);
     $name .= '@'.$snapname if $snapname;
 
-    my $rbd_dev_path = get_rbd_dev_path($scfg, $storeid, $name);
-    return ($rbd_dev_path, $vmid, $vtype) if $scfg->{krbd};
+    if ($scfg->{krbd}) {
+       my $rbd_dev_path = get_rbd_dev_path($scfg, $storeid, $name);
+       return ($rbd_dev_path, $vmid, $vtype);
+    }
 
     my $rbd_path = get_rbd_path($scfg, $name);
     my $path = "rbd:${rbd_path}";
@@ -716,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 {
@@ -754,8 +807,6 @@ sub volume_snapshot_rollback {
 sub volume_snapshot_delete {
     my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
 
-    return 1 if $running && !$scfg->{krbd}; # FIXME: ????
-
     $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
 
     my ($vtype, $name, $vmid) = $class->parse_volname($volname);