1 package PVE
::Storage
::ZFSPoolPlugin
;
7 use PVE
::Tools
qw(run_command);
8 use PVE
::Storage
::Plugin
;
9 use PVE
::RPCEnvironment
;
11 use base
qw(PVE::Storage::Plugin);
19 content
=> [ {images
=> 1, rootdir
=> 1}, {images
=> 1 , rootdir
=> 1}],
20 format
=> [ { raw
=> 1, subvol
=> 1 } , 'raw' ],
27 description
=> "block size",
31 description
=> "use sparse volumes",
39 pool
=> { fixed
=> 1 },
40 blocksize
=> { optional
=> 1 },
41 sparse
=> { optional
=> 1 },
42 nodes
=> { optional
=> 1 },
43 disable
=> { optional
=> 1 },
44 content
=> { optional
=> 1 },
48 # static zfs helper methods
55 if ($text =~ m/^(\d+(\.\d+)?)([TGMK])?$/) {
57 my ($size, $reminder, $unit) = ($1, $2, $3);
62 } elsif ($unit eq 'M') {
64 } elsif ($unit eq 'G') {
65 $size *= 1024*1024*1024;
66 } elsif ($unit eq 'T') {
67 $size *= 1024*1024*1024*1024;
69 die "got unknown zfs size unit '$unit'\n";
81 warn "unable to parse zfs size '$text'\n";
86 sub zfs_parse_zvol_list
{
91 return $list if !$text;
93 my @lines = split /\n/, $text;
94 foreach my $line (@lines) {
95 my ($dataset, $size, $origin, $type, $refquota) = split(/\s+/, $line);
96 next if !($type eq 'volume' || $type eq 'filesystem');
99 my @parts = split /\//, $dataset;
100 next if scalar(@parts) < 2; # we need pool/name
101 my $name = pop @parts;
102 my $pool = join('/', @parts);
104 next unless $name =~ m!^(vm|base|subvol|basevol)-(\d+)-(\S+)$!;
107 $zvol->{pool
} = $pool;
108 $zvol->{name
} = $name;
109 if ($type eq 'filesystem') {
110 if ($refquota eq 'none') {
113 $zvol->{size
} = zfs_parse_size
($refquota);
115 $zvol->{format
} = 'subvol';
117 $zvol->{size
} = zfs_parse_size
($size);
118 $zvol->{format
} = 'raw';
120 if ($origin !~ /^-$/) {
121 $zvol->{origin
} = $origin;
130 my ($class, $volname) = @_;
132 if ($volname =~ m/^(((base|basevol)-(\d+)-\S+)\/)?
((base
|basevol
|vm
|subvol
)-(\d
+)-\S
+)$/) {
133 my $format = ($6 eq 'subvol' || $6 eq 'basevol') ?
'subvol' : 'raw';
134 my $isBase = ($6 eq 'base' || $6 eq 'basevol');
135 return ('images', $5, $7, $2, $4, $isBase, $format);
138 die "unable to parse zfs volume name '$volname'\n";
141 # virtual zfs methods (subclass can overwrite them)
144 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
146 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
150 if ($vtype eq "images") {
151 if ($name =~ m/^subvol-/ || $name =~ m/^basevol-/) {
152 # fixme: we currently assume standard mount point?!
153 $path = "/$scfg->{pool}/$name";
155 $path = "/dev/zvol/$scfg->{pool}/$name";
157 $path .= "\@$snapname" if defined($snapname);
159 die "$vtype is not allowed in ZFSPool!";
162 return ($path, $vmid, $vtype);
166 my ($class, $scfg, $timeout, $method, @params) = @_;
168 my $default_timeout = PVE
::RPCEnvironment-
>is_worker() ?
60*60 : 5;
172 if ($method eq 'zpool_list') {
173 push @$cmd, 'zpool', 'list';
174 } elsif ($method eq 'zpool_import') {
175 push @$cmd, 'zpool', 'import';
176 $default_timeout = 15 if $default_timeout < 15;
178 push @$cmd, 'zfs', $method;
190 $timeout = $default_timeout if !$timeout;
192 run_command
($cmd, errmsg
=> "zfs error", outfunc
=> $output, timeout
=> $timeout);
198 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
204 die "illegal name '$volname' - sould be 'vm-$vmid-*'\n"
205 if $volname && $volname !~ m/^vm-$vmid-/;
206 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt)
209 $class->zfs_create_zvol($scfg, $volname, $size);
210 my $devname = "/dev/zvol/$scfg->{pool}/$volname";
212 run_command
("udevadm trigger --subsystem-match block");
213 system('udevadm', 'settle', '--timeout', '10', "--exit-if-exists=${devname}");
215 } elsif ( $fmt eq 'subvol') {
217 die "illegal name '$volname' - sould be 'subvol-$vmid-*'\n"
218 if $volname && $volname !~ m/^subvol-$vmid-/;
219 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt)
222 die "illegal name '$volname' - sould be 'subvol-$vmid-*'\n"
223 if $volname !~ m/^subvol-$vmid-/;
225 $class->zfs_create_subvol($scfg, $volname, $size);
228 die "unsupported format '$fmt'";
235 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
237 my (undef, $name, undef) = $class->parse_volname($volname);
239 $class->zfs_delete_zvol($scfg, $name);
245 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
247 $cache->{zfs
} = $class->zfs_list_zvol($scfg) if !$cache->{zfs
};
248 my $zfspool = $scfg->{pool
};
251 if (my $dat = $cache->{zfs
}->{$zfspool}) {
253 foreach my $image (keys %$dat) {
255 my $info = $dat->{$image};
257 my $volname = $info->{name
};
258 my $parent = $info->{parent
};
259 my $owner = $info->{vmid
};
261 if ($parent && $parent =~ m/^(\S+)\@__base__$/) {
262 my ($basename) = ($1);
263 $info->{volid
} = "$storeid:$basename/$volname";
265 $info->{volid
} = "$storeid:$volname";
269 my $found = grep { $_ eq $info->{volid
} } @$vollist;
272 next if defined ($vmid) && ($owner ne $vmid);
281 sub zfs_get_pool_stats
{
282 my ($class, $scfg) = @_;
287 my $text = $class->zfs_request($scfg, undef, 'get', '-o', 'value', '-Hp',
288 'available,used', $scfg->{pool
});
290 my @lines = split /\n/, $text;
292 if($lines[0] =~ /^(\d+)$/) {
296 if($lines[1] =~ /^(\d+)$/) {
300 return ($available, $used);
303 sub zfs_create_zvol
{
304 my ($class, $scfg, $zvol, $size) = @_;
306 my $cmd = ['create'];
308 push @$cmd, '-s' if $scfg->{sparse
};
310 push @$cmd, '-b', $scfg->{blocksize
} if $scfg->{blocksize
};
312 push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol";
314 $class->zfs_request($scfg, undef, @$cmd);
317 sub zfs_create_subvol
{
318 my ($class, $scfg, $volname, $size) = @_;
320 my $dataset = "$scfg->{pool}/$volname";
322 my $cmd = ['create', '-o', 'acltype=posixacl', '-o', 'xattr=sa',
323 '-o', "refquota=${size}k", $dataset];
325 $class->zfs_request($scfg, undef, @$cmd);
328 sub zfs_delete_zvol
{
329 my ($class, $scfg, $zvol) = @_;
333 for (my $i = 0; $i < 6; $i++) {
335 eval { $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol"); };
337 if ($err =~ m/^zfs error:(.*): dataset is busy.*/) {
339 } elsif ($err =~ m/^zfs error:.*: dataset does not exist.*$/) {
354 my ($class, $scfg) = @_;
356 my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hr');
357 my $zvols = zfs_parse_zvol_list
($text);
358 return undef if !$zvols;
361 foreach my $zvol (@$zvols) {
362 my $pool = $zvol->{pool
};
363 my $name = $zvol->{name
};
364 my $parent = $zvol->{origin
};
365 if($zvol->{origin
} && $zvol->{origin
} =~ m/^$scfg->{pool}\/(\S
+)$/){
369 $list->{$pool}->{$name} = {
371 size
=> $zvol->{size
},
373 format
=> $zvol->{format
},
374 vmid
=> $zvol->{owner
},
381 sub zfs_find_free_diskname
{
382 my ($class, $storeid, $scfg, $vmid, $format) = @_;
385 my $volumes = $class->zfs_list_zvol($scfg);
388 my $dat = $volumes->{$scfg->{pool
}};
390 foreach my $image (keys %$dat) {
391 my $volname = $dat->{$image}->{name
};
392 if ($volname =~ m/(vm|base|subvol|basevol)-$vmid-disk-(\d+)/){
397 for (my $i = 1; $i < 100; $i++) {
398 if (!$disk_ids->{$i}) {
399 return $format eq 'subvol' ?
"subvol-$vmid-disk-$i" : "vm-$vmid-disk-$i";
403 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
406 sub zfs_get_latest_snapshot
{
407 my ($class, $scfg, $volname) = @_;
409 my $vname = ($class->parse_volname($volname))[1];
411 # abort rollback if snapshot is not the latest
412 my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
413 my $text = $class->zfs_request($scfg, undef, 'list', @params);
414 my @snapshots = split(/\n/, $text);
417 foreach (@snapshots) {
418 if (/$scfg->{pool}\/$vname/) {
428 my ($class, $storeid, $scfg, $cache) = @_;
436 ($free, $used) = $class->zfs_get_pool_stats($scfg);
438 $total = $free + $used;
442 return ($total, $free, $used, $active);
445 sub volume_size_info
{
446 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
448 my (undef, $vname, undef, undef, undef, undef, $format) =
449 $class->parse_volname($volname);
451 my $attr = $format eq 'subvol' ?
'refquota' : 'volsize';
452 my $text = $class->zfs_request($scfg, undef, 'get', '-Hp', $attr, "$scfg->{pool}/$vname");
453 if ($text =~ /\s$attr\s(\d+)\s/) {
457 die "Could not get zfs volume size\n";
460 sub volume_snapshot
{
461 my ($class, $scfg, $storeid, $volname, $snap) = @_;
463 my $vname = ($class->parse_volname($volname))[1];
465 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$vname\@$snap");
469 my ($class, $scfg, $storeid, $volname, $ip, $snap,
470 $incremental_snap, $verbose, $limit, $target_path) = @_;
472 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
474 my $zpath = "$scfg->{pool}/$name";
476 die "$vtype is not allowed in ZFSPool!" if ($vtype ne "images");
481 push @$cmdsend, 'zfs', 'send', '-R';
482 push @$cmdsend, '-v' if defined($verbose);
484 if( defined($incremental_snap)) {
485 push @$cmdsend, '-I', "$zpath\@${incremental_snap}";
488 push @$cmdsend, '--', "$zpath\@${snap}";
492 my $bwl = $limit * 1024;
493 push @$cmdlimit, 'cstream', '-t', $bwl;
498 push @$cmdrecv, 'ssh', '-o', 'BatchMode=yes', "root\@${ip}", '--' if $ip;
499 push @$cmdrecv, 'zfs', 'recv', '-F', '--';
501 $zpath = $target_path if defined($target_path);
502 push @$cmdrecv, $zpath;
506 eval { run_command
([$cmdsend, $cmdlimit, $cmdrecv]) };
508 eval { run_command
([$cmdsend, $cmdrecv]) };
516 sub volume_snapshot_delete
{
517 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
519 my $vname = ($class->parse_volname($volname))[1];
521 $class->deactivate_volume($storeid, $scfg, $vname, $snap, {});
522 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$vname\@$snap");
525 sub volume_snapshot_rollback
{
526 my ($class, $scfg, $storeid, $volname, $snap) = @_;
528 my $vname = ($class->parse_volname($volname))[1];
530 $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$vname\@$snap");
533 sub volume_rollback_is_possible
{
534 my ($class, $scfg, $storeid, $volname, $snap) = @_;
536 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
537 if ($snap ne $recentsnap) {
538 die "can't rollback, more recent snapshots exist\n";
544 sub activate_storage
{
545 my ($class, $storeid, $scfg, $cache) = @_;
547 # Note: $scfg->{pool} can include dataset <pool>/<dataset>
548 my $pool = $scfg->{pool
};
551 my @param = ('-o', 'name', '-H', "$pool");
554 $res = $class->zfs_request($scfg, undef, 'zpool_list', @param);
557 if ($@ || !defined($res) || $res !~ $pool) {
559 @param = ('-d', '/dev/disk/by-id/', "$pool");
560 $class->zfs_request($scfg, undef, 'zpool_import', @param);
562 die "could not activate storage '$storeid', $@\n" if $@;
567 sub deactivate_storage
{
568 my ($class, $storeid, $scfg, $cache) = @_;
572 sub activate_volume
{
573 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
577 sub deactivate_volume
{
578 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
583 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
585 $snap ||= '__base__';
587 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
588 $class->parse_volname($volname);
590 die "clone_image only works on base images\n" if !$isBase;
592 my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $format);
594 if ($format eq 'subvol') {
595 my $size = $class->zfs_request($scfg, undef, 'list', '-H', '-o', 'refquota', "$scfg->{pool}/$basename");
597 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name", '-o', "refquota=$size");
599 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
602 return "$basename/$name";
606 my ($class, $storeid, $scfg, $volname) = @_;
608 my $snap = '__base__';
610 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
611 $class->parse_volname($volname);
613 die "create_base not possible with base image\n" if $isBase;
616 if ( $format eq 'subvol' ) {
617 $newname =~ s/^subvol-/basevol-/;
619 $newname =~ s/^vm-/base-/;
621 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
623 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
625 my $running = undef; #fixme : is create_base always offline ?
627 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
633 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
635 my $new_size = int($size/1024);
637 my (undef, $vname, undef, undef, undef, undef, $format) =
638 $class->parse_volname($volname);
640 my $attr = $format eq 'subvol' ?
'refquota' : 'volsize';
642 $class->zfs_request($scfg, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$vname");
647 sub volume_has_feature
{
648 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
651 snapshot
=> { current
=> 1, snap
=> 1},
652 clone
=> { base
=> 1},
653 template
=> { current
=> 1},
654 copy
=> { base
=> 1, current
=> 1},
655 sparseinit
=> { base
=> 1, current
=> 1},
658 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
659 $class->parse_volname($volname);
666 $key = $isBase ?
'base' : 'current';
669 return 1 if $features->{$feature}->{$key};