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, 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)-(\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|vm)-(\d+)-\S+)\/)?
((base
)?
(vm
|subvol
)?
-(\d
+)-\S
+)$/) {
133 my $format = $7 && $7 eq 'subvol' ?
'subvol' : 'raw';
134 return ('images', $5, $8, $2, $4, $6, $format);
137 die "unable to parse zfs volume name '$volname'\n";
140 # virtual zfs methods (subclass can overwrite them)
143 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
145 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
149 if ($vtype eq "images") {
150 if ($volname =~ m/^subvol-/) {
151 # fixme: we currently assume standard mount point?!
152 $path = "/$scfg->{pool}/$volname";
154 $path = "/dev/zvol/$scfg->{pool}/$volname";
156 $path .= "\@$snapname" if defined($snapname);
158 die "$vtype is not allowed in ZFSPool!";
161 return ($path, $vmid, $vtype);
165 my ($class, $scfg, $timeout, $method, @params) = @_;
167 $timeout = 5 if !$timeout;
171 if ($method eq 'zpool_list') {
172 push @$cmd, 'zpool', 'list';
174 push @$cmd, 'zfs', $method;
186 run_command
($cmd, errmsg
=> "zfs error", outfunc
=> $output, timeout
=> $timeout);
192 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
198 die "illegal name '$volname' - sould be 'vm-$vmid-*'\n"
199 if $volname && $volname !~ m/^vm-$vmid-/;
200 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt)
203 $class->zfs_create_zvol($scfg, $volname, $size);
204 my $devname = "/dev/zvol/$scfg->{pool}/$volname";
206 run_command
("udevadm trigger --subsystem-match block");
207 system("udevadm settle --timeout 10 --exit-if-exists=${devname}");
209 } elsif ( $fmt eq 'subvol') {
211 die "illegal name '$volname' - sould be 'subvol-$vmid-*'\n"
212 if $volname && $volname !~ m/^subvol-$vmid-/;
213 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt)
216 die "illegal name '$volname' - sould be 'subvol-$vmid-*'\n"
217 if $volname !~ m/^subvol-$vmid-/;
219 $class->zfs_create_subvol($scfg, $volname, $size);
222 die "unsupported format '$fmt'";
229 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
231 my (undef, $name, undef) = $class->parse_volname($volname);
233 $class->zfs_delete_zvol($scfg, $name);
239 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
241 $cache->{zfs
} = $class->zfs_list_zvol($scfg) if !$cache->{zfs
};
242 my $zfspool = $scfg->{pool
};
245 if (my $dat = $cache->{zfs
}->{$zfspool}) {
247 foreach my $image (keys %$dat) {
249 my $volname = $dat->{$image}->{name
};
250 my $parent = $dat->{$image}->{parent
};
253 if ($parent && $parent =~ m/^(\S+)@(\S+)$/) {
254 my ($basename) = ($1);
255 $volid = "$storeid:$basename/$volname";
257 $volid = "$storeid:$volname";
260 my $owner = $dat->{$volname}->{vmid
};
262 my $found = grep { $_ eq $volid } @$vollist;
265 next if defined ($vmid) && ($owner ne $vmid);
268 my $info = $dat->{$volname};
269 $info->{volid
} = $volid;
276 sub zfs_get_pool_stats
{
277 my ($class, $scfg) = @_;
282 my $text = $class->zfs_request($scfg, undef, 'get', '-o', 'value', '-Hp',
283 'available,used', $scfg->{pool
});
285 my @lines = split /\n/, $text;
287 if($lines[0] =~ /^(\d+)$/) {
291 if($lines[1] =~ /^(\d+)$/) {
295 return ($available, $used);
298 sub zfs_create_zvol
{
299 my ($class, $scfg, $zvol, $size) = @_;
301 my $cmd = ['create'];
303 push @$cmd, '-s' if $scfg->{sparse
};
305 push @$cmd, '-b', $scfg->{blocksize
} if $scfg->{blocksize
};
307 push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol";
309 $class->zfs_request($scfg, undef, @$cmd);
312 sub zfs_create_subvol
{
313 my ($class, $scfg, $volname, $size) = @_;
315 my $dataset = "$scfg->{pool}/$volname";
317 my $cmd = ['create', '-o', 'acltype=posixacl', '-o', 'xattr=sa',
318 '-o', "refquota=${size}k", $dataset];
320 $class->zfs_request($scfg, undef, @$cmd);
323 sub zfs_delete_zvol
{
324 my ($class, $scfg, $zvol) = @_;
328 for (my $i = 0; $i < 6; $i++) {
330 eval { $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol"); };
332 if ($err =~ m/^zfs error:(.*): dataset is busy.*/) {
334 } elsif ($err =~ m/^zfs error:.*: dataset does not exist.*$/) {
349 my ($class, $scfg) = @_;
351 my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hr');
352 my $zvols = zfs_parse_zvol_list
($text);
353 return undef if !$zvols;
356 foreach my $zvol (@$zvols) {
357 my $pool = $zvol->{pool
};
358 my $name = $zvol->{name
};
359 my $parent = $zvol->{origin
};
360 if($zvol->{origin
} && $zvol->{origin
} =~ m/^$scfg->{pool}\/(\S
+)$/){
364 $list->{$pool}->{$name} = {
366 size
=> $zvol->{size
},
368 format
=> $zvol->{format
},
369 vmid
=> $zvol->{owner
},
376 sub zfs_find_free_diskname
{
377 my ($class, $storeid, $scfg, $vmid, $format) = @_;
380 my $volumes = $class->zfs_list_zvol($scfg);
383 my $dat = $volumes->{$scfg->{pool
}};
385 foreach my $image (keys %$dat) {
386 my $volname = $dat->{$image}->{name
};
387 if ($volname =~ m/(vm|base|subvol)-$vmid-disk-(\d+)/){
392 for (my $i = 1; $i < 100; $i++) {
393 if (!$disk_ids->{$i}) {
394 return $format eq 'subvol' ?
"subvol-$vmid-disk-$i" : "vm-$vmid-disk-$i";
398 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
401 sub zfs_get_latest_snapshot
{
402 my ($class, $scfg, $volname) = @_;
404 # abort rollback if snapshot is not the latest
405 my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
406 my $text = $class->zfs_request($scfg, undef, 'list', @params);
407 my @snapshots = split(/\n/, $text);
410 foreach (@snapshots) {
411 if (/$scfg->{pool}\/$volname/) {
421 my ($class, $storeid, $scfg, $cache) = @_;
429 ($free, $used) = $class->zfs_get_pool_stats($scfg);
431 $total = $free + $used;
435 return ($total, $free, $used, $active);
438 sub volume_size_info
{
439 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
441 my (undef, undef, undef, undef, undef, undef, $format) =
442 $class->parse_volname($volname);
444 my $attr = $format eq 'subvol' ?
'refquota' : 'volsize';
445 my $text = $class->zfs_request($scfg, undef, 'get', '-Hp', $attr, "$scfg->{pool}/$volname");
447 if ($text =~ /\s$attr\s(\d+)\s/) {
451 die "Could not get zfs volume size\n";
454 sub volume_snapshot
{
455 my ($class, $scfg, $storeid, $volname, $snap) = @_;
457 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$volname\@$snap");
460 sub volume_snapshot_delete
{
461 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
463 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
464 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$volname\@$snap");
467 sub volume_snapshot_rollback
{
468 my ($class, $scfg, $storeid, $volname, $snap) = @_;
470 $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$volname\@$snap");
473 sub volume_rollback_is_possible
{
474 my ($class, $scfg, $storeid, $volname, $snap) = @_;
476 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
477 if ($snap ne $recentsnap) {
478 die "can't rollback, more recent snapshots exist\n";
484 sub activate_storage
{
485 my ($class, $storeid, $scfg, $cache) = @_;
487 my @param = ('-o', 'name', '-H');
489 my $text = $class->zfs_request($scfg, undef, 'zpool_list', @param);
491 # Note: $scfg->{pool} can include dataset <pool>/<dataset>
492 my $pool = $scfg->{pool
};
495 if ($text !~ $pool) {
496 run_command
("zpool import -d /dev/disk/by-id/ -a");
501 sub deactivate_storage
{
502 my ($class, $storeid, $scfg, $cache) = @_;
506 sub activate_volume
{
507 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
511 sub deactivate_volume
{
512 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
517 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
519 $snap ||= '__base__';
521 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
522 $class->parse_volname($volname);
524 die "clone_image only works on base images\n" if !$isBase;
526 my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $format);
528 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
534 my ($class, $storeid, $scfg, $volname) = @_;
536 my $snap = '__base__';
538 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
539 $class->parse_volname($volname);
541 die "create_base not possible with base image\n" if $isBase;
544 $newname =~ s/^vm-/base-/;
546 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
548 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
550 my $running = undef; #fixme : is create_base always offline ?
552 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
558 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
560 my $new_size = int($size/1024);
562 my (undef, undef, undef, undef, undef, undef, $format) =
563 $class->parse_volname($volname);
565 my $attr = $format eq 'subvol' ?
'refquota' : 'volsize';
567 $class->zfs_request($scfg, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$volname");
572 sub volume_has_feature
{
573 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
576 snapshot
=> { current
=> 1, snap
=> 1},
577 clone
=> { base
=> 1},
578 template
=> { current
=> 1},
579 copy
=> { base
=> 1, current
=> 1},
580 sparseinit
=> { base
=> 1, current
=> 1},
583 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
584 $class->parse_volname($volname);
591 $key = $isBase ?
'base' : 'current';
594 return 1 if $features->{$feature}->{$key};