]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/Storage/ZFSPoolPlugin.pm
cifs: small line bloat reduction
[pve-storage.git] / PVE / Storage / ZFSPoolPlugin.pm
index 10354b3af957596736751cf498b44dcba34d7c8b..9fbd1497fc49090de1aa6a9fdda9738f7fd6ff71 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);
 
@@ -55,7 +58,7 @@ sub options {
 # static zfs helper methods
 
 sub zfs_parse_zvol_list {
-    my ($text) = @_;
+    my ($text, $pool) = @_;
 
     my $list = ();
 
@@ -70,12 +73,12 @@ sub zfs_parse_zvol_list {
        my @parts = split /\//, $dataset;
        next if scalar(@parts) < 2; # we need pool/name
        my $name = pop @parts;
-       my $pool = join('/', @parts);
+       my $parsed_pool = join('/', @parts);
+       next if $parsed_pool ne $pool;
 
        next unless $name =~ m!^(vm|base|subvol|basevol)-(\d+)-(\S+)$!;
        $zvol->{owner} = $2;
 
-       $zvol->{pool} = $pool;
        $zvol->{name} = $name;
        if ($type eq 'filesystem') {
            if ($refquota eq 'none') {
@@ -131,6 +134,8 @@ sub on_add_hook {
     } else {
        $scfg->{mountpoint} = $mountpoint;
     }
+
+    return;
 }
 
 sub path {
@@ -173,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);
 
@@ -244,36 +254,30 @@ sub free_image {
 sub list_images {
     my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
 
-    $cache->{zfs} = $class->zfs_list_zvol($scfg) if !$cache->{zfs};
-    my $zfspool = $scfg->{pool};
-    my $res = [];
-
-    if (my $dat = $cache->{zfs}->{$zfspool}) {
-
-       foreach my $image (keys %$dat) {
+    my $zfs_list = $class->zfs_list_zvol($scfg);
 
-           my $info = $dat->{$image};
-
-           my $volname = $info->{name};
-           my $parent = $info->{parent};
-           my $owner = $info->{vmid};
+    my $res = [];
 
-           if ($parent && $parent =~ m/^(\S+)\@__base__$/) {
-               my ($basename) = ($1);
-               $info->{volid} = "$storeid:$basename/$volname";
-           } else {
-               $info->{volid} = "$storeid:$volname";
-           }
+    for my $info (values $zfs_list->%*) {
+       my $volname = $info->{name};
+       my $parent = $info->{parent};
+       my $owner = $info->{vmid};
 
-           if ($vollist) {
-               my $found = grep { $_ eq $info->{volid} } @$vollist;
-               next if !$found;
-           } else {
-               next if defined ($vmid) && ($owner ne $vmid);
-           }
+       if ($parent && $parent =~ m/^(\S+)\@__base__$/) {
+           my ($basename) = ($1);
+           $info->{volid} = "$storeid:$basename/$volname";
+       } else {
+           $info->{volid} = "$storeid:$volname";
+       }
 
-           push @$res, $info;
+       if ($vollist) {
+           my $found = grep { $_ eq $info->{volid} } @$vollist;
+           next if !$found;
+       } else {
+           next if defined ($vmid) && ($owner ne $vmid);
        }
+
+       push @$res, $info;
     }
     return $res;
 }
@@ -329,9 +333,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);
 }
@@ -364,20 +369,32 @@ sub zfs_delete_zvol {
 sub zfs_list_zvol {
     my ($class, $scfg) = @_;
 
-    my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hrp');
-    my $zvols = zfs_parse_zvol_list($text);
-    return undef if !$zvols;
-
-    my $list = ();
+    my $text = $class->zfs_request(
+       $scfg,
+       10,
+       'list',
+       '-o',
+       'name,volsize,origin,type,refquota',
+       '-t',
+       'volume,filesystem',
+       '-d1',
+       '-Hp',
+       $scfg->{pool},
+    );
+    # It's still required to have zfs_parse_zvol_list filter by pool, because -d1 lists
+    # $scfg->{pool} too and while unlikely, it could be named to be mistaken for a volume.
+    my $zvols = zfs_parse_zvol_list($text, $scfg->{pool});
+    return {} if !$zvols;
+
+    my $list = {};
     foreach my $zvol (@$zvols) {
-       my $pool = $zvol->{pool};
        my $name = $zvol->{name};
        my $parent = $zvol->{origin};
        if($zvol->{origin} && $zvol->{origin} =~ m/^$scfg->{pool}\/(\S+)$/){
            $parent = $1;
        }
 
-       $list->{$pool}->{$name} = {
+       $list->{$name} = {
            name => $name,
            size => $zvol->{size},
            parent => $parent,
@@ -389,25 +406,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 {
@@ -463,74 +478,118 @@ 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 $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
-    if ($snap ne $recentsnap) {
-       die "can't rollback, more recent snapshots exist\n";
+    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 $volid = "${storeid}:${volname}";
+
+    die "can't rollback, snapshot '$snap' does not exist on '$volid'\n"
+       if !$found;
+
+    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 {
     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;
 }
 
@@ -544,11 +603,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;
 }
@@ -647,6 +711,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) =
@@ -707,7 +772,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';
@@ -716,17 +781,19 @@ 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],
-                            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) {
        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";
     }
 
@@ -744,9 +811,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;