1 package PVE
::Storage
::ZFSPoolPlugin
;
7 use PVE
::Tools
qw(run_command);
8 use PVE
::Storage
::Plugin
;
9 use PVE
::RPCEnvironment
;
12 use base
qw(PVE::Storage::Plugin);
20 content
=> [ {images
=> 1, rootdir
=> 1}, {images
=> 1 , rootdir
=> 1}],
21 format
=> [ { raw
=> 1, subvol
=> 1 } , 'raw' ],
28 description
=> "block size",
32 description
=> "use sparse volumes",
36 description
=> "mount point",
37 type
=> 'string', format
=> 'pve-storage-path',
44 pool
=> { fixed
=> 1 },
45 blocksize
=> { optional
=> 1 },
46 sparse
=> { optional
=> 1 },
47 nodes
=> { optional
=> 1 },
48 disable
=> { optional
=> 1 },
49 content
=> { optional
=> 1 },
50 bwlimit
=> { optional
=> 1 },
51 mountpoint
=> { optional
=> 1 },
55 # static zfs helper methods
62 if ($text =~ m/^(\d+(\.\d+)?)([TGMK])?$/) {
64 my ($size, $reminder, $unit) = ($1, $2, $3);
69 } elsif ($unit eq 'M') {
71 } elsif ($unit eq 'G') {
72 $size *= 1024*1024*1024;
73 } elsif ($unit eq 'T') {
74 $size *= 1024*1024*1024*1024;
76 die "got unknown zfs size unit '$unit'\n";
88 warn "unable to parse zfs size '$text'\n";
93 sub zfs_parse_zvol_list
{
98 return $list if !$text;
100 my @lines = split /\n/, $text;
101 foreach my $line (@lines) {
102 my ($dataset, $size, $origin, $type, $refquota) = split(/\s+/, $line);
103 next if !($type eq 'volume' || $type eq 'filesystem');
106 my @parts = split /\//, $dataset;
107 next if scalar(@parts) < 2; # we need pool/name
108 my $name = pop @parts;
109 my $pool = join('/', @parts);
111 next unless $name =~ m!^(vm|base|subvol|basevol)-(\d+)-(\S+)$!;
114 $zvol->{pool
} = $pool;
115 $zvol->{name
} = $name;
116 if ($type eq 'filesystem') {
117 if ($refquota eq 'none') {
120 $zvol->{size
} = zfs_parse_size
($refquota);
122 $zvol->{format
} = 'subvol';
124 $zvol->{size
} = zfs_parse_size
($size);
125 $zvol->{format
} = 'raw';
127 if ($origin !~ /^-$/) {
128 $zvol->{origin
} = $origin;
137 my ($class, $volname) = @_;
139 if ($volname =~ m/^(((base|basevol)-(\d+)-\S+)\/)?
((base
|basevol
|vm
|subvol
)-(\d
+)-\S
+)$/) {
140 my $format = ($6 eq 'subvol' || $6 eq 'basevol') ?
'subvol' : 'raw';
141 my $isBase = ($6 eq 'base' || $6 eq 'basevol');
142 return ('images', $5, $7, $2, $4, $isBase, $format);
145 die "unable to parse zfs volume name '$volname'\n";
148 # virtual zfs methods (subclass can overwrite them)
151 my ($class, $storeid, $scfg, %param) = @_;
153 my $cfg_mountpoint = $scfg->{mountpoint
};
155 # ignore failure, pool might currently not be imported
156 my $mountpoint = eval {
157 $class->zfs_get_properties($scfg, 'mountpoint', $scfg->{pool
}, 1)
160 if (defined($cfg_mountpoint)) {
161 if (defined($mountpoint) && !($cfg_mountpoint =~ m
|^\Q
$mountpoint\E/?$|)) {
162 warn "warning for $storeid - mountpoint: $cfg_mountpoint " .
163 "does not match current mount point: $mountpoint\n";
166 $scfg->{mountpoint
} = $mountpoint;
171 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
173 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
176 my $mountpoint = $scfg->{mountpoint
} // "/$scfg->{pool}";
178 if ($vtype eq "images") {
179 if ($name =~ m/^subvol-/ || $name =~ m/^basevol-/) {
180 $path = "$mountpoint/$name";
182 $path = "/dev/zvol/$scfg->{pool}/$name";
184 $path .= "\@$snapname" if defined($snapname);
186 die "$vtype is not allowed in ZFSPool!";
189 return ($path, $vmid, $vtype);
193 my ($class, $scfg, $timeout, $method, @params) = @_;
197 if ($method eq 'zpool_list') {
198 push @$cmd, 'zpool', 'list';
199 } elsif ($method eq 'zpool_import') {
200 push @$cmd, 'zpool', 'import';
201 $timeout = 15 if !$timeout || $timeout < 15;
203 push @$cmd, 'zfs', $method;
208 my $output = sub { $msg .= "$_[0]\n" };
210 $timeout = PVE
::RPCEnvironment-
>is_worker() ?
60*60 : 5 if !$timeout;
212 run_command
($cmd, errmsg
=> "zfs error", outfunc
=> $output, timeout
=> $timeout);
217 sub zfs_wait_for_zvol_link
{
218 my ($class, $scfg, $volname, $timeout) = @_;
220 my $default_timeout = PVE
::RPCEnvironment-
>is_worker() ?
60*5 : 10;
221 $timeout = $default_timeout if !defined($timeout);
223 my ($devname, undef, undef) = $class->path($scfg, $volname);
225 for (my $i = 1; $i <= $timeout; $i++) {
227 die "timeout: no zvol device link for '$volname' found after $timeout sec found.\n"
235 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
241 die "illegal name '$volname' - should be 'vm-$vmid-*'\n"
242 if $volname && $volname !~ m/^vm-$vmid-/;
243 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt)
246 $class->zfs_create_zvol($scfg, $volname, $size);
247 $class->zfs_wait_for_zvol_link($scfg, $volname);
249 } elsif ( $fmt eq 'subvol') {
251 die "illegal name '$volname' - should be 'subvol-$vmid-*'\n"
252 if $volname && $volname !~ m/^subvol-$vmid-/;
253 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt)
256 die "illegal name '$volname' - should be 'subvol-$vmid-*'\n"
257 if $volname !~ m/^subvol-$vmid-/;
259 $class->zfs_create_subvol($scfg, $volname, $size);
262 die "unsupported format '$fmt'";
269 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
271 my (undef, $name, undef) = $class->parse_volname($volname);
273 $class->zfs_delete_zvol($scfg, $name);
279 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
281 $cache->{zfs
} = $class->zfs_list_zvol($scfg) if !$cache->{zfs
};
282 my $zfspool = $scfg->{pool
};
285 if (my $dat = $cache->{zfs
}->{$zfspool}) {
287 foreach my $image (keys %$dat) {
289 my $info = $dat->{$image};
291 my $volname = $info->{name
};
292 my $parent = $info->{parent
};
293 my $owner = $info->{vmid
};
295 if ($parent && $parent =~ m/^(\S+)\@__base__$/) {
296 my ($basename) = ($1);
297 $info->{volid
} = "$storeid:$basename/$volname";
299 $info->{volid
} = "$storeid:$volname";
303 my $found = grep { $_ eq $info->{volid
} } @$vollist;
306 next if defined ($vmid) && ($owner ne $vmid);
315 sub zfs_get_properties
{
316 my ($class, $scfg, $properties, $dataset, $timeout) = @_;
318 my $result = $class->zfs_request($scfg, $timeout, 'get', '-o', 'value',
319 '-Hp', $properties, $dataset);
320 my @values = split /\n/, $result;
321 return wantarray ?
@values : $values[0];
324 sub zfs_get_pool_stats
{
325 my ($class, $scfg) = @_;
330 my @lines = $class->zfs_get_properties($scfg, 'available,used', $scfg->{pool
});
332 if($lines[0] =~ /^(\d+)$/) {
336 if($lines[1] =~ /^(\d+)$/) {
340 return ($available, $used);
343 sub zfs_create_zvol
{
344 my ($class, $scfg, $zvol, $size) = @_;
346 # always align size to 1M as workaround until
347 # https://github.com/zfsonlinux/zfs/issues/8541 is solved
348 my $padding = (1024 - $size % 1024) % 1024;
349 $size = $size + $padding;
351 my $cmd = ['create'];
353 push @$cmd, '-s' if $scfg->{sparse
};
355 push @$cmd, '-b', $scfg->{blocksize
} if $scfg->{blocksize
};
357 push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol";
359 $class->zfs_request($scfg, undef, @$cmd);
362 sub zfs_create_subvol
{
363 my ($class, $scfg, $volname, $size) = @_;
365 my $dataset = "$scfg->{pool}/$volname";
367 my $cmd = ['create', '-o', 'acltype=posixacl', '-o', 'xattr=sa',
368 '-o', "refquota=${size}k", $dataset];
370 $class->zfs_request($scfg, undef, @$cmd);
373 sub zfs_delete_zvol
{
374 my ($class, $scfg, $zvol) = @_;
378 for (my $i = 0; $i < 6; $i++) {
380 eval { $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol"); };
382 if ($err =~ m/^zfs error:(.*): dataset is busy.*/) {
384 } elsif ($err =~ m/^zfs error:.*: dataset does not exist.*$/) {
399 my ($class, $scfg) = @_;
401 my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hr');
402 my $zvols = zfs_parse_zvol_list
($text);
403 return undef if !$zvols;
406 foreach my $zvol (@$zvols) {
407 my $pool = $zvol->{pool
};
408 my $name = $zvol->{name
};
409 my $parent = $zvol->{origin
};
410 if($zvol->{origin
} && $zvol->{origin
} =~ m/^$scfg->{pool}\/(\S
+)$/){
414 $list->{$pool}->{$name} = {
416 size
=> $zvol->{size
},
418 format
=> $zvol->{format
},
419 vmid
=> $zvol->{owner
},
426 sub zfs_find_free_diskname
{
427 my ($class, $storeid, $scfg, $vmid, $format) = @_;
429 my $volumes = $class->zfs_list_zvol($scfg);
430 my $dat = $volumes->{$scfg->{pool
}};
432 my $disk_list = [ keys %$dat ];
433 return PVE
::Storage
::Plugin
::get_next_vm_diskname
($disk_list, $storeid, $vmid, $format, $scfg);
436 sub zfs_get_latest_snapshot
{
437 my ($class, $scfg, $volname) = @_;
439 my $vname = ($class->parse_volname($volname))[1];
441 # abort rollback if snapshot is not the latest
442 my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
443 my $text = $class->zfs_request($scfg, undef, 'list', @params);
444 my @snapshots = split(/\n/, $text);
447 foreach (@snapshots) {
448 if (/$scfg->{pool}\/$vname/) {
458 my ($class, $storeid, $scfg, $cache) = @_;
466 ($free, $used) = $class->zfs_get_pool_stats($scfg);
468 $total = $free + $used;
472 return ($total, $free, $used, $active);
475 sub volume_size_info
{
476 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
478 my (undef, $vname, undef, undef, undef, undef, $format) =
479 $class->parse_volname($volname);
481 my $attr = $format eq 'subvol' ?
'refquota' : 'volsize';
482 my $value = $class->zfs_get_properties($scfg, $attr, "$scfg->{pool}/$vname");
483 if ($value =~ /^(\d+)$/) {
487 die "Could not get zfs volume size\n";
490 sub volume_snapshot
{
491 my ($class, $scfg, $storeid, $volname, $snap) = @_;
493 my $vname = ($class->parse_volname($volname))[1];
495 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$vname\@$snap");
498 sub volume_snapshot_delete
{
499 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
501 my $vname = ($class->parse_volname($volname))[1];
503 $class->deactivate_volume($storeid, $scfg, $vname, $snap, {});
504 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$vname\@$snap");
507 sub volume_snapshot_rollback
{
508 my ($class, $scfg, $storeid, $volname, $snap) = @_;
510 my $vname = ($class->parse_volname($volname))[1];
512 $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$vname\@$snap");
515 sub volume_rollback_is_possible
{
516 my ($class, $scfg, $storeid, $volname, $snap) = @_;
518 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
519 if ($snap ne $recentsnap) {
520 die "can't rollback, more recent snapshots exist\n";
526 sub volume_snapshot_list
{
527 my ($class, $scfg, $storeid, $volname) = @_;
529 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
531 my $zpath = "$scfg->{pool}/$name";
535 my $cmd = ['zfs', 'list', '-r', '-H', '-S', 'name', '-t', 'snap', '-o',
541 if ($line =~ m/^\Q$zpath\E@(.*)$/) {
546 eval { run_command
( [$cmd], outfunc
=> $outfunc , errfunc
=> sub{}); };
548 # return an empty array if dataset does not exist.
552 sub activate_storage
{
553 my ($class, $storeid, $scfg, $cache) = @_;
555 # Note: $scfg->{pool} can include dataset <pool>/<dataset>
556 my $pool = $scfg->{pool
};
559 my $pool_imported = sub {
560 my @param = ('-o', 'name', '-H', "$pool");
561 my $res = eval { $class->zfs_request($scfg, undef, 'zpool_list', @param) };
566 return defined($res) && $res =~ m/$pool/;
569 if (!$pool_imported->()) {
570 # import can only be done if not yet imported!
571 my @param = ('-d', '/dev/disk/by-id/', '-o', 'cachefile=none', "$pool");
572 eval { $class->zfs_request($scfg, undef, 'zpool_import', @param) };
574 # just could've raced with another import, so recheck if it is imported
575 die "could not activate storage '$storeid', $@\n" if !$pool_imported->();
581 sub deactivate_storage
{
582 my ($class, $storeid, $scfg, $cache) = @_;
586 sub activate_volume
{
587 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
589 return 1 if defined($snapname);
591 my (undef, undef, undef, undef, undef, undef, $format) = $class->parse_volname($volname);
593 return 1 if $format ne 'raw';
595 $class->zfs_wait_for_zvol_link($scfg, $volname);
600 sub deactivate_volume
{
601 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
606 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
608 $snap ||= '__base__';
610 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
611 $class->parse_volname($volname);
613 die "clone_image only works on base images\n" if !$isBase;
615 my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $format);
617 if ($format eq 'subvol') {
618 my $size = $class->zfs_request($scfg, undef, 'list', '-H', '-o', 'refquota', "$scfg->{pool}/$basename");
620 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name", '-o', "refquota=$size");
622 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
625 return "$basename/$name";
629 my ($class, $storeid, $scfg, $volname) = @_;
631 my $snap = '__base__';
633 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
634 $class->parse_volname($volname);
636 die "create_base not possible with base image\n" if $isBase;
639 if ( $format eq 'subvol' ) {
640 $newname =~ s/^subvol-/basevol-/;
642 $newname =~ s/^vm-/base-/;
644 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
646 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
648 my $running = undef; #fixme : is create_base always offline ?
650 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
656 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
658 my $new_size = int($size/1024);
660 my (undef, $vname, undef, undef, undef, undef, $format) =
661 $class->parse_volname($volname);
663 my $attr = $format eq 'subvol' ?
'refquota' : 'volsize';
665 $class->zfs_request($scfg, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$vname");
670 sub storage_can_replicate
{
671 my ($class, $scfg, $storeid, $format) = @_;
673 return 1 if $format eq 'raw' || $format eq 'subvol';
678 sub volume_has_feature
{
679 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
682 snapshot
=> { current
=> 1, snap
=> 1},
683 clone
=> { base
=> 1},
684 template
=> { current
=> 1},
685 copy
=> { base
=> 1, current
=> 1},
686 sparseinit
=> { base
=> 1, current
=> 1},
687 replicate
=> { base
=> 1, current
=> 1},
690 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
691 $class->parse_volname($volname);
698 $key = $isBase ?
'base' : 'current';
701 return 1 if $features->{$feature}->{$key};
707 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
709 die "unsupported export stream format for $class: $format\n"
712 die "$class storage can only export snapshots\n"
713 if !defined($snapshot);
715 my $dataset = ($class->parse_volname($volname))[1];
717 my $fd = fileno($fh);
718 die "internal error: invalid file handle for volume_export\n"
722 # For zfs we always create a replication stream (-R) which means the remote
723 # side will always delete non-existing source snapshots. This should work
724 # for all our use cases.
725 my $cmd = ['zfs', 'send', '-Rpv'];
726 if (defined($base_snapshot)) {
727 my $arg = $with_snapshots ?
'-I' : '-i';
728 push @$cmd, $arg, $base_snapshot;
730 push @$cmd, '--', "$scfg->{pool}/$dataset\@$snapshot";
732 run_command
($cmd, output
=> $fd);
737 sub volume_export_formats
{
738 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
740 my @formats = ('zfs');
742 # push @formats, 'fies' if $volname !~ /^(?:basevol|subvol)-/;
743 # push @formats, 'raw' if !$base_snapshot && !$with_snapshots;
748 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_;
750 die "unsupported import stream format for $class: $format\n"
753 my $fd = fileno($fh);
754 die "internal error: invalid file handle for volume_import\n"
757 my $dataset = ($class->parse_volname($volname))[1];
758 my $zfspath = "$scfg->{pool}/$dataset";
759 my $suffix = defined($base_snapshot) ?
"\@$base_snapshot" : '';
760 my $exists = 0 == run_command
(['zfs', 'get', '-H', 'name', $zfspath.$suffix],
761 noerr
=> 1, errfunc
=> sub {});
762 if (defined($base_snapshot)) {
763 die "base snapshot '$zfspath\@$base_snapshot' doesn't exist\n" if !$exists;
765 die "volume '$zfspath' already exists\n" if $exists;
768 eval { run_command
(['zfs', 'recv', '-F', '--', $zfspath], input
=> "<&$fd") };
770 if (defined($base_snapshot)) {
771 eval { run_command
(['zfs', 'rollback', '-r', '--', "$zfspath\@$base_snapshot"]) };
773 eval { run_command
(['zfs', 'destroy', '-r', '--', $zfspath]) };
781 sub volume_import_formats
{
782 my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
784 return $class->volume_export_formats($scfg, $storeid, $volname, undef, $base_snapshot, $with_snapshots);