]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/Storage/ZFSPoolPlugin.pm
plugin: remove volume_snapshot_list
[pve-storage.git] / PVE / Storage / ZFSPoolPlugin.pm
index c734429f93433456a62b83002e8cbedb46db5bfe..278438bcd5cc0cb7154463bd107bd38eff1563ba 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);
 }
@@ -394,25 +395,21 @@ sub zfs_list_zvol {
     return $list;
 }
 
-sub zfs_get_latest_snapshot {
-    my ($class, $scfg, $volname) = @_;
+sub zfs_get_sorted_snapshot_list {
+    my ($class, $scfg, $volname, $sort_params) = @_;
 
     my $vname = ($class->parse_volname($volname))[1];
 
-    # abort rollback if snapshot is not the latest
-    my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
+    my @params = ('-H', '-t', 'snapshot', '-o', 'name', $sort_params->@*, "$scfg->{pool}\/$vname");
     my $text = $class->zfs_request($scfg, undef, 'list', @params);
     my @snapshots = split(/\n/, $text);
 
-    my $recentsnap;
-    foreach (@snapshots) {
-        if (/$scfg->{pool}\/$vname/) {
-            s/^.*@//;
-            $recentsnap = $_;
-        }
+    my $snap_names = [];
+    for my $snapshot (@snapshots) {
+       (my $snap_name = $snapshot) =~ s/^.*@//;
+       push $snap_names->@*, $snap_name;
     }
-
-    return $recentsnap;
+    return $snap_names;
 }
 
 sub status {
@@ -476,50 +473,78 @@ 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;
 }
 
 sub volume_rollback_is_possible {
-    my ($class, $scfg, $storeid, $volname, $snap) = @_;
+    my ($class, $scfg, $storeid, $volname, $snap, $blockers) = @_;
+
+    # can't use '-S creation', because zfs list won't reverse the order when the
+    # creation time is the same second, breaking at least our tests.
+    my $snapshots = $class->zfs_get_sorted_snapshot_list($scfg, $volname, ['-s', 'creation']);
+
+    my $found;
+    $blockers //= []; # not guaranteed to be set by caller
+    for my $snapshot ($snapshots->@*) {
+       if ($snapshot eq $snap) {
+           $found = 1;
+       } elsif ($found) {
+           push $blockers->@*, $snapshot;
+       }
+    }
 
-    my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
+    my $volid = "${storeid}:${volname}";
 
-    die "can't rollback, no snapshots exist at all\n"
-       if !defined($recentsnap);
+    die "can't rollback, snapshot '$snap' does not exist on '$volid'\n"
+       if !$found;
 
-    die "can't rollback, '$snap' is not most recent snapshot\n"
-       if $snap ne $recentsnap;
+    die "can't rollback, '$snap' is not most recent snapshot on '$volid'\n"
+       if scalar($blockers->@*) > 0;
 
     return 1;
 }
 
-sub volume_snapshot_list {
+sub volume_snapshot_info {
     my ($class, $scfg, $storeid, $volname) = @_;
 
-    my ($vtype, $name, $vmid) = $class->parse_volname($volname);
-
-    my $zpath = "$scfg->{pool}/$name";
-
-    my $snaps = [];
+    my $vname = ($class->parse_volname($volname))[1];
 
-    my $cmd = ['zfs', 'list', '-r', '-H', '-S', 'name', '-t', 'snap', '-o',
-              'name', $zpath];
+    my @params = ('-Hp', '-t', 'snapshot', '-o', 'name,guid,creation', "$scfg->{pool}\/$vname");
+    my $text = $class->zfs_request($scfg, undef, 'list', @params);
+    my @lines = split(/\n/, $text);
 
-    my $outfunc = sub {
-       my $line = shift;
+    my $info = {};
+    for my $line (@lines) {
+       my ($snapshot, $guid, $creation) = split(/\s+/, $line);
+       (my $snap_name = $snapshot) =~ s/^.*@//;
 
-       if ($line =~ m/^\Q$zpath\E@(.*)$/) {
-           push @$snaps, $1;
-       }
-    };
+       $info->{$snap_name} = {
+           id => $guid,
+           timestamp => $creation,
+       };
+    }
+    return $info;
+}
 
-    eval { run_command( [$cmd], outfunc => $outfunc , errfunc => sub{}); };
+my sub dataset_mounted_heuristic {
+    my ($dataset) = @_;
 
-    # return an empty array if dataset does not exist.
-    return $snaps;
+    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 {
@@ -529,19 +554,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 +564,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 +756,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 +793,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;