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",
40 pool
=> { fixed
=> 1 },
41 blocksize
=> { optional
=> 1 },
42 sparse
=> { optional
=> 1 },
43 nodes
=> { optional
=> 1 },
44 disable
=> { optional
=> 1 },
45 content
=> { optional
=> 1 },
46 bwlimit
=> { optional
=> 1 },
50 # static zfs helper methods
57 if ($text =~ m/^(\d+(\.\d+)?)([TGMK])?$/) {
59 my ($size, $reminder, $unit) = ($1, $2, $3);
64 } elsif ($unit eq 'M') {
66 } elsif ($unit eq 'G') {
67 $size *= 1024*1024*1024;
68 } elsif ($unit eq 'T') {
69 $size *= 1024*1024*1024*1024;
71 die "got unknown zfs size unit '$unit'\n";
83 warn "unable to parse zfs size '$text'\n";
88 sub zfs_parse_zvol_list
{
93 return $list if !$text;
95 my @lines = split /\n/, $text;
96 foreach my $line (@lines) {
97 my ($dataset, $size, $origin, $type, $refquota) = split(/\s+/, $line);
98 next if !($type eq 'volume' || $type eq 'filesystem');
101 my @parts = split /\//, $dataset;
102 next if scalar(@parts) < 2; # we need pool/name
103 my $name = pop @parts;
104 my $pool = join('/', @parts);
106 next unless $name =~ m!^(vm|base|subvol|basevol)-(\d+)-(\S+)$!;
109 $zvol->{pool
} = $pool;
110 $zvol->{name
} = $name;
111 if ($type eq 'filesystem') {
112 if ($refquota eq 'none') {
115 $zvol->{size
} = zfs_parse_size
($refquota);
117 $zvol->{format
} = 'subvol';
119 $zvol->{size
} = zfs_parse_size
($size);
120 $zvol->{format
} = 'raw';
122 if ($origin !~ /^-$/) {
123 $zvol->{origin
} = $origin;
132 my ($class, $volname) = @_;
134 if ($volname =~ m/^(((base|basevol)-(\d+)-\S+)\/)?
((base
|basevol
|vm
|subvol
)-(\d
+)-\S
+)$/) {
135 my $format = ($6 eq 'subvol' || $6 eq 'basevol') ?
'subvol' : 'raw';
136 my $isBase = ($6 eq 'base' || $6 eq 'basevol');
137 return ('images', $5, $7, $2, $4, $isBase, $format);
140 die "unable to parse zfs volume name '$volname'\n";
143 # virtual zfs methods (subclass can overwrite them)
146 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
148 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
152 if ($vtype eq "images") {
153 if ($name =~ m/^subvol-/ || $name =~ m/^basevol-/) {
154 # fixme: we currently assume standard mount point?!
155 $path = "/$scfg->{pool}/$name";
157 $path = "/dev/zvol/$scfg->{pool}/$name";
159 $path .= "\@$snapname" if defined($snapname);
161 die "$vtype is not allowed in ZFSPool!";
164 return ($path, $vmid, $vtype);
168 my ($class, $scfg, $timeout, $method, @params) = @_;
172 if ($method eq 'zpool_list') {
173 push @$cmd, 'zpool', 'list';
174 } elsif ($method eq 'zpool_import') {
175 push @$cmd, 'zpool', 'import';
176 $timeout = 15 if !$timeout || $timeout < 15;
178 push @$cmd, 'zfs', $method;
183 my $output = sub { $msg .= "$_[0]\n" };
185 $timeout = PVE
::RPCEnvironment-
>is_worker() ?
60*60 : 5 if !$timeout;
187 run_command
($cmd, errmsg
=> "zfs error", outfunc
=> $output, timeout
=> $timeout);
192 sub zfs_wait_for_zvol_link
{
193 my ($class, $scfg, $volname, $timeout) = @_;
195 my $default_timeout = PVE
::RPCEnvironment-
>is_worker() ?
60*5 : 10;
196 $timeout = $default_timeout if !defined($timeout);
198 my ($devname, undef, undef) = $class->path($scfg, $volname);
200 for (my $i = 1; $i <= $timeout; $i++) {
202 die "timeout: no zvol device link for '$volname' found after $timeout sec found.\n"
210 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
216 die "illegal name '$volname' - should be 'vm-$vmid-*'\n"
217 if $volname && $volname !~ m/^vm-$vmid-/;
218 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt)
221 $class->zfs_create_zvol($scfg, $volname, $size);
222 $class->zfs_wait_for_zvol_link($scfg, $volname);
224 } elsif ( $fmt eq 'subvol') {
226 die "illegal name '$volname' - should be 'subvol-$vmid-*'\n"
227 if $volname && $volname !~ m/^subvol-$vmid-/;
228 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt)
231 die "illegal name '$volname' - should be 'subvol-$vmid-*'\n"
232 if $volname !~ m/^subvol-$vmid-/;
234 $class->zfs_create_subvol($scfg, $volname, $size);
237 die "unsupported format '$fmt'";
244 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
246 my (undef, $name, undef) = $class->parse_volname($volname);
248 $class->zfs_delete_zvol($scfg, $name);
254 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
256 $cache->{zfs
} = $class->zfs_list_zvol($scfg) if !$cache->{zfs
};
257 my $zfspool = $scfg->{pool
};
260 if (my $dat = $cache->{zfs
}->{$zfspool}) {
262 foreach my $image (keys %$dat) {
264 my $info = $dat->{$image};
266 my $volname = $info->{name
};
267 my $parent = $info->{parent
};
268 my $owner = $info->{vmid
};
270 if ($parent && $parent =~ m/^(\S+)\@__base__$/) {
271 my ($basename) = ($1);
272 $info->{volid
} = "$storeid:$basename/$volname";
274 $info->{volid
} = "$storeid:$volname";
278 my $found = grep { $_ eq $info->{volid
} } @$vollist;
281 next if defined ($vmid) && ($owner ne $vmid);
290 sub zfs_get_properties
{
291 my ($class, $scfg, $properties, $dataset, $timeout) = @_;
293 my $result = $class->zfs_request($scfg, $timeout, 'get', '-o', 'value',
294 '-Hp', $properties, $dataset);
295 my @values = split /\n/, $result;
296 return wantarray ?
@values : $values[0];
299 sub zfs_get_pool_stats
{
300 my ($class, $scfg) = @_;
305 my @lines = $class->zfs_get_properties($scfg, 'available,used', $scfg->{pool
});
307 if($lines[0] =~ /^(\d+)$/) {
311 if($lines[1] =~ /^(\d+)$/) {
315 return ($available, $used);
318 sub zfs_create_zvol
{
319 my ($class, $scfg, $zvol, $size) = @_;
321 # always align size to 1M as workaround until
322 # https://github.com/zfsonlinux/zfs/issues/8541 is solved
323 my $padding = (1024 - $size % 1024) % 1024;
324 $size = $size + $padding;
326 my $cmd = ['create'];
328 push @$cmd, '-s' if $scfg->{sparse
};
330 push @$cmd, '-b', $scfg->{blocksize
} if $scfg->{blocksize
};
332 push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol";
334 $class->zfs_request($scfg, undef, @$cmd);
337 sub zfs_create_subvol
{
338 my ($class, $scfg, $volname, $size) = @_;
340 my $dataset = "$scfg->{pool}/$volname";
342 my $cmd = ['create', '-o', 'acltype=posixacl', '-o', 'xattr=sa',
343 '-o', "refquota=${size}k", $dataset];
345 $class->zfs_request($scfg, undef, @$cmd);
348 sub zfs_delete_zvol
{
349 my ($class, $scfg, $zvol) = @_;
353 for (my $i = 0; $i < 6; $i++) {
355 eval { $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol"); };
357 if ($err =~ m/^zfs error:(.*): dataset is busy.*/) {
359 } elsif ($err =~ m/^zfs error:.*: dataset does not exist.*$/) {
374 my ($class, $scfg) = @_;
376 my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hr');
377 my $zvols = zfs_parse_zvol_list
($text);
378 return undef if !$zvols;
381 foreach my $zvol (@$zvols) {
382 my $pool = $zvol->{pool
};
383 my $name = $zvol->{name
};
384 my $parent = $zvol->{origin
};
385 if($zvol->{origin
} && $zvol->{origin
} =~ m/^$scfg->{pool}\/(\S
+)$/){
389 $list->{$pool}->{$name} = {
391 size
=> $zvol->{size
},
393 format
=> $zvol->{format
},
394 vmid
=> $zvol->{owner
},
401 sub zfs_find_free_diskname
{
402 my ($class, $storeid, $scfg, $vmid, $format) = @_;
404 my $volumes = $class->zfs_list_zvol($scfg);
405 my $dat = $volumes->{$scfg->{pool
}};
407 my $disk_list = [ keys %$dat ];
408 return PVE
::Storage
::Plugin
::get_next_vm_diskname
($disk_list, $storeid, $vmid, $format, $scfg);
411 sub zfs_get_latest_snapshot
{
412 my ($class, $scfg, $volname) = @_;
414 my $vname = ($class->parse_volname($volname))[1];
416 # abort rollback if snapshot is not the latest
417 my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
418 my $text = $class->zfs_request($scfg, undef, 'list', @params);
419 my @snapshots = split(/\n/, $text);
422 foreach (@snapshots) {
423 if (/$scfg->{pool}\/$vname/) {
433 my ($class, $storeid, $scfg, $cache) = @_;
441 ($free, $used) = $class->zfs_get_pool_stats($scfg);
443 $total = $free + $used;
447 return ($total, $free, $used, $active);
450 sub volume_size_info
{
451 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
453 my (undef, $vname, undef, undef, undef, undef, $format) =
454 $class->parse_volname($volname);
456 my $attr = $format eq 'subvol' ?
'refquota' : 'volsize';
457 my $value = $class->zfs_get_properties($scfg, $attr, "$scfg->{pool}/$vname");
458 if ($value =~ /^(\d+)$/) {
462 die "Could not get zfs volume size\n";
465 sub volume_snapshot
{
466 my ($class, $scfg, $storeid, $volname, $snap) = @_;
468 my $vname = ($class->parse_volname($volname))[1];
470 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$vname\@$snap");
473 sub volume_snapshot_delete
{
474 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
476 my $vname = ($class->parse_volname($volname))[1];
478 $class->deactivate_volume($storeid, $scfg, $vname, $snap, {});
479 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$vname\@$snap");
482 sub volume_snapshot_rollback
{
483 my ($class, $scfg, $storeid, $volname, $snap) = @_;
485 my $vname = ($class->parse_volname($volname))[1];
487 $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$vname\@$snap");
490 sub volume_rollback_is_possible
{
491 my ($class, $scfg, $storeid, $volname, $snap) = @_;
493 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
494 if ($snap ne $recentsnap) {
495 die "can't rollback, more recent snapshots exist\n";
501 sub volume_snapshot_list
{
502 my ($class, $scfg, $storeid, $volname) = @_;
504 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
506 my $zpath = "$scfg->{pool}/$name";
510 my $cmd = ['zfs', 'list', '-r', '-H', '-S', 'name', '-t', 'snap', '-o',
516 if ($line =~ m/^\Q$zpath\E@(.*)$/) {
521 eval { run_command
( [$cmd], outfunc
=> $outfunc , errfunc
=> sub{}); };
523 # return an empty array if dataset does not exist.
527 sub activate_storage
{
528 my ($class, $storeid, $scfg, $cache) = @_;
530 # Note: $scfg->{pool} can include dataset <pool>/<dataset>
531 my $pool = $scfg->{pool
};
534 my $pool_imported = sub {
535 my @param = ('-o', 'name', '-H', "$pool");
536 my $res = eval { $class->zfs_request($scfg, undef, 'zpool_list', @param) };
541 return defined($res) && $res =~ m/$pool/;
544 if (!$pool_imported->()) {
545 # import can only be done if not yet imported!
546 my @param = ('-d', '/dev/disk/by-id/', '-o', 'cachefile=none', "$pool");
547 eval { $class->zfs_request($scfg, undef, 'zpool_import', @param) };
549 # just could've raced with another import, so recheck if it is imported
550 die "could not activate storage '$storeid', $@\n" if !$pool_imported->();
556 sub deactivate_storage
{
557 my ($class, $storeid, $scfg, $cache) = @_;
561 sub activate_volume
{
562 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
564 return 1 if defined($snapname);
566 my (undef, undef, undef, undef, undef, undef, $format) = $class->parse_volname($volname);
568 return 1 if $format ne 'raw';
570 $class->zfs_wait_for_zvol_link($scfg, $volname);
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->zfs_find_free_diskname($storeid, $scfg, $vmid, $format);
592 if ($format eq 'subvol') {
593 my $size = $class->zfs_request($scfg, undef, 'list', '-H', '-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 $class->zfs_request($scfg, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$vname");
645 sub storage_can_replicate
{
646 my ($class, $scfg, $storeid, $format) = @_;
648 return 1 if $format eq 'raw' || $format eq 'subvol';
653 sub volume_has_feature
{
654 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
657 snapshot
=> { current
=> 1, snap
=> 1},
658 clone
=> { base
=> 1},
659 template
=> { current
=> 1},
660 copy
=> { base
=> 1, current
=> 1},
661 sparseinit
=> { base
=> 1, current
=> 1},
662 replicate
=> { base
=> 1, current
=> 1},
665 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
666 $class->parse_volname($volname);
673 $key = $isBase ?
'base' : 'current';
676 return 1 if $features->{$feature}->{$key};
682 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
684 die "unsupported export stream format for $class: $format\n"
687 die "$class storage can only export snapshots\n"
688 if !defined($snapshot);
690 my $dataset = ($class->parse_volname($volname))[1];
692 my $fd = fileno($fh);
693 die "internal error: invalid file handle for volume_export\n"
697 # For zfs we always create a replication stream (-R) which means the remote
698 # side will always delete non-existing source snapshots. This should work
699 # for all our use cases.
700 my $cmd = ['zfs', 'send', '-Rpv'];
701 if (defined($base_snapshot)) {
702 my $arg = $with_snapshots ?
'-I' : '-i';
703 push @$cmd, $arg, $base_snapshot;
705 push @$cmd, '--', "$scfg->{pool}/$dataset\@$snapshot";
707 run_command
($cmd, output
=> $fd);
712 sub volume_export_formats
{
713 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
715 my @formats = ('zfs');
717 # push @formats, 'fies' if $volname !~ /^(?:basevol|subvol)-/;
718 # push @formats, 'raw' if !$base_snapshot && !$with_snapshots;
723 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_;
725 die "unsupported import stream format for $class: $format\n"
728 my $fd = fileno($fh);
729 die "internal error: invalid file handle for volume_import\n"
732 my $dataset = ($class->parse_volname($volname))[1];
733 my $zfspath = "$scfg->{pool}/$dataset";
734 my $suffix = defined($base_snapshot) ?
"\@$base_snapshot" : '';
735 my $exists = 0 == run_command
(['zfs', 'get', '-H', 'name', $zfspath.$suffix],
736 noerr
=> 1, errfunc
=> sub {});
737 if (defined($base_snapshot)) {
738 die "base snapshot '$zfspath\@$base_snapshot' doesn't exist\n" if !$exists;
740 die "volume '$zfspath' already exists\n" if $exists;
743 eval { run_command
(['zfs', 'recv', '-F', '--', $zfspath], input
=> "<&$fd") };
745 if (defined($base_snapshot)) {
746 eval { run_command
(['zfs', 'rollback', '-r', '--', "$zfspath\@$base_snapshot"]) };
748 eval { run_command
(['zfs', 'destroy', '-r', '--', $zfspath]) };
756 sub volume_import_formats
{
757 my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
759 return $class->volume_export_formats($scfg, $storeid, $volname, undef, $base_snapshot, $with_snapshots);