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;
139 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
141 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
144 my $mountpoint = $scfg->{mountpoint
} // "/$scfg->{pool}";
146 if ($vtype eq "images") {
147 if ($name =~ m/^subvol-/ || $name =~ m/^basevol-/) {
148 $path = "$mountpoint/$name";
150 $path = "/dev/zvol/$scfg->{pool}/$name";
152 $path .= "\@$snapname" if defined($snapname);
154 die "$vtype is not allowed in ZFSPool!";
157 return ($path, $vmid, $vtype);
161 my ($class, $scfg, $timeout, $method, @params) = @_;
165 if ($method eq 'zpool_list') {
166 push @$cmd, 'zpool', 'list';
167 } elsif ($method eq 'zpool_import') {
168 push @$cmd, 'zpool', 'import';
169 $timeout = 15 if !$timeout || $timeout < 15;
171 push @$cmd, 'zfs', $method;
176 my $output = sub { $msg .= "$_[0]\n" };
178 $timeout = PVE
::RPCEnvironment-
>is_worker() ?
60*60 : 5 if !$timeout;
180 run_command
($cmd, errmsg
=> "zfs error", outfunc
=> $output, timeout
=> $timeout);
185 sub zfs_wait_for_zvol_link
{
186 my ($class, $scfg, $volname, $timeout) = @_;
188 my $default_timeout = PVE
::RPCEnvironment-
>is_worker() ?
60*5 : 10;
189 $timeout = $default_timeout if !defined($timeout);
191 my ($devname, undef, undef) = $class->path($scfg, $volname);
193 for (my $i = 1; $i <= $timeout; $i++) {
195 die "timeout: no zvol device link for '$volname' found after $timeout sec found.\n"
203 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
209 die "illegal name '$volname' - should be 'vm-$vmid-*'\n"
210 if $volname && $volname !~ m/^vm-$vmid-/;
211 $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt)
214 $class->zfs_create_zvol($scfg, $volname, $size);
215 $class->zfs_wait_for_zvol_link($scfg, $volname);
217 } elsif ( $fmt eq 'subvol') {
219 die "illegal name '$volname' - should be 'subvol-$vmid-*'\n"
220 if $volname && $volname !~ m/^subvol-$vmid-/;
221 $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt)
224 die "illegal name '$volname' - should be 'subvol-$vmid-*'\n"
225 if $volname !~ m/^subvol-$vmid-/;
227 $class->zfs_create_subvol($scfg, $volname, $size);
230 die "unsupported format '$fmt'";
237 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
239 my (undef, $name, undef) = $class->parse_volname($volname);
241 $class->zfs_delete_zvol($scfg, $name);
247 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
249 $cache->{zfs
} = $class->zfs_list_zvol($scfg) if !$cache->{zfs
};
250 my $zfspool = $scfg->{pool
};
253 if (my $dat = $cache->{zfs
}->{$zfspool}) {
255 foreach my $image (keys %$dat) {
257 my $info = $dat->{$image};
259 my $volname = $info->{name
};
260 my $parent = $info->{parent
};
261 my $owner = $info->{vmid
};
263 if ($parent && $parent =~ m/^(\S+)\@__base__$/) {
264 my ($basename) = ($1);
265 $info->{volid
} = "$storeid:$basename/$volname";
267 $info->{volid
} = "$storeid:$volname";
271 my $found = grep { $_ eq $info->{volid
} } @$vollist;
274 next if defined ($vmid) && ($owner ne $vmid);
283 sub zfs_get_properties
{
284 my ($class, $scfg, $properties, $dataset, $timeout) = @_;
286 my $result = $class->zfs_request($scfg, $timeout, 'get', '-o', 'value',
287 '-Hp', $properties, $dataset);
288 my @values = split /\n/, $result;
289 return wantarray ?
@values : $values[0];
292 sub zfs_get_pool_stats
{
293 my ($class, $scfg) = @_;
298 my @lines = $class->zfs_get_properties($scfg, 'available,used', $scfg->{pool
});
300 if($lines[0] =~ /^(\d+)$/) {
304 if($lines[1] =~ /^(\d+)$/) {
308 return ($available, $used);
311 sub zfs_create_zvol
{
312 my ($class, $scfg, $zvol, $size) = @_;
314 # always align size to 1M as workaround until
315 # https://github.com/zfsonlinux/zfs/issues/8541 is solved
316 my $padding = (1024 - $size % 1024) % 1024;
317 $size = $size + $padding;
319 my $cmd = ['create'];
321 push @$cmd, '-s' if $scfg->{sparse
};
323 push @$cmd, '-b', $scfg->{blocksize
} if $scfg->{blocksize
};
325 push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol";
327 $class->zfs_request($scfg, undef, @$cmd);
330 sub zfs_create_subvol
{
331 my ($class, $scfg, $volname, $size) = @_;
333 my $dataset = "$scfg->{pool}/$volname";
335 my $cmd = ['create', '-o', 'acltype=posixacl', '-o', 'xattr=sa',
336 '-o', "refquota=${size}k", $dataset];
338 $class->zfs_request($scfg, undef, @$cmd);
341 sub zfs_delete_zvol
{
342 my ($class, $scfg, $zvol) = @_;
346 for (my $i = 0; $i < 6; $i++) {
348 eval { $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol"); };
350 if ($err =~ m/^zfs error:(.*): dataset is busy.*/) {
352 } elsif ($err =~ m/^zfs error:.*: dataset does not exist.*$/) {
367 my ($class, $scfg) = @_;
369 my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hrp');
370 my $zvols = zfs_parse_zvol_list
($text);
371 return undef if !$zvols;
374 foreach my $zvol (@$zvols) {
375 my $pool = $zvol->{pool
};
376 my $name = $zvol->{name
};
377 my $parent = $zvol->{origin
};
378 if($zvol->{origin
} && $zvol->{origin
} =~ m/^$scfg->{pool}\/(\S
+)$/){
382 $list->{$pool}->{$name} = {
384 size
=> $zvol->{size
},
386 format
=> $zvol->{format
},
387 vmid
=> $zvol->{owner
},
394 sub zfs_get_latest_snapshot
{
395 my ($class, $scfg, $volname) = @_;
397 my $vname = ($class->parse_volname($volname))[1];
399 # abort rollback if snapshot is not the latest
400 my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
401 my $text = $class->zfs_request($scfg, undef, 'list', @params);
402 my @snapshots = split(/\n/, $text);
405 foreach (@snapshots) {
406 if (/$scfg->{pool}\/$vname/) {
416 my ($class, $storeid, $scfg, $cache) = @_;
424 ($free, $used) = $class->zfs_get_pool_stats($scfg);
426 $total = $free + $used;
430 return ($total, $free, $used, $active);
433 sub volume_size_info
{
434 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
436 my (undef, $vname, undef, undef, undef, undef, $format) =
437 $class->parse_volname($volname);
439 my $attr = $format eq 'subvol' ?
'refquota' : 'volsize';
440 my $value = $class->zfs_get_properties($scfg, $attr, "$scfg->{pool}/$vname");
441 if ($value =~ /^(\d+)$/) {
445 die "Could not get zfs volume size\n";
448 sub volume_snapshot
{
449 my ($class, $scfg, $storeid, $volname, $snap) = @_;
451 my $vname = ($class->parse_volname($volname))[1];
453 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$vname\@$snap");
456 sub volume_snapshot_delete
{
457 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
459 my $vname = ($class->parse_volname($volname))[1];
461 $class->deactivate_volume($storeid, $scfg, $vname, $snap, {});
462 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$vname\@$snap");
465 sub volume_snapshot_rollback
{
466 my ($class, $scfg, $storeid, $volname, $snap) = @_;
468 my $vname = ($class->parse_volname($volname))[1];
470 $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$vname\@$snap");
473 sub volume_rollback_is_possible
{
474 my ($class, $scfg, $storeid, $volname, $snap) = @_;
476 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
478 die "can't rollback, no snapshots exist at all\n"
479 if !defined($recentsnap);
481 die "can't rollback, '$snap' is not most recent snapshot\n"
482 if $snap ne $recentsnap;
487 sub volume_snapshot_list
{
488 my ($class, $scfg, $storeid, $volname) = @_;
490 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
492 my $zpath = "$scfg->{pool}/$name";
496 my $cmd = ['zfs', 'list', '-r', '-H', '-S', 'name', '-t', 'snap', '-o',
502 if ($line =~ m/^\Q$zpath\E@(.*)$/) {
507 eval { run_command
( [$cmd], outfunc
=> $outfunc , errfunc
=> sub{}); };
509 # return an empty array if dataset does not exist.
513 sub activate_storage
{
514 my ($class, $storeid, $scfg, $cache) = @_;
516 # Note: $scfg->{pool} can include dataset <pool>/<dataset>
517 my $pool = $scfg->{pool
};
520 my $pool_imported = sub {
521 my @param = ('-o', 'name', '-H', "$pool");
522 my $res = eval { $class->zfs_request($scfg, undef, 'zpool_list', @param) };
527 return defined($res) && $res =~ m/$pool/;
530 if (!$pool_imported->()) {
531 # import can only be done if not yet imported!
532 my @param = ('-d', '/dev/disk/by-id/', '-o', 'cachefile=none', "$pool");
533 eval { $class->zfs_request($scfg, undef, 'zpool_import', @param) };
535 # just could've raced with another import, so recheck if it is imported
536 die "could not activate storage '$storeid', $@\n" if !$pool_imported->();
542 sub deactivate_storage
{
543 my ($class, $storeid, $scfg, $cache) = @_;
547 sub activate_volume
{
548 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
550 return 1 if defined($snapname);
552 my (undef, undef, undef, undef, undef, undef, $format) = $class->parse_volname($volname);
554 if ($format eq 'raw') {
555 $class->zfs_wait_for_zvol_link($scfg, $volname);
556 } elsif ($format eq 'subvol') {
557 my $mounted = $class->zfs_get_properties($scfg, 'mounted', "$scfg->{pool}/$volname");
558 if ($mounted !~ m/^yes$/) {
559 $class->zfs_request($scfg, undef, 'mount', "$scfg->{pool}/$volname");
566 sub deactivate_volume
{
567 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
572 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
574 $snap ||= '__base__';
576 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
577 $class->parse_volname($volname);
579 die "clone_image only works on base images\n" if !$isBase;
581 my $name = $class->find_free_diskname($storeid, $scfg, $vmid, $format);
583 if ($format eq 'subvol') {
584 my $size = $class->zfs_request($scfg, undef, 'list', '-Hp', '-o', 'refquota', "$scfg->{pool}/$basename");
586 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name", '-o', "refquota=$size");
588 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
591 return "$basename/$name";
595 my ($class, $storeid, $scfg, $volname) = @_;
597 my $snap = '__base__';
599 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
600 $class->parse_volname($volname);
602 die "create_base not possible with base image\n" if $isBase;
605 if ( $format eq 'subvol' ) {
606 $newname =~ s/^subvol-/basevol-/;
608 $newname =~ s/^vm-/base-/;
610 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
612 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
614 my $running = undef; #fixme : is create_base always offline ?
616 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
622 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
624 my $new_size = int($size/1024);
626 my (undef, $vname, undef, undef, undef, undef, $format) =
627 $class->parse_volname($volname);
629 my $attr = $format eq 'subvol' ?
'refquota' : 'volsize';
631 # align size to 1M so we always have a valid multiple of the volume block size
632 if ($format eq 'raw') {
633 my $padding = (1024 - $new_size % 1024) % 1024;
634 $new_size = $new_size + $padding;
637 $class->zfs_request($scfg, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$vname");
642 sub storage_can_replicate
{
643 my ($class, $scfg, $storeid, $format) = @_;
645 return 1 if $format eq 'raw' || $format eq 'subvol';
650 sub volume_has_feature
{
651 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
654 snapshot
=> { current
=> 1, snap
=> 1},
655 clone
=> { base
=> 1},
656 template
=> { current
=> 1},
657 copy
=> { base
=> 1, current
=> 1},
658 sparseinit
=> { base
=> 1, current
=> 1},
659 replicate
=> { base
=> 1, current
=> 1},
662 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
663 $class->parse_volname($volname);
670 $key = $isBase ?
'base' : 'current';
673 return 1 if $features->{$feature}->{$key};
679 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
681 die "unsupported export stream format for $class: $format\n"
684 die "$class storage can only export snapshots\n"
685 if !defined($snapshot);
687 my $dataset = ($class->parse_volname($volname))[1];
689 my $fd = fileno($fh);
690 die "internal error: invalid file handle for volume_export\n"
694 # For zfs we always create a replication stream (-R) which means the remote
695 # side will always delete non-existing source snapshots. This should work
696 # for all our use cases.
697 my $cmd = ['zfs', 'send', '-Rpv'];
698 if (defined($base_snapshot)) {
699 my $arg = $with_snapshots ?
'-I' : '-i';
700 push @$cmd, $arg, $base_snapshot;
702 push @$cmd, '--', "$scfg->{pool}/$dataset\@$snapshot";
704 run_command
($cmd, output
=> $fd);
709 sub volume_export_formats
{
710 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
712 my @formats = ('zfs');
714 # push @formats, 'fies' if $volname !~ /^(?:basevol|subvol)-/;
715 # push @formats, 'raw' if !$base_snapshot && !$with_snapshots;
720 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots, $allow_rename) = @_;
722 die "unsupported import stream format for $class: $format\n"
725 my $fd = fileno($fh);
726 die "internal error: invalid file handle for volume_import\n"
729 my (undef, $dataset, $vmid) = $class->parse_volname($volname);
730 my $zfspath = "$scfg->{pool}/$dataset";
731 my $suffix = defined($base_snapshot) ?
"\@$base_snapshot" : '';
732 my $exists = 0 == run_command
(['zfs', 'get', '-H', 'name', $zfspath.$suffix],
733 noerr
=> 1, errfunc
=> sub {});
734 if (defined($base_snapshot)) {
735 die "base snapshot '$zfspath\@$base_snapshot' doesn't exist\n" if !$exists;
737 die "volume '$zfspath' already exists\n" if !$allow_rename;
738 warn "volume '$zfspath' already exists - importing with a different name\n";
739 $dataset = $class->find_free_diskname($storeid, $scfg, $vmid, $format);
740 $zfspath = "$scfg->{pool}/$dataset";
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]) };
753 return "$storeid:$dataset";
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);