1 package PVE
::Storage
::ZFSPoolPlugin
;
7 use PVE
::Tools
qw(run_command);
8 use PVE
::Storage
::Plugin
;
11 use base
qw(PVE::Storage::Plugin);
19 content
=> [ {images
=> 1}, { images
=> 1 }],
26 description
=> "block size",
30 description
=> "use sparse volumes",
38 pool
=> { fixed
=> 1 },
39 blocksize
=> { optional
=> 1 },
40 sparse
=> { optional
=> 1 },
41 nodes
=> { optional
=> 1 },
42 disable
=> { optional
=> 1 },
43 maxfiles
=> { 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 if ($line =~ /^(.+)\s+([a-zA-Z0-9\.]+|\-)\s+(.+)$/) {
99 my @parts = split /\//, $1;
100 my $name = pop @parts;
101 my $pool = join('/', @parts);
103 next unless $name =~ m!^(\w+)-(\d+)-(\w+)-(\d+)$!;
104 $name = $pool . '/' . $name;
106 $zvol->{pool
} = $pool;
107 $zvol->{name
} = $name;
108 $zvol->{size
} = zfs_parse_size
($size);
110 $zvol->{origin
} = $origin;
120 my ($class, $volname) = @_;
122 if ($volname =~ m/^(((base|vm)-(\d+)-\S+)\/)?
((base
)?
(vm
)?
-(\d
+)-\S
+)$/) {
123 return ('images', $5, $8, $2, $4, $6);
126 die "unable to parse zfs volume name '$volname'\n";
129 # virtual zfs methods (subclass can overwrite them)
132 my ($class, $scfg, $volname) = @_;
134 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
138 if($vtype eq "images"){
139 $path = "/dev/zvol/$scfg->{pool}/$volname";
141 die "$vtype is not allowed in ZFSPool!";
144 return ($path, $vmid, $vtype);
148 my ($class, $scfg, $timeout, $method, @params) = @_;
150 $timeout = 5 if !$timeout;
154 if ($method eq 'zpool_list') {
155 push @$cmd, 'zpool', 'list';
157 push @$cmd, 'zfs', $method;
169 run_command
($cmd, errmsg
=> "zfs error", outfunc
=> $output, timeout
=> $timeout);
175 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
177 die "unsupported format '$fmt'" if $fmt ne 'raw';
179 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
180 if $name && $name !~ m/^vm-$vmid-/;
184 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid) if !$volname;
186 $class->zfs_create_zvol($scfg, $volname, $size);
188 my $devname = "/dev/zvol/$scfg->{pool}/$volname";
190 run_command
("udevadm trigger --subsystem-match block");
191 system("udevadm settle --timeout 10 --exit-if-exists=${devname}");
197 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
199 my (undef, $name, undef) = $class->parse_volname($volname);
201 $class->zfs_delete_zvol($scfg, $name);
207 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
209 $cache->{zfs
} = $class->zfs_list_zvol($scfg) if !$cache->{zfs
};
210 my $zfspool = $scfg->{pool
};
213 if (my $dat = $cache->{zfs
}->{$zfspool}) {
215 foreach my $image (keys %$dat) {
217 my $volname = $dat->{$image}->{name
};
218 my $parent = $dat->{$image}->{parent
};
221 if ($parent && $parent =~ m/^(\S+)@(\S+)$/) {
222 my ($basename) = ($1);
223 $volid = "$storeid:$basename/$volname";
225 $volid = "$storeid:$volname";
228 my $owner = $dat->{$volname}->{vmid
};
230 my $found = grep { $_ eq $volid } @$vollist;
233 next if defined ($vmid) && ($owner ne $vmid);
236 my $info = $dat->{$volname};
237 $info->{volid
} = $volid;
245 sub zfs_get_pool_stats
{
246 my ($class, $scfg) = @_;
251 my $text = $class->zfs_request($scfg, undef, 'get', '-o', 'value', '-Hp',
252 'available,used', $scfg->{pool
});
254 my @lines = split /\n/, $text;
256 if($lines[0] =~ /^(\d+)$/) {
260 if($lines[1] =~ /^(\d+)$/) {
264 return ($available, $used);
267 sub zfs_get_zvol_size
{
268 my ($class, $scfg, $zvol) = @_;
270 my $text = $class->zfs_request($scfg, undef, 'get', '-Hp', 'volsize', "$scfg->{pool}/$zvol");
272 if ($text =~ /volsize\s(\d+)/) {
276 die "Could not get zvol size";
279 sub zfs_create_zvol
{
280 my ($class, $scfg, $zvol, $size) = @_;
282 my $cmd = ['create'];
284 push @$cmd, '-s' if $scfg->{sparse
};
286 push @$cmd, '-b', $scfg->{blocksize
} if $scfg->{blocksize
};
288 push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol";
290 $class->zfs_request($scfg, undef, @$cmd);
293 sub zfs_delete_zvol
{
294 my ($class, $scfg, $zvol) = @_;
298 for (my $i = 0; $i < 6; $i++) {
300 eval { $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol"); };
302 if ($err =~ m/^zfs error:(.*): dataset is busy.*/) {
316 my ($class, $scfg) = @_;
318 my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin', '-t', 'volume', '-Hr');
319 my $zvols = zfs_parse_zvol_list
($text);
320 return undef if !$zvols;
323 foreach my $zvol (@$zvols) {
324 my @values = split('/', $zvol->{name
});
326 my $image = pop @values;
327 my $pool = join('/', @values);
329 next if $image !~ m/^((vm|base)-(\d+)-\S+)$/;
332 my $parent = $zvol->{origin
};
333 if($zvol->{origin
} && $zvol->{origin
} =~ m/^$scfg->{pool}\/(\S
+)$/){
337 $list->{$pool}->{$image} = {
339 size
=> $zvol->{size
},
349 sub zfs_find_free_diskname
{
350 my ($class, $storeid, $scfg, $vmid) = @_;
353 my $volumes = $class->zfs_list_zvol($scfg);
356 my $dat = $volumes->{$scfg->{pool
}};
358 foreach my $image (keys %$dat) {
359 my $volname = $dat->{$image}->{name
};
360 if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
365 for (my $i = 1; $i < 100; $i++) {
366 if (!$disk_ids->{$i}) {
367 return "vm-$vmid-disk-$i";
371 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
374 sub zfs_get_latest_snapshot
{
375 my ($class, $scfg, $volname) = @_;
377 # abort rollback if snapshot is not the latest
378 my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
379 my $text = zfs_request
($class, $scfg, undef, 'list', @params);
380 my @snapshots = split(/\n/, $text);
383 foreach (@snapshots) {
384 if (/$scfg->{pool}\/$volname/) {
394 my ($class, $storeid, $scfg, $cache) = @_;
402 ($free, $used) = $class->zfs_get_pool_stats($scfg);
404 $total = $free + $used;
408 return ($total, $free, $used, $active);
411 sub volume_size_info
{
412 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
414 return $class->zfs_get_zvol_size($scfg, $volname);
417 sub volume_snapshot
{
418 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
420 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$volname\@$snap");
423 sub volume_snapshot_delete
{
424 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
426 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$volname\@$snap");
429 sub volume_snapshot_rollback
{
430 my ($class, $scfg, $storeid, $volname, $snap) = @_;
432 zfs_request
($class, $scfg, undef, 'rollback', "$scfg->{pool}/$volname\@$snap");
435 sub volume_rollback_is_possible
{
436 my ($class, $scfg, $storeid, $volname, $snap) = @_;
438 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
439 if ($snap ne $recentsnap) {
440 die "can't rollback, more recent snapshots exist\n";
446 sub activate_storage
{
447 my ($class, $storeid, $scfg, $cache) = @_;
449 my @param = ('-o', 'name', '-H');
451 my $text = zfs_request
($class, $scfg, undef, 'zpool_list', @param);
453 if ($text !~ $scfg->{pool
}) {
454 run_command
("zpool import -d /dev/disk/by-id/ -a");
459 sub deactivate_storage
{
460 my ($class, $storeid, $scfg, $cache) = @_;
464 sub activate_volume
{
465 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
469 sub deactivate_volume
{
470 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
475 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
477 $snap ||= '__base__';
479 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
480 $class->parse_volname($volname);
482 die "clone_image only works on base images\n" if !$isBase;
484 my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid);
486 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
492 my ($class, $storeid, $scfg, $volname) = @_;
494 my $snap = '__base__';
496 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
497 $class->parse_volname($volname);
499 die "create_base not possible with base image\n" if $isBase;
502 $newname =~ s/^vm-/base-/;
504 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
506 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
508 my $running = undef; #fixme : is create_base always offline ?
510 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
516 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
518 my $new_size = ($size/1024);
520 $class->zfs_request($scfg, undef, 'set', 'volsize=' . $new_size . 'k', "$scfg->{pool}/$volname");
525 sub volume_has_feature
{
526 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
529 snapshot
=> { current
=> 1, snap
=> 1},
530 clone
=> { base
=> 1},
531 template
=> { current
=> 1},
532 copy
=> { base
=> 1, current
=> 1},
535 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
536 $class->parse_volname($volname);
543 $key = $isBase ?
'base' : 'current';
546 return 1 if $features->{$feature}->{$key};