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 },
49 # static zfs helper methods
56 if ($text =~ m/^(\d+(\.\d+)?)([TGMK])?$/) {
58 my ($size, $reminder, $unit) = ($1, $2, $3);
63 } elsif ($unit eq 'M') {
65 } elsif ($unit eq 'G') {
66 $size *= 1024*1024*1024;
67 } elsif ($unit eq 'T') {
68 $size *= 1024*1024*1024*1024;
70 die "got unknown zfs size unit '$unit'\n";
82 warn "unable to parse zfs size '$text'\n";
87 sub zfs_parse_zvol_list
{
92 return $list if !$text;
94 my @lines = split /\n/, $text;
95 foreach my $line (@lines) {
96 my ($dataset, $size, $origin, $type, $refquota) = split(/\s+/, $line);
97 next if !($type eq 'volume' || $type eq 'filesystem');
100 my @parts = split /\//, $dataset;
101 next if scalar(@parts) < 2; # we need pool/name
102 my $name = pop @parts;
103 my $pool = join('/', @parts);
105 next unless $name =~ m!^(vm|base|subvol|basevol)-(\d+)-(\S+)$!;
108 $zvol->{pool
} = $pool;
109 $zvol->{name
} = $name;
110 if ($type eq 'filesystem') {
111 if ($refquota eq 'none') {
114 $zvol->{size
} = zfs_parse_size
($refquota);
116 $zvol->{format
} = 'subvol';
118 $zvol->{size
} = zfs_parse_size
($size);
119 $zvol->{format
} = 'raw';
121 if ($origin !~ /^-$/) {
122 $zvol->{origin
} = $origin;
131 my ($class, $volname) = @_;
133 if ($volname =~ m/^(((base|basevol)-(\d+)-\S+)\/)?
((base
|basevol
|vm
|subvol
)-(\d
+)-\S
+)$/) {
134 my $format = ($6 eq 'subvol' || $6 eq 'basevol') ?
'subvol' : 'raw';
135 my $isBase = ($6 eq 'base' || $6 eq 'basevol');
136 return ('images', $5, $7, $2, $4, $isBase, $format);
139 die "unable to parse zfs volume name '$volname'\n";
142 # virtual zfs methods (subclass can overwrite them)
145 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
147 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
151 if ($vtype eq "images") {
152 if ($name =~ m/^subvol-/ || $name =~ m/^basevol-/) {
153 # fixme: we currently assume standard mount point?!
154 $path = "/$scfg->{pool}/$name";
156 $path = "/dev/zvol/$scfg->{pool}/$name";
158 $path .= "\@$snapname" if defined($snapname);
160 die "$vtype is not allowed in ZFSPool!";
163 return ($path, $vmid, $vtype);
167 my ($class, $scfg, $timeout, $method, @params) = @_;
169 my $default_timeout = PVE
::RPCEnvironment-
>is_worker() ?
60*60 : 5;
173 if ($method eq 'zpool_list') {
174 push @$cmd, 'zpool', 'list';
175 } elsif ($method eq 'zpool_import') {
176 push @$cmd, 'zpool', 'import';
177 $default_timeout = 15 if $default_timeout < 15;
179 push @$cmd, 'zfs', $method;
191 $timeout = $default_timeout if !$timeout;
193 run_command
($cmd, errmsg
=> "zfs error", outfunc
=> $output, timeout
=> $timeout);
199 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
205 die "illegal name '$volname' - sould be 'vm-$vmid-*'\n"
206 if $volname && $volname !~ m/^vm-$vmid-/;
207 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt)
210 $class->zfs_create_zvol($scfg, $volname, $size);
211 my $devname = "/dev/zvol/$scfg->{pool}/$volname";
213 run_command
("udevadm trigger --subsystem-match block");
214 system('udevadm', 'settle', '--timeout', '10', "--exit-if-exists=${devname}");
216 } elsif ( $fmt eq 'subvol') {
218 die "illegal name '$volname' - sould be 'subvol-$vmid-*'\n"
219 if $volname && $volname !~ m/^subvol-$vmid-/;
220 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt)
223 die "illegal name '$volname' - sould be 'subvol-$vmid-*'\n"
224 if $volname !~ m/^subvol-$vmid-/;
226 $class->zfs_create_subvol($scfg, $volname, $size);
229 die "unsupported format '$fmt'";
236 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
238 my (undef, $name, undef) = $class->parse_volname($volname);
240 $class->zfs_delete_zvol($scfg, $name);
246 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
248 $cache->{zfs
} = $class->zfs_list_zvol($scfg) if !$cache->{zfs
};
249 my $zfspool = $scfg->{pool
};
252 if (my $dat = $cache->{zfs
}->{$zfspool}) {
254 foreach my $image (keys %$dat) {
256 my $info = $dat->{$image};
258 my $volname = $info->{name
};
259 my $parent = $info->{parent
};
260 my $owner = $info->{vmid
};
262 if ($parent && $parent =~ m/^(\S+)\@__base__$/) {
263 my ($basename) = ($1);
264 $info->{volid
} = "$storeid:$basename/$volname";
266 $info->{volid
} = "$storeid:$volname";
270 my $found = grep { $_ eq $info->{volid
} } @$vollist;
273 next if defined ($vmid) && ($owner ne $vmid);
282 sub zfs_get_pool_stats
{
283 my ($class, $scfg) = @_;
288 my $text = $class->zfs_request($scfg, undef, 'get', '-o', 'value', '-Hp',
289 'available,used', $scfg->{pool
});
291 my @lines = split /\n/, $text;
293 if($lines[0] =~ /^(\d+)$/) {
297 if($lines[1] =~ /^(\d+)$/) {
301 return ($available, $used);
304 sub zfs_create_zvol
{
305 my ($class, $scfg, $zvol, $size) = @_;
307 my $cmd = ['create'];
309 push @$cmd, '-s' if $scfg->{sparse
};
311 push @$cmd, '-b', $scfg->{blocksize
} if $scfg->{blocksize
};
313 push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol";
315 $class->zfs_request($scfg, undef, @$cmd);
318 sub zfs_create_subvol
{
319 my ($class, $scfg, $volname, $size) = @_;
321 my $dataset = "$scfg->{pool}/$volname";
323 my $cmd = ['create', '-o', 'acltype=posixacl', '-o', 'xattr=sa',
324 '-o', "refquota=${size}k", $dataset];
326 $class->zfs_request($scfg, undef, @$cmd);
329 sub zfs_delete_zvol
{
330 my ($class, $scfg, $zvol) = @_;
334 for (my $i = 0; $i < 6; $i++) {
336 eval { $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol"); };
338 if ($err =~ m/^zfs error:(.*): dataset is busy.*/) {
340 } elsif ($err =~ m/^zfs error:.*: dataset does not exist.*$/) {
355 my ($class, $scfg) = @_;
357 my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hr');
358 my $zvols = zfs_parse_zvol_list
($text);
359 return undef if !$zvols;
362 foreach my $zvol (@$zvols) {
363 my $pool = $zvol->{pool
};
364 my $name = $zvol->{name
};
365 my $parent = $zvol->{origin
};
366 if($zvol->{origin
} && $zvol->{origin
} =~ m/^$scfg->{pool}\/(\S
+)$/){
370 $list->{$pool}->{$name} = {
372 size
=> $zvol->{size
},
374 format
=> $zvol->{format
},
375 vmid
=> $zvol->{owner
},
382 sub zfs_find_free_diskname
{
383 my ($class, $storeid, $scfg, $vmid, $format) = @_;
386 my $volumes = $class->zfs_list_zvol($scfg);
389 my $dat = $volumes->{$scfg->{pool
}};
391 foreach my $image (keys %$dat) {
392 my $volname = $dat->{$image}->{name
};
393 if ($volname =~ m/(vm|base|subvol|basevol)-$vmid-disk-(\d+)/){
398 for (my $i = 1; $i < 100; $i++) {
399 if (!$disk_ids->{$i}) {
400 return $format eq 'subvol' ?
"subvol-$vmid-disk-$i" : "vm-$vmid-disk-$i";
404 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
407 sub zfs_get_latest_snapshot
{
408 my ($class, $scfg, $volname) = @_;
410 my $vname = ($class->parse_volname($volname))[1];
412 # abort rollback if snapshot is not the latest
413 my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
414 my $text = $class->zfs_request($scfg, undef, 'list', @params);
415 my @snapshots = split(/\n/, $text);
418 foreach (@snapshots) {
419 if (/$scfg->{pool}\/$vname/) {
429 my ($class, $storeid, $scfg, $cache) = @_;
437 ($free, $used) = $class->zfs_get_pool_stats($scfg);
439 $total = $free + $used;
443 return ($total, $free, $used, $active);
446 sub volume_size_info
{
447 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
449 my (undef, $vname, undef, undef, undef, undef, $format) =
450 $class->parse_volname($volname);
452 my $attr = $format eq 'subvol' ?
'refquota' : 'volsize';
453 my $text = $class->zfs_request($scfg, undef, 'get', '-Hp', $attr, "$scfg->{pool}/$vname");
454 if ($text =~ /\s$attr\s(\d+)\s/) {
458 die "Could not get zfs volume size\n";
461 sub volume_snapshot
{
462 my ($class, $scfg, $storeid, $volname, $snap) = @_;
464 my $vname = ($class->parse_volname($volname))[1];
466 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$vname\@$snap");
469 sub volume_snapshot_delete
{
470 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
472 my $vname = ($class->parse_volname($volname))[1];
474 $class->deactivate_volume($storeid, $scfg, $vname, $snap, {});
475 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$vname\@$snap");
478 sub volume_snapshot_rollback
{
479 my ($class, $scfg, $storeid, $volname, $snap) = @_;
481 my $vname = ($class->parse_volname($volname))[1];
483 $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$vname\@$snap");
486 sub volume_rollback_is_possible
{
487 my ($class, $scfg, $storeid, $volname, $snap) = @_;
489 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
490 if ($snap ne $recentsnap) {
491 die "can't rollback, more recent snapshots exist\n";
497 sub volume_snapshot_list
{
498 my ($class, $scfg, $storeid, $volname) = @_;
500 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
502 my $zpath = "$scfg->{pool}/$name";
504 $prefix = '' if !defined($prefix);
507 my $cmd = ['zfs', 'list', '-r', '-H', '-S', 'name', '-t', 'snap', '-o',
513 if ($line =~ m/^\Q$zpath\E@(.*)$/) {
518 eval { run_command
( [$cmd], outfunc
=> $outfunc , errfunc
=> sub{}); };
520 # return an empty array if dataset does not exist.
524 sub activate_storage
{
525 my ($class, $storeid, $scfg, $cache) = @_;
527 # Note: $scfg->{pool} can include dataset <pool>/<dataset>
528 my $pool = $scfg->{pool
};
531 my @param = ('-o', 'name', '-H', "$pool");
534 $res = $class->zfs_request($scfg, undef, 'zpool_list', @param);
537 if ($@ || !defined($res) || $res !~ $pool) {
539 @param = ('-d', '/dev/disk/by-id/', "$pool");
540 $class->zfs_request($scfg, undef, 'zpool_import', @param);
542 die "could not activate storage '$storeid', $@\n" if $@;
547 sub deactivate_storage
{
548 my ($class, $storeid, $scfg, $cache) = @_;
552 sub activate_volume
{
553 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
557 sub deactivate_volume
{
558 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
563 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
565 $snap ||= '__base__';
567 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
568 $class->parse_volname($volname);
570 die "clone_image only works on base images\n" if !$isBase;
572 my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $format);
574 if ($format eq 'subvol') {
575 my $size = $class->zfs_request($scfg, undef, 'list', '-H', '-o', 'refquota', "$scfg->{pool}/$basename");
577 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name", '-o', "refquota=$size");
579 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
582 return "$basename/$name";
586 my ($class, $storeid, $scfg, $volname) = @_;
588 my $snap = '__base__';
590 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
591 $class->parse_volname($volname);
593 die "create_base not possible with base image\n" if $isBase;
596 if ( $format eq 'subvol' ) {
597 $newname =~ s/^subvol-/basevol-/;
599 $newname =~ s/^vm-/base-/;
601 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
603 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
605 my $running = undef; #fixme : is create_base always offline ?
607 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
613 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
615 my $new_size = int($size/1024);
617 my (undef, $vname, undef, undef, undef, undef, $format) =
618 $class->parse_volname($volname);
620 my $attr = $format eq 'subvol' ?
'refquota' : 'volsize';
622 $class->zfs_request($scfg, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$vname");
627 sub volume_has_feature
{
628 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
631 snapshot
=> { current
=> 1, snap
=> 1},
632 clone
=> { base
=> 1},
633 template
=> { current
=> 1},
634 copy
=> { base
=> 1, current
=> 1},
635 sparseinit
=> { base
=> 1, current
=> 1},
636 replicate
=> { base
=> 1, current
=> 1},
639 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
640 $class->parse_volname($volname);
647 $key = $isBase ?
'base' : 'current';
650 return 1 if $features->{$feature}->{$key};
656 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
658 die "unsupported export stream format for $class: $format\n"
661 die "$class storage can only export snapshots\n"
662 if !defined($snapshot);
664 my $fd = fileno($fh);
665 die "internal error: invalid file handle for volume_export\n"
669 # For zfs we always create a replication stream (-R) which means the remote
670 # side will always delete non-existing source snapshots. This should work
671 # for all our use cases.
672 my $cmd = ['zfs', 'send', '-Rpv'];
673 if (defined($base_snapshot)) {
674 my $arg = $with_snapshots ?
'-I' : '-i';
675 push @$cmd, $arg, $base_snapshot;
677 push @$cmd, '--', "$scfg->{pool}/$volname\@$snapshot";
679 run_command
($cmd, output
=> $fd);
684 sub volume_export_formats
{
685 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
687 my @formats = ('zfs');
689 # push @formats, 'fies' if $volname !~ /^(?:basevol|subvol)-/;
690 # push @formats, 'raw' if !$base_snapshot && !$with_snapshots;
695 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_;
697 die "unsupported import stream format for $class: $format\n"
700 my $fd = fileno($fh);
701 die "internal error: invalid file handle for volume_import\n"
704 my $zfspath = "$scfg->{pool}/$volname";
705 my $suffix = defined($base_snapshot) ?
"\@$base_snapshot" : '';
706 my $exists = 0 == run_command
(['zfs', 'get', '-H', 'name', $zfspath.$suffix],
707 noerr
=> 1, errfunc
=> sub {});
708 if (defined($base_snapshot)) {
709 die "base snapshot '$zfspath\@$base_snapshot' doesn't exist\n" if !$exists;
711 die "volume '$zfspath' already exists\n" if $exists;
714 eval { run_command
(['zfs', 'recv', '-F', '--', $zfspath], input
=> "<&$fd") };
716 if (defined($base_snapshot)) {
717 eval { run_command
(['zfs', 'rollback', '-r', '--', "$zfspath\@$base_snapshot"]) };
719 eval { run_command
(['zfs', 'destroy', '-r', '--', $zfspath]) };
727 sub volume_import_formats
{
728 my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
730 return $class->volume_export_formats($scfg, $storeid, $volname, undef, $base_snapshot, $with_snapshots);