use Net::IP;
use POSIX;
+use PVE::ProcFSTools;
use PVE::RPCEnvironment;
use PVE::Storage::Plugin;
use PVE::Tools qw(run_command);
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);
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);
}
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 {
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");
+
+ # 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$/;
+ }
+ }
- $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$vname\@$snap");
+ 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 {
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;
}
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) =
}
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';
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],
} 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";
}
}
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;