]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/Storage/ZFSPoolPlugin.pm
lvm thin: don't assume that a thin pool and its volumes are active
[pve-storage.git] / PVE / Storage / ZFSPoolPlugin.pm
index 7c180eafd9fafcb9978ce5bb1183767b9eb5b93f..5f6befd23d57bc28b28f69eeca989aa826e19784 100644 (file)
@@ -2,12 +2,15 @@ package PVE::Storage::ZFSPoolPlugin;
 
 use strict;
 use warnings;
+
 use IO::File;
+use Net::IP;
 use POSIX;
-use PVE::Tools qw(run_command);
-use PVE::Storage::Plugin;
+
+use PVE::ProcFSTools;
 use PVE::RPCEnvironment;
-use Net::IP;
+use PVE::Storage::Plugin;
+use PVE::Tools qw(run_command);
 
 use base qw(PVE::Storage::Plugin);
 
@@ -131,6 +134,8 @@ sub on_add_hook {
     } else {
        $scfg->{mountpoint} = $mountpoint;
     }
+
+    return;
 }
 
 sub path {
@@ -329,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);
 }
@@ -389,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 {
@@ -463,77 +465,116 @@ sub volume_snapshot_delete {
 sub volume_snapshot_rollback {
     my ($class, $scfg, $storeid, $volname, $snap) = @_;
 
-    my $vname = ($class->parse_volname($volname))[1];
+    my (undef, $vname, undef, undef, undef, undef, $format) = $class->parse_volname($volname);
+
+    my $msg = $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$vname\@$snap");
 
-    $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$vname\@$snap");
+    # we have to unmount rollbacked subvols, to invalidate wrong kernel
+    # caches, they get mounted in activate volume again
+    # see zfs bug #10931 https://github.com/openzfs/zfs/issues/10931
+    if ($format eq 'subvol') {
+       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 {
     my ($class, $storeid, $scfg, $cache) = @_;
 
     # Note: $scfg->{pool} can include dataset <pool>/<dataset>
-    my $pool = $scfg->{pool};
-    $pool =~ s!/.*$!!;
+    my $dataset = $scfg->{pool};
+    my $pool = ($dataset =~ s!/.*$!!r);
+
+    return 1 if dataset_mounted_heuristic($dataset); # early return
 
     my $pool_imported = sub {
-       my @param = ('-o', 'name', '-H', "$pool");
+       my @param = ('-o', 'name', '-H', $pool);
        my $res = eval { $class->zfs_request($scfg, undef, 'zpool_list', @param) };
-       if ($@) {
-           warn "$@\n";
-           return undef;
-       }
+       warn "$@\n" if $@;
+
        return defined($res) && $res =~ m/$pool/;
     };
 
     if (!$pool_imported->()) {
        # import can only be done if not yet imported!
-       my @param = ('-d', '/dev/disk/by-id/', '-o', 'cachefile=none', "$pool");
+       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->();
+           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 $@;
     return 1;
 }
 
@@ -547,11 +588,16 @@ sub activate_volume {
 
     return 1 if defined($snapname);
 
-    my (undef, undef, undef, undef, undef, undef, $format) = $class->parse_volname($volname);
-
-    return 1 if $format ne 'raw';
+    my (undef, $dataset, undef, undef, undef, undef, $format) = $class->parse_volname($volname);
 
-    $class->zfs_wait_for_zvol_link($scfg, $volname);
+    if ($format eq 'raw') {
+       $class->zfs_wait_for_zvol_link($scfg, $volname);
+    } elsif ($format eq 'subvol') {
+       my $mounted = $class->zfs_get_properties($scfg, 'mounted', "$scfg->{pool}/$dataset");
+       if ($mounted !~ m/^yes$/) {
+           $class->zfs_request($scfg, undef, 'mount', "$scfg->{pool}/$dataset");
+       }
+    }
 
     return 1;
 }
@@ -650,6 +696,7 @@ sub volume_has_feature {
        copy => { base => 1, current => 1},
        sparseinit => { base => 1, current => 1},
        replicate => { base => 1, current => 1},
+       rename => {current => 1},
     };
 
     my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
@@ -710,7 +757,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';
@@ -723,7 +770,7 @@ sub volume_import {
     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 {});
+                                 noerr => 1, quiet => 1);
     if (defined($base_snapshot)) {
        die "base snapshot '$zfspath\@$base_snapshot' doesn't exist\n" if !$exists;
     } elsif ($exists) {
@@ -747,9 +794,39 @@ 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, $snapshot, $base_snapshot, $with_snapshots);
+}
+
+sub rename_volume {
+    my ($class, $scfg, $storeid, $source_volname, $target_vmid, $target_volname) = @_;
+
+    my (
+       undef,
+       $source_image,
+       $source_vmid,
+       $base_name,
+       $base_vmid,
+       undef,
+       $format
+    ) = $class->parse_volname($source_volname);
+    $target_volname = $class->find_free_diskname($storeid, $scfg, $target_vmid, $format)
+       if !$target_volname;
+
+    my $pool = $scfg->{pool};
+    my $source_zfspath = "${pool}/${source_image}";
+    my $target_zfspath = "${pool}/${target_volname}";
+
+    my $exists = 0 == run_command(['zfs', 'get', '-H', 'name', $target_zfspath],
+                                 noerr => 1, quiet => 1);
+    die "target volume '${target_volname}' already exists\n" if $exists;
+
+    $class->zfs_request($scfg, 5, 'rename', ${source_zfspath}, ${target_zfspath});
+
+    $base_name = $base_name ? "${base_name}/" : '';
 
-    return $class->volume_export_formats($scfg, $storeid, $volname, undef, $base_snapshot, $with_snapshots);
+    return "${storeid}:${base_name}${target_volname}";
 }
 
 1;