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 maxfiles
=> { 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)-(\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|vm)-(\d+)-\S+)\/)?
((base
)?
(vm
|subvol
)?
-(\d
+)-\S
+)$/) {
134 my $format = $7 && $7 eq 'subvol' ?
'subvol' : 'raw';
135 return ('images', $5, $8, $2, $4, $6, $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 ($volname =~ m/^subvol-/) {
152 # fixme: we currently assume standard mount point?!
153 $path = "/$scfg->{pool}/$volname";
155 $path = "/dev/zvol/$scfg->{pool}/$volname";
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 $timeout = 5 if !$timeout;
172 if ($method eq 'zpool_list') {
173 push @$cmd, 'zpool', 'list';
175 push @$cmd, 'zfs', $method;
187 run_command
($cmd, errmsg
=> "zfs error", outfunc
=> $output, timeout
=> $timeout);
193 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
199 die "illegal name '$volname' - sould be 'vm-$vmid-*'\n"
200 if $volname && $volname !~ m/^vm-$vmid-/;
201 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt)
204 $class->zfs_create_zvol($scfg, $volname, $size);
205 my $devname = "/dev/zvol/$scfg->{pool}/$volname";
207 run_command
("udevadm trigger --subsystem-match block");
208 system("udevadm settle --timeout 10 --exit-if-exists=${devname}");
210 } elsif ( $fmt eq 'subvol') {
212 die "illegal name '$volname' - sould be 'subvol-$vmid-*'\n"
213 if $volname && $volname !~ m/^subvol-$vmid-/;
214 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt)
217 die "illegal name '$volname' - sould be 'subvol-$vmid-*'\n"
218 if $volname !~ m/^subvol-$vmid-/;
220 $class->zfs_create_subvol($scfg, $volname, $size);
223 die "unsupported format '$fmt'";
230 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
232 my (undef, $name, undef) = $class->parse_volname($volname);
234 $class->zfs_delete_zvol($scfg, $name);
240 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
242 $cache->{zfs
} = $class->zfs_list_zvol($scfg) if !$cache->{zfs
};
243 my $zfspool = $scfg->{pool
};
246 if (my $dat = $cache->{zfs
}->{$zfspool}) {
248 foreach my $image (keys %$dat) {
250 my $volname = $dat->{$image}->{name
};
251 my $parent = $dat->{$image}->{parent
};
254 if ($parent && $parent =~ m/^(\S+)@(\S+)$/) {
255 my ($basename) = ($1);
256 $volid = "$storeid:$basename/$volname";
258 $volid = "$storeid:$volname";
261 my $owner = $dat->{$volname}->{vmid
};
263 my $found = grep { $_ eq $volid } @$vollist;
266 next if defined ($vmid) && ($owner ne $vmid);
269 my $info = $dat->{$volname};
270 $info->{volid
} = $volid;
277 sub zfs_get_pool_stats
{
278 my ($class, $scfg) = @_;
283 my $text = $class->zfs_request($scfg, undef, 'get', '-o', 'value', '-Hp',
284 'available,used', $scfg->{pool
});
286 my @lines = split /\n/, $text;
288 if($lines[0] =~ /^(\d+)$/) {
292 if($lines[1] =~ /^(\d+)$/) {
296 return ($available, $used);
299 sub zfs_create_zvol
{
300 my ($class, $scfg, $zvol, $size) = @_;
302 my $cmd = ['create'];
304 push @$cmd, '-s' if $scfg->{sparse
};
306 push @$cmd, '-b', $scfg->{blocksize
} if $scfg->{blocksize
};
308 push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol";
310 $class->zfs_request($scfg, undef, @$cmd);
313 sub zfs_create_subvol
{
314 my ($class, $scfg, $volname, $size) = @_;
316 my $dataset = "$scfg->{pool}/$volname";
318 my $cmd = ['create', '-o', 'acltype=posixacl', '-o', 'xattr=sa',
319 '-o', "refquota=${size}k", $dataset];
321 $class->zfs_request($scfg, undef, @$cmd);
324 sub zfs_delete_zvol
{
325 my ($class, $scfg, $zvol) = @_;
329 for (my $i = 0; $i < 6; $i++) {
331 eval { $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol"); };
333 if ($err =~ m/^zfs error:(.*): dataset is busy.*/) {
335 } elsif ($err =~ m/^zfs error:.*: dataset does not exist.*$/) {
350 my ($class, $scfg) = @_;
352 my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hr');
353 my $zvols = zfs_parse_zvol_list
($text);
354 return undef if !$zvols;
357 foreach my $zvol (@$zvols) {
358 my $pool = $zvol->{pool
};
359 my $name = $zvol->{name
};
360 my $parent = $zvol->{origin
};
361 if($zvol->{origin
} && $zvol->{origin
} =~ m/^$scfg->{pool}\/(\S
+)$/){
365 $list->{$pool}->{$name} = {
367 size
=> $zvol->{size
},
369 format
=> $zvol->{format
},
370 vmid
=> $zvol->{owner
},
377 sub zfs_find_free_diskname
{
378 my ($class, $storeid, $scfg, $vmid, $format) = @_;
381 my $volumes = $class->zfs_list_zvol($scfg);
384 my $dat = $volumes->{$scfg->{pool
}};
386 foreach my $image (keys %$dat) {
387 my $volname = $dat->{$image}->{name
};
388 if ($volname =~ m/(vm|base|subvol)-$vmid-disk-(\d+)/){
393 for (my $i = 1; $i < 100; $i++) {
394 if (!$disk_ids->{$i}) {
395 return $format eq 'subvol' ?
"subvol-$vmid-disk-$i" : "vm-$vmid-disk-$i";
399 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
402 sub zfs_get_latest_snapshot
{
403 my ($class, $scfg, $volname) = @_;
405 # abort rollback if snapshot is not the latest
406 my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
407 my $text = $class->zfs_request($scfg, undef, 'list', @params);
408 my @snapshots = split(/\n/, $text);
411 foreach (@snapshots) {
412 if (/$scfg->{pool}\/$volname/) {
422 my ($class, $storeid, $scfg, $cache) = @_;
430 ($free, $used) = $class->zfs_get_pool_stats($scfg);
432 $total = $free + $used;
436 return ($total, $free, $used, $active);
439 sub volume_size_info
{
440 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
442 my (undef, undef, undef, undef, undef, undef, $format) =
443 $class->parse_volname($volname);
445 my $attr = $format eq 'subvol' ?
'refquota' : 'volsize';
446 my $text = $class->zfs_request($scfg, undef, 'get', '-Hp', $attr, "$scfg->{pool}/$volname");
448 if ($text =~ /\s$attr\s(\d+)\s/) {
452 die "Could not get zfs volume size\n";
455 sub volume_snapshot
{
456 my ($class, $scfg, $storeid, $volname, $snap) = @_;
458 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$volname\@$snap");
461 sub volume_snapshot_delete
{
462 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
464 $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
465 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$volname\@$snap");
468 sub volume_snapshot_rollback
{
469 my ($class, $scfg, $storeid, $volname, $snap) = @_;
471 $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$volname\@$snap");
474 sub volume_rollback_is_possible
{
475 my ($class, $scfg, $storeid, $volname, $snap) = @_;
477 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
478 if ($snap ne $recentsnap) {
479 die "can't rollback, more recent snapshots exist\n";
485 sub activate_storage
{
486 my ($class, $storeid, $scfg, $cache) = @_;
488 my @param = ('-o', 'name', '-H');
490 my $text = $class->zfs_request($scfg, undef, 'zpool_list', @param);
492 # Note: $scfg->{pool} can include dataset <pool>/<dataset>
493 my $pool = $scfg->{pool
};
496 if ($text !~ $pool) {
497 run_command
("zpool import -d /dev/disk/by-id/ -a");
502 sub deactivate_storage
{
503 my ($class, $storeid, $scfg, $cache) = @_;
507 sub activate_volume
{
508 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
512 sub deactivate_volume
{
513 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
518 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
520 $snap ||= '__base__';
522 my ($vtype, $basename, $basevmid, undef, undef, $isBase, $format) =
523 $class->parse_volname($volname);
525 die "clone_image only works on base images\n" if !$isBase;
527 my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $format);
529 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
535 my ($class, $storeid, $scfg, $volname) = @_;
537 my $snap = '__base__';
539 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
540 $class->parse_volname($volname);
542 die "create_base not possible with base image\n" if $isBase;
545 $newname =~ s/^vm-/base-/;
547 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
549 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
551 my $running = undef; #fixme : is create_base always offline ?
553 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
559 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
561 my $new_size = int($size/1024);
563 my (undef, undef, undef, undef, undef, undef, $format) =
564 $class->parse_volname($volname);
566 my $attr = $format eq 'subvol' ?
'refquota' : 'volsize';
568 $class->zfs_request($scfg, undef, 'set', "$attr=${new_size}k", "$scfg->{pool}/$volname");
573 sub volume_has_feature
{
574 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
577 snapshot
=> { current
=> 1, snap
=> 1},
578 clone
=> { base
=> 1},
579 template
=> { current
=> 1},
580 copy
=> { 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};