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) = @_;
170 my $default_timeout = PVE
::RPCEnvironment-
>is_worker() ?
60*60 : 5;
174 if ($method eq 'zpool_list') {
175 push @$cmd, 'zpool', 'list';
176 } elsif ($method eq 'zpool_import') {
177 push @$cmd, 'zpool', 'import';
178 $default_timeout = 15 if $default_timeout < 15;
180 push @$cmd, 'zfs', $method;
192 $timeout = $default_timeout if !$timeout;
194 run_command
($cmd, errmsg
=> "zfs error", outfunc
=> $output, timeout
=> $timeout);
200 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
206 die "illegal name '$volname' - sould be 'vm-$vmid-*'\n"
207 if $volname && $volname !~ m/^vm-$vmid-/;
208 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt)
211 $class->zfs_create_zvol($scfg, $volname, $size);
212 my $devname = "/dev/zvol/$scfg->{pool}/$volname";
215 for (my $i = 1; $i <= $timeout; $i++) {
217 die "Timeout: no zvol after $timeout sec found.\n"
222 } elsif ( $fmt eq 'subvol') {
224 die "illegal name '$volname' - sould be 'subvol-$vmid-*'\n"
225 if $volname && $volname !~ m/^subvol-$vmid-/;
226 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt)
229 die "illegal name '$volname' - sould be 'subvol-$vmid-*'\n"
230 if $volname !~ m/^subvol-$vmid-/;
232 $class->zfs_create_subvol($scfg, $volname, $size);
235 die "unsupported format '$fmt'";
242 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
244 my (undef, $name, undef) = $class->parse_volname($volname);
246 $class->zfs_delete_zvol($scfg, $name);
252 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
254 $cache->{zfs
} = $class->zfs_list_zvol($scfg) if !$cache->{zfs
};
255 my $zfspool = $scfg->{pool
};
258 if (my $dat = $cache->{zfs
}->{$zfspool}) {
260 foreach my $image (keys %$dat) {
262 my $info = $dat->{$image};
264 my $volname = $info->{name
};
265 my $parent = $info->{parent
};
266 my $owner = $info->{vmid
};
268 if ($parent && $parent =~ m/^(\S+)\@__base__$/) {
269 my ($basename) = ($1);
270 $info->{volid
} = "$storeid:$basename/$volname";
272 $info->{volid
} = "$storeid:$volname";
276 my $found = grep { $_ eq $info->{volid
} } @$vollist;
279 next if defined ($vmid) && ($owner ne $vmid);
288 sub zfs_get_pool_stats
{
289 my ($class, $scfg) = @_;
294 my $text = $class->zfs_request($scfg, undef, 'get', '-o', 'value', '-Hp',
295 'available,used', $scfg->{pool
});
297 my @lines = split /\n/, $text;
299 if($lines[0] =~ /^(\d+)$/) {
303 if($lines[1] =~ /^(\d+)$/) {
307 return ($available, $used);
310 sub zfs_create_zvol
{
311 my ($class, $scfg, $zvol, $size) = @_;
313 my $cmd = ['create'];
315 push @$cmd, '-s' if $scfg->{sparse
};
317 push @$cmd, '-b', $scfg->{blocksize
} if $scfg->{blocksize
};
319 push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol";
321 $class->zfs_request($scfg, undef, @$cmd);
324 sub zfs_create_subvol
{
325 my ($class, $scfg, $volname, $size) = @_;
327 my $dataset = "$scfg->{pool}/$volname";
329 my $cmd = ['create', '-o', 'acltype=posixacl', '-o', 'xattr=sa',
330 '-o', "refquota=${size}k", $dataset];
332 $class->zfs_request($scfg, undef, @$cmd);
335 sub zfs_delete_zvol
{
336 my ($class, $scfg, $zvol) = @_;
340 for (my $i = 0; $i < 6; $i++) {
342 eval { $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol"); };
344 if ($err =~ m/^zfs error:(.*): dataset is busy.*/) {
346 } elsif ($err =~ m/^zfs error:.*: dataset does not exist.*$/) {
361 my ($class, $scfg) = @_;
363 my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hr');
364 my $zvols = zfs_parse_zvol_list
($text);
365 return undef if !$zvols;
368 foreach my $zvol (@$zvols) {
369 my $pool = $zvol->{pool
};
370 my $name = $zvol->{name
};
371 my $parent = $zvol->{origin
};
372 if($zvol->{origin
} && $zvol->{origin
} =~ m/^$scfg->{pool}\/(\S
+)$/){
376 $list->{$pool}->{$name} = {
378 size
=> $zvol->{size
},
380 format
=> $zvol->{format
},
381 vmid
=> $zvol->{owner
},
388 sub zfs_find_free_diskname
{
389 my ($class, $storeid, $scfg, $vmid, $format) = @_;
392 my $volumes = $class->zfs_list_zvol($scfg);
395 my $dat = $volumes->{$scfg->{pool
}};
397 foreach my $image (keys %$dat) {
398 my $volname = $dat->{$image}->{name
};
399 if ($volname =~ m/(vm|base|subvol|basevol)-$vmid-disk-(\d+)/){
404 for (my $i = 1; $i < 100; $i++) {
405 if (!$disk_ids->{$i}) {
406 return $format eq 'subvol' ?
"subvol-$vmid-disk-$i" : "vm-$vmid-disk-$i";
410 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
413 sub zfs_get_latest_snapshot
{
414 my ($class, $scfg, $volname) = @_;
416 my $vname = ($class->parse_volname($volname))[1];
418 # abort rollback if snapshot is not the latest
419 my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
420 my $text = $class->zfs_request($scfg, undef, 'list', @params);
421 my @snapshots = split(/\n/, $text);
424 foreach (@snapshots) {
425 if (/$scfg->{pool}\/$vname/) {
435 my ($class, $storeid, $scfg, $cache) = @_;
443 ($free, $used) = $class->zfs_get_pool_stats($scfg);
445 $total = $free + $used;
449 return ($total, $free, $used, $active);
452 sub volume_size_info
{
453 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
455 my (undef, $vname, undef, undef, undef, undef, $format) =
456 $class->parse_volname($volname);
458 my $attr = $format eq 'subvol' ?
'refquota' : 'volsize';
459 my $text = $class->zfs_request($scfg, undef, 'get', '-Hp', $attr, "$scfg->{pool}/$vname");
460 if ($text =~ /\s$attr\s(\d+)\s/) {
464 die "Could not get zfs volume size\n";
467 sub volume_snapshot
{
468 my ($class, $scfg, $storeid, $volname, $snap) = @_;
470 my $vname = ($class->parse_volname($volname))[1];
472 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$vname\@$snap");
475 sub volume_snapshot_delete
{
476 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
478 my $vname = ($class->parse_volname($volname))[1];
480 $class->deactivate_volume($storeid, $scfg, $vname, $snap, {});
481 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$vname\@$snap");
484 sub volume_snapshot_rollback
{
485 my ($class, $scfg, $storeid, $volname, $snap) = @_;
487 my $vname = ($class->parse_volname($volname))[1];
489 $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$vname\@$snap");
492 sub volume_rollback_is_possible
{
493 my ($class, $scfg, $storeid, $volname, $snap) = @_;
495 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
496 if ($snap ne $recentsnap) {
497 die "can't rollback, more recent snapshots exist\n";
503 sub volume_snapshot_list
{
504 my ($class, $scfg, $storeid, $volname) = @_;
506 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
508 my $zpath = "$scfg->{pool}/$name";
512 my $cmd = ['zfs', 'list', '-r', '-H', '-S', 'name', '-t', 'snap', '-o',
518 if ($line =~ m/^\Q$zpath\E@(.*)$/) {
523 eval { run_command
( [$cmd], outfunc
=> $outfunc , errfunc
=> sub{}); };
525 # return an empty array if dataset does not exist.
529 sub activate_storage
{
530 my ($class, $storeid, $scfg, $cache) = @_;
532 # Note: $scfg->{pool} can include dataset <pool>/<dataset>
533 my $pool = $scfg->{pool
};
536 my @param = ('-o', 'name', '-H', "$pool");
539 $res = $class->zfs_request($scfg, undef, 'zpool_list', @param);
542 if ($@ || !defined($res) || $res !~ $pool) {
544 @param = ('-d', '/dev/disk/by-id/', "$pool");
545 $class->zfs_request($scfg, undef, 'zpool_import', @param);
547 die "could not activate storage '$storeid', $@\n" if $@;
552 sub deactivate_storage
{
553 my ($class, $storeid, $scfg, $cache) = @_;
557 sub activate_volume
{
558 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
562 sub deactivate_volume
{
563 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
568 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
570 $snap ||= '__base__';
572 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
573 $class->parse_volname($volname);
575 die "clone_image only works on base images\n" if !$isBase;
577 my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $format);
579 if ($format eq 'subvol') {
580 my $size = $class->zfs_request($scfg, undef, 'list', '-H', '-o', 'refquota', "$scfg->{pool}/$basename");
582 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name", '-o', "refquota=$size");
584 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
587 return "$basename/$name";
591 my ($class, $storeid, $scfg, $volname) = @_;
593 my $snap = '__base__';
595 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
596 $class->parse_volname($volname);
598 die "create_base not possible with base image\n" if $isBase;
601 if ( $format eq 'subvol' ) {
602 $newname =~ s/^subvol-/basevol-/;
604 $newname =~ s/^vm-/base-/;
606 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
608 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
610 my $running = undef; #fixme : is create_base always offline ?
612 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
618 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
620 my $new_size = int($size/1024);
622 my (undef, $vname, undef, undef, undef, undef, $format) =
623 $class->parse_volname($volname);
625 my $attr = $format eq 'subvol' ?
'refquota' : 'volsize';
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 $fd = fileno($fh);
678 die "internal error: invalid file handle for volume_export\n"
682 # For zfs we always create a replication stream (-R) which means the remote
683 # side will always delete non-existing source snapshots. This should work
684 # for all our use cases.
685 my $cmd = ['zfs', 'send', '-Rpv'];
686 if (defined($base_snapshot)) {
687 my $arg = $with_snapshots ?
'-I' : '-i';
688 push @$cmd, $arg, $base_snapshot;
690 push @$cmd, '--', "$scfg->{pool}/$volname\@$snapshot";
692 run_command
($cmd, output
=> $fd);
697 sub volume_export_formats
{
698 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
700 my @formats = ('zfs');
702 # push @formats, 'fies' if $volname !~ /^(?:basevol|subvol)-/;
703 # push @formats, 'raw' if !$base_snapshot && !$with_snapshots;
708 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_;
710 die "unsupported import stream format for $class: $format\n"
713 my $fd = fileno($fh);
714 die "internal error: invalid file handle for volume_import\n"
717 my $zfspath = "$scfg->{pool}/$volname";
718 my $suffix = defined($base_snapshot) ?
"\@$base_snapshot" : '';
719 my $exists = 0 == run_command
(['zfs', 'get', '-H', 'name', $zfspath.$suffix],
720 noerr
=> 1, errfunc
=> sub {});
721 if (defined($base_snapshot)) {
722 die "base snapshot '$zfspath\@$base_snapshot' doesn't exist\n" if !$exists;
724 die "volume '$zfspath' already exists\n" if $exists;
727 eval { run_command
(['zfs', 'recv', '-F', '--', $zfspath], input
=> "<&$fd") };
729 if (defined($base_snapshot)) {
730 eval { run_command
(['zfs', 'rollback', '-r', '--', "$zfspath\@$base_snapshot"]) };
732 eval { run_command
(['zfs', 'destroy', '-r', '--', $zfspath]) };
740 sub volume_import_formats
{
741 my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
743 return $class->volume_export_formats($scfg, $storeid, $volname, undef, $base_snapshot, $with_snapshots);