]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/Storage/RBDPlugin.pm
add disk rename feature
[pve-storage.git] / PVE / Storage / RBDPlugin.pm
index 42641e28911dc745c59f68de109c4bd992ed0114..2607d259d1ab20f0f430ee7d0083bcd77289c2ac 100644 (file)
@@ -305,6 +305,10 @@ sub properties {
            description => "Always access rbd through krbd kernel module.",
            type => 'boolean',
        },
+       keyring => {
+           description => "Client keyring contents (for external clusters).",
+           type => 'string',
+       },
     };
 }
 
@@ -318,6 +322,7 @@ sub options {
        username => { optional => 1 },
        content => { optional => 1 },
        krbd => { optional => 1 },
+       keyring => { optional => 1 },
        bwlimit => { optional => 1 },
     };
 }
@@ -327,20 +332,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;
+}
+
+sub on_update_hook {
+    my ($class, $storeid, $scfg, %param) = @_;
 
-    PVE::CephConfig::ceph_create_keyfile($scfg->{type}, $storeid);
+    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;
 }
 
@@ -503,7 +517,7 @@ 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");
+    run_rbd_command($cmd, errmsg => "rbd create '$name' error");
 
     return $name;
 }
@@ -526,10 +540,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;
 }
@@ -728,6 +742,7 @@ 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);
@@ -743,4 +758,37 @@ sub volume_has_feature {
     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}'",
+    );
+
+    $base_name = $base_name ? "${base_name}/" : '';
+
+    return "${storeid}:${base_name}${target_volname}";
+}
+
 1;