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 my $name = pop @parts;
102 my $pool = join('/', @parts);
104 next unless $name =~ m!^(vm|base|subvol)-(\d+)-(\S+)$!;
107 $name = $pool . '/' . $name;
109 $zvol->{pool
} = $pool;
110 $zvol->{name
} = $name;
111 if ($type eq 'filesystem') {
112 if ($refquota eq 'none') {
115 $zvol->{size
} = zfs_parse_size
($refquota);
117 $zvol->{format
} = 'subvol';
119 $zvol->{size
} = zfs_parse_size
($size);
120 $zvol->{format
} = 'raw';
122 if ($origin !~ /^-$/) {
123 $zvol->{origin
} = $origin;
132 my ($class, $volname) = @_;
134 if ($volname =~ m/^(((base|vm)-(\d+)-\S+)\/)?
((base
)?
(vm
|subvol
)?
-(\d
+)-\S
+)$/) {
135 return ('images', $5, $8, $2, $4, $6);
138 die "unable to parse zfs volume name '$volname'\n";
141 # virtual zfs methods (subclass can overwrite them)
144 my ($class, $scfg, $volname) = @_;
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";
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)
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 "subvolume allocation without name\n" if !$volname;
212 die "illegal name '$volname' - sould be 'subvol-$vmid-*'\n"
213 if $volname !~ m/^subvol-$vmid-/;
215 $class->zfs_create_subvol($scfg, $volname, $size);
218 die "unsupported format '$fmt'";
225 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
227 my (undef, $name, undef) = $class->parse_volname($volname);
229 $class->zfs_delete_zvol($scfg, $name);
235 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
237 $cache->{zfs
} = $class->zfs_list_zvol($scfg) if !$cache->{zfs
};
238 my $zfspool = $scfg->{pool
};
241 if (my $dat = $cache->{zfs
}->{$zfspool}) {
243 foreach my $image (keys %$dat) {
245 my $volname = $dat->{$image}->{name
};
246 my $parent = $dat->{$image}->{parent
};
249 if ($parent && $parent =~ m/^(\S+)@(\S+)$/) {
250 my ($basename) = ($1);
251 $volid = "$storeid:$basename/$volname";
253 $volid = "$storeid:$volname";
256 my $owner = $dat->{$volname}->{vmid
};
258 my $found = grep { $_ eq $volid } @$vollist;
261 next if defined ($vmid) && ($owner ne $vmid);
264 my $info = $dat->{$volname};
265 $info->{volid
} = $volid;
272 sub zfs_get_pool_stats
{
273 my ($class, $scfg) = @_;
278 my $text = $class->zfs_request($scfg, undef, 'get', '-o', 'value', '-Hp',
279 'available,used', $scfg->{pool
});
281 my @lines = split /\n/, $text;
283 if($lines[0] =~ /^(\d+)$/) {
287 if($lines[1] =~ /^(\d+)$/) {
291 return ($available, $used);
294 sub zfs_get_zvol_size
{
295 my ($class, $scfg, $zvol) = @_;
297 my $text = $class->zfs_request($scfg, undef, 'get', '-Hp', 'volsize', "$scfg->{pool}/$zvol");
299 if ($text =~ /volsize\s(\d+)/) {
303 die "Could not get zvol size";
306 sub zfs_create_zvol
{
307 my ($class, $scfg, $zvol, $size) = @_;
309 my $cmd = ['create'];
311 push @$cmd, '-s' if $scfg->{sparse
};
313 push @$cmd, '-b', $scfg->{blocksize
} if $scfg->{blocksize
};
315 push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol";
317 $class->zfs_request($scfg, undef, @$cmd);
320 sub zfs_create_subvol
{
321 my ($class, $scfg, $volname, $size) = @_;
323 my $dataset = "$scfg->{pool}/$volname";
325 my $cmd = ['create', '-o', "refquota=${size}k", $dataset];
327 $class->zfs_request($scfg, undef, @$cmd);
330 sub zfs_delete_zvol
{
331 my ($class, $scfg, $zvol) = @_;
335 for (my $i = 0; $i < 6; $i++) {
337 eval { $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol"); };
339 if ($err =~ m/^zfs error:(.*): dataset is busy.*/) {
353 my ($class, $scfg) = @_;
355 my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin,type,refquota', '-t', 'volume,filesystem', '-Hr');
356 my $zvols = zfs_parse_zvol_list
($text);
357 return undef if !$zvols;
360 foreach my $zvol (@$zvols) {
361 my $pool = $zvol->{pool
};
362 my $name = $zvol->{name
};
363 my $parent = $zvol->{origin
};
364 if($zvol->{origin
} && $zvol->{origin
} =~ m/^$scfg->{pool}\/(\S
+)$/){
368 $list->{$pool}->{$name} = {
370 size
=> $zvol->{size
},
372 format
=> $zvol->{format
},
373 vmid
=> $zvol->{owner
},
380 sub zfs_find_free_diskname
{
381 my ($class, $storeid, $scfg, $vmid) = @_;
384 my $volumes = $class->zfs_list_zvol($scfg);
387 my $dat = $volumes->{$scfg->{pool
}};
389 foreach my $image (keys %$dat) {
390 my $volname = $dat->{$image}->{name
};
391 if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
396 for (my $i = 1; $i < 100; $i++) {
397 if (!$disk_ids->{$i}) {
398 return "vm-$vmid-disk-$i";
402 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
405 sub zfs_get_latest_snapshot
{
406 my ($class, $scfg, $volname) = @_;
408 # abort rollback if snapshot is not the latest
409 my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
410 my $text = zfs_request
($class, $scfg, undef, 'list', @params);
411 my @snapshots = split(/\n/, $text);
414 foreach (@snapshots) {
415 if (/$scfg->{pool}\/$volname/) {
425 my ($class, $storeid, $scfg, $cache) = @_;
433 ($free, $used) = $class->zfs_get_pool_stats($scfg);
435 $total = $free + $used;
439 return ($total, $free, $used, $active);
442 sub volume_size_info
{
443 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
445 return $class->zfs_get_zvol_size($scfg, $volname);
448 sub volume_snapshot
{
449 my ($class, $scfg, $storeid, $volname, $snap) = @_;
451 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$volname\@$snap");
454 sub volume_snapshot_delete
{
455 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
457 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$volname\@$snap");
460 sub volume_snapshot_rollback
{
461 my ($class, $scfg, $storeid, $volname, $snap) = @_;
463 zfs_request
($class, $scfg, undef, 'rollback', "$scfg->{pool}/$volname\@$snap");
466 sub volume_rollback_is_possible
{
467 my ($class, $scfg, $storeid, $volname, $snap) = @_;
469 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
470 if ($snap ne $recentsnap) {
471 die "can't rollback, more recent snapshots exist\n";
477 sub activate_storage
{
478 my ($class, $storeid, $scfg, $cache) = @_;
480 my @param = ('-o', 'name', '-H');
482 my $text = zfs_request
($class, $scfg, undef, 'zpool_list', @param);
484 # Note: $scfg->{pool} can include dataset <pool>/<dataset>
485 my $pool = $scfg->{pool
};
488 if ($text !~ $pool) {
489 run_command
("zpool import -d /dev/disk/by-id/ -a");
494 sub deactivate_storage
{
495 my ($class, $storeid, $scfg, $cache) = @_;
499 sub activate_volume
{
500 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
504 sub deactivate_volume
{
505 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
510 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
512 $snap ||= '__base__';
514 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
515 $class->parse_volname($volname);
517 die "clone_image only works on base images\n" if !$isBase;
519 my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid);
521 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
527 my ($class, $storeid, $scfg, $volname) = @_;
529 my $snap = '__base__';
531 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
532 $class->parse_volname($volname);
534 die "create_base not possible with base image\n" if $isBase;
537 $newname =~ s/^vm-/base-/;
539 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
541 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
543 my $running = undef; #fixme : is create_base always offline ?
545 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
551 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
553 my $new_size = ($size/1024);
555 $class->zfs_request($scfg, undef, 'set', 'volsize=' . $new_size . 'k', "$scfg->{pool}/$volname");
560 sub volume_has_feature
{
561 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
564 snapshot
=> { current
=> 1, snap
=> 1},
565 clone
=> { base
=> 1},
566 template
=> { current
=> 1},
567 copy
=> { base
=> 1, current
=> 1},
570 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
571 $class->parse_volname($volname);
578 $key = $isBase ?
'base' : 'current';
581 return 1 if $features->{$feature}->{$key};