]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/Storage/ZFSPoolPlugin.pm
Introduce allow_rename parameter for pvesm import and storage_migrate
[pve-storage.git] / PVE / Storage / ZFSPoolPlugin.pm
index 456fb4037267726d55450bd831c3420d30f2984c..b4fd65f15ccc7e5e405ce6290c3f4c3f68cb4135 100644 (file)
@@ -153,8 +153,10 @@ sub on_add_hook {
     my $cfg_mountpoint = $scfg->{mountpoint};
 
     # ignore failure, pool might currently not be imported
-    my $mountpoint = eval {
-       $class->zfs_get_properties($scfg, 'mountpoint', $scfg->{pool}, 1)
+    my $mountpoint;
+    eval {
+       my $res = $class->zfs_get_properties($scfg, 'mountpoint', $scfg->{pool}, 1);
+       $mountpoint = PVE::Storage::Plugin::verify_path($res, 1) if defined($res);
     };
 
     if (defined($cfg_mountpoint)) {
@@ -240,7 +242,7 @@ sub alloc_image {
 
        die "illegal name '$volname' - should be 'vm-$vmid-*'\n"
            if $volname && $volname !~ m/^vm-$vmid-/;
-       $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt)
+       $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt)
            if !$volname;
 
        $class->zfs_create_zvol($scfg, $volname, $size);
@@ -250,7 +252,7 @@ sub alloc_image {
 
        die "illegal name '$volname' - should be 'subvol-$vmid-*'\n"
            if $volname && $volname !~ m/^subvol-$vmid-/;
-       $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt)
+       $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt)
            if !$volname;
 
        die "illegal name '$volname' - should be 'subvol-$vmid-*'\n"
@@ -423,16 +425,6 @@ sub zfs_list_zvol {
     return $list;
 }
 
-sub zfs_find_free_diskname {
-    my ($class, $storeid, $scfg, $vmid, $format) = @_;
-
-    my $volumes = $class->zfs_list_zvol($scfg);
-    my $dat = $volumes->{$scfg->{pool}};
-
-    my $disk_list = [ keys %$dat ];
-    return PVE::Storage::Plugin::get_next_vm_diskname($disk_list, $storeid, $vmid, $format, $scfg);
-}
-
 sub zfs_get_latest_snapshot {
     my ($class, $scfg, $volname) = @_;
 
@@ -612,7 +604,7 @@ sub clone_image {
 
     die "clone_image only works on base images\n" if !$isBase;
 
-    my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $format);
+    my $name = $class->find_free_diskname($storeid, $scfg, $vmid, $format);
 
     if ($format eq 'subvol') {
        my $size = $class->zfs_request($scfg, undef, 'list', '-H', '-o', 'refquota', "$scfg->{pool}/$basename");
@@ -662,6 +654,12 @@ sub volume_resize {
 
     my $attr = $format eq 'subvol' ? 'refquota' : 'volsize';
 
+    # align size to 1M so we always have a valid multiple of the volume block size
+    if ($format eq 'raw') {
+       my $padding = (1024 - $new_size % 1024) % 1024;
+       $new_size = $new_size + $padding;
+    }
+
     $class->zfs_request($scfg, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$vname");
 
     return $new_size;
@@ -745,7 +743,7 @@ sub volume_export_formats {
 }
 
 sub volume_import {
-    my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_;
+    my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots, $allow_rename) = @_;
 
     die "unsupported import stream format for $class: $format\n"
        if $format ne 'zfs';
@@ -754,15 +752,18 @@ sub volume_import {
     die "internal error: invalid file handle for volume_import\n"
        if !defined($fd);
 
-    my $dataset = ($class->parse_volname($volname))[1];
+    my (undef, $dataset, $vmid) = $class->parse_volname($volname);
     my $zfspath = "$scfg->{pool}/$dataset";
     my $suffix = defined($base_snapshot) ? "\@$base_snapshot" : '';
     my $exists = 0 == run_command(['zfs', 'get', '-H', 'name', $zfspath.$suffix],
                             noerr => 1, errfunc => sub {});
     if (defined($base_snapshot)) {
        die "base snapshot '$zfspath\@$base_snapshot' doesn't exist\n" if !$exists;
-    } else {
-       die "volume '$zfspath' already exists\n" if $exists;
+    } elsif ($exists) {
+       die "volume '$zfspath' already exists\n" if !$allow_rename;
+       warn "volume '$zfspath' already exists - importing with a different name\n";
+       $dataset = $class->find_free_diskname($storeid, $scfg, $vmid, $format);
+       $zfspath = "$scfg->{pool}/$dataset";
     }
 
     eval { run_command(['zfs', 'recv', '-F', '--', $zfspath], input => "<&$fd") };
@@ -775,7 +776,7 @@ sub volume_import {
        die $err;
     }
 
-    return;
+    return "$storeid:$dataset";
 }
 
 sub volume_import_formats {