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, $ip, $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;
184 $ip = "[$ip]" if Net
::IP
::ip_is_ipv6
($ip);
185 unshift @$cmd, 'ssh', '-o', 'BatchMode=yes', "root\@${ip}", '--';
194 $timeout = $default_timeout if !$timeout;
196 run_command
($cmd, errmsg
=> "zfs error", outfunc
=> $output, timeout
=> $timeout);
202 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
208 die "illegal name '$volname' - sould be 'vm-$vmid-*'\n"
209 if $volname && $volname !~ m/^vm-$vmid-/;
210 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt)
213 $class->zfs_create_zvol($scfg, $volname, $size);
214 my $devname = "/dev/zvol/$scfg->{pool}/$volname";
216 run_command
("udevadm trigger --subsystem-match block");
217 system('udevadm', 'settle', '--timeout', '10', "--exit-if-exists=${devname}");
219 } elsif ( $fmt eq 'subvol') {
221 die "illegal name '$volname' - sould be 'subvol-$vmid-*'\n"
222 if $volname && $volname !~ m/^subvol-$vmid-/;
223 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt)
226 die "illegal name '$volname' - sould be 'subvol-$vmid-*'\n"
227 if $volname !~ m/^subvol-$vmid-/;
229 $class->zfs_create_subvol($scfg, $volname, $size);
232 die "unsupported format '$fmt'";
239 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
241 my (undef, $name, undef) = $class->parse_volname($volname);
243 $class->zfs_delete_zvol($scfg, $name);
249 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
251 $cache->{zfs
} = $class->zfs_list_zvol($scfg) if !$cache->{zfs
};
252 my $zfspool = $scfg->{pool
};
255 if (my $dat = $cache->{zfs
}->{$zfspool}) {
257 foreach my $image (keys %$dat) {
259 my $info = $dat->{$image};
261 my $volname = $info->{name
};
262 my $parent = $info->{parent
};
263 my $owner = $info->{vmid
};
265 if ($parent && $parent =~ m/^(\S+)\@__base__$/) {
266 my ($basename) = ($1);
267 $info->{volid
} = "$storeid:$basename/$volname";
269 $info->{volid
} = "$storeid:$volname";
273 my $found = grep { $_ eq $info->{volid
} } @$vollist;
276 next if defined ($vmid) && ($owner ne $vmid);
285 sub zfs_get_pool_stats
{
286 my ($class, $scfg) = @_;
291 my $text = $class->zfs_request($scfg, undef, undef, 'get', '-o', 'value', '-Hp',
292 'available,used', $scfg->{pool
});
294 my @lines = split /\n/, $text;
296 if($lines[0] =~ /^(\d+)$/) {
300 if($lines[1] =~ /^(\d+)$/) {
304 return ($available, $used);
307 sub zfs_create_zvol
{
308 my ($class, $scfg, $zvol, $size) = @_;
310 my $cmd = ['create'];
312 push @$cmd, '-s' if $scfg->{sparse
};
314 push @$cmd, '-b', $scfg->{blocksize
} if $scfg->{blocksize
};
316 push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol";
318 $class->zfs_request($scfg, undef, undef, @$cmd);
321 sub zfs_create_subvol
{
322 my ($class, $scfg, $volname, $size) = @_;
324 my $dataset = "$scfg->{pool}/$volname";
326 my $cmd = ['create', '-o', 'acltype=posixacl', '-o', 'xattr=sa',
327 '-o', "refquota=${size}k", $dataset];
329 $class->zfs_request($scfg, undef, undef, @$cmd);
332 sub zfs_delete_zvol
{
333 my ($class, $scfg, $zvol) = @_;
337 for (my $i = 0; $i < 6; $i++) {
339 eval { $class->zfs_request($scfg, undef, undef, 'destroy', '-r', "$scfg->{pool}/$zvol"); };
341 if ($err =~ m/^zfs error:(.*): dataset is busy.*/) {
343 } elsif ($err =~ m/^zfs error:.*: dataset does not exist.*$/) {
358 my ($class, $scfg) = @_;
360 my $text = $class->zfs_request($scfg, undef, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hr');
361 my $zvols = zfs_parse_zvol_list
($text);
362 return undef if !$zvols;
365 foreach my $zvol (@$zvols) {
366 my $pool = $zvol->{pool
};
367 my $name = $zvol->{name
};
368 my $parent = $zvol->{origin
};
369 if($zvol->{origin
} && $zvol->{origin
} =~ m/^$scfg->{pool}\/(\S
+)$/){
373 $list->{$pool}->{$name} = {
375 size
=> $zvol->{size
},
377 format
=> $zvol->{format
},
378 vmid
=> $zvol->{owner
},
385 sub zfs_find_free_diskname
{
386 my ($class, $storeid, $scfg, $vmid, $format) = @_;
389 my $volumes = $class->zfs_list_zvol($scfg);
392 my $dat = $volumes->{$scfg->{pool
}};
394 foreach my $image (keys %$dat) {
395 my $volname = $dat->{$image}->{name
};
396 if ($volname =~ m/(vm|base|subvol|basevol)-$vmid-disk-(\d+)/){
401 for (my $i = 1; $i < 100; $i++) {
402 if (!$disk_ids->{$i}) {
403 return $format eq 'subvol' ?
"subvol-$vmid-disk-$i" : "vm-$vmid-disk-$i";
407 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
410 sub zfs_get_latest_snapshot
{
411 my ($class, $scfg, $volname) = @_;
413 my $vname = ($class->parse_volname($volname))[1];
415 # abort rollback if snapshot is not the latest
416 my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
417 my $text = $class->zfs_request($scfg, undef, undef, 'list', @params);
418 my @snapshots = split(/\n/, $text);
421 foreach (@snapshots) {
422 if (/$scfg->{pool}\/$vname/) {
432 my ($class, $storeid, $scfg, $cache) = @_;
440 ($free, $used) = $class->zfs_get_pool_stats($scfg);
442 $total = $free + $used;
446 return ($total, $free, $used, $active);
449 sub volume_size_info
{
450 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
452 my (undef, $vname, undef, undef, undef, undef, $format) =
453 $class->parse_volname($volname);
455 my $attr = $format eq 'subvol' ?
'refquota' : 'volsize';
456 my $text = $class->zfs_request($scfg, undef, undef, 'get', '-Hp', $attr, "$scfg->{pool}/$vname");
457 if ($text =~ /\s$attr\s(\d+)\s/) {
461 die "Could not get zfs volume size\n";
464 sub volume_snapshot
{
465 my ($class, $scfg, $storeid, $volname, $snap) = @_;
467 my $vname = ($class->parse_volname($volname))[1];
469 $class->zfs_request($scfg, undef, undef, 'snapshot', "$scfg->{pool}/$vname\@$snap");
473 my ($class, $scfg, $storeid, $volname, $ip, $snap,
474 $incremental_snap, $verbose, $limit, $target_path) = @_;
476 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
478 my $zpath = "$scfg->{pool}/$name";
480 die "$vtype is not allowed in ZFSPool!" if ($vtype ne "images");
485 push @$cmdsend, 'zfs', 'send', '-R';
486 push @$cmdsend, '-v' if defined($verbose);
488 if( defined($incremental_snap)) {
489 push @$cmdsend, '-I', "$zpath\@${incremental_snap}";
492 push @$cmdsend, '--', "$zpath\@${snap}";
496 my $bwl = $limit * 1024;
497 push @$cmdlimit, 'cstream', '-t', $bwl;
503 $ip = "[$ip]" if Net
::IP
::ip_is_ipv6
($ip);
504 push @$cmdrecv, 'ssh', '-o', 'BatchMode=yes', "root\@${ip}", '--';
507 push @$cmdrecv, 'zfs', 'recv', '-F', '--';
509 $zpath = $target_path if defined($target_path);
510 push @$cmdrecv, $zpath;
513 eval { run_command
([$cmdsend, $cmdlimit, $cmdrecv]) };
515 eval { run_command
([$cmdsend, $cmdrecv]) };
523 sub volume_snapshot_delete_remote
{
524 my ($class, $scfg, $storeid, $volname, $snap, $ip) = @_;
526 my $vname = ($class->parse_volname($volname))[1];
527 $class->zfs_request($scfg, $ip, undef, 'destroy', "$scfg->{pool}/$vname\@$snap");
530 sub volume_snapshot_delete
{
531 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
533 my $vname = ($class->parse_volname($volname))[1];
535 $class->deactivate_volume($storeid, $scfg, $vname, $snap, {});
537 $class->zfs_request($scfg, undef, undef, 'destroy', "$scfg->{pool}/$vname\@$snap");
540 sub volume_snapshot_rollback
{
541 my ($class, $scfg, $storeid, $volname, $snap) = @_;
543 my $vname = ($class->parse_volname($volname))[1];
545 $class->zfs_request($scfg, undef, undef, 'rollback', "$scfg->{pool}/$vname\@$snap");
548 sub volume_rollback_is_possible
{
549 my ($class, $scfg, $storeid, $volname, $snap) = @_;
551 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
552 if ($snap ne $recentsnap) {
553 die "can't rollback, more recent snapshots exist\n";
559 sub volume_snapshot_list
{
560 my ($class, $scfg, $storeid, $volname, $prefix, $ip) = @_;
562 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
564 my $zpath = "$scfg->{pool}/$name";
566 $prefix = '' if !defined($prefix);
569 my $cmd = ['zfs', 'list', '-r', '-H', '-S', 'name', '-t', 'snap', '-o',
573 $ip = "[$ip]" if Net
::IP
::ip_is_ipv6
($ip);
574 unshift @$cmd, 'ssh', '-o', ' BatchMode=yes', "root\@${ip}", '--';
580 if ($line =~ m/^\Q$zpath\E@(\Q$prefix\E.*)$/) {
585 eval { run_command
( [$cmd], outfunc
=> $outfunc , errfunc
=> sub{}); };
587 # return an empty array if dataset does not exist.
591 sub activate_storage
{
592 my ($class, $storeid, $scfg, $cache) = @_;
594 # Note: $scfg->{pool} can include dataset <pool>/<dataset>
595 my $pool = $scfg->{pool
};
598 my @param = ('-o', 'name', '-H', "$pool");
601 $res = $class->zfs_request($scfg, undef, undef, 'zpool_list', @param);
604 if ($@ || !defined($res) || $res !~ $pool) {
606 @param = ('-d', '/dev/disk/by-id/', "$pool");
607 $class->zfs_request($scfg, undef, undef, 'zpool_import', @param);
609 die "could not activate storage '$storeid', $@\n" if $@;
614 sub deactivate_storage
{
615 my ($class, $storeid, $scfg, $cache) = @_;
619 sub activate_volume
{
620 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
624 sub deactivate_volume
{
625 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
630 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
632 $snap ||= '__base__';
634 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
635 $class->parse_volname($volname);
637 die "clone_image only works on base images\n" if !$isBase;
639 my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $format);
641 if ($format eq 'subvol') {
642 my $size = $class->zfs_request($scfg, undef, undef, 'list', '-H', '-o', 'refquota', "$scfg->{pool}/$basename");
644 $class->zfs_request($scfg, undef, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name", '-o', "refquota=$size");
646 $class->zfs_request($scfg, undef, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
649 return "$basename/$name";
653 my ($class, $storeid, $scfg, $volname) = @_;
655 my $snap = '__base__';
657 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
658 $class->parse_volname($volname);
660 die "create_base not possible with base image\n" if $isBase;
663 if ( $format eq 'subvol' ) {
664 $newname =~ s/^subvol-/basevol-/;
666 $newname =~ s/^vm-/base-/;
668 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
670 $class->zfs_request($scfg, undef, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
672 my $running = undef; #fixme : is create_base always offline ?
674 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
680 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
682 my $new_size = int($size/1024);
684 my (undef, $vname, undef, undef, undef, undef, $format) =
685 $class->parse_volname($volname);
687 my $attr = $format eq 'subvol' ?
'refquota' : 'volsize';
689 $class->zfs_request($scfg, undef, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$vname");
694 sub volume_has_feature
{
695 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
698 snapshot
=> { current
=> 1, snap
=> 1},
699 clone
=> { base
=> 1},
700 template
=> { current
=> 1},
701 copy
=> { base
=> 1, current
=> 1},
702 sparseinit
=> { base
=> 1, current
=> 1},
705 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
706 $class->parse_volname($volname);
713 $key = $isBase ?
'base' : 'current';
716 return 1 if $features->{$feature}->{$key};