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);
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';
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';
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) = @_;
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";
$path .= ":auth_supported=none";
}
+ my $cephconfig = "/etc/pve/priv/ceph/${storeid}.conf";
+
+ if(-e $cephconfig){
+ $path .= ":conf=$cephconfig";
+ }
+
return ($path, $vmid, $vtype);
}
run_rbd_command($cmd, errmsg => "rbd clone '$basename' error");
+ &$krdb_feature_disable($scfg, $storeid, $name);
+
return $newvol;
}
die "illegal name '$name' - sould 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");
}
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_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);
return 1 if $running;
+ $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
+
my ($vtype, $name, $vmid) = $class->parse_volname($volname);
+ my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $name, $snap);
+ if ($protected){
+ my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
+ run_rbd_command($cmd, errmsg => "rbd unprotect $name snap '$snap' error");
+ }
+
my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name);
run_rbd_command($cmd, errmsg => "rbd snapshot '$volname' error");
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) =