]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/Storage/ZFSPoolPlugin.pm
Don't remove and recreate lun when changing a volume
[pve-storage.git] / PVE / Storage / ZFSPoolPlugin.pm
index 72da44dc38fbbd62cc71cb4a63b0f5513047982a..f66b2771d6227cc5f5b47b8710fce7b67ea3a818 100644 (file)
@@ -43,6 +43,7 @@ sub options {
        nodes => { optional => 1 },
        disable => { optional => 1 },
        content => { optional => 1 },
+       bwlimit => { optional => 1 },
     };
 }
 
@@ -166,35 +167,45 @@ sub path {
 sub zfs_request {
     my ($class, $scfg, $timeout, $method, @params) = @_;
 
-    my $default_timeout = PVE::RPCEnvironment->is_worker() ? 60*60 : 5;
-
     my $cmd = [];
 
     if ($method eq 'zpool_list') {
        push @$cmd, 'zpool', 'list';
     } elsif ($method eq 'zpool_import') {
        push @$cmd, 'zpool', 'import';
-       $default_timeout = 15 if $default_timeout < 15;
+       $timeout = 15 if !$timeout || $timeout < 15;
     } else {
        push @$cmd, 'zfs', $method;
     }
-
     push @$cmd, @params;
-    my $msg = '';
 
-    my $output = sub {
-        my $line = shift;
-        $msg .= "$line\n";
-    };
+    my $msg = '';
+    my $output = sub { $msg .= "$_[0]\n" };
 
-    $timeout =  $default_timeout if !$timeout;
+    $timeout = PVE::RPCEnvironment->is_worker() ? 60*60 : 5 if !$timeout;
 
     run_command($cmd, errmsg => "zfs error", outfunc => $output, timeout => $timeout);
 
     return $msg;
 }
 
+sub zfs_wait_for_zvol_link {
+    my ($class, $scfg, $volname, $timeout) = @_;
+
+    my $default_timeout = PVE::RPCEnvironment->is_worker() ? 60*5 : 10;
+    $timeout = $default_timeout if !defined($timeout);
+
+    my ($devname, undef, undef) = $class->path($scfg, $volname);
+
+    for (my $i = 1; $i <= $timeout; $i++) {
+       last if -b $devname;
+       die "timeout: no zvol device link for '$volname' found after $timeout sec found.\n"
+           if $i == $timeout;
+
+       sleep(1);
+    }
+}
+
 sub alloc_image {
     my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
 
@@ -202,25 +213,22 @@ sub alloc_image {
     
     if ($fmt eq 'raw') {
 
-       die "illegal name '$volname' - sould be 'vm-$vmid-*'\n"
+       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) 
            if !$volname;
 
        $class->zfs_create_zvol($scfg, $volname, $size);
-       my $devname = "/dev/zvol/$scfg->{pool}/$volname";
-
-       run_command("udevadm trigger --subsystem-match block");
-       system('udevadm', 'settle', '--timeout', '10', "--exit-if-exists=${devname}");
+       $class->zfs_wait_for_zvol_link($scfg, $volname);
 
     } elsif ( $fmt eq 'subvol') {
 
-       die "illegal name '$volname' - sould be 'subvol-$vmid-*'\n"
+       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) 
            if !$volname;
 
-       die "illegal name '$volname' - sould be 'subvol-$vmid-*'\n"
+       die "illegal name '$volname' - should be 'subvol-$vmid-*'\n"
            if $volname !~ m/^subvol-$vmid-/;
 
        $class->zfs_create_subvol($scfg, $volname, $size);      
@@ -303,7 +311,12 @@ sub zfs_get_pool_stats {
 
 sub zfs_create_zvol {
     my ($class, $scfg, $zvol, $size) = @_;
-    
+
+    # always align size to 1M as workaround until
+    # https://github.com/zfsonlinux/zfs/issues/8541 is solved
+    my $padding = (1024 - $size % 1024) % 1024;
+    $size = $size + $padding;
+
     my $cmd = ['create'];
 
     push @$cmd, '-s' if $scfg->{sparse};
@@ -382,26 +395,11 @@ sub zfs_list_zvol {
 sub zfs_find_free_diskname {
     my ($class, $storeid, $scfg, $vmid, $format) = @_;
 
-    my $name = undef;
     my $volumes = $class->zfs_list_zvol($scfg);
-
-    my $disk_ids = {};
     my $dat = $volumes->{$scfg->{pool}};
 
-    foreach my $image (keys %$dat) {
-        my $volname = $dat->{$image}->{name};
-        if ($volname =~ m/(vm|base|subvol|basevol)-$vmid-disk-(\d+)/){
-            $disk_ids->{$2} = 1;
-        }
-    }
-
-    for (my $i = 1; $i < 100; $i++) {
-        if (!$disk_ids->{$i}) {
-            return $format eq 'subvol' ? "subvol-$vmid-disk-$i" : "vm-$vmid-disk-$i";
-        }
-    }
-
-    die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
+    my $disk_list = [ keys %$dat ];
+    return PVE::Storage::Plugin::get_next_vm_diskname($disk_list, $storeid, $vmid, $format, $scfg);
 }
 
 sub zfs_get_latest_snapshot {
@@ -495,13 +493,12 @@ sub volume_rollback_is_possible {
 }
 
 sub volume_snapshot_list {
-    my ($class, $scfg, $storeid, $volname, $prefix) = @_;
+    my ($class, $scfg, $storeid, $volname) = @_;
 
     my ($vtype, $name, $vmid) = $class->parse_volname($volname);
 
     my $zpath = "$scfg->{pool}/$name";
 
-    $prefix = '' if !defined($prefix);
     my $snaps = [];
 
     my $cmd = ['zfs', 'list', '-r', '-H', '-S', 'name', '-t', 'snap', '-o',
@@ -510,7 +507,7 @@ sub volume_snapshot_list {
     my $outfunc = sub {
        my $line = shift;
 
-       if ($line =~ m/^\Q$zpath\E@(\Q$prefix\E.*)$/) {
+       if ($line =~ m/^\Q$zpath\E@(.*)$/) {
            push @$snaps, $1;
        }
     };
@@ -528,18 +525,24 @@ sub activate_storage {
     my $pool = $scfg->{pool};
     $pool =~ s!/.*$!!;
 
-    my @param = ('-o', 'name', '-H', "$pool");
-    my $res;
-    eval {
-       $res = $class->zfs_request($scfg, undef, 'zpool_list', @param);
+    my $pool_imported = sub {
+       my @param = ('-o', 'name', '-H', "$pool");
+       my $res = eval { $class->zfs_request($scfg, undef, 'zpool_list', @param) };
+       if ($@) {
+           warn "$@\n";
+           return undef;
+       }
+       return defined($res) && $res =~ m/$pool/;
     };
 
-    if ($@ || !defined($res) || $res !~ $pool) {
-       eval {
-           @param = ('-d', '/dev/disk/by-id/', "$pool");
-           $class->zfs_request($scfg, undef, 'zpool_import', @param);
-       };
-       die "could not activate storage '$storeid', $@\n" if $@;
+    if (!$pool_imported->()) {
+       # import can only be done if not yet imported!
+       my @param = ('-d', '/dev/disk/by-id/', '-o', 'cachefile=none', "$pool");
+       eval { $class->zfs_request($scfg, undef, 'zpool_import', @param) };
+       if (my $err = $@) {
+           # just could've raced with another import, so recheck if it is imported
+           die "could not activate storage '$storeid', $@\n" if !$pool_imported->();
+       }
     }
     return 1;
 }
@@ -551,6 +554,15 @@ sub deactivate_storage {
 
 sub activate_volume {
     my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
+
+    return 1 if defined($snapname);
+
+    my (undef, undef, undef, undef, undef, undef, $format) = $class->parse_volname($volname);
+
+    return 1 if $format ne 'raw';
+
+    $class->zfs_wait_for_zvol_link($scfg, $volname);
+
     return 1;
 }
 
@@ -624,6 +636,14 @@ sub volume_resize {
     return $new_size;
 }
 
+sub storage_can_replicate {
+    my ($class, $scfg, $storeid, $format) = @_;
+
+    return 1 if $format eq 'raw' || $format eq 'subvol';
+
+    return 0;
+}
+
 sub volume_has_feature {
     my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
 
@@ -661,6 +681,8 @@ sub volume_export {
     die "$class storage can only export snapshots\n"
        if !defined($snapshot);
 
+    my $dataset = ($class->parse_volname($volname))[1];
+
     my $fd = fileno($fh);
     die "internal error: invalid file handle for volume_export\n"
        if !defined($fd);
@@ -674,13 +696,23 @@ sub volume_export {
        my $arg = $with_snapshots ? '-I' : '-i';
        push @$cmd, $arg, $base_snapshot;
     }
-    push @$cmd, '--', "$scfg->{pool}/$volname\@$snapshot";
+    push @$cmd, '--', "$scfg->{pool}/$dataset\@$snapshot";
 
     run_command($cmd, output => $fd);
 
     return;
 }
 
+sub volume_export_formats {
+    my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
+
+    my @formats = ('zfs');
+    # TODOs:
+    # push @formats, 'fies' if $volname !~ /^(?:basevol|subvol)-/;
+    # push @formats, 'raw' if !$base_snapshot && !$with_snapshots;
+    return @formats;
+}
+
 sub volume_import {
     my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_;
 
@@ -691,7 +723,8 @@ sub volume_import {
     die "internal error: invalid file handle for volume_import\n"
        if !defined($fd);
 
-    my $zfspath = "$scfg->{pool}/$volname";
+    my $dataset = ($class->parse_volname($volname))[1];
+    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 {});
@@ -714,4 +747,10 @@ sub volume_import {
     return;
 }
 
+sub volume_import_formats {
+    my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
+
+    return $class->volume_export_formats($scfg, $storeid, $volname, undef, $base_snapshot, $with_snapshots);
+}
+
 1;