1 package PVE
::Storage
::ZFSPoolPlugin
;
10 use PVE
::RPCEnvironment
;
11 use PVE
::Storage
::Plugin
;
12 use PVE
::Tools
qw(run_command);
14 use base
qw(PVE::Storage::Plugin);
22 content
=> [ {images
=> 1, rootdir
=> 1}, {images
=> 1 , rootdir
=> 1}],
23 format
=> [ { raw
=> 1, subvol
=> 1 } , 'raw' ],
30 description
=> "block size",
34 description
=> "use sparse volumes",
38 description
=> "mount point",
39 type
=> 'string', format
=> 'pve-storage-path',
46 pool
=> { fixed
=> 1 },
47 blocksize
=> { optional
=> 1 },
48 sparse
=> { optional
=> 1 },
49 nodes
=> { optional
=> 1 },
50 disable
=> { optional
=> 1 },
51 content
=> { optional
=> 1 },
52 bwlimit
=> { optional
=> 1 },
53 mountpoint
=> { optional
=> 1 },
57 # static zfs helper methods
59 sub zfs_parse_zvol_list
{
64 return $list if !$text;
66 my @lines = split /\n/, $text;
67 foreach my $line (@lines) {
68 my ($dataset, $size, $origin, $type, $refquota) = split(/\s+/, $line);
69 next if !($type eq 'volume' || $type eq 'filesystem');
72 my @parts = split /\//, $dataset;
73 next if scalar(@parts) < 2; # we need pool/name
74 my $name = pop @parts;
75 my $pool = join('/', @parts);
77 next unless $name =~ m!^(vm|base|subvol|basevol)-(\d+)-(\S+)$!;
80 $zvol->{pool
} = $pool;
81 $zvol->{name
} = $name;
82 if ($type eq 'filesystem') {
83 if ($refquota eq 'none') {
86 $zvol->{size
} = $refquota + 0;
88 $zvol->{format
} = 'subvol';
90 $zvol->{size
} = $size + 0;
91 $zvol->{format
} = 'raw';
93 if ($origin !~ /^-$/) {
94 $zvol->{origin
} = $origin;
103 my ($class, $volname) = @_;
105 if ($volname =~ m/^(((base|basevol)-(\d+)-\S+)\/)?
((base
|basevol
|vm
|subvol
)-(\d
+)-\S
+)$/) {
106 my $format = ($6 eq 'subvol' || $6 eq 'basevol') ?
'subvol' : 'raw';
107 my $isBase = ($6 eq 'base' || $6 eq 'basevol');
108 return ('images', $5, $7, $2, $4, $isBase, $format);
111 die "unable to parse zfs volume name '$volname'\n";
114 # virtual zfs methods (subclass can overwrite them)
117 my ($class, $storeid, $scfg, %param) = @_;
119 my $cfg_mountpoint = $scfg->{mountpoint
};
121 # ignore failure, pool might currently not be imported
124 my $res = $class->zfs_get_properties($scfg, 'mountpoint', $scfg->{pool
}, 1);
125 $mountpoint = PVE
::Storage
::Plugin
::verify_path
($res, 1) if defined($res);
128 if (defined($cfg_mountpoint)) {
129 if (defined($mountpoint) && !($cfg_mountpoint =~ m
|^\Q
$mountpoint\E/?$|)) {
130 warn "warning for $storeid - mountpoint: $cfg_mountpoint " .
131 "does not match current mount point: $mountpoint\n";
134 $scfg->{mountpoint
} = $mountpoint;
141 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
143 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
146 my $mountpoint = $scfg->{mountpoint
} // "/$scfg->{pool}";
148 if ($vtype eq "images") {
149 if ($name =~ m/^subvol-/ || $name =~ m/^basevol-/) {
150 $path = "$mountpoint/$name";
152 $path = "/dev/zvol/$scfg->{pool}/$name";
154 $path .= "\@$snapname" if defined($snapname);
156 die "$vtype is not allowed in ZFSPool!";
159 return ($path, $vmid, $vtype);
163 my ($class, $scfg, $timeout, $method, @params) = @_;
167 if ($method eq 'zpool_list') {
168 push @$cmd, 'zpool', 'list';
169 } elsif ($method eq 'zpool_import') {
170 push @$cmd, 'zpool', 'import';
171 $timeout = 15 if !$timeout || $timeout < 15;
173 push @$cmd, 'zfs', $method;
178 my $output = sub { $msg .= "$_[0]\n" };
180 $timeout = PVE
::RPCEnvironment-
>is_worker() ?
60*60 : 5 if !$timeout;
182 run_command
($cmd, errmsg
=> "zfs error", outfunc
=> $output, timeout
=> $timeout);
187 sub zfs_wait_for_zvol_link
{
188 my ($class, $scfg, $volname, $timeout) = @_;
190 my $default_timeout = PVE
::RPCEnvironment-
>is_worker() ?
60*5 : 10;
191 $timeout = $default_timeout if !defined($timeout);
193 my ($devname, undef, undef) = $class->path($scfg, $volname);
195 for (my $i = 1; $i <= $timeout; $i++) {
197 die "timeout: no zvol device link for '$volname' found after $timeout sec found.\n"
205 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
211 die "illegal name '$volname' - should be 'vm-$vmid-*'\n"
212 if $volname && $volname !~ m/^vm-$vmid-/;
213 $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt)
216 $class->zfs_create_zvol($scfg, $volname, $size);
217 $class->zfs_wait_for_zvol_link($scfg, $volname);
219 } elsif ( $fmt eq 'subvol') {
221 die "illegal name '$volname' - should be 'subvol-$vmid-*'\n"
222 if $volname && $volname !~ m/^subvol-$vmid-/;
223 $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt)
226 die "illegal name '$volname' - should be 'subvol-$vmid-*'\n"
227 if $volname !~ m/^subvol-$vmid-/;
229 $class->zfs_create_subvol($scfg, $volname, $size);
232 die "unsupported format '$fmt'";
239 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
241 my (undef, $name, undef) = $class->parse_volname($volname);
243 $class->zfs_delete_zvol($scfg, $name);
249 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
251 $cache->{zfs
} = $class->zfs_list_zvol($scfg) if !$cache->{zfs
};
252 my $zfspool = $scfg->{pool
};
255 if (my $dat = $cache->{zfs
}->{$zfspool}) {
257 foreach my $image (keys %$dat) {
259 my $info = $dat->{$image};
261 my $volname = $info->{name
};
262 my $parent = $info->{parent
};
263 my $owner = $info->{vmid
};
265 if ($parent && $parent =~ m/^(\S+)\@__base__$/) {
266 my ($basename) = ($1);
267 $info->{volid
} = "$storeid:$basename/$volname";
269 $info->{volid
} = "$storeid:$volname";
273 my $found = grep { $_ eq $info->{volid
} } @$vollist;
276 next if defined ($vmid) && ($owner ne $vmid);
285 sub zfs_get_properties
{
286 my ($class, $scfg, $properties, $dataset, $timeout) = @_;
288 my $result = $class->zfs_request($scfg, $timeout, 'get', '-o', 'value',
289 '-Hp', $properties, $dataset);
290 my @values = split /\n/, $result;
291 return wantarray ?
@values : $values[0];
294 sub zfs_get_pool_stats
{
295 my ($class, $scfg) = @_;
300 my @lines = $class->zfs_get_properties($scfg, 'available,used', $scfg->{pool
});
302 if($lines[0] =~ /^(\d+)$/) {
306 if($lines[1] =~ /^(\d+)$/) {
310 return ($available, $used);
313 sub zfs_create_zvol
{
314 my ($class, $scfg, $zvol, $size) = @_;
316 # always align size to 1M as workaround until
317 # https://github.com/zfsonlinux/zfs/issues/8541 is solved
318 my $padding = (1024 - $size % 1024) % 1024;
319 $size = $size + $padding;
321 my $cmd = ['create'];
323 push @$cmd, '-s' if $scfg->{sparse
};
325 push @$cmd, '-b', $scfg->{blocksize
} if $scfg->{blocksize
};
327 push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol";
329 $class->zfs_request($scfg, undef, @$cmd);
332 sub zfs_create_subvol
{
333 my ($class, $scfg, $volname, $size) = @_;
335 my $dataset = "$scfg->{pool}/$volname";
337 my $cmd = ['create', '-o', 'acltype=posixacl', '-o', 'xattr=sa',
338 '-o', "refquota=${size}k", $dataset];
340 $class->zfs_request($scfg, undef, @$cmd);
343 sub zfs_delete_zvol
{
344 my ($class, $scfg, $zvol) = @_;
348 for (my $i = 0; $i < 6; $i++) {
350 eval { $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol"); };
352 if ($err =~ m/^zfs error:(.*): dataset is busy.*/) {
354 } elsif ($err =~ m/^zfs error:.*: dataset does not exist.*$/) {
369 my ($class, $scfg) = @_;
371 my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hrp');
372 my $zvols = zfs_parse_zvol_list
($text);
373 return undef if !$zvols;
376 foreach my $zvol (@$zvols) {
377 my $pool = $zvol->{pool
};
378 my $name = $zvol->{name
};
379 my $parent = $zvol->{origin
};
380 if($zvol->{origin
} && $zvol->{origin
} =~ m/^$scfg->{pool}\/(\S
+)$/){
384 $list->{$pool}->{$name} = {
386 size
=> $zvol->{size
},
388 format
=> $zvol->{format
},
389 vmid
=> $zvol->{owner
},
396 sub zfs_get_latest_snapshot
{
397 my ($class, $scfg, $volname) = @_;
399 my $vname = ($class->parse_volname($volname))[1];
401 # abort rollback if snapshot is not the latest
402 my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
403 my $text = $class->zfs_request($scfg, undef, 'list', @params);
404 my @snapshots = split(/\n/, $text);
407 foreach (@snapshots) {
408 if (/$scfg->{pool}\/$vname/) {
418 my ($class, $storeid, $scfg, $cache) = @_;
426 ($free, $used) = $class->zfs_get_pool_stats($scfg);
428 $total = $free + $used;
432 return ($total, $free, $used, $active);
435 sub volume_size_info
{
436 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
438 my (undef, $vname, undef, undef, undef, undef, $format) =
439 $class->parse_volname($volname);
441 my $attr = $format eq 'subvol' ?
'refquota' : 'volsize';
442 my $value = $class->zfs_get_properties($scfg, $attr, "$scfg->{pool}/$vname");
443 if ($value =~ /^(\d+)$/) {
447 die "Could not get zfs volume size\n";
450 sub volume_snapshot
{
451 my ($class, $scfg, $storeid, $volname, $snap) = @_;
453 my $vname = ($class->parse_volname($volname))[1];
455 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$vname\@$snap");
458 sub volume_snapshot_delete
{
459 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
461 my $vname = ($class->parse_volname($volname))[1];
463 $class->deactivate_volume($storeid, $scfg, $vname, $snap, {});
464 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$vname\@$snap");
467 sub volume_snapshot_rollback
{
468 my ($class, $scfg, $storeid, $volname, $snap) = @_;
470 my (undef, $vname, undef, undef, undef, undef, $format) = $class->parse_volname($volname);
472 my $msg = $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$vname\@$snap");
474 # we have to unmount rollbacked subvols, to invalidate wrong kernel
475 # caches, they get mounted in activate volume again
476 # see zfs bug #10931 https://github.com/openzfs/zfs/issues/10931
477 if ($format eq 'subvol') {
478 $class->zfs_request($scfg, undef, 'unmount', "$scfg->{pool}/$vname");
484 sub volume_rollback_is_possible
{
485 my ($class, $scfg, $storeid, $volname, $snap) = @_;
487 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
489 die "can't rollback, no snapshots exist at all\n"
490 if !defined($recentsnap);
492 die "can't rollback, '$snap' is not most recent snapshot\n"
493 if $snap ne $recentsnap;
498 sub volume_snapshot_list
{
499 my ($class, $scfg, $storeid, $volname) = @_;
501 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
503 my $zpath = "$scfg->{pool}/$name";
507 my $cmd = ['zfs', 'list', '-r', '-H', '-S', 'name', '-t', 'snap', '-o',
513 if ($line =~ m/^\Q$zpath\E@(.*)$/) {
518 eval { run_command
( [$cmd], outfunc
=> $outfunc , errfunc
=> sub{}); };
520 # return an empty array if dataset does not exist.
524 sub activate_storage
{
525 my ($class, $storeid, $scfg, $cache) = @_;
527 # Note: $scfg->{pool} can include dataset <pool>/<dataset>
528 my $pool = $scfg->{pool
};
531 my $pool_imported = sub {
532 my @param = ('-o', 'name', '-H', $pool);
533 my $res = eval { $class->zfs_request($scfg, undef, 'zpool_list', @param) };
536 return defined($res) && $res =~ m/$pool/;
539 if (!$pool_imported->()) {
540 # import can only be done if not yet imported!
541 my @param = ('-d', '/dev/disk/by-id/', '-o', 'cachefile=none', $pool);
542 eval { $class->zfs_request($scfg, undef, 'zpool_import', @param) };
544 # just could've raced with another import, so recheck if it is imported
545 die "could not activate storage '$storeid', $err\n" if !$pool_imported->();
551 sub deactivate_storage
{
552 my ($class, $storeid, $scfg, $cache) = @_;
556 sub activate_volume
{
557 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
559 return 1 if defined($snapname);
561 my (undef, $dataset, undef, undef, undef, undef, $format) = $class->parse_volname($volname);
563 if ($format eq 'raw') {
564 $class->zfs_wait_for_zvol_link($scfg, $volname);
565 } elsif ($format eq 'subvol') {
566 my $mounted = $class->zfs_get_properties($scfg, 'mounted', "$scfg->{pool}/$dataset");
567 if ($mounted !~ m/^yes$/) {
568 $class->zfs_request($scfg, undef, 'mount', "$scfg->{pool}/$dataset");
575 sub deactivate_volume
{
576 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
581 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
583 $snap ||= '__base__';
585 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
586 $class->parse_volname($volname);
588 die "clone_image only works on base images\n" if !$isBase;
590 my $name = $class->find_free_diskname($storeid, $scfg, $vmid, $format);
592 if ($format eq 'subvol') {
593 my $size = $class->zfs_request($scfg, undef, 'list', '-Hp', '-o', 'refquota', "$scfg->{pool}/$basename");
595 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name", '-o', "refquota=$size");
597 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
600 return "$basename/$name";
604 my ($class, $storeid, $scfg, $volname) = @_;
606 my $snap = '__base__';
608 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
609 $class->parse_volname($volname);
611 die "create_base not possible with base image\n" if $isBase;
614 if ( $format eq 'subvol' ) {
615 $newname =~ s/^subvol-/basevol-/;
617 $newname =~ s/^vm-/base-/;
619 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
621 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
623 my $running = undef; #fixme : is create_base always offline ?
625 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
631 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
633 my $new_size = int($size/1024);
635 my (undef, $vname, undef, undef, undef, undef, $format) =
636 $class->parse_volname($volname);
638 my $attr = $format eq 'subvol' ?
'refquota' : 'volsize';
640 # align size to 1M so we always have a valid multiple of the volume block size
641 if ($format eq 'raw') {
642 my $padding = (1024 - $new_size % 1024) % 1024;
643 $new_size = $new_size + $padding;
646 $class->zfs_request($scfg, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$vname");
651 sub storage_can_replicate
{
652 my ($class, $scfg, $storeid, $format) = @_;
654 return 1 if $format eq 'raw' || $format eq 'subvol';
659 sub volume_has_feature
{
660 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
663 snapshot
=> { current
=> 1, snap
=> 1},
664 clone
=> { base
=> 1},
665 template
=> { current
=> 1},
666 copy
=> { base
=> 1, current
=> 1},
667 sparseinit
=> { base
=> 1, current
=> 1},
668 replicate
=> { base
=> 1, current
=> 1},
671 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
672 $class->parse_volname($volname);
679 $key = $isBase ?
'base' : 'current';
682 return 1 if $features->{$feature}->{$key};
688 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
690 die "unsupported export stream format for $class: $format\n"
693 die "$class storage can only export snapshots\n"
694 if !defined($snapshot);
696 my $dataset = ($class->parse_volname($volname))[1];
698 my $fd = fileno($fh);
699 die "internal error: invalid file handle for volume_export\n"
703 # For zfs we always create a replication stream (-R) which means the remote
704 # side will always delete non-existing source snapshots. This should work
705 # for all our use cases.
706 my $cmd = ['zfs', 'send', '-Rpv'];
707 if (defined($base_snapshot)) {
708 my $arg = $with_snapshots ?
'-I' : '-i';
709 push @$cmd, $arg, $base_snapshot;
711 push @$cmd, '--', "$scfg->{pool}/$dataset\@$snapshot";
713 run_command
($cmd, output
=> $fd);
718 sub volume_export_formats
{
719 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
721 my @formats = ('zfs');
723 # push @formats, 'fies' if $volname !~ /^(?:basevol|subvol)-/;
724 # push @formats, 'raw' if !$base_snapshot && !$with_snapshots;
729 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots, $allow_rename) = @_;
731 die "unsupported import stream format for $class: $format\n"
734 my $fd = fileno($fh);
735 die "internal error: invalid file handle for volume_import\n"
738 my (undef, $dataset, $vmid) = $class->parse_volname($volname);
739 my $zfspath = "$scfg->{pool}/$dataset";
740 my $suffix = defined($base_snapshot) ?
"\@$base_snapshot" : '';
741 my $exists = 0 == run_command
(['zfs', 'get', '-H', 'name', $zfspath.$suffix],
742 noerr
=> 1, quiet
=> 1);
743 if (defined($base_snapshot)) {
744 die "base snapshot '$zfspath\@$base_snapshot' doesn't exist\n" if !$exists;
746 die "volume '$zfspath' already exists\n" if !$allow_rename;
747 warn "volume '$zfspath' already exists - importing with a different name\n";
748 $dataset = $class->find_free_diskname($storeid, $scfg, $vmid, $format);
749 $zfspath = "$scfg->{pool}/$dataset";
752 eval { run_command
(['zfs', 'recv', '-F', '--', $zfspath], input
=> "<&$fd") };
754 if (defined($base_snapshot)) {
755 eval { run_command
(['zfs', 'rollback', '-r', '--', "$zfspath\@$base_snapshot"]) };
757 eval { run_command
(['zfs', 'destroy', '-r', '--', $zfspath]) };
762 return "$storeid:$dataset";
765 sub volume_import_formats
{
766 my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
768 return $class->volume_export_formats($scfg, $storeid, $volname, undef, $base_snapshot, $with_snapshots);