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
57 sub zfs_parse_zvol_list
{
62 return $list if !$text;
64 my @lines = split /\n/, $text;
65 foreach my $line (@lines) {
66 my ($dataset, $size, $origin, $type, $refquota) = split(/\s+/, $line);
67 next if !($type eq 'volume' || $type eq 'filesystem');
70 my @parts = split /\//, $dataset;
71 next if scalar(@parts) < 2; # we need pool/name
72 my $name = pop @parts;
73 my $pool = join('/', @parts);
75 next unless $name =~ m!^(vm|base|subvol|basevol)-(\d+)-(\S+)$!;
78 $zvol->{pool
} = $pool;
79 $zvol->{name
} = $name;
80 if ($type eq 'filesystem') {
81 if ($refquota eq 'none') {
84 $zvol->{size
} = $refquota + 0;
86 $zvol->{format
} = 'subvol';
88 $zvol->{size
} = $size + 0;
89 $zvol->{format
} = 'raw';
91 if ($origin !~ /^-$/) {
92 $zvol->{origin
} = $origin;
101 my ($class, $volname) = @_;
103 if ($volname =~ m/^(((base|basevol)-(\d+)-\S+)\/)?
((base
|basevol
|vm
|subvol
)-(\d
+)-\S
+)$/) {
104 my $format = ($6 eq 'subvol' || $6 eq 'basevol') ?
'subvol' : 'raw';
105 my $isBase = ($6 eq 'base' || $6 eq 'basevol');
106 return ('images', $5, $7, $2, $4, $isBase, $format);
109 die "unable to parse zfs volume name '$volname'\n";
112 # virtual zfs methods (subclass can overwrite them)
115 my ($class, $storeid, $scfg, %param) = @_;
117 my $cfg_mountpoint = $scfg->{mountpoint
};
119 # ignore failure, pool might currently not be imported
122 my $res = $class->zfs_get_properties($scfg, 'mountpoint', $scfg->{pool
}, 1);
123 $mountpoint = PVE
::Storage
::Plugin
::verify_path
($res, 1) if defined($res);
126 if (defined($cfg_mountpoint)) {
127 if (defined($mountpoint) && !($cfg_mountpoint =~ m
|^\Q
$mountpoint\E/?$|)) {
128 warn "warning for $storeid - mountpoint: $cfg_mountpoint " .
129 "does not match current mount point: $mountpoint\n";
132 $scfg->{mountpoint
} = $mountpoint;
137 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
139 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
142 my $mountpoint = $scfg->{mountpoint
} // "/$scfg->{pool}";
144 if ($vtype eq "images") {
145 if ($name =~ m/^subvol-/ || $name =~ m/^basevol-/) {
146 $path = "$mountpoint/$name";
148 $path = "/dev/zvol/$scfg->{pool}/$name";
150 $path .= "\@$snapname" if defined($snapname);
152 die "$vtype is not allowed in ZFSPool!";
155 return ($path, $vmid, $vtype);
159 my ($class, $scfg, $timeout, $method, @params) = @_;
163 if ($method eq 'zpool_list') {
164 push @$cmd, 'zpool', 'list';
165 } elsif ($method eq 'zpool_import') {
166 push @$cmd, 'zpool', 'import';
167 $timeout = 15 if !$timeout || $timeout < 15;
169 push @$cmd, 'zfs', $method;
174 my $output = sub { $msg .= "$_[0]\n" };
176 $timeout = PVE
::RPCEnvironment-
>is_worker() ?
60*60 : 5 if !$timeout;
178 run_command
($cmd, errmsg
=> "zfs error", outfunc
=> $output, timeout
=> $timeout);
183 sub zfs_wait_for_zvol_link
{
184 my ($class, $scfg, $volname, $timeout) = @_;
186 my $default_timeout = PVE
::RPCEnvironment-
>is_worker() ?
60*5 : 10;
187 $timeout = $default_timeout if !defined($timeout);
189 my ($devname, undef, undef) = $class->path($scfg, $volname);
191 for (my $i = 1; $i <= $timeout; $i++) {
193 die "timeout: no zvol device link for '$volname' found after $timeout sec found.\n"
201 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
207 die "illegal name '$volname' - should be 'vm-$vmid-*'\n"
208 if $volname && $volname !~ m/^vm-$vmid-/;
209 $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt)
212 $class->zfs_create_zvol($scfg, $volname, $size);
213 $class->zfs_wait_for_zvol_link($scfg, $volname);
215 } elsif ( $fmt eq 'subvol') {
217 die "illegal name '$volname' - should be 'subvol-$vmid-*'\n"
218 if $volname && $volname !~ m/^subvol-$vmid-/;
219 $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt)
222 die "illegal name '$volname' - should be 'subvol-$vmid-*'\n"
223 if $volname !~ m/^subvol-$vmid-/;
225 $class->zfs_create_subvol($scfg, $volname, $size);
228 die "unsupported format '$fmt'";
235 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
237 my (undef, $name, undef) = $class->parse_volname($volname);
239 $class->zfs_delete_zvol($scfg, $name);
245 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
247 $cache->{zfs
} = $class->zfs_list_zvol($scfg) if !$cache->{zfs
};
248 my $zfspool = $scfg->{pool
};
251 if (my $dat = $cache->{zfs
}->{$zfspool}) {
253 foreach my $image (keys %$dat) {
255 my $info = $dat->{$image};
257 my $volname = $info->{name
};
258 my $parent = $info->{parent
};
259 my $owner = $info->{vmid
};
261 if ($parent && $parent =~ m/^(\S+)\@__base__$/) {
262 my ($basename) = ($1);
263 $info->{volid
} = "$storeid:$basename/$volname";
265 $info->{volid
} = "$storeid:$volname";
269 my $found = grep { $_ eq $info->{volid
} } @$vollist;
272 next if defined ($vmid) && ($owner ne $vmid);
281 sub zfs_get_properties
{
282 my ($class, $scfg, $properties, $dataset, $timeout) = @_;
284 my $result = $class->zfs_request($scfg, $timeout, 'get', '-o', 'value',
285 '-Hp', $properties, $dataset);
286 my @values = split /\n/, $result;
287 return wantarray ?
@values : $values[0];
290 sub zfs_get_pool_stats
{
291 my ($class, $scfg) = @_;
296 my @lines = $class->zfs_get_properties($scfg, 'available,used', $scfg->{pool
});
298 if($lines[0] =~ /^(\d+)$/) {
302 if($lines[1] =~ /^(\d+)$/) {
306 return ($available, $used);
309 sub zfs_create_zvol
{
310 my ($class, $scfg, $zvol, $size) = @_;
312 # always align size to 1M as workaround until
313 # https://github.com/zfsonlinux/zfs/issues/8541 is solved
314 my $padding = (1024 - $size % 1024) % 1024;
315 $size = $size + $padding;
317 my $cmd = ['create'];
319 push @$cmd, '-s' if $scfg->{sparse
};
321 push @$cmd, '-b', $scfg->{blocksize
} if $scfg->{blocksize
};
323 push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol";
325 $class->zfs_request($scfg, undef, @$cmd);
328 sub zfs_create_subvol
{
329 my ($class, $scfg, $volname, $size) = @_;
331 my $dataset = "$scfg->{pool}/$volname";
333 my $cmd = ['create', '-o', 'acltype=posixacl', '-o', 'xattr=sa',
334 '-o', "refquota=${size}k", $dataset];
336 $class->zfs_request($scfg, undef, @$cmd);
339 sub zfs_delete_zvol
{
340 my ($class, $scfg, $zvol) = @_;
344 for (my $i = 0; $i < 6; $i++) {
346 eval { $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol"); };
348 if ($err =~ m/^zfs error:(.*): dataset is busy.*/) {
350 } elsif ($err =~ m/^zfs error:.*: dataset does not exist.*$/) {
365 my ($class, $scfg) = @_;
367 my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hrp');
368 my $zvols = zfs_parse_zvol_list
($text);
369 return undef if !$zvols;
372 foreach my $zvol (@$zvols) {
373 my $pool = $zvol->{pool
};
374 my $name = $zvol->{name
};
375 my $parent = $zvol->{origin
};
376 if($zvol->{origin
} && $zvol->{origin
} =~ m/^$scfg->{pool}\/(\S
+)$/){
380 $list->{$pool}->{$name} = {
382 size
=> $zvol->{size
},
384 format
=> $zvol->{format
},
385 vmid
=> $zvol->{owner
},
392 sub zfs_get_latest_snapshot
{
393 my ($class, $scfg, $volname) = @_;
395 my $vname = ($class->parse_volname($volname))[1];
397 # abort rollback if snapshot is not the latest
398 my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
399 my $text = $class->zfs_request($scfg, undef, 'list', @params);
400 my @snapshots = split(/\n/, $text);
403 foreach (@snapshots) {
404 if (/$scfg->{pool}\/$vname/) {
414 my ($class, $storeid, $scfg, $cache) = @_;
422 ($free, $used) = $class->zfs_get_pool_stats($scfg);
424 $total = $free + $used;
428 return ($total, $free, $used, $active);
431 sub volume_size_info
{
432 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
434 my (undef, $vname, undef, undef, undef, undef, $format) =
435 $class->parse_volname($volname);
437 my $attr = $format eq 'subvol' ?
'refquota' : 'volsize';
438 my $value = $class->zfs_get_properties($scfg, $attr, "$scfg->{pool}/$vname");
439 if ($value =~ /^(\d+)$/) {
443 die "Could not get zfs volume size\n";
446 sub volume_snapshot
{
447 my ($class, $scfg, $storeid, $volname, $snap) = @_;
449 my $vname = ($class->parse_volname($volname))[1];
451 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$vname\@$snap");
454 sub volume_snapshot_delete
{
455 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
457 my $vname = ($class->parse_volname($volname))[1];
459 $class->deactivate_volume($storeid, $scfg, $vname, $snap, {});
460 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$vname\@$snap");
463 sub volume_snapshot_rollback
{
464 my ($class, $scfg, $storeid, $volname, $snap) = @_;
466 my $vname = ($class->parse_volname($volname))[1];
468 $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$vname\@$snap");
471 sub volume_rollback_is_possible
{
472 my ($class, $scfg, $storeid, $volname, $snap) = @_;
474 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
475 if ($snap ne $recentsnap) {
476 die "can't rollback, more recent snapshots exist\n";
482 sub volume_snapshot_list
{
483 my ($class, $scfg, $storeid, $volname) = @_;
485 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
487 my $zpath = "$scfg->{pool}/$name";
491 my $cmd = ['zfs', 'list', '-r', '-H', '-S', 'name', '-t', 'snap', '-o',
497 if ($line =~ m/^\Q$zpath\E@(.*)$/) {
502 eval { run_command
( [$cmd], outfunc
=> $outfunc , errfunc
=> sub{}); };
504 # return an empty array if dataset does not exist.
508 sub activate_storage
{
509 my ($class, $storeid, $scfg, $cache) = @_;
511 # Note: $scfg->{pool} can include dataset <pool>/<dataset>
512 my $pool = $scfg->{pool
};
515 my $pool_imported = sub {
516 my @param = ('-o', 'name', '-H', "$pool");
517 my $res = eval { $class->zfs_request($scfg, undef, 'zpool_list', @param) };
522 return defined($res) && $res =~ m/$pool/;
525 if (!$pool_imported->()) {
526 # import can only be done if not yet imported!
527 my @param = ('-d', '/dev/disk/by-id/', '-o', 'cachefile=none', "$pool");
528 eval { $class->zfs_request($scfg, undef, 'zpool_import', @param) };
530 # just could've raced with another import, so recheck if it is imported
531 die "could not activate storage '$storeid', $@\n" if !$pool_imported->();
537 sub deactivate_storage
{
538 my ($class, $storeid, $scfg, $cache) = @_;
542 sub activate_volume
{
543 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
545 return 1 if defined($snapname);
547 my (undef, undef, undef, undef, undef, undef, $format) = $class->parse_volname($volname);
549 return 1 if $format ne 'raw';
551 $class->zfs_wait_for_zvol_link($scfg, $volname);
556 sub deactivate_volume
{
557 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
562 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
564 $snap ||= '__base__';
566 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
567 $class->parse_volname($volname);
569 die "clone_image only works on base images\n" if !$isBase;
571 my $name = $class->find_free_diskname($storeid, $scfg, $vmid, $format);
573 if ($format eq 'subvol') {
574 my $size = $class->zfs_request($scfg, undef, 'list', '-H', '-o', 'refquota', "$scfg->{pool}/$basename");
576 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name", '-o', "refquota=$size");
578 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
581 return "$basename/$name";
585 my ($class, $storeid, $scfg, $volname) = @_;
587 my $snap = '__base__';
589 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
590 $class->parse_volname($volname);
592 die "create_base not possible with base image\n" if $isBase;
595 if ( $format eq 'subvol' ) {
596 $newname =~ s/^subvol-/basevol-/;
598 $newname =~ s/^vm-/base-/;
600 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
602 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
604 my $running = undef; #fixme : is create_base always offline ?
606 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
612 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
614 my $new_size = int($size/1024);
616 my (undef, $vname, undef, undef, undef, undef, $format) =
617 $class->parse_volname($volname);
619 my $attr = $format eq 'subvol' ?
'refquota' : 'volsize';
621 # align size to 1M so we always have a valid multiple of the volume block size
622 if ($format eq 'raw') {
623 my $padding = (1024 - $new_size % 1024) % 1024;
624 $new_size = $new_size + $padding;
627 $class->zfs_request($scfg, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$vname");
632 sub storage_can_replicate
{
633 my ($class, $scfg, $storeid, $format) = @_;
635 return 1 if $format eq 'raw' || $format eq 'subvol';
640 sub volume_has_feature
{
641 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
644 snapshot
=> { current
=> 1, snap
=> 1},
645 clone
=> { base
=> 1},
646 template
=> { current
=> 1},
647 copy
=> { base
=> 1, current
=> 1},
648 sparseinit
=> { base
=> 1, current
=> 1},
649 replicate
=> { base
=> 1, current
=> 1},
652 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
653 $class->parse_volname($volname);
660 $key = $isBase ?
'base' : 'current';
663 return 1 if $features->{$feature}->{$key};
669 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
671 die "unsupported export stream format for $class: $format\n"
674 die "$class storage can only export snapshots\n"
675 if !defined($snapshot);
677 my $dataset = ($class->parse_volname($volname))[1];
679 my $fd = fileno($fh);
680 die "internal error: invalid file handle for volume_export\n"
684 # For zfs we always create a replication stream (-R) which means the remote
685 # side will always delete non-existing source snapshots. This should work
686 # for all our use cases.
687 my $cmd = ['zfs', 'send', '-Rpv'];
688 if (defined($base_snapshot)) {
689 my $arg = $with_snapshots ?
'-I' : '-i';
690 push @$cmd, $arg, $base_snapshot;
692 push @$cmd, '--', "$scfg->{pool}/$dataset\@$snapshot";
694 run_command
($cmd, output
=> $fd);
699 sub volume_export_formats
{
700 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
702 my @formats = ('zfs');
704 # push @formats, 'fies' if $volname !~ /^(?:basevol|subvol)-/;
705 # push @formats, 'raw' if !$base_snapshot && !$with_snapshots;
710 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots, $allow_rename) = @_;
712 die "unsupported import stream format for $class: $format\n"
715 my $fd = fileno($fh);
716 die "internal error: invalid file handle for volume_import\n"
719 my (undef, $dataset, $vmid) = $class->parse_volname($volname);
720 my $zfspath = "$scfg->{pool}/$dataset";
721 my $suffix = defined($base_snapshot) ?
"\@$base_snapshot" : '';
722 my $exists = 0 == run_command
(['zfs', 'get', '-H', 'name', $zfspath.$suffix],
723 noerr
=> 1, errfunc
=> sub {});
724 if (defined($base_snapshot)) {
725 die "base snapshot '$zfspath\@$base_snapshot' doesn't exist\n" if !$exists;
727 die "volume '$zfspath' already exists\n" if !$allow_rename;
728 warn "volume '$zfspath' already exists - importing with a different name\n";
729 $dataset = $class->find_free_diskname($storeid, $scfg, $vmid, $format);
730 $zfspath = "$scfg->{pool}/$dataset";
733 eval { run_command
(['zfs', 'recv', '-F', '--', $zfspath], input
=> "<&$fd") };
735 if (defined($base_snapshot)) {
736 eval { run_command
(['zfs', 'rollback', '-r', '--', "$zfspath\@$base_snapshot"]) };
738 eval { run_command
(['zfs', 'destroy', '-r', '--', $zfspath]) };
743 return "$storeid:$dataset";
746 sub volume_import_formats
{
747 my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
749 return $class->volume_export_formats($scfg, $storeid, $volname, undef, $base_snapshot, $with_snapshots);