use strict;
use warnings;
use IO::File;
+use Net::IP;
use PVE::Tools qw(run_command trim);
use PVE::Storage::Plugin;
use PVE::JSONSchema qw(get_standard_option);
use base qw(PVE::Storage::Plugin);
-sub rbd_unittobytes {
- {
- "M" => 1024*1024,
- "G" => 1024*1024*1024,
- "T" => 1024*1024*1024*1024,
- }
-}
+my $rbd_unittobytes = {
+ "k" => 1024,
+ "M" => 1024*1024,
+ "G" => 1024*1024*1024,
+ "T" => 1024*1024*1024*1024,
+};
my $add_pool_to_disk = sub {
my ($scfg, $disk) = @_;
return "$pool/$disk";
};
+my $hostlist = sub {
+ my ($list_text, $separator) = @_;
+
+ my @monhostlist = PVE::Tools::split_list($list_text);
+ return join($separator, map {
+ my ($host, $port) = PVE::Tools::parse_host_and_port($_);
+ $port = defined($port) ? ":$port" : '';
+ $host = "[$host]" if Net::IP::ip_is_ipv6($host);
+ "${host}${port}"
+ } @monhostlist);
+};
+
my $rbd_cmd = sub {
my ($scfg, $storeid, $op, @options) = @_;
- my $monhost = $scfg->{monhost};
- $monhost =~ s/;/,/g;
+ my $monhost = &$hostlist($scfg->{monhost}, ',');
my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
my $cmd = ['/usr/bin/rbd', '-p', $pool, '-m', $monhost];
- if(-e $keyring){
+ if (-e $keyring) {
push @$cmd, '-n', "client.$username";
push @$cmd, '--keyring', $keyring;
push @$cmd, '--auth_supported', 'cephx';
- }else{
+ } else {
push @$cmd, '--auth_supported', 'none';
}
+ my $cephconfig = "/etc/pve/priv/ceph/${storeid}.conf";
+
+ if (-e $cephconfig) {
+ push @$cmd, '-c', $cephconfig;
+ }
+
push @$cmd, $op;
push @$cmd, @options if scalar(@options);
my $rados_cmd = sub {
my ($scfg, $storeid, $op, @options) = @_;
- my $monhost = $scfg->{monhost};
- $monhost =~ s/;/,/g;
+ my $monhost = &$hostlist($scfg->{monhost}, ',');
my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
my $cmd = ['/usr/bin/rados', '-p', $pool, '-m', $monhost];
- if(-e $keyring){
+ if (-e $keyring) {
push @$cmd, '-n', "client.$username";
push @$cmd, '--keyring', $keyring;
push @$cmd, '--auth_supported', 'cephx';
- }else{
+ } else {
push @$cmd, '--auth_supported', 'none';
}
+ my $cephconfig = "/etc/pve/priv/ceph/${storeid}.conf";
+
+ if (-e $cephconfig) {
+ push @$cmd, '-c', $cephconfig;
+ }
+
push @$cmd, $op;
push @$cmd, @options if scalar(@options);
return $cmd;
};
+# needed for volumes created using ceph jewel (or higher)
+my $krdb_feature_disable = sub {
+ my ($scfg, $storeid, $name) = @_;
+
+ return 1 if !$scfg->{krbd};
+
+ my ($major, undef, undef, undef) = ceph_version();
+ return 1 if $major < 10;
+
+ my $feature_cmd = &$rbd_cmd($scfg, $storeid, 'feature', 'disable', $name, 'deep-flatten,fast-diff,object-map,exclusive-lock');
+ run_rbd_command($feature_cmd, errmsg => "could not disable krbd-incompatible image features of rbd volume $name");
+};
+
+my $ceph_version_parser = sub {
+ my $line = shift;
+ if ($line =~ m/^ceph version ((\d+)\.(\d+)\.(\d+))(?: \([a-fA-F0-9]+\))?$/) {
+ return ($2, $3, $4, $1);
+ } else {
+ warn "Could not parse Ceph version: '$line'\n";
+ }
+};
+
+sub ceph_version {
+ my ($cache) = @_;
+
+ my $version_string = $cache;
+
+ my $major;
+ my $minor;
+ my $bugfix;
+
+ if (defined($version_string)) {
+ ($major, $minor, $bugfix, $version_string) = &$ceph_version_parser($version_string);
+ } else {
+ run_command('ceph --version', outfunc => sub {
+ my $line = shift;
+ ($major, $minor, $bugfix, $version_string) = &$ceph_version_parser($line);
+ });
+ }
+ return undef if !defined($version_string);
+ return wantarray ? ($major, $minor, $bugfix, $version_string) : $version_string;
+}
+
sub run_rbd_command {
my ($cmd, %args) = @_;
my $parser = sub {
my $line = shift;
- if ($line =~ m/^((vm|base)-(\d+)-disk-\d+)\s+(\d+)(M|G|T)\s((\S+)\/((vm|base)-\d+-\S+@\S+))?/) {
+ if ($line =~ m/^((vm|base)-(\d+)-\S+)\s+(\d+)(k|M|G|T)\s((\S+)\/((vm|base)-\d+-\S+@\S+))?/) {
my ($image, $owner, $size, $unit, $parent) = ($1, $3, $4, $5, $8);
+ return if $image =~ /@/; #skip snapshots
$list->{$pool}->{$image} = {
name => $image,
- size => $size*rbd_unittobytes()->{$unit},
+ size => $size*$rbd_unittobytes->{$unit},
parent => $parent,
vmid => $owner
};
my $parser = sub {
my $line = shift;
- if ($line =~ m/size (\d+) (M|G|T)B in (\d+) objects/) {
- $size = $1 * rbd_unittobytes()->{$2} if ($1);
+ if ($line =~ m/size (\d+) (k|M|G|T)B in (\d+) objects/) {
+ $size = $1 * $rbd_unittobytes->{$2} if ($1);
} elsif ($line =~ m/parent:\s(\S+)\/(\S+)/) {
$parent = $2;
} elsif ($line =~ m/format:\s(\d+)/) {
return ($size, $parent, $format, $protected);
}
-sub addslashes {
- my $text = shift;
- $text =~ s/;/\\;/g;
- $text =~ s/:/\\:/g;
- return $text;
-}
-
# Configuration
-PVE::JSONSchema::register_format('pve-storage-monhost', \&parse_monhost);
-sub parse_monhost {
- my ($name, $noerr) = @_;
-
- if ($name !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) {
- return undef if $noerr;
- die "lvm name '$name' contains illegal characters\n";
- }
-
- return $name;
-}
-
sub type {
return 'rbd';
}
sub plugindata {
return {
- content => [ {images => 1}, { images => 1 }],
+ content => [ {images => 1, rootdir => 1}, { images => 1 }],
};
}
return {
monhost => {
description => "Monitors daemon ips.",
- type => 'string',
+ type => 'string', format => 'pve-storage-portal-dns-list',
},
pool => {
description => "Pool.",
description => "Authsupported.",
type => 'string',
},
+ krbd => {
+ description => "Access rbd through krbd kernel module.",
+ type => 'boolean',
+ },
};
}
pool => { optional => 1 },
username => { optional => 1 },
content => { optional => 1 },
+ krbd => { optional => 1 },
};
}
my ($class, $volname) = @_;
if ($volname =~ m/^((base-(\d+)-\S+)\/)?((base)?(vm)?-(\d+)-\S+)$/) {
- return ('images', $4, $7, $2, $3, $5);
+ return ('images', $4, $7, $2, $3, $5, 'raw');
}
die "unable to parse rbd volume name '$volname'\n";
my ($vtype, $name, $vmid) = $class->parse_volname($volname);
$name .= '@'.$snapname if $snapname;
- my $monhost = addslashes($scfg->{monhost});
my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
+ return ("/dev/rbd/$pool/$name", $vmid, $vtype) if $scfg->{krbd};
+
+ my $monhost = &$hostlist($scfg->{monhost}, ';');
+ $monhost =~ s/:/\\:/g;
+
my $username = $scfg->{username} ? $scfg->{username} : 'admin';
my $path = "rbd:$pool/$name:mon_host=$monhost";
my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
- if(-e $keyring ){
+ if (-e $keyring) {
$path .= ":id=$username:auth_supported=cephx:keyring=$keyring";
- }else{
+ } else {
$path .= ":auth_supported=none";
}
+ my $cephconfig = "/etc/pve/priv/ceph/${storeid}.conf";
+
+ if (-e $cephconfig) {
+ $path .= ":conf=$cephconfig";
+ }
+
return ($path, $vmid, $vtype);
}
my $find_free_diskname = sub {
my ($storeid, $scfg, $vmid) = @_;
- my $rbd = rbd_ls($scfg, $storeid);
- my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
+ my $cmd = &$rbd_cmd($scfg, $storeid, 'ls');
my $disk_ids = {};
- my $dat = $rbd->{$pool};
- foreach my $image (keys %$dat) {
- my $volname = $dat->{$image}->{name};
- if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
+ my $parser = sub {
+ my $line = shift;
+
+ if ($line =~ m/^(vm|base)-\Q$vmid\E+-disk-(\d+)$/) {
$disk_ids->{$2} = 1;
}
- }
+ };
+
+ eval {
+ run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
+ };
+ my $err = $@;
+
+ die $err if $err && $err !~ m/doesn't contain rbd images/;
+
#fix: can we search in $rbd hash key with a regex to find (vm|base) ?
for (my $i = 1; $i < 100; $i++) {
if (!$disk_ids->{$i}) {
run_rbd_command($cmd, errmsg => "rbd clone '$basename' error");
+ &$krdb_feature_disable($scfg, $storeid, $name);
+
return $newvol;
}
my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
- die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
+ die "illegal name '$name' - should be 'vm-$vmid-*'\n"
if $name && $name !~ m/^vm-$vmid-/;
- $name = &$find_free_diskname($storeid, $scfg, $vmid);
+ $name = &$find_free_diskname($storeid, $scfg, $vmid) if !$name;
my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--image-format' , 2, '--size', int(($size+1023)/1024), $name);
run_rbd_command($cmd, errmsg => "rbd create $name' error");
+ &$krdb_feature_disable($scfg, $storeid, $name);
+
return $name;
}
}
}
+ $class->deactivate_volume($storeid, $scfg, $volname);
+
my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'purge', $name);
run_rbd_command($cmd, errmsg => "rbd snap purge '$volname' error");
my $res = [];
if (my $dat = $cache->{rbd}->{$pool}) {
- foreach my $image (keys %$dat) {
+ foreach my $image (keys %$dat) {
- my $volname = $dat->{$image}->{name};
+ my $info = $dat->{$image};
- my $volid = "$storeid:$volname";
+ my $volname = $info->{name};
+ my $parent = $info->{parent};
+ my $owner = $info->{vmid};
- my $owner = $dat->{$volname}->{vmid};
- if ($vollist) {
- my $found = grep { $_ eq $volid } @$vollist;
- next if !$found;
- } else {
- next if defined ($vmid) && ($owner ne $vmid);
- }
+ if ($parent && $parent =~ m/^(base-\d+-\S+)\@__base__$/) {
+ $info->{volid} = "$storeid:$1/$volname";
+ } else {
+ $info->{volid} = "$storeid:$volname";
+ }
+
+ if ($vollist) {
+ my $found = grep { $_ eq $info->{volid} } @$vollist;
+ next if !$found;
+ } else {
+ next if defined ($vmid) && ($owner ne $vmid);
+ }
- my $info = $dat->{$volname};
- $info->{volid} = $volid;
$info->{format} = 'raw';
- push @$res, $info;
- }
+ push @$res, $info;
+ }
}
return $res;
my $parser = sub {
my $line = shift;
- if ($line =~ m/^\s+total\s(\S+)\s+(\d+)/) {
+ if ($line =~ m/^\s*total(?:\s|_)(\S+)\s+(\d+)(k|M|G|T)?/) {
$stats->{$1} = $2;
+ # luminous has units here..
+ if ($3) {
+ $stats->{$1} *= $rbd_unittobytes->{$3}/1024;
+ }
}
};
}
sub activate_volume {
- my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
+ my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
+
+ return 1 if !$scfg->{krbd};
+
+ my ($vtype, $name, $vmid) = $class->parse_volname($volname);
+ my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
+
+ my $path = "/dev/rbd/$pool/$name";
+ $path .= '@'.$snapname if $snapname;
+ return if -b $path;
+
+ $name .= '@'.$snapname if $snapname;
+ my $cmd = &$rbd_cmd($scfg, $storeid, 'map', $name);
+ run_rbd_command($cmd, errmsg => "can't mount rbd volume $name");
+
return 1;
}
sub deactivate_volume {
- my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
+ my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
+
+ return 1 if !$scfg->{krbd};
+
+ my ($vtype, $name, $vmid) = $class->parse_volname($volname);
+ my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
+
+ my $path = "/dev/rbd/$pool/$name";
+ $path .= '@'.$snapname if $snapname;
+ return if ! -b $path;
+
+ my $cmd = &$rbd_cmd($scfg, $storeid, 'unmap', $path);
+ run_rbd_command($cmd, errmsg => "can't unmap rbd volume $name");
+
return 1;
}
sub volume_resize {
my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
- return 1 if $running;
+ return 1 if $running && !$scfg->{krbd};
my ($vtype, $name, $vmid) = $class->parse_volname($volname);
- my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--size', ($size/1024/1024), $name);
+ my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--allow-shrink', '--size', ($size/1024/1024), $name);
run_rbd_command($cmd, errmsg => "rbd resize '$volname' error");
return undef;
}
sub volume_snapshot {
- my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
-
- return 1 if $running;
+ my ($class, $scfg, $storeid, $volname, $snap) = @_;
my ($vtype, $name, $vmid) = $class->parse_volname($volname);
sub volume_snapshot_delete {
my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
- return 1 if $running;
+ return 1 if $running && !$scfg->{krbd};
+
+ $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
my ($vtype, $name, $vmid) = $class->parse_volname($volname);
my $features = {
snapshot => { current => 1, snap => 1},
- clone => { base => 1},
+ clone => { base => 1, snap => 1},
template => { current => 1},
copy => { base => 1, current => 1, snap => 1},
+ sparseinit => { base => 1, current => 1},
};
my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =