[pve-devel] [PATCH 9/9] zfs: move and refactor code

Wolfgang Link w.link at proxmox.com
Fri Jan 23 10:32:45 CET 2015


copy and modify create_base and refactor clone_image

Signed-off-by: Wolfgang Link <w.link at proxmox.com>
---
 PVE/Storage/ZFSDirPlugin.pm |   43 +++++++++++++++++++++++++++++++++++++++++++
 PVE/Storage/ZFSPlugin.pm    |   13 +------------
 2 files changed, 44 insertions(+), 12 deletions(-)

diff --git a/PVE/Storage/ZFSDirPlugin.pm b/PVE/Storage/ZFSDirPlugin.pm
index 9081d01..fa5a070 100644
--- a/PVE/Storage/ZFSDirPlugin.pm
+++ b/PVE/Storage/ZFSDirPlugin.pm
@@ -421,6 +421,49 @@ sub deactivate_volume {
     return 1;
 }
 
+sub clone_image {
+    my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
+
+    $snap ||= '__base__';
+
+    my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
+        $class->parse_volname($volname);
+
+    die "clone_image only works on base images\n" if !$isBase;
+
+    my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid);
+
+    warn "clone $volname: $basename to $name\n";
+
+    $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
+
+    return $name;
+}
+
+sub create_base {
+    my ($class, $storeid, $scfg, $volname) = @_;
+
+    my $snap = '__base__';
+
+    my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
+        $class->parse_volname($volname);
+
+    die "create_base not possible with base image\n" if $isBase;
+
+    my $newname = $name;
+    $newname =~ s/^vm-/base-/;
+
+    my $newvolname = $basename ? "$basename/$newname" : "$newname";
+
+    $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
+
+    my $running  = undef; #fixme : is create_base always offline ?
+
+    $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
+
+    return $newvolname;
+}
+
 sub volume_has_feature {
     my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
 
diff --git a/PVE/Storage/ZFSPlugin.pm b/PVE/Storage/ZFSPlugin.pm
index 28600dc..afead22 100644
--- a/PVE/Storage/ZFSPlugin.pm
+++ b/PVE/Storage/ZFSPlugin.pm
@@ -254,18 +254,7 @@ sub create_base {
 sub clone_image {
     my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
 
-    $snap ||= '__base__';
-
-    my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
-        $class->parse_volname($volname);
-
-    die "clone_image only works on base images\n" if !$isBase;
-
-    my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid);
-
-    warn "clone $volname: $basename to $name\n";
-
-    $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
+    my $name  = $class->SUPER::clone_image($class, $scfg, $storeid, $volname, $vmid, $snap);
 
     my $guid = $class->zfs_create_lu($scfg, $name);
     $class->zfs_add_lun_mapping_entry($scfg, $name, $guid);
-- 
1.7.10.4





More information about the pve-devel mailing list