]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/Storage/ZFSPoolPlugin.pm
zfs: fix unmount request
[pve-storage.git] / PVE / Storage / ZFSPoolPlugin.pm
index c734429f93433456a62b83002e8cbedb46db5bfe..660b3d9b18783eab37c1d7f9ac0f51e59c7f68d6 100644 (file)
@@ -334,9 +334,10 @@ sub zfs_create_subvol {
     my ($class, $scfg, $volname, $size) = @_;
 
     my $dataset = "$scfg->{pool}/$volname";
+    my $quota = $size ? "${size}k" : "none";
 
     my $cmd = ['create', '-o', 'acltype=posixacl', '-o', 'xattr=sa',
-              '-o', "refquota=${size}k", $dataset];
+              '-o', "refquota=${quota}", $dataset];
 
     $class->zfs_request($scfg, undef, @$cmd);
 }
@@ -476,7 +477,10 @@ sub volume_snapshot_rollback {
     # caches, they get mounted in activate volume again
     # see zfs bug #10931 https://github.com/openzfs/zfs/issues/10931
     if ($format eq 'subvol') {
-       $class->zfs_request($scfg, undef, 'unmount', "$scfg->{pool}/$vname");
+       eval { $class->zfs_request($scfg, undef, 'unmount', "$scfg->{pool}/$vname"); };
+       if (my $err = $@) {
+           die $err if $err !~ m/not currently mounted$/;
+       }
     }
 
     return $msg;
@@ -522,6 +526,21 @@ sub volume_snapshot_list {
     return $snaps;
 }
 
+my sub dataset_mounted_heuristic {
+    my ($dataset) = @_;
+
+    my $mounts = PVE::ProcFSTools::parse_proc_mounts();
+    for my $mp (@$mounts) {
+       my ($what, $dir, $fs) = $mp->@*;
+       next if $fs ne 'zfs';
+       # check for root-dataset or any child-dataset (root-dataset could have 'canmount=off')
+       # If any child is mounted heuristically assume that `zfs mount -a` was successful
+       next if $what !~ m!^$dataset(?:/|$)!;
+       return 1;
+    }
+    return 0;
+}
+
 sub activate_storage {
     my ($class, $storeid, $scfg, $cache) = @_;
 
@@ -529,19 +548,7 @@ sub activate_storage {
     my $dataset = $scfg->{pool};
     my $pool = ($dataset =~ s!/.*$!!r);
 
-    my $dataset_mounted = sub {
-       my $mounts = PVE::ProcFSTools::parse_proc_mounts();
-       foreach my $mp (@$mounts) {
-           my ($what, $dir, $fs) = @$mp;
-           next if $fs ne 'zfs';
-           # check for root-dataset of storage or any child-dataset.
-           # root-dataset could have 'canmount=off'. If any child is mounted
-           # heuristically assume that `zfs mount -a` was successful
-           next if $what !~ m!^$dataset(?:/|$)!;
-           return 1;
-       }
-       return 0;
-    };
+    return 1 if dataset_mounted_heuristic($dataset); # early return
 
     my $pool_imported = sub {
        my @param = ('-o', 'name', '-H', $pool);
@@ -551,19 +558,17 @@ sub activate_storage {
        return defined($res) && $res =~ m/$pool/;
     };
 
-    if (!$dataset_mounted->()) {
-       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', $err\n" if !$pool_imported->();
-           }
+    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', $err\n" if !$pool_imported->();
        }
-       eval { $class->zfs_request($scfg, undef, 'mount', '-a') };
-       die "could not activate storage '$storeid', $@\n" if $@;
     }
+    eval { $class->zfs_request($scfg, undef, 'mount', '-a') };
+    die "could not activate storage '$storeid', $@\n" if $@;
     return 1;
 }
 
@@ -745,7 +750,7 @@ sub volume_export_formats {
 }
 
 sub volume_import {
-    my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots, $allow_rename) = @_;
+    my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots, $allow_rename) = @_;
 
     die "unsupported import stream format for $class: $format\n"
        if $format ne 'zfs';
@@ -782,9 +787,9 @@ sub volume_import {
 }
 
 sub volume_import_formats {
-    my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
+    my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
 
-    return $class->volume_export_formats($scfg, $storeid, $volname, undef, $base_snapshot, $with_snapshots);
+    return $class->volume_export_formats($scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots);
 }
 
 1;