1 package PVE
::Storage
::ZFSPoolPlugin
;
11 use PVE
::RPCEnvironment
;
12 use PVE
::Storage
::Plugin
;
13 use PVE
::Tools
qw(run_command);
15 use base
qw(PVE::Storage::Plugin);
23 content
=> [ {images
=> 1, rootdir
=> 1}, {images
=> 1 , rootdir
=> 1}],
24 format
=> [ { raw
=> 1, subvol
=> 1 } , 'raw' ],
31 description
=> "block size",
35 description
=> "use sparse volumes",
39 description
=> "mount point",
40 type
=> 'string', format
=> 'pve-storage-path',
47 pool
=> { fixed
=> 1 },
48 blocksize
=> { optional
=> 1 },
49 sparse
=> { optional
=> 1 },
50 nodes
=> { optional
=> 1 },
51 disable
=> { optional
=> 1 },
52 content
=> { optional
=> 1 },
53 bwlimit
=> { optional
=> 1 },
54 mountpoint
=> { optional
=> 1 },
58 # static zfs helper methods
60 sub zfs_parse_zvol_list
{
65 return $list if !$text;
67 my @lines = split /\n/, $text;
68 foreach my $line (@lines) {
69 my ($dataset, $size, $origin, $type, $refquota) = split(/\s+/, $line);
70 next if !($type eq 'volume' || $type eq 'filesystem');
73 my @parts = split /\//, $dataset;
74 next if scalar(@parts) < 2; # we need pool/name
75 my $name = pop @parts;
76 my $pool = join('/', @parts);
78 next unless $name =~ m!^(vm|base|subvol|basevol)-(\d+)-(\S+)$!;
81 $zvol->{pool
} = $pool;
82 $zvol->{name
} = $name;
83 if ($type eq 'filesystem') {
84 if ($refquota eq 'none') {
87 $zvol->{size
} = $refquota + 0;
89 $zvol->{format
} = 'subvol';
91 $zvol->{size
} = $size + 0;
92 $zvol->{format
} = 'raw';
94 if ($origin !~ /^-$/) {
95 $zvol->{origin
} = $origin;
104 my ($class, $volname) = @_;
106 if ($volname =~ m/^(((base|basevol)-(\d+)-\S+)\/)?
((base
|basevol
|vm
|subvol
)-(\d
+)-\S
+)$/) {
107 my $format = ($6 eq 'subvol' || $6 eq 'basevol') ?
'subvol' : 'raw';
108 my $isBase = ($6 eq 'base' || $6 eq 'basevol');
109 return ('images', $5, $7, $2, $4, $isBase, $format);
112 die "unable to parse zfs volume name '$volname'\n";
115 # virtual zfs methods (subclass can overwrite them)
118 my ($class, $storeid, $scfg, %param) = @_;
120 my $cfg_mountpoint = $scfg->{mountpoint
};
122 # ignore failure, pool might currently not be imported
125 my $res = $class->zfs_get_properties($scfg, 'mountpoint', $scfg->{pool
}, 1);
126 $mountpoint = PVE
::Storage
::Plugin
::verify_path
($res, 1) if defined($res);
129 if (defined($cfg_mountpoint)) {
130 if (defined($mountpoint) && !($cfg_mountpoint =~ m
|^\Q
$mountpoint\E/?$|)) {
131 warn "warning for $storeid - mountpoint: $cfg_mountpoint " .
132 "does not match current mount point: $mountpoint\n";
135 $scfg->{mountpoint
} = $mountpoint;
142 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
144 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
147 my $mountpoint = $scfg->{mountpoint
} // "/$scfg->{pool}";
149 if ($vtype eq "images") {
150 if ($name =~ m/^subvol-/ || $name =~ m/^basevol-/) {
151 $path = "$mountpoint/$name";
153 $path = "/dev/zvol/$scfg->{pool}/$name";
155 $path .= "\@$snapname" if defined($snapname);
157 die "$vtype is not allowed in ZFSPool!";
160 return ($path, $vmid, $vtype);
164 my ($class, $scfg, $timeout, $method, @params) = @_;
168 if ($method eq 'zpool_list') {
169 push @$cmd, 'zpool', 'list';
170 } elsif ($method eq 'zpool_import') {
171 push @$cmd, 'zpool', 'import';
172 $timeout = 15 if !$timeout || $timeout < 15;
174 push @$cmd, 'zfs', $method;
179 my $output = sub { $msg .= "$_[0]\n" };
181 $timeout = PVE
::RPCEnvironment-
>is_worker() ?
60*60 : 5 if !$timeout;
183 run_command
($cmd, errmsg
=> "zfs error", outfunc
=> $output, timeout
=> $timeout);
188 sub zfs_wait_for_zvol_link
{
189 my ($class, $scfg, $volname, $timeout) = @_;
191 my $default_timeout = PVE
::RPCEnvironment-
>is_worker() ?
60*5 : 10;
192 $timeout = $default_timeout if !defined($timeout);
194 my ($devname, undef, undef) = $class->path($scfg, $volname);
196 for (my $i = 1; $i <= $timeout; $i++) {
198 die "timeout: no zvol device link for '$volname' found after $timeout sec found.\n"
206 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
212 die "illegal name '$volname' - should be 'vm-$vmid-*'\n"
213 if $volname && $volname !~ m/^vm-$vmid-/;
214 $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt)
217 $class->zfs_create_zvol($scfg, $volname, $size);
218 $class->zfs_wait_for_zvol_link($scfg, $volname);
220 } elsif ( $fmt eq 'subvol') {
222 die "illegal name '$volname' - should be 'subvol-$vmid-*'\n"
223 if $volname && $volname !~ m/^subvol-$vmid-/;
224 $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt)
227 die "illegal name '$volname' - should be 'subvol-$vmid-*'\n"
228 if $volname !~ m/^subvol-$vmid-/;
230 $class->zfs_create_subvol($scfg, $volname, $size);
233 die "unsupported format '$fmt'";
240 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
242 my (undef, $name, undef) = $class->parse_volname($volname);
244 $class->zfs_delete_zvol($scfg, $name);
250 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
252 $cache->{zfs
} = $class->zfs_list_zvol($scfg) if !$cache->{zfs
};
253 my $zfspool = $scfg->{pool
};
256 if (my $dat = $cache->{zfs
}->{$zfspool}) {
258 foreach my $image (keys %$dat) {
260 my $info = $dat->{$image};
262 my $volname = $info->{name
};
263 my $parent = $info->{parent
};
264 my $owner = $info->{vmid
};
266 if ($parent && $parent =~ m/^(\S+)\@__base__$/) {
267 my ($basename) = ($1);
268 $info->{volid
} = "$storeid:$basename/$volname";
270 $info->{volid
} = "$storeid:$volname";
274 my $found = grep { $_ eq $info->{volid
} } @$vollist;
277 next if defined ($vmid) && ($owner ne $vmid);
286 sub zfs_get_properties
{
287 my ($class, $scfg, $properties, $dataset, $timeout) = @_;
289 my $result = $class->zfs_request($scfg, $timeout, 'get', '-o', 'value',
290 '-Hp', $properties, $dataset);
291 my @values = split /\n/, $result;
292 return wantarray ?
@values : $values[0];
295 sub zfs_get_pool_stats
{
296 my ($class, $scfg) = @_;
301 my @lines = $class->zfs_get_properties($scfg, 'available,used', $scfg->{pool
});
303 if($lines[0] =~ /^(\d+)$/) {
307 if($lines[1] =~ /^(\d+)$/) {
311 return ($available, $used);
314 sub zfs_create_zvol
{
315 my ($class, $scfg, $zvol, $size) = @_;
317 # always align size to 1M as workaround until
318 # https://github.com/zfsonlinux/zfs/issues/8541 is solved
319 my $padding = (1024 - $size % 1024) % 1024;
320 $size = $size + $padding;
322 my $cmd = ['create'];
324 push @$cmd, '-s' if $scfg->{sparse
};
326 push @$cmd, '-b', $scfg->{blocksize
} if $scfg->{blocksize
};
328 push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol";
330 $class->zfs_request($scfg, undef, @$cmd);
333 sub zfs_create_subvol
{
334 my ($class, $scfg, $volname, $size) = @_;
336 my $dataset = "$scfg->{pool}/$volname";
337 my $quota = $size ?
"${size}k" : "none";
339 my $cmd = ['create', '-o', 'acltype=posixacl', '-o', 'xattr=sa',
340 '-o', "refquota=${quota}", $dataset];
342 $class->zfs_request($scfg, undef, @$cmd);
345 sub zfs_delete_zvol
{
346 my ($class, $scfg, $zvol) = @_;
350 for (my $i = 0; $i < 6; $i++) {
352 eval { $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol"); };
354 if ($err =~ m/^zfs error:(.*): dataset is busy.*/) {
356 } elsif ($err =~ m/^zfs error:.*: dataset does not exist.*$/) {
371 my ($class, $scfg) = @_;
373 my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hrp');
374 my $zvols = zfs_parse_zvol_list
($text);
375 return undef if !$zvols;
378 foreach my $zvol (@$zvols) {
379 my $pool = $zvol->{pool
};
380 my $name = $zvol->{name
};
381 my $parent = $zvol->{origin
};
382 if($zvol->{origin
} && $zvol->{origin
} =~ m/^$scfg->{pool}\/(\S
+)$/){
386 $list->{$pool}->{$name} = {
388 size
=> $zvol->{size
},
390 format
=> $zvol->{format
},
391 vmid
=> $zvol->{owner
},
398 sub zfs_get_sorted_snapshot_list
{
399 my ($class, $scfg, $volname, $sort_params) = @_;
401 my $vname = ($class->parse_volname($volname))[1];
403 my @params = ('-H', '-t', 'snapshot', '-o', 'name', $sort_params->@*, "$scfg->{pool}\/$vname");
404 my $text = $class->zfs_request($scfg, undef, 'list', @params);
405 my @snapshots = split(/\n/, $text);
408 for my $snapshot (@snapshots) {
409 (my $snap_name = $snapshot) =~ s/^.*@//;
410 push $snap_names->@*, $snap_name;
416 my ($class, $storeid, $scfg, $cache) = @_;
424 ($free, $used) = $class->zfs_get_pool_stats($scfg);
426 $total = $free + $used;
430 return ($total, $free, $used, $active);
433 sub volume_size_info
{
434 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
436 my (undef, $vname, undef, undef, undef, undef, $format) =
437 $class->parse_volname($volname);
439 my $attr = $format eq 'subvol' ?
'refquota' : 'volsize';
440 my $value = $class->zfs_get_properties($scfg, $attr, "$scfg->{pool}/$vname");
441 if ($value =~ /^(\d+)$/) {
445 die "Could not get zfs volume size\n";
448 sub volume_snapshot
{
449 my ($class, $scfg, $storeid, $volname, $snap) = @_;
451 my $vname = ($class->parse_volname($volname))[1];
453 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$vname\@$snap");
456 sub volume_snapshot_delete
{
457 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
459 my $vname = ($class->parse_volname($volname))[1];
461 $class->deactivate_volume($storeid, $scfg, $vname, $snap, {});
462 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$vname\@$snap");
465 sub volume_snapshot_rollback
{
466 my ($class, $scfg, $storeid, $volname, $snap) = @_;
468 my (undef, $vname, undef, undef, undef, undef, $format) = $class->parse_volname($volname);
470 my $msg = $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$vname\@$snap");
472 # we have to unmount rollbacked subvols, to invalidate wrong kernel
473 # caches, they get mounted in activate volume again
474 # see zfs bug #10931 https://github.com/openzfs/zfs/issues/10931
475 if ($format eq 'subvol') {
476 eval { $class->zfs_request($scfg, undef, 'unmount', "$scfg->{pool}/$vname"); };
478 die $err if $err !~ m/not currently mounted$/;
485 sub volume_rollback_is_possible
{
486 my ($class, $scfg, $storeid, $volname, $snap) = @_;
488 # can't use '-S creation', because zfs list won't reverse the order when the
489 # creation time is the same second, breaking at least our tests.
490 my $snapshots = $class->zfs_get_sorted_snapshot_list($scfg, $volname, ['-s', 'creation']);
491 my $recentsnap = $snapshots->[-1];
493 die "can't rollback, no snapshots exist at all\n"
494 if !defined($recentsnap);
496 die "can't rollback, '$snap' is not most recent snapshot\n"
497 if $snap ne $recentsnap;
502 sub volume_snapshot_list
{
503 my ($class, $scfg, $storeid, $volname) = @_;
506 # return an empty array if dataset does not exist.
507 eval { $snaps = $class->zfs_get_sorted_snapshot_list($scfg, $volname, ['-S', 'name']); };
511 my sub dataset_mounted_heuristic
{
514 my $mounts = PVE
::ProcFSTools
::parse_proc_mounts
();
515 for my $mp (@$mounts) {
516 my ($what, $dir, $fs) = $mp->@*;
517 next if $fs ne 'zfs';
518 # check for root-dataset or any child-dataset (root-dataset could have 'canmount=off')
519 # If any child is mounted heuristically assume that `zfs mount -a` was successful
520 next if $what !~ m!^$dataset(?:/|$)!;
526 sub activate_storage
{
527 my ($class, $storeid, $scfg, $cache) = @_;
529 # Note: $scfg->{pool} can include dataset <pool>/<dataset>
530 my $dataset = $scfg->{pool
};
531 my $pool = ($dataset =~ s!/.*$!!r);
533 return 1 if dataset_mounted_heuristic
($dataset); # early return
535 my $pool_imported = sub {
536 my @param = ('-o', 'name', '-H', $pool);
537 my $res = eval { $class->zfs_request($scfg, undef, 'zpool_list', @param) };
540 return defined($res) && $res =~ m/$pool/;
543 if (!$pool_imported->()) {
544 # import can only be done if not yet imported!
545 my @param = ('-d', '/dev/disk/by-id/', '-o', 'cachefile=none', $pool);
546 eval { $class->zfs_request($scfg, undef, 'zpool_import', @param) };
548 # just could've raced with another import, so recheck if it is imported
549 die "could not activate storage '$storeid', $err\n" if !$pool_imported->();
552 eval { $class->zfs_request($scfg, undef, 'mount', '-a') };
553 die "could not activate storage '$storeid', $@\n" if $@;
557 sub deactivate_storage
{
558 my ($class, $storeid, $scfg, $cache) = @_;
562 sub activate_volume
{
563 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
565 return 1 if defined($snapname);
567 my (undef, $dataset, undef, undef, undef, undef, $format) = $class->parse_volname($volname);
569 if ($format eq 'raw') {
570 $class->zfs_wait_for_zvol_link($scfg, $volname);
571 } elsif ($format eq 'subvol') {
572 my $mounted = $class->zfs_get_properties($scfg, 'mounted', "$scfg->{pool}/$dataset");
573 if ($mounted !~ m/^yes$/) {
574 $class->zfs_request($scfg, undef, 'mount', "$scfg->{pool}/$dataset");
581 sub deactivate_volume
{
582 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
587 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
589 $snap ||= '__base__';
591 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
592 $class->parse_volname($volname);
594 die "clone_image only works on base images\n" if !$isBase;
596 my $name = $class->find_free_diskname($storeid, $scfg, $vmid, $format);
598 if ($format eq 'subvol') {
599 my $size = $class->zfs_request($scfg, undef, 'list', '-Hp', '-o', 'refquota', "$scfg->{pool}/$basename");
601 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name", '-o', "refquota=$size");
603 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
606 return "$basename/$name";
610 my ($class, $storeid, $scfg, $volname) = @_;
612 my $snap = '__base__';
614 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
615 $class->parse_volname($volname);
617 die "create_base not possible with base image\n" if $isBase;
620 if ( $format eq 'subvol' ) {
621 $newname =~ s/^subvol-/basevol-/;
623 $newname =~ s/^vm-/base-/;
625 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
627 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
629 my $running = undef; #fixme : is create_base always offline ?
631 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
637 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
639 my $new_size = int($size/1024);
641 my (undef, $vname, undef, undef, undef, undef, $format) =
642 $class->parse_volname($volname);
644 my $attr = $format eq 'subvol' ?
'refquota' : 'volsize';
646 # align size to 1M so we always have a valid multiple of the volume block size
647 if ($format eq 'raw') {
648 my $padding = (1024 - $new_size % 1024) % 1024;
649 $new_size = $new_size + $padding;
652 $class->zfs_request($scfg, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$vname");
657 sub storage_can_replicate
{
658 my ($class, $scfg, $storeid, $format) = @_;
660 return 1 if $format eq 'raw' || $format eq 'subvol';
665 sub volume_has_feature
{
666 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
669 snapshot
=> { current
=> 1, snap
=> 1},
670 clone
=> { base
=> 1},
671 template
=> { current
=> 1},
672 copy
=> { base
=> 1, current
=> 1},
673 sparseinit
=> { base
=> 1, current
=> 1},
674 replicate
=> { base
=> 1, current
=> 1},
677 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
678 $class->parse_volname($volname);
685 $key = $isBase ?
'base' : 'current';
688 return 1 if $features->{$feature}->{$key};
694 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
696 die "unsupported export stream format for $class: $format\n"
699 die "$class storage can only export snapshots\n"
700 if !defined($snapshot);
702 my $dataset = ($class->parse_volname($volname))[1];
704 my $fd = fileno($fh);
705 die "internal error: invalid file handle for volume_export\n"
709 # For zfs we always create a replication stream (-R) which means the remote
710 # side will always delete non-existing source snapshots. This should work
711 # for all our use cases.
712 my $cmd = ['zfs', 'send', '-Rpv'];
713 if (defined($base_snapshot)) {
714 my $arg = $with_snapshots ?
'-I' : '-i';
715 push @$cmd, $arg, $base_snapshot;
717 push @$cmd, '--', "$scfg->{pool}/$dataset\@$snapshot";
719 run_command
($cmd, output
=> $fd);
724 sub volume_export_formats
{
725 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
727 my @formats = ('zfs');
729 # push @formats, 'fies' if $volname !~ /^(?:basevol|subvol)-/;
730 # push @formats, 'raw' if !$base_snapshot && !$with_snapshots;
735 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots, $allow_rename) = @_;
737 die "unsupported import stream format for $class: $format\n"
740 my $fd = fileno($fh);
741 die "internal error: invalid file handle for volume_import\n"
744 my (undef, $dataset, $vmid) = $class->parse_volname($volname);
745 my $zfspath = "$scfg->{pool}/$dataset";
746 my $suffix = defined($base_snapshot) ?
"\@$base_snapshot" : '';
747 my $exists = 0 == run_command
(['zfs', 'get', '-H', 'name', $zfspath.$suffix],
748 noerr
=> 1, quiet
=> 1);
749 if (defined($base_snapshot)) {
750 die "base snapshot '$zfspath\@$base_snapshot' doesn't exist\n" if !$exists;
752 die "volume '$zfspath' already exists\n" if !$allow_rename;
753 warn "volume '$zfspath' already exists - importing with a different name\n";
754 $dataset = $class->find_free_diskname($storeid, $scfg, $vmid, $format);
755 $zfspath = "$scfg->{pool}/$dataset";
758 eval { run_command
(['zfs', 'recv', '-F', '--', $zfspath], input
=> "<&$fd") };
760 if (defined($base_snapshot)) {
761 eval { run_command
(['zfs', 'rollback', '-r', '--', "$zfspath\@$base_snapshot"]) };
763 eval { run_command
(['zfs', 'destroy', '-r', '--', $zfspath]) };
768 return "$storeid:$dataset";
771 sub volume_import_formats
{
772 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
774 return $class->volume_export_formats($scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots);