]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/Storage/ZFSPoolPlugin.pm
rbd: warn if no stats for a pool could be gathered
[pve-storage.git] / PVE / Storage / ZFSPoolPlugin.pm
index 9dfe86f2bf5e6977ce3286f5884bacc4957eca4b..e264fde91c028b587a3a05e004352b9bdbe7b68c 100644 (file)
@@ -178,7 +178,12 @@ sub zfs_request {
     my $msg = '';
     my $output = sub { $msg .= "$_[0]\n" };
 
-    $timeout = PVE::RPCEnvironment->is_worker() ? 60*60 : 5 if !$timeout;
+    if (PVE::RPCEnvironment->is_worker()) {
+       $timeout = 60*60 if !$timeout;
+       $timeout = 60*5 if $timeout < 60*5;
+    } else {
+       $timeout = 10 if !$timeout;
+    }
 
     run_command($cmd, errmsg => "zfs error", outfunc => $output, timeout => $timeout);
 
@@ -334,9 +339,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 +400,23 @@ 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 @params = ('-H', '-r', '-t', 'snapshot', '-o', 'name', $sort_params->@*);
 
     my $vname = ($class->parse_volname($volname))[1];
+    push @params, "$scfg->{pool}\/$vname";
 
-    # abort rollback if snapshot is not the latest
-    my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
     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 +480,80 @@ 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 @params = ('-Hp', '-r', '-t', 'snapshot', '-o', 'name,guid,creation');
 
-    my $snaps = [];
+    my $vname = ($class->parse_volname($volname))[1];
+    push @params, "$scfg->{pool}\/$vname";
 
-    my $cmd = ['zfs', 'list', '-r', '-H', '-S', 'name', '-t', 'snap', '-o',
-              'name', $zpath];
+    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,23 +563,7 @@ sub activate_storage {
     my $dataset = $scfg->{pool};
     my $pool = ($dataset =~ s!/.*$!!r);
 
-    my $dataset_mounted = sub {
-       my $mounted = 0;
-
-       my $mounts = eval { PVE::ProcFSTools::parse_proc_mounts() };
-       warn "$@\n" if $@;
-       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(?:/|$)!;
-           $mounted = 1;
-           last;
-       }
-       return $mounted;
-    };
+    return 1 if dataset_mounted_heuristic($dataset); # early return
 
     my $pool_imported = sub {
        my @param = ('-o', 'name', '-H', $pool);
@@ -555,19 +573,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;
 }
 
@@ -689,6 +705,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) =
@@ -749,7 +766,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';
@@ -758,7 +775,9 @@ sub volume_import {
     die "internal error: invalid file handle for volume_import\n"
        if !defined($fd);
 
-    my (undef, $dataset, $vmid) = $class->parse_volname($volname);
+    my (undef, $dataset, $vmid, undef, undef, undef, $volume_format) =
+       $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],
@@ -768,7 +787,7 @@ sub volume_import {
     } 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);
+       $dataset = $class->find_free_diskname($storeid, $scfg, $vmid, $volume_format);
        $zfspath = "$scfg->{pool}/$dataset";
     }
 
@@ -786,9 +805,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;