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
158 my $res = $class->zfs_get_properties($scfg, 'mountpoint', $scfg->{pool
}, 1);
159 $mountpoint = PVE
::Storage
::Plugin
::verify_path
($res, 1) if defined($res);
162 if (defined($cfg_mountpoint)) {
163 if (defined($mountpoint) && !($cfg_mountpoint =~ m
|^\Q
$mountpoint\E/?$|)) {
164 warn "warning for $storeid - mountpoint: $cfg_mountpoint " .
165 "does not match current mount point: $mountpoint\n";
168 $scfg->{mountpoint
} = $mountpoint;
173 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
175 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
178 my $mountpoint = $scfg->{mountpoint
} // "/$scfg->{pool}";
180 if ($vtype eq "images") {
181 if ($name =~ m/^subvol-/ || $name =~ m/^basevol-/) {
182 $path = "$mountpoint/$name";
184 $path = "/dev/zvol/$scfg->{pool}/$name";
186 $path .= "\@$snapname" if defined($snapname);
188 die "$vtype is not allowed in ZFSPool!";
191 return ($path, $vmid, $vtype);
195 my ($class, $scfg, $timeout, $method, @params) = @_;
199 if ($method eq 'zpool_list') {
200 push @$cmd, 'zpool', 'list';
201 } elsif ($method eq 'zpool_import') {
202 push @$cmd, 'zpool', 'import';
203 $timeout = 15 if !$timeout || $timeout < 15;
205 push @$cmd, 'zfs', $method;
210 my $output = sub { $msg .= "$_[0]\n" };
212 $timeout = PVE
::RPCEnvironment-
>is_worker() ?
60*60 : 5 if !$timeout;
214 run_command
($cmd, errmsg
=> "zfs error", outfunc
=> $output, timeout
=> $timeout);
219 sub zfs_wait_for_zvol_link
{
220 my ($class, $scfg, $volname, $timeout) = @_;
222 my $default_timeout = PVE
::RPCEnvironment-
>is_worker() ?
60*5 : 10;
223 $timeout = $default_timeout if !defined($timeout);
225 my ($devname, undef, undef) = $class->path($scfg, $volname);
227 for (my $i = 1; $i <= $timeout; $i++) {
229 die "timeout: no zvol device link for '$volname' found after $timeout sec found.\n"
237 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
243 die "illegal name '$volname' - should be 'vm-$vmid-*'\n"
244 if $volname && $volname !~ m/^vm-$vmid-/;
245 $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt)
248 $class->zfs_create_zvol($scfg, $volname, $size);
249 $class->zfs_wait_for_zvol_link($scfg, $volname);
251 } elsif ( $fmt eq 'subvol') {
253 die "illegal name '$volname' - should be 'subvol-$vmid-*'\n"
254 if $volname && $volname !~ m/^subvol-$vmid-/;
255 $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt)
258 die "illegal name '$volname' - should be 'subvol-$vmid-*'\n"
259 if $volname !~ m/^subvol-$vmid-/;
261 $class->zfs_create_subvol($scfg, $volname, $size);
264 die "unsupported format '$fmt'";
271 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
273 my (undef, $name, undef) = $class->parse_volname($volname);
275 $class->zfs_delete_zvol($scfg, $name);
281 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
283 $cache->{zfs
} = $class->zfs_list_zvol($scfg) if !$cache->{zfs
};
284 my $zfspool = $scfg->{pool
};
287 if (my $dat = $cache->{zfs
}->{$zfspool}) {
289 foreach my $image (keys %$dat) {
291 my $info = $dat->{$image};
293 my $volname = $info->{name
};
294 my $parent = $info->{parent
};
295 my $owner = $info->{vmid
};
297 if ($parent && $parent =~ m/^(\S+)\@__base__$/) {
298 my ($basename) = ($1);
299 $info->{volid
} = "$storeid:$basename/$volname";
301 $info->{volid
} = "$storeid:$volname";
305 my $found = grep { $_ eq $info->{volid
} } @$vollist;
308 next if defined ($vmid) && ($owner ne $vmid);
317 sub zfs_get_properties
{
318 my ($class, $scfg, $properties, $dataset, $timeout) = @_;
320 my $result = $class->zfs_request($scfg, $timeout, 'get', '-o', 'value',
321 '-Hp', $properties, $dataset);
322 my @values = split /\n/, $result;
323 return wantarray ?
@values : $values[0];
326 sub zfs_get_pool_stats
{
327 my ($class, $scfg) = @_;
332 my @lines = $class->zfs_get_properties($scfg, 'available,used', $scfg->{pool
});
334 if($lines[0] =~ /^(\d+)$/) {
338 if($lines[1] =~ /^(\d+)$/) {
342 return ($available, $used);
345 sub zfs_create_zvol
{
346 my ($class, $scfg, $zvol, $size) = @_;
348 # always align size to 1M as workaround until
349 # https://github.com/zfsonlinux/zfs/issues/8541 is solved
350 my $padding = (1024 - $size % 1024) % 1024;
351 $size = $size + $padding;
353 my $cmd = ['create'];
355 push @$cmd, '-s' if $scfg->{sparse
};
357 push @$cmd, '-b', $scfg->{blocksize
} if $scfg->{blocksize
};
359 push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol";
361 $class->zfs_request($scfg, undef, @$cmd);
364 sub zfs_create_subvol
{
365 my ($class, $scfg, $volname, $size) = @_;
367 my $dataset = "$scfg->{pool}/$volname";
369 my $cmd = ['create', '-o', 'acltype=posixacl', '-o', 'xattr=sa',
370 '-o', "refquota=${size}k", $dataset];
372 $class->zfs_request($scfg, undef, @$cmd);
375 sub zfs_delete_zvol
{
376 my ($class, $scfg, $zvol) = @_;
380 for (my $i = 0; $i < 6; $i++) {
382 eval { $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol"); };
384 if ($err =~ m/^zfs error:(.*): dataset is busy.*/) {
386 } elsif ($err =~ m/^zfs error:.*: dataset does not exist.*$/) {
401 my ($class, $scfg) = @_;
403 my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hr');
404 my $zvols = zfs_parse_zvol_list
($text);
405 return undef if !$zvols;
408 foreach my $zvol (@$zvols) {
409 my $pool = $zvol->{pool
};
410 my $name = $zvol->{name
};
411 my $parent = $zvol->{origin
};
412 if($zvol->{origin
} && $zvol->{origin
} =~ m/^$scfg->{pool}\/(\S
+)$/){
416 $list->{$pool}->{$name} = {
418 size
=> $zvol->{size
},
420 format
=> $zvol->{format
},
421 vmid
=> $zvol->{owner
},
428 sub zfs_get_latest_snapshot
{
429 my ($class, $scfg, $volname) = @_;
431 my $vname = ($class->parse_volname($volname))[1];
433 # abort rollback if snapshot is not the latest
434 my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
435 my $text = $class->zfs_request($scfg, undef, 'list', @params);
436 my @snapshots = split(/\n/, $text);
439 foreach (@snapshots) {
440 if (/$scfg->{pool}\/$vname/) {
450 my ($class, $storeid, $scfg, $cache) = @_;
458 ($free, $used) = $class->zfs_get_pool_stats($scfg);
460 $total = $free + $used;
464 return ($total, $free, $used, $active);
467 sub volume_size_info
{
468 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
470 my (undef, $vname, undef, undef, undef, undef, $format) =
471 $class->parse_volname($volname);
473 my $attr = $format eq 'subvol' ?
'refquota' : 'volsize';
474 my $value = $class->zfs_get_properties($scfg, $attr, "$scfg->{pool}/$vname");
475 if ($value =~ /^(\d+)$/) {
479 die "Could not get zfs volume size\n";
482 sub volume_snapshot
{
483 my ($class, $scfg, $storeid, $volname, $snap) = @_;
485 my $vname = ($class->parse_volname($volname))[1];
487 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$vname\@$snap");
490 sub volume_snapshot_delete
{
491 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
493 my $vname = ($class->parse_volname($volname))[1];
495 $class->deactivate_volume($storeid, $scfg, $vname, $snap, {});
496 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$vname\@$snap");
499 sub volume_snapshot_rollback
{
500 my ($class, $scfg, $storeid, $volname, $snap) = @_;
502 my $vname = ($class->parse_volname($volname))[1];
504 $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$vname\@$snap");
507 sub volume_rollback_is_possible
{
508 my ($class, $scfg, $storeid, $volname, $snap) = @_;
510 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
511 if ($snap ne $recentsnap) {
512 die "can't rollback, more recent snapshots exist\n";
518 sub volume_snapshot_list
{
519 my ($class, $scfg, $storeid, $volname) = @_;
521 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
523 my $zpath = "$scfg->{pool}/$name";
527 my $cmd = ['zfs', 'list', '-r', '-H', '-S', 'name', '-t', 'snap', '-o',
533 if ($line =~ m/^\Q$zpath\E@(.*)$/) {
538 eval { run_command
( [$cmd], outfunc
=> $outfunc , errfunc
=> sub{}); };
540 # return an empty array if dataset does not exist.
544 sub activate_storage
{
545 my ($class, $storeid, $scfg, $cache) = @_;
547 # Note: $scfg->{pool} can include dataset <pool>/<dataset>
548 my $pool = $scfg->{pool
};
551 my $pool_imported = sub {
552 my @param = ('-o', 'name', '-H', "$pool");
553 my $res = eval { $class->zfs_request($scfg, undef, 'zpool_list', @param) };
558 return defined($res) && $res =~ m/$pool/;
561 if (!$pool_imported->()) {
562 # import can only be done if not yet imported!
563 my @param = ('-d', '/dev/disk/by-id/', '-o', 'cachefile=none', "$pool");
564 eval { $class->zfs_request($scfg, undef, 'zpool_import', @param) };
566 # just could've raced with another import, so recheck if it is imported
567 die "could not activate storage '$storeid', $@\n" if !$pool_imported->();
573 sub deactivate_storage
{
574 my ($class, $storeid, $scfg, $cache) = @_;
578 sub activate_volume
{
579 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
581 return 1 if defined($snapname);
583 my (undef, undef, undef, undef, undef, undef, $format) = $class->parse_volname($volname);
585 return 1 if $format ne 'raw';
587 $class->zfs_wait_for_zvol_link($scfg, $volname);
592 sub deactivate_volume
{
593 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
598 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
600 $snap ||= '__base__';
602 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
603 $class->parse_volname($volname);
605 die "clone_image only works on base images\n" if !$isBase;
607 my $name = $class->find_free_diskname($storeid, $scfg, $vmid, $format);
609 if ($format eq 'subvol') {
610 my $size = $class->zfs_request($scfg, undef, 'list', '-H', '-o', 'refquota', "$scfg->{pool}/$basename");
612 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name", '-o', "refquota=$size");
614 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
617 return "$basename/$name";
621 my ($class, $storeid, $scfg, $volname) = @_;
623 my $snap = '__base__';
625 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
626 $class->parse_volname($volname);
628 die "create_base not possible with base image\n" if $isBase;
631 if ( $format eq 'subvol' ) {
632 $newname =~ s/^subvol-/basevol-/;
634 $newname =~ s/^vm-/base-/;
636 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
638 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
640 my $running = undef; #fixme : is create_base always offline ?
642 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
648 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
650 my $new_size = int($size/1024);
652 my (undef, $vname, undef, undef, undef, undef, $format) =
653 $class->parse_volname($volname);
655 my $attr = $format eq 'subvol' ?
'refquota' : 'volsize';
657 # align size to 1M so we always have a valid multiple of the volume block size
658 if ($format eq 'raw') {
659 my $padding = (1024 - $new_size % 1024) % 1024;
660 $new_size = $new_size + $padding;
663 $class->zfs_request($scfg, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$vname");
668 sub storage_can_replicate
{
669 my ($class, $scfg, $storeid, $format) = @_;
671 return 1 if $format eq 'raw' || $format eq 'subvol';
676 sub volume_has_feature
{
677 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
680 snapshot
=> { current
=> 1, snap
=> 1},
681 clone
=> { base
=> 1},
682 template
=> { current
=> 1},
683 copy
=> { base
=> 1, current
=> 1},
684 sparseinit
=> { base
=> 1, current
=> 1},
685 replicate
=> { base
=> 1, current
=> 1},
688 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
689 $class->parse_volname($volname);
696 $key = $isBase ?
'base' : 'current';
699 return 1 if $features->{$feature}->{$key};
705 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
707 die "unsupported export stream format for $class: $format\n"
710 die "$class storage can only export snapshots\n"
711 if !defined($snapshot);
713 my $dataset = ($class->parse_volname($volname))[1];
715 my $fd = fileno($fh);
716 die "internal error: invalid file handle for volume_export\n"
720 # For zfs we always create a replication stream (-R) which means the remote
721 # side will always delete non-existing source snapshots. This should work
722 # for all our use cases.
723 my $cmd = ['zfs', 'send', '-Rpv'];
724 if (defined($base_snapshot)) {
725 my $arg = $with_snapshots ?
'-I' : '-i';
726 push @$cmd, $arg, $base_snapshot;
728 push @$cmd, '--', "$scfg->{pool}/$dataset\@$snapshot";
730 run_command
($cmd, output
=> $fd);
735 sub volume_export_formats
{
736 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
738 my @formats = ('zfs');
740 # push @formats, 'fies' if $volname !~ /^(?:basevol|subvol)-/;
741 # push @formats, 'raw' if !$base_snapshot && !$with_snapshots;
746 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_;
748 die "unsupported import stream format for $class: $format\n"
751 my $fd = fileno($fh);
752 die "internal error: invalid file handle for volume_import\n"
755 my $dataset = ($class->parse_volname($volname))[1];
756 my $zfspath = "$scfg->{pool}/$dataset";
757 my $suffix = defined($base_snapshot) ?
"\@$base_snapshot" : '';
758 my $exists = 0 == run_command
(['zfs', 'get', '-H', 'name', $zfspath.$suffix],
759 noerr
=> 1, errfunc
=> sub {});
760 if (defined($base_snapshot)) {
761 die "base snapshot '$zfspath\@$base_snapshot' doesn't exist\n" if !$exists;
763 die "volume '$zfspath' already exists\n" if $exists;
766 eval { run_command
(['zfs', 'recv', '-F', '--', $zfspath], input
=> "<&$fd") };
768 if (defined($base_snapshot)) {
769 eval { run_command
(['zfs', 'rollback', '-r', '--', "$zfspath\@$base_snapshot"]) };
771 eval { run_command
(['zfs', 'destroy', '-r', '--', $zfspath]) };
779 sub volume_import_formats
{
780 my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
782 return $class->volume_export_formats($scfg, $storeid, $volname, undef, $base_snapshot, $with_snapshots);