]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/Storage/RBDPlugin.pm
rbd : clone_image
[pve-storage.git] / PVE / Storage / RBDPlugin.pm
index 7f46329b66de6f7453f7f7a1850c0ae9387edb2d..4936d8f20bd3e61eca49e77ea036c7eab4849517 100644 (file)
@@ -59,8 +59,8 @@ sub rbd_ls {
     my $parser = sub {
        my $line = shift;
 
-       if ($line =~  m/^(vm-(\d+)-disk-\d+)\s+(\d+)(M|G|T)\s((\S+)\/(vm-\d+-\S+@\S+))?/) {
-           my ($image, $owner, $size, $unit, $parent) = ($1, $2, $3, $4, $7);
+       if ($line =~  m/^((vm|base)-(\d+)-disk-\d+)\s+(\d+)(M|G|T)\s((\S+)\/((vm|base)-\d+-\S+@\S+))?/) {
+           my ($image, $owner, $size, $unit, $parent) = ($1, $3, $4, $5, $8);
 
            $list->{$scfg->{pool}}->{$image} = {
                name => $image,
@@ -82,11 +82,20 @@ sub rbd_ls {
 }
 
 sub rbd_volume_info {
-    my ($scfg, $storeid, $volname) = @_;
+    my ($scfg, $storeid, $volname, $snap) = @_;
+
+    my $cmd = undef;
+
+    if($snap){
+       $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname, '--snap', $snap);
+    }else{
+       $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname);
+    }
 
-    my $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname);
     my $size = undef;
     my $parent = undef;
+    my $format = undef;
+    my $protected = undef;
 
     my $parser = sub {
        my $line = shift;
@@ -95,12 +104,17 @@ sub rbd_volume_info {
            $size = $1 * rbd_unittobytes()->{$2} if ($1);
        } elsif ($line =~ m/parent:\s(\S+)\/(\S+)/) {
            $parent = $2;
+       } elsif ($line =~ m/format:\s(\d+)/) {
+           $format = $1;
+       } elsif ($line =~ m/protected:\s(\S+)/) {
+           $protected = 1 if $1 eq "True";
        }
+
     };
 
     run_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
 
-    return ($size, $parent);
+    return ($size, $parent, $format, $protected);
 }
 
 sub addslashes {
@@ -172,8 +186,8 @@ sub options {
 sub parse_volname {
     my ($class, $volname) = @_;
 
-    if ($volname =~ m/^(vm-(\d+)-\S+)$/) {
-       return ('images', $1, $2);
+    if ($volname =~ m/^((base-(\d+)-\S+)\/)?((base)?(vm)?-(\d+)-\S+)$/) {
+       return ('images', $4, $7, $2, $3, $5);
     }
 
     die "unable to parse rbd volume name '$volname'\n";
@@ -194,27 +208,100 @@ sub path {
     return ($path, $vmid, $vtype);
 }
 
-sub alloc_image {
-    my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
+sub create_base {
+    my ($class, $storeid, $scfg, $volname) = @_;
 
+    my $snap = '__base__';
 
-    die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
-       if  $name && $name !~ m/^vm-$vmid-/;
+    my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
+        $class->parse_volname($volname);
+
+    die "create_base not possible with base image\n" if $isBase;
+
+    my ($size, $parent, $format, undef) = rbd_volume_info($scfg, $storeid, $name);
+    die "rbd volume info on '$name' failed\n" if !($size);
+
+    die "rbd image must be at format V2" if $format ne "2";
+
+    die "volname '$volname' contains wrong information about parent $parent $basename\n"
+        if $basename && (!$parent || $parent ne $basename."@".$snap);
+
+    my $newname = $name;
+    $newname =~ s/^vm-/base-/;
+
+    my $newvolname = $basename ? "$basename/$newname" : "$newname";
+
+    my $cmd = &$rbd_cmd($scfg, $storeid, 'rename', $name, $newname);
+    run_command($cmd, errmsg => "rbd rename $name' error", errfunc => sub {});
+
+    my $running  = undef; #fixme : is create_base always offline ?
+
+    $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
+
+    my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $newname, $snap);
+
+    if (!$protected){
+       my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'protect', $newname, '--snap', $snap);
+       run_command($cmd, errmsg => "rbd protect $newname snap $snap' error", errfunc => sub {});
+    }
+
+    return $newvolname;
+
+}
+
+sub clone_image {
+    my ($class, $scfg, $storeid, $volname, $vmid) = @_;
+
+    my $snap = '__base__';
+
+    my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
+        $class->parse_volname($volname);
+
+    die "clone_image onyl works on base images\n" if !$isBase;
+
+    my $name = &$find_free_diskname($storeid, $scfg, $vmid);
+
+    warn "clone $volname: $basename to $name\n";
+
+    my $newvol = "$basename/$name";
+
+    my $cmd = &$rbd_cmd($scfg, $storeid, 'clone', $basename, '--snap', $snap, $name);
+    run_command($cmd, errmsg => "rbd clone $basename' error", errfunc => sub {});
+
+    return $newvol;
+}
+
+my $find_free_diskname = sub {
+    my ($storeid, $scfg, $vmid) = @_;
 
-    if (!$name) {
-       my $rdb = rbd_ls($scfg, $storeid);
+    my $rbd = rbd_ls($scfg, $storeid);
+    my $disk_ids = {};
+    my $dat = $rbd->{$scfg->{pool}};
 
-       for (my $i = 1; $i < 100; $i++) {
-           my $tn = "vm-$vmid-disk-$i";
-           if (!defined ($rdb->{$scfg->{pool}}->{$tn})) {
-               $name = $tn;
-               last;
-           }
+    foreach my $image (keys %$dat) {
+       my $volname = $dat->{$image}->{name};
+       if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
+           $disk_ids->{$2} = 1;
        }
     }
+    #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}) {
+            return "vm-$vmid-disk-$i";
+        }
+    } 
+
+    die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
+};
+
+sub alloc_image {
+    my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
 
-    die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
-       if !$name;
+
+    die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
+       if  $name && $name !~ m/^vm-$vmid-/;
+
+    $name = &$find_free_diskname($storeid, $scfg, $vmid);
 
     my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--format' , 2, '--size', ($size/1024), $name);
     run_command($cmd, errmsg => "rbd create $name' error", errfunc => sub {});
@@ -223,7 +310,7 @@ sub alloc_image {
 }
 
 sub free_image {
-    my ($class, $storeid, $scfg, $volname) = @_;
+    my ($class, $storeid, $scfg, $volname, $isBase) = @_;
 
     my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'purge',  $volname);
     run_command($cmd, errmsg => "rbd snap purge $volname' error", outfunc => sub {}, errfunc => sub {});