]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/Storage/RBDPlugin.pm
rbd: allow to use client custom ceph conf for each storeid
[pve-storage.git] / PVE / Storage / RBDPlugin.pm
index 2c45a68dc09871721c9a6074b963e8de1624eb5b..43e1210af41cedae879554fd03db0ca3c9b3bd36 100644 (file)
@@ -3,6 +3,7 @@ package PVE::Storage::RBDPlugin;
 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);
@@ -25,11 +26,21 @@ my $add_pool_to_disk = sub {
     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';
@@ -45,6 +56,12 @@ my $rbd_cmd = sub {
        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);
@@ -55,8 +72,7 @@ my $rbd_cmd = sub {
 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';
@@ -72,6 +88,12 @@ my $rados_cmd = sub {
        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);
@@ -79,6 +101,49 @@ my $rados_cmd = sub {
     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) = @_;
 
@@ -179,25 +244,13 @@ sub rbd_volume_info {
 
 # 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 }],
     };
 }
 
@@ -205,7 +258,7 @@ sub properties {
     return {
        monhost => {
            description => "Monitors daemon ips.",
-           type => 'string',
+           type => 'string', format => 'pve-storage-portal-dns-list',
        },
        pool => {
            description => "Pool.",
@@ -219,6 +272,10 @@ sub properties {
            description => "Authsupported.",
            type => 'string',
        },
+       krbd => {
+           description => "Access rbd through krbd kernel module.",
+           type => 'boolean',
+       },
     };
 }
 
@@ -230,6 +287,7 @@ sub options {
        pool => { optional => 1 },
        username => { optional => 1 },
        content => { optional => 1 },
+       krbd => { optional => 1 },
     };
 }
 
@@ -239,7 +297,7 @@ sub parse_volname {
     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";
@@ -251,10 +309,12 @@ sub path {
     my ($vtype, $name, $vmid) = $class->parse_volname($volname);
     $name .= '@'.$snapname if $snapname;
 
-    my $monhost = $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 $pool =  $scfg->{pool} ? $scfg->{pool} : 'rbd';
     my $username =  $scfg->{username} ? $scfg->{username} : 'admin';
 
     my $path = "rbd:$pool/$name:mon_host=$monhost";
@@ -266,6 +326,12 @@ sub path {
        $path .= ":auth_supported=none";
     }
 
+    my $cephconfig = "/etc/pve/priv/ceph/${storeid}.conf";
+
+    if(-e $cephconfig){
+       $path .= ":conf=$cephconfig";
+    }
+
     return ($path, $vmid, $vtype);
 }
 
@@ -367,6 +433,8 @@ sub clone_image {
 
     run_rbd_command($cmd, errmsg => "rbd clone '$basename' error");
 
+    &$krdb_feature_disable($scfg, $storeid, $name);
+
     return $newvol;
 }
 
@@ -382,6 +450,8 @@ sub alloc_image {
     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;
 }
 
@@ -400,6 +470,8 @@ sub free_image {
        }
     }
 
+    $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");
 
@@ -480,12 +552,39 @@ sub deactivate_storage {
 }
 
 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;
 }
 
@@ -510,9 +609,7 @@ sub volume_resize {
 }
 
 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);
 
@@ -535,6 +632,8 @@ sub volume_snapshot_delete {
 
     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);
@@ -558,6 +657,7 @@ sub volume_has_feature {
        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) =