]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/Storage/RBDPlugin.pm
RBD plugin: librados connect: increase timeout when in worker
[pve-storage.git] / PVE / Storage / RBDPlugin.pm
index a0389687c341f52b25974ed36183b8305af83892..dc6e79d1dac88b47753f7af02e1c3c60be6d7ef1 100644 (file)
@@ -3,16 +3,19 @@ package PVE::Storage::RBDPlugin;
 use strict;
 use warnings;
 
+use Cwd qw(abs_path);
 use IO::File;
 use JSON;
 use Net::IP;
 
 use PVE::CephConfig;
+use PVE::Cluster qw(cfs_read_file);;
 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);
 
@@ -22,15 +25,62 @@ my $get_parent_image_name = sub {
     return $parent->{image} . "@" . $parent->{snapshot};
 };
 
+my $librados_connect = sub {
+    my ($scfg, $storeid, $options) = @_;
+
+    $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);
+
+    return $rados;
+};
+
 my sub get_rbd_path {
     my ($scfg, $volume) = @_;
-    my $pool =  $scfg->{pool} ? $scfg->{pool} : 'rbd';
-    my $namespace = $scfg->{namespace};
-
-    return "${pool}/${namespace}/${volume}" if defined($namespace);
-    return "${pool}/${volume}";
+    my $path = $scfg->{pool} ? $scfg->{pool} : 'rbd';
+    $path .= "/$scfg->{namespace}" if defined($scfg->{namespace});
+    $path .= "/$volume" if defined($volume);
+    return $path;
 };
 
+my sub get_rbd_dev_path {
+    my ($scfg, $storeid, $volume) = @_;
+
+    my $cluster_id = '';
+    if ($scfg->{fsid}) {
+       # NOTE: the config doesn't support this currently (but it could!), hack for qemu-server tests
+       $cluster_id = $scfg->{fsid};
+    } elsif ($scfg->{monhost}) {
+       my $rados = $librados_connect->($scfg, $storeid);
+       $cluster_id = $rados->mon_command({ prefix => 'fsid', format => 'json' })->{fsid};
+    } else {
+       $cluster_id = cfs_read_file('ceph.conf')->{global}->{fsid};
+    }
+
+    my $uuid_pattern = "([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})";
+    if ($cluster_id =~ qr/^${uuid_pattern}$/is) {
+       $cluster_id = $1; # use untained value
+    } else {
+       die "cluster fsid has invalid format\n";
+    }
+
+    my $rbd_path = get_rbd_path($scfg, $volume);
+    my $pve_path = "/dev/rbd-pve/${cluster_id}/${rbd_path}";
+    my $path = "/dev/rbd/${rbd_path}";
+
+    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;
+}
+
 my $build_cmd = sub {
     my ($binary, $scfg, $storeid, $op, @options) = @_;
 
@@ -71,16 +121,6 @@ my $rados_cmd = sub {
     return $build_cmd->('/usr/bin/rados', $scfg, $storeid, $op, @options);
 };
 
-my $librados_connect = sub {
-    my ($scfg, $storeid, $options) = @_;
-
-    my $librados_config = PVE::CephConfig::ceph_connect_option($scfg, $storeid);
-
-    my $rados = PVE::RADOS->new(%$librados_config);
-
-    return $rados;
-};
-
 # needed for volumes created using ceph jewel (or higher)
 my $krbd_feature_update = sub {
     my ($scfg, $storeid, $name) = @_;
@@ -147,7 +187,7 @@ sub run_rbd_command {
            *STDERR->flush();
        };
     }
-    
+
     eval { run_command($cmd, %args); };
     if (my $err = $@) {
        die $errmsg . $lasterr if length($lasterr);
@@ -290,8 +330,12 @@ sub properties {
            description => "Pool.",
            type => 'string',
        },
-       namespace=> {
-           description => "RBD Namespace.",
+       'data-pool' => {
+           description => "Data Pool (for erasure coding only)",
+           type => 'string',
+       },
+       namespace => {
+           description => "Namespace.",
            type => 'string',
        },
        username => {
@@ -306,6 +350,10 @@ sub properties {
            description => "Always access rbd through krbd kernel module.",
            type => 'boolean',
        },
+       keyring => {
+           description => "Client keyring contents (for external clusters).",
+           type => 'string',
+       },
     };
 }
 
@@ -315,10 +363,12 @@ sub options {
        disable => { optional => 1 },
        monhost => { optional => 1},
        pool => { optional => 1 },
+       'data-pool' => { optional => 1 },
        namespace => { optional => 1 },
        username => { optional => 1 },
        content => { optional => 1 },
        krbd => { optional => 1 },
+       keyring => { optional => 1 },
        bwlimit => { optional => 1 },
     };
 }
@@ -328,20 +378,29 @@ sub options {
 sub on_add_hook {
     my ($class, $storeid, $scfg, %param) = @_;
 
-    return if defined($scfg->{monhost}); # nothing to do if not pve managed ceph
+    my $secret = $param{keyring} if defined $param{keyring} // undef;
+    PVE::CephConfig::ceph_create_keyfile($scfg->{type}, $storeid, $secret);
+
+    return;
+}
 
-    PVE::CephConfig::ceph_create_keyfile($scfg->{type}, $storeid);
+sub on_update_hook {
+    my ($class, $storeid, $scfg, %param) = @_;
+
+    if (exists($param{keyring})) {
+       if (defined($param{keyring})) {
+           PVE::CephConfig::ceph_create_keyfile($scfg->{type}, $storeid, $param{keyring});
+       } else {
+           PVE::CephConfig::ceph_remove_keyfile($scfg->{type}, $storeid);
+       }
+    }
 
     return;
 }
 
 sub on_delete_hook {
     my ($class, $storeid, $scfg) = @_;
-
-    return if defined($scfg->{monhost}); # nothing to do if not pve managed ceph
-
     PVE::CephConfig::ceph_remove_keyfile($scfg->{type}, $storeid);
-
     return;
 }
 
@@ -362,9 +421,12 @@ sub path {
     my ($vtype, $name, $vmid) = $class->parse_volname($volname);
     $name .= '@'.$snapname if $snapname;
 
-    my $rbd_path = get_rbd_path($scfg, $name);
-    return ("/dev/rbd/${rbd_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}";
 
     $path .= ":conf=$cmd_option->{ceph_conf}" if $cmd_option->{ceph_conf};
@@ -436,6 +498,9 @@ sub create_base {
     );
     run_rbd_command($cmd, errmsg => "rbd rename '$name' error");
 
+    eval { $class->unmap_volume($storeid, $scfg, $volname); };
+    warn $@ if $@;
+
     my $running  = undef; #fixme : is create_base always offline ?
 
     $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
@@ -479,16 +544,13 @@ sub clone_image {
     my $newvol = "$basename/$name";
     $newvol = $name if length($snapname);
 
-    my $cmd = $rbd_cmd->(
-       $scfg,
-       $storeid,
-       'clone',
+    my @options = (
        get_rbd_path($scfg, $basename),
-       '--snap',
-       $snap,
-       get_rbd_path($scfg, $name),
+       '--snap', $snap,
     );
+    push @options, ('--data-pool', $scfg->{'data-pool'}) if $scfg->{'data-pool'};
 
+    my $cmd = $rbd_cmd->($scfg, $storeid, 'clone', @options, get_rbd_path($scfg, $name));
     run_rbd_command($cmd, errmsg => "rbd clone '$basename' error");
 
     return $newvol;
@@ -503,8 +565,14 @@ sub alloc_image {
 
     $name = $class->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");
+    my @options = (
+       '--image-format' , 2,
+       '--size', int(($size + 1023) / 1024),
+    );
+    push @options, ('--data-pool', $scfg->{'data-pool'}) if $scfg->{'data-pool'};
+
+    my $cmd = $rbd_cmd->($scfg, $storeid, 'create', @options, $name);
+    run_rbd_command($cmd, errmsg => "rbd create '$name' error");
 
     return $name;
 }
@@ -527,10 +595,10 @@ 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");
+    run_rbd_command($cmd, errmsg => "rbd snap purge '$name' error");
 
     $cmd = $rbd_cmd->($scfg, $storeid, 'rm', $name);
-    run_rbd_command($cmd, errmsg => "rbd rm '$volname' error");
+    run_rbd_command($cmd, errmsg => "rbd rm '$name' error");
 
     return undef;
 }
@@ -539,39 +607,33 @@ sub list_images {
     my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
 
     $cache->{rbd} = rbd_ls($scfg, $storeid) if !$cache->{rbd};
-    my $pool =  $scfg->{pool} ? $scfg->{pool} : 'rbd';
-    $pool .= "/$scfg->{namespace}" if defined($scfg->{namespace});
 
-    my $res = [];
-
-    if (my $dat = $cache->{rbd}->{$pool}) {
-       foreach my $image (keys %$dat) {
-
-           my $info = $dat->{$image};
+    my $dat = $cache->{rbd}->{get_rbd_path($scfg)};
+    return [] if !$dat; # nothing found
 
-           my $volname = $info->{name};
-           my $parent = $info->{parent};
-           my $owner = $info->{vmid};
-
-           if ($parent && $parent =~ m/^(base-\d+-\S+)\@__base__$/) {
-               $info->{volid} = "$storeid:$1/$volname";
-           } else {
-               $info->{volid} = "$storeid:$volname";
-           }
+    my $res = [];
+    for my $image (sort keys %$dat) {
+       my $info = $dat->{$image};
+       my ($volname, $parent, $owner) = $info->@{'name', 'parent', '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);
-           }
+       if ($vollist) {
+           my $found = grep { $_ eq $info->{volid} } @$vollist;
+           next if !$found;
+       } else {
+           next if defined ($vmid) && ($owner ne $vmid);
+       }
 
-           $info->{format} = 'raw';
+       $info->{format} = 'raw';
 
-           push @$res, $info;
-       }
+       push @$res, $info;
     }
-    
+
     return $res;
 }
 
@@ -581,7 +643,14 @@ sub status {
     my $rados = $librados_connect->($scfg, $storeid);
     my $df = $rados->mon_command({ prefix => 'df', format => 'json' });
 
-    my ($d) = grep { $_->{name} eq $scfg->{pool} } @{$df->{pools}};
+    my $pool = $scfg->{'data-pool'} // $scfg->{pool} // 'rbd';
+
+    my ($d) = grep { $_->{name} eq $pool } @{$df->{pools}};
+
+    if (!defined($d)) {
+       warn "could not get usage stats for pool '$pool'\n";
+       return;
+    }
 
     # max_avail -> max available space for data w/o replication in the pool
     # bytes_used -> data w/o replication in the pool
@@ -603,11 +672,6 @@ sub deactivate_storage {
     return 1;
 }
 
-my $get_kernel_device_name = sub {
-    my ($scfg, $name) = @_;
-    return "/dev/rbd/" . get_rbd_path($scfg, $name);
-};
-
 sub map_volume {
     my ($class, $storeid, $scfg, $volname, $snapname) = @_;
 
@@ -616,7 +680,7 @@ sub map_volume {
     my $name = $img_name;
     $name .= '@'.$snapname if $snapname;
 
-    my $kerneldev = $get_kernel_device_name->($scfg, $name);
+    my $kerneldev = get_rbd_dev_path($scfg, $storeid, $name);
 
     return $kerneldev if -b $kerneldev; # already mapped
 
@@ -635,7 +699,7 @@ sub unmap_volume {
     my ($vtype, $name, $vmid) = $class->parse_volname($volname);
     $name .= '@'.$snapname if $snapname;
 
-    my $kerneldev = $get_kernel_device_name->($scfg, $name);
+    my $kerneldev = get_rbd_dev_path($scfg, $storeid, $name);
 
     if (-b $kerneldev) {
        my $cmd = $rbd_cmd->($scfg, $storeid, 'unmap', $kerneldev);
@@ -723,7 +787,6 @@ sub volume_snapshot_delete {
 }
 
 sub volume_snapshot_needs_fsfreeze {
-
     return 1;
 }
 
@@ -736,20 +799,56 @@ sub volume_has_feature {
        template => { current => 1},
        copy => { base => 1, current => 1, snap => 1},
        sparseinit => { base => 1, current => 1},
+       rename => {current => 1},
     };
 
-    my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
-        $class->parse_volname($volname);
+    my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = $class->parse_volname($volname);
 
     my $key = undef;
-    if($snapname){
+    if ($snapname){
        $key = 'snap';
-    }else{
-       $key =  $isBase ? 'base' : 'current';
+    } else {
+       $key = $isBase ? 'base' : 'current';
     }
     return 1 if $features->{$feature}->{$key};
 
     return undef;
 }
 
+sub rename_volume {
+    my ($class, $scfg, $storeid, $source_volname, $target_vmid, $target_volname) = @_;
+
+    my (
+       undef,
+       $source_image,
+       $source_vmid,
+       $base_name,
+       $base_vmid,
+       undef,
+       $format
+    ) = $class->parse_volname($source_volname);
+    $target_volname = $class->find_free_diskname($storeid, $scfg, $target_vmid, $format)
+       if !$target_volname;
+
+    eval {
+       my $cmd = $rbd_cmd->($scfg, $storeid, 'info', $target_volname);
+       run_rbd_command($cmd, errmsg => "exist check",  quiet => 1);
+    };
+    die "target volume '${target_volname}' already exists\n" if !$@;
+
+    my $cmd = $rbd_cmd->($scfg, $storeid, 'rename', $source_image, $target_volname);
+
+    run_rbd_command(
+       $cmd,
+       errmsg => "could not rename image '${source_image}' to '${target_volname}'",
+    );
+
+    eval { $class->unmap_volume($storeid, $scfg, $source_volname); };
+    warn $@ if $@;
+
+    $base_name = $base_name ? "${base_name}/" : '';
+
+    return "${storeid}:${base_name}${target_volname}";
+}
+
 1;