]> 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 16fb0d62fedd0b457a60887a2c8f49c4edd7fe8c..b4fd65f15ccc7e5e405ce6290c3f4c3f68cb4135 100644 (file)
@@ -32,6 +32,10 @@ sub properties {
            description => "use sparse volumes",
            type => 'boolean',
        },
+       mountpoint => {
+           description => "mount point",
+           type => 'string', format => 'pve-storage-path',
+       },
     };
 }
 
@@ -44,6 +48,7 @@ sub options {
        disable => { optional => 1 },
        content => { optional => 1 },
        bwlimit => { optional => 1 },
+       mountpoint => { optional => 1 },
     };
 }
 
@@ -142,17 +147,39 @@ sub parse_volname {
 
 # virtual zfs methods (subclass can overwrite them)
 
+sub on_add_hook {
+    my ($class, $storeid, $scfg, %param) = @_;
+
+    my $cfg_mountpoint = $scfg->{mountpoint};
+
+    # ignore failure, pool might currently not be imported
+    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)) {
+       if (defined($mountpoint) && !($cfg_mountpoint =~ m|^\Q$mountpoint\E/?$|)) {
+           warn "warning for $storeid - mountpoint: $cfg_mountpoint " .
+                "does not match current mount point: $mountpoint\n";
+       }
+    } else {
+       $scfg->{mountpoint} = $mountpoint;
+    }
+}
+
 sub path {
     my ($class, $scfg, $volname, $storeid, $snapname) = @_;
 
     my ($vtype, $name, $vmid) = $class->parse_volname($volname);
 
     my $path = '';
+    my $mountpoint = $scfg->{mountpoint} // "/$scfg->{pool}";
 
     if ($vtype eq "images") {
        if ($name =~ m/^subvol-/ || $name =~ m/^basevol-/) {
-           # fixme: we currently assume standard mount point?!
-           $path = "/$scfg->{pool}/$name";
+           $path = "$mountpoint/$name";
        } else {
            $path = "/dev/zvol/$scfg->{pool}/$name";
        }
@@ -215,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);
@@ -225,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"
@@ -287,16 +314,22 @@ sub list_images {
     return $res;
 }
 
+sub zfs_get_properties {
+    my ($class, $scfg, $properties, $dataset, $timeout) = @_;
+
+    my $result = $class->zfs_request($scfg, $timeout, 'get', '-o', 'value',
+                                    '-Hp', $properties, $dataset);
+    my @values = split /\n/, $result;
+    return wantarray ? @values : $values[0];
+}
+
 sub zfs_get_pool_stats {
     my ($class, $scfg) = @_;
 
     my $available = 0;
     my $used = 0;
 
-    my $text = $class->zfs_request($scfg, undef, 'get', '-o', 'value', '-Hp',
-               'available,used', $scfg->{pool});
-
-    my @lines = split /\n/, $text;
+    my @lines = $class->zfs_get_properties($scfg, 'available,used', $scfg->{pool});
 
     if($lines[0] =~ /^(\d+)$/) {
        $available = $1;
@@ -392,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) = @_;
 
@@ -448,8 +471,8 @@ sub volume_size_info {
         $class->parse_volname($volname);
 
     my $attr = $format eq 'subvol' ? 'refquota' : 'volsize';
-    my $text = $class->zfs_request($scfg, undef, 'get', '-Hp', $attr, "$scfg->{pool}/$vname");
-    if ($text =~ /\s$attr\s(\d+)\s/) {
+    my $value = $class->zfs_get_properties($scfg, $attr, "$scfg->{pool}/$vname");
+    if ($value =~ /^(\d+)$/) {
        return $1;
     }
 
@@ -581,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");
@@ -631,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;
@@ -714,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';
@@ -723,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") };
@@ -744,7 +776,7 @@ sub volume_import {
        die $err;
     }
 
-    return;
+    return "$storeid:$dataset";
 }
 
 sub volume_import_formats {