use strict;
use warnings;
+use Cwd qw(abs_path);
use IO::File;
use JSON;
use Net::IP;
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);
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);
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;
}
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 {
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}";
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 {
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);