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 if (PVE
::RPCEnvironment-
>is_worker()) {
182 $timeout = 60*60 if !$timeout;
183 $timeout = 60*5 if $timeout < 60*5;
185 $timeout = 10 if !$timeout;
188 run_command
($cmd, errmsg
=> "zfs error", outfunc
=> $output, timeout
=> $timeout);
193 sub zfs_wait_for_zvol_link
{
194 my ($class, $scfg, $volname, $timeout) = @_;
196 my $default_timeout = PVE
::RPCEnvironment-
>is_worker() ?
60*5 : 10;
197 $timeout = $default_timeout if !defined($timeout);
199 my ($devname, undef, undef) = $class->path($scfg, $volname);
201 for (my $i = 1; $i <= $timeout; $i++) {
203 die "timeout: no zvol device link for '$volname' found after $timeout sec found.\n"
211 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
217 die "illegal name '$volname' - should be 'vm-$vmid-*'\n"
218 if $volname && $volname !~ m/^vm-$vmid-/;
219 $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt)
222 $class->zfs_create_zvol($scfg, $volname, $size);
223 $class->zfs_wait_for_zvol_link($scfg, $volname);
225 } elsif ( $fmt eq 'subvol') {
227 die "illegal name '$volname' - should be 'subvol-$vmid-*'\n"
228 if $volname && $volname !~ m/^subvol-$vmid-/;
229 $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt)
232 die "illegal name '$volname' - should be 'subvol-$vmid-*'\n"
233 if $volname !~ m/^subvol-$vmid-/;
235 $class->zfs_create_subvol($scfg, $volname, $size);
238 die "unsupported format '$fmt'";
245 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
247 my (undef, $name, undef) = $class->parse_volname($volname);
249 $class->zfs_delete_zvol($scfg, $name);
255 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
257 $cache->{zfs
}->{$storeid} = $class->zfs_list_zvol($scfg)
258 if !$cache->{zfs
}->{$storeid};
262 if (my $dat = $cache->{zfs
}->{$storeid}) {
264 foreach my $image (keys %$dat) {
266 my $info = $dat->{$image};
268 my $volname = $info->{name
};
269 my $parent = $info->{parent
};
270 my $owner = $info->{vmid
};
272 if ($parent && $parent =~ m/^(\S+)\@__base__$/) {
273 my ($basename) = ($1);
274 $info->{volid
} = "$storeid:$basename/$volname";
276 $info->{volid
} = "$storeid:$volname";
280 my $found = grep { $_ eq $info->{volid
} } @$vollist;
283 next if defined ($vmid) && ($owner ne $vmid);
292 sub zfs_get_properties
{
293 my ($class, $scfg, $properties, $dataset, $timeout) = @_;
295 my $result = $class->zfs_request($scfg, $timeout, 'get', '-o', 'value',
296 '-Hp', $properties, $dataset);
297 my @values = split /\n/, $result;
298 return wantarray ?
@values : $values[0];
301 sub zfs_get_pool_stats
{
302 my ($class, $scfg) = @_;
307 my @lines = $class->zfs_get_properties($scfg, 'available,used', $scfg->{pool
});
309 if($lines[0] =~ /^(\d+)$/) {
313 if($lines[1] =~ /^(\d+)$/) {
317 return ($available, $used);
320 sub zfs_create_zvol
{
321 my ($class, $scfg, $zvol, $size) = @_;
323 # always align size to 1M as workaround until
324 # https://github.com/zfsonlinux/zfs/issues/8541 is solved
325 my $padding = (1024 - $size % 1024) % 1024;
326 $size = $size + $padding;
328 my $cmd = ['create'];
330 push @$cmd, '-s' if $scfg->{sparse
};
332 push @$cmd, '-b', $scfg->{blocksize
} if $scfg->{blocksize
};
334 push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol";
336 $class->zfs_request($scfg, undef, @$cmd);
339 sub zfs_create_subvol
{
340 my ($class, $scfg, $volname, $size) = @_;
342 my $dataset = "$scfg->{pool}/$volname";
343 my $quota = $size ?
"${size}k" : "none";
345 my $cmd = ['create', '-o', 'acltype=posixacl', '-o', 'xattr=sa',
346 '-o', "refquota=${quota}", $dataset];
348 $class->zfs_request($scfg, undef, @$cmd);
351 sub zfs_delete_zvol
{
352 my ($class, $scfg, $zvol) = @_;
356 for (my $i = 0; $i < 6; $i++) {
358 eval { $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol"); };
360 if ($err =~ m/^zfs error:(.*): dataset is busy.*/) {
362 } elsif ($err =~ m/^zfs error:.*: dataset does not exist.*$/) {
377 my ($class, $scfg) = @_;
379 my $text = $class->zfs_request(
384 'name,volsize,origin,type,refquota',
390 my $zvols = zfs_parse_zvol_list
($text);
391 return undef if !$zvols;
394 foreach my $zvol (@$zvols) {
395 # The "pool" in $scfg is not the same as ZFS pool, so it's necessary to filter here.
396 next if $scfg->{pool
} ne $zvol->{pool
};
398 my $name = $zvol->{name
};
399 my $parent = $zvol->{origin
};
400 if($zvol->{origin
} && $zvol->{origin
} =~ m/^$scfg->{pool}\/(\S
+)$/){
406 size
=> $zvol->{size
},
408 format
=> $zvol->{format
},
409 vmid
=> $zvol->{owner
},
416 sub zfs_get_sorted_snapshot_list
{
417 my ($class, $scfg, $volname, $sort_params) = @_;
419 my @params = ('-H', '-r', '-t', 'snapshot', '-o', 'name', $sort_params->@*);
421 my $vname = ($class->parse_volname($volname))[1];
422 push @params, "$scfg->{pool}\/$vname";
424 my $text = $class->zfs_request($scfg, undef, 'list', @params);
425 my @snapshots = split(/\n/, $text);
428 for my $snapshot (@snapshots) {
429 (my $snap_name = $snapshot) =~ s/^.*@//;
430 push $snap_names->@*, $snap_name;
436 my ($class, $storeid, $scfg, $cache) = @_;
444 ($free, $used) = $class->zfs_get_pool_stats($scfg);
446 $total = $free + $used;
450 return ($total, $free, $used, $active);
453 sub volume_size_info
{
454 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
456 my (undef, $vname, undef, undef, undef, undef, $format) =
457 $class->parse_volname($volname);
459 my $attr = $format eq 'subvol' ?
'refquota' : 'volsize';
460 my $value = $class->zfs_get_properties($scfg, $attr, "$scfg->{pool}/$vname");
461 if ($value =~ /^(\d+)$/) {
465 die "Could not get zfs volume size\n";
468 sub volume_snapshot
{
469 my ($class, $scfg, $storeid, $volname, $snap) = @_;
471 my $vname = ($class->parse_volname($volname))[1];
473 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$vname\@$snap");
476 sub volume_snapshot_delete
{
477 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
479 my $vname = ($class->parse_volname($volname))[1];
481 $class->deactivate_volume($storeid, $scfg, $vname, $snap, {});
482 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$vname\@$snap");
485 sub volume_snapshot_rollback
{
486 my ($class, $scfg, $storeid, $volname, $snap) = @_;
488 my (undef, $vname, undef, undef, undef, undef, $format) = $class->parse_volname($volname);
490 my $msg = $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$vname\@$snap");
492 # we have to unmount rollbacked subvols, to invalidate wrong kernel
493 # caches, they get mounted in activate volume again
494 # see zfs bug #10931 https://github.com/openzfs/zfs/issues/10931
495 if ($format eq 'subvol') {
496 eval { $class->zfs_request($scfg, undef, 'unmount', "$scfg->{pool}/$vname"); };
498 die $err if $err !~ m/not currently mounted$/;
505 sub volume_rollback_is_possible
{
506 my ($class, $scfg, $storeid, $volname, $snap, $blockers) = @_;
508 # can't use '-S creation', because zfs list won't reverse the order when the
509 # creation time is the same second, breaking at least our tests.
510 my $snapshots = $class->zfs_get_sorted_snapshot_list($scfg, $volname, ['-s', 'creation']);
513 $blockers //= []; # not guaranteed to be set by caller
514 for my $snapshot ($snapshots->@*) {
515 if ($snapshot eq $snap) {
518 push $blockers->@*, $snapshot;
522 my $volid = "${storeid}:${volname}";
524 die "can't rollback, snapshot '$snap' does not exist on '$volid'\n"
527 die "can't rollback, '$snap' is not most recent snapshot on '$volid'\n"
528 if scalar($blockers->@*) > 0;
533 sub volume_snapshot_info
{
534 my ($class, $scfg, $storeid, $volname) = @_;
536 my @params = ('-Hp', '-r', '-t', 'snapshot', '-o', 'name,guid,creation');
538 my $vname = ($class->parse_volname($volname))[1];
539 push @params, "$scfg->{pool}\/$vname";
541 my $text = $class->zfs_request($scfg, undef, 'list', @params);
542 my @lines = split(/\n/, $text);
545 for my $line (@lines) {
546 my ($snapshot, $guid, $creation) = split(/\s+/, $line);
547 (my $snap_name = $snapshot) =~ s/^.*@//;
549 $info->{$snap_name} = {
551 timestamp
=> $creation,
557 my sub dataset_mounted_heuristic
{
560 my $mounts = PVE
::ProcFSTools
::parse_proc_mounts
();
561 for my $mp (@$mounts) {
562 my ($what, $dir, $fs) = $mp->@*;
563 next if $fs ne 'zfs';
564 # check for root-dataset or any child-dataset (root-dataset could have 'canmount=off')
565 # If any child is mounted heuristically assume that `zfs mount -a` was successful
566 next if $what !~ m!^$dataset(?:/|$)!;
572 sub activate_storage
{
573 my ($class, $storeid, $scfg, $cache) = @_;
575 # Note: $scfg->{pool} can include dataset <pool>/<dataset>
576 my $dataset = $scfg->{pool
};
577 my $pool = ($dataset =~ s!/.*$!!r);
579 return 1 if dataset_mounted_heuristic
($dataset); # early return
581 my $pool_imported = sub {
582 my @param = ('-o', 'name', '-H', $pool);
583 my $res = eval { $class->zfs_request($scfg, undef, 'zpool_list', @param) };
586 return defined($res) && $res =~ m/$pool/;
589 if (!$pool_imported->()) {
590 # import can only be done if not yet imported!
591 my @param = ('-d', '/dev/disk/by-id/', '-o', 'cachefile=none', $pool);
592 eval { $class->zfs_request($scfg, undef, 'zpool_import', @param) };
594 # just could've raced with another import, so recheck if it is imported
595 die "could not activate storage '$storeid', $err\n" if !$pool_imported->();
598 eval { $class->zfs_request($scfg, undef, 'mount', '-a') };
599 die "could not activate storage '$storeid', $@\n" if $@;
603 sub deactivate_storage
{
604 my ($class, $storeid, $scfg, $cache) = @_;
608 sub activate_volume
{
609 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
611 return 1 if defined($snapname);
613 my (undef, $dataset, undef, undef, undef, undef, $format) = $class->parse_volname($volname);
615 if ($format eq 'raw') {
616 $class->zfs_wait_for_zvol_link($scfg, $volname);
617 } elsif ($format eq 'subvol') {
618 my $mounted = $class->zfs_get_properties($scfg, 'mounted', "$scfg->{pool}/$dataset");
619 if ($mounted !~ m/^yes$/) {
620 $class->zfs_request($scfg, undef, 'mount', "$scfg->{pool}/$dataset");
627 sub deactivate_volume
{
628 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
633 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
635 $snap ||= '__base__';
637 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
638 $class->parse_volname($volname);
640 die "clone_image only works on base images\n" if !$isBase;
642 my $name = $class->find_free_diskname($storeid, $scfg, $vmid, $format);
644 if ($format eq 'subvol') {
645 my $size = $class->zfs_request($scfg, undef, 'list', '-Hp', '-o', 'refquota', "$scfg->{pool}/$basename");
647 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name", '-o', "refquota=$size");
649 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
652 return "$basename/$name";
656 my ($class, $storeid, $scfg, $volname) = @_;
658 my $snap = '__base__';
660 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
661 $class->parse_volname($volname);
663 die "create_base not possible with base image\n" if $isBase;
666 if ( $format eq 'subvol' ) {
667 $newname =~ s/^subvol-/basevol-/;
669 $newname =~ s/^vm-/base-/;
671 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
673 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
675 my $running = undef; #fixme : is create_base always offline ?
677 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
683 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
685 my $new_size = int($size/1024);
687 my (undef, $vname, undef, undef, undef, undef, $format) =
688 $class->parse_volname($volname);
690 my $attr = $format eq 'subvol' ?
'refquota' : 'volsize';
692 # align size to 1M so we always have a valid multiple of the volume block size
693 if ($format eq 'raw') {
694 my $padding = (1024 - $new_size % 1024) % 1024;
695 $new_size = $new_size + $padding;
698 $class->zfs_request($scfg, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$vname");
703 sub storage_can_replicate
{
704 my ($class, $scfg, $storeid, $format) = @_;
706 return 1 if $format eq 'raw' || $format eq 'subvol';
711 sub volume_has_feature
{
712 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
715 snapshot
=> { current
=> 1, snap
=> 1},
716 clone
=> { base
=> 1},
717 template
=> { current
=> 1},
718 copy
=> { base
=> 1, current
=> 1},
719 sparseinit
=> { base
=> 1, current
=> 1},
720 replicate
=> { base
=> 1, current
=> 1},
721 rename => {current
=> 1},
724 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
725 $class->parse_volname($volname);
732 $key = $isBase ?
'base' : 'current';
735 return 1 if $features->{$feature}->{$key};
741 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
743 die "unsupported export stream format for $class: $format\n"
746 die "$class storage can only export snapshots\n"
747 if !defined($snapshot);
749 my $dataset = ($class->parse_volname($volname))[1];
751 my $fd = fileno($fh);
752 die "internal error: invalid file handle for volume_export\n"
756 # For zfs we always create a replication stream (-R) which means the remote
757 # side will always delete non-existing source snapshots. This should work
758 # for all our use cases.
759 my $cmd = ['zfs', 'send', '-Rpv'];
760 if (defined($base_snapshot)) {
761 my $arg = $with_snapshots ?
'-I' : '-i';
762 push @$cmd, $arg, $base_snapshot;
764 push @$cmd, '--', "$scfg->{pool}/$dataset\@$snapshot";
766 run_command
($cmd, output
=> $fd);
771 sub volume_export_formats
{
772 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
774 my @formats = ('zfs');
776 # push @formats, 'fies' if $volname !~ /^(?:basevol|subvol)-/;
777 # push @formats, 'raw' if !$base_snapshot && !$with_snapshots;
782 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots, $allow_rename) = @_;
784 die "unsupported import stream format for $class: $format\n"
787 my $fd = fileno($fh);
788 die "internal error: invalid file handle for volume_import\n"
791 my (undef, $dataset, $vmid, undef, undef, undef, $volume_format) =
792 $class->parse_volname($volname);
794 my $zfspath = "$scfg->{pool}/$dataset";
795 my $suffix = defined($base_snapshot) ?
"\@$base_snapshot" : '';
796 my $exists = 0 == run_command
(['zfs', 'get', '-H', 'name', $zfspath.$suffix],
797 noerr
=> 1, quiet
=> 1);
798 if (defined($base_snapshot)) {
799 die "base snapshot '$zfspath\@$base_snapshot' doesn't exist\n" if !$exists;
801 die "volume '$zfspath' already exists\n" if !$allow_rename;
802 warn "volume '$zfspath' already exists - importing with a different name\n";
803 $dataset = $class->find_free_diskname($storeid, $scfg, $vmid, $volume_format);
804 $zfspath = "$scfg->{pool}/$dataset";
807 eval { run_command
(['zfs', 'recv', '-F', '--', $zfspath], input
=> "<&$fd") };
809 if (defined($base_snapshot)) {
810 eval { run_command
(['zfs', 'rollback', '-r', '--', "$zfspath\@$base_snapshot"]) };
812 eval { run_command
(['zfs', 'destroy', '-r', '--', $zfspath]) };
817 return "$storeid:$dataset";
820 sub volume_import_formats
{
821 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
823 return $class->volume_export_formats($scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots);
827 my ($class, $scfg, $storeid, $source_volname, $target_vmid, $target_volname) = @_;
837 ) = $class->parse_volname($source_volname);
838 $target_volname = $class->find_free_diskname($storeid, $scfg, $target_vmid, $format)
841 my $pool = $scfg->{pool
};
842 my $source_zfspath = "${pool}/${source_image}";
843 my $target_zfspath = "${pool}/${target_volname}";
845 my $exists = 0 == run_command
(['zfs', 'get', '-H', 'name', $target_zfspath],
846 noerr
=> 1, quiet
=> 1);
847 die "target volume '${target_volname}' already exists\n" if $exists;
849 $class->zfs_request($scfg, 5, 'rename', ${source_zfspath
}, ${target_zfspath
});
851 $base_name = $base_name ?
"${base_name}/" : '';
853 return "${storeid}:${base_name}${target_volname}";