]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/Storage/RBDPlugin.pm
export: add missing format query call parameter
[pve-storage.git] / PVE / Storage / RBDPlugin.pm
index 56930af299b356f878847809a5e392dfbd04adeb..0db9b95ec40f32ac2670f18c3a8f0706cfe83c81 100644 (file)
@@ -3,19 +3,19 @@ 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);
 
 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) = @_;
@@ -25,11 +25,22 @@ 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';
@@ -37,14 +48,20 @@ my $rbd_cmd = sub {
 
     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);
@@ -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';
@@ -64,14 +80,20 @@ my $rados_cmd = sub {
 
     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);
@@ -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) = @_;
 
@@ -119,12 +184,13 @@ sub rbd_ls {
     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
            };
@@ -160,8 +226,8 @@ sub rbd_volume_info {
     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+)/) {
@@ -177,34 +243,15 @@ sub rbd_volume_info {
     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 }],
     };
 }
 
@@ -212,7 +259,7 @@ sub properties {
     return {
        monhost => {
            description => "Monitors daemon ips.",
-           type => 'string',
+           type => 'string', format => 'pve-storage-portal-dns-list',
        },
        pool => {
            description => "Pool.",
@@ -226,6 +273,10 @@ sub properties {
            description => "Authsupported.",
            type => 'string',
        },
+       krbd => {
+           description => "Access rbd through krbd kernel module.",
+           type => 'boolean',
+       },
     };
 }
 
@@ -237,6 +288,7 @@ sub options {
        pool => { optional => 1 },
        username => { optional => 1 },
        content => { optional => 1 },
+       krbd => { optional => 1 },
     };
 }
 
@@ -246,7 +298,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";
@@ -258,36 +310,53 @@ sub path {
     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}) {
@@ -372,6 +441,8 @@ sub clone_image {
 
     run_rbd_command($cmd, errmsg => "rbd clone '$basename' error");
 
+    &$krdb_feature_disable($scfg, $storeid, $name);
+
     return $newvol;
 }
 
@@ -379,14 +450,16 @@ sub alloc_image {
     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;
 }
 
@@ -405,6 +478,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");
 
@@ -423,26 +498,31 @@ sub list_images {
     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;
@@ -457,8 +537,12 @@ sub status {
 
     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;
+           }
        }
     };
 
@@ -485,12 +569,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;
 }
 
@@ -505,19 +616,17 @@ sub volume_size_info {
 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);
 
@@ -538,7 +647,9 @@ sub volume_snapshot_rollback {
 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);
 
@@ -560,9 +671,10 @@ sub volume_has_feature {
 
    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) =