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");
470 my ($class, $scfg, $storeid, $volname, $ip, $snap,
471 $incremental_snap, $verbose, $limit, $target_path) = @_;
473 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
475 my $zpath = "$scfg->{pool}/$name";
477 die "$vtype is not allowed in ZFSPool!" if ($vtype ne "images");
482 push @$cmdsend, 'zfs', 'send', '-R';
483 push @$cmdsend, '-v' if defined($verbose);
485 if( defined($incremental_snap)) {
486 push @$cmdsend, '-I', "$zpath\@${incremental_snap}";
489 push @$cmdsend, '--', "$zpath\@${snap}";
493 my $bwl = $limit * 1024;
494 push @$cmdlimit, 'cstream', '-t', $bwl;
500 $ip = "[$ip]" if Net
::IP
::ip_is_ipv6
($ip);
501 push @$cmdrecv, 'ssh', '-o', 'BatchMode=yes', "root\@${ip}", '--';
504 push @$cmdrecv, 'zfs', 'recv', '-F', '--';
506 $zpath = $target_path if defined($target_path);
507 push @$cmdrecv, $zpath;
511 eval { run_command
([$cmdsend, $cmdlimit, $cmdrecv]) };
513 eval { run_command
([$cmdsend, $cmdrecv]) };
521 sub volume_snapshot_delete
{
522 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
524 my $vname = ($class->parse_volname($volname))[1];
526 $class->deactivate_volume($storeid, $scfg, $vname, $snap, {});
527 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$vname\@$snap");
530 sub volume_snapshot_rollback
{
531 my ($class, $scfg, $storeid, $volname, $snap) = @_;
533 my $vname = ($class->parse_volname($volname))[1];
535 $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$vname\@$snap");
538 sub volume_rollback_is_possible
{
539 my ($class, $scfg, $storeid, $volname, $snap) = @_;
541 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
542 if ($snap ne $recentsnap) {
543 die "can't rollback, more recent snapshots exist\n";
549 sub volume_snapshot_list
{
550 my ($class, $scfg, $storeid, $volname, $prefix, $ip) = @_;
552 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
554 my $zpath = "$scfg->{pool}/$name";
556 $prefix = '' if !defined($prefix);
559 my $cmd = ['zfs', 'list', '-r', '-H', '-S', 'name', '-t', 'snap', '-o',
563 $ip = "[$ip]" if Net
::IP
::ip_is_ipv6
($ip);
564 unshift @$cmd, 'ssh', '-o', ' BatchMode=yes', "root\@${ip}", '--';
570 if ($line =~ m/^\Q$zpath\E@(\Q$prefix\E.*)$/) {
575 eval { run_command
( [$cmd], outfunc
=> $outfunc , errfunc
=> sub{}); };
577 # return an empty array if dataset does not exist.
581 sub activate_storage
{
582 my ($class, $storeid, $scfg, $cache) = @_;
584 # Note: $scfg->{pool} can include dataset <pool>/<dataset>
585 my $pool = $scfg->{pool
};
588 my @param = ('-o', 'name', '-H', "$pool");
591 $res = $class->zfs_request($scfg, undef, 'zpool_list', @param);
594 if ($@ || !defined($res) || $res !~ $pool) {
596 @param = ('-d', '/dev/disk/by-id/', "$pool");
597 $class->zfs_request($scfg, undef, 'zpool_import', @param);
599 die "could not activate storage '$storeid', $@\n" if $@;
604 sub deactivate_storage
{
605 my ($class, $storeid, $scfg, $cache) = @_;
609 sub activate_volume
{
610 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
614 sub deactivate_volume
{
615 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
620 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
622 $snap ||= '__base__';
624 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
625 $class->parse_volname($volname);
627 die "clone_image only works on base images\n" if !$isBase;
629 my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $format);
631 if ($format eq 'subvol') {
632 my $size = $class->zfs_request($scfg, undef, 'list', '-H', '-o', 'refquota', "$scfg->{pool}/$basename");
634 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name", '-o', "refquota=$size");
636 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
639 return "$basename/$name";
643 my ($class, $storeid, $scfg, $volname) = @_;
645 my $snap = '__base__';
647 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
648 $class->parse_volname($volname);
650 die "create_base not possible with base image\n" if $isBase;
653 if ( $format eq 'subvol' ) {
654 $newname =~ s/^subvol-/basevol-/;
656 $newname =~ s/^vm-/base-/;
658 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
660 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
662 my $running = undef; #fixme : is create_base always offline ?
664 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
670 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
672 my $new_size = int($size/1024);
674 my (undef, $vname, undef, undef, undef, undef, $format) =
675 $class->parse_volname($volname);
677 my $attr = $format eq 'subvol' ?
'refquota' : 'volsize';
679 $class->zfs_request($scfg, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$vname");
684 sub volume_has_feature
{
685 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
688 snapshot
=> { current
=> 1, snap
=> 1},
689 clone
=> { base
=> 1},
690 template
=> { current
=> 1},
691 copy
=> { base
=> 1, current
=> 1},
692 sparseinit
=> { base
=> 1, current
=> 1},
695 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
696 $class->parse_volname($volname);
703 $key = $isBase ?
'base' : 'current';
706 return 1 if $features->{$feature}->{$key};