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+(.+)$/) {
97 my @parts = split /\//, $1;
98 my $name = pop @parts;
99 my $pool = join('/', @parts);
101 next unless $name =~ m!^(\w+)-(\d+)-(\w+)-(\d+)$!;
102 $name = $pool . '/' . $name;
104 $zvol->{pool
} = $pool;
105 $zvol->{name
} = $name;
106 $zvol->{size
} = zfs_parse_size
($2);
108 $zvol->{origin
} = $3;
118 my ($class, $volname) = @_;
120 if ($volname =~ m/^(((base|vm)-(\d+)-\S+)\/)?
((base
)?
(vm
)?
-(\d
+)-\S
+)$/) {
121 return ('images', $5, $8, $2, $4, $6);
124 die "unable to parse zfs volume name '$volname'\n";
127 # virtual zfs methods (subclass can overwrite them)
130 my ($class, $scfg, $volname) = @_;
132 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
136 if($vtype eq "images"){
137 $path = "/dev/zvol/$scfg->{pool}/$volname";
139 die "$vtype is not allowed in ZFSPool!";
142 return ($path, $vmid, $vtype);
146 my ($class, $scfg, $timeout, $method, @params) = @_;
148 $timeout = 5 if !$timeout;
152 if ($method eq 'zpool_list') {
153 push @$cmd, 'zpool', 'list';
155 push @$cmd, 'zfs', $method;
167 run_command
($cmd, outfunc
=> $output, timeout
=> $timeout);
173 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
175 die "unsupported format '$fmt'" if $fmt ne 'raw';
177 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
178 if $name && $name !~ m/^vm-$vmid-/;
180 $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid) if !$name;
182 $class->zfs_create_zvol($scfg, $name, $size);
184 my $devname = "/dev/zvol/$scfg->{pool}/$name";
186 run_command
("udevadm trigger --subsystem-match block");
187 system("udevadm settle --timeout 10 --exit-if-exists=${devname}");
193 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
195 my (undef, $name, undef) = $class->parse_volname($volname);
197 $class->zfs_delete_zvol($scfg, $name);
203 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
205 $cache->{zfs
} = $class->zfs_list_zvol($scfg) if !$cache->{zfs
};
206 my $zfspool = $scfg->{pool
};
209 if (my $dat = $cache->{zfs
}->{$zfspool}) {
211 foreach my $image (keys %$dat) {
213 my $volname = $dat->{$image}->{name
};
214 my $parent = $dat->{$image}->{parent
};
217 if ($parent && $parent =~ m/^(\S+)@(\S+)$/) {
218 my ($basename) = ($1);
219 $volid = "$storeid:$basename/$volname";
221 $volid = "$storeid:$volname";
224 my $owner = $dat->{$volname}->{vmid
};
226 my $found = grep { $_ eq $volid } @$vollist;
229 next if defined ($vmid) && ($owner ne $vmid);
232 my $info = $dat->{$volname};
233 $info->{volid
} = $volid;
241 sub zfs_get_pool_stats
{
242 my ($class, $scfg) = @_;
247 my $text = $class->zfs_request($scfg, undef, 'get', '-o', 'value', '-Hp',
248 'available,used', $scfg->{pool
});
250 my @lines = split /\n/, $text;
252 if($lines[0] =~ /^(\d+)$/) {
256 if($lines[1] =~ /^(\d+)$/) {
260 return ($available, $used);
263 sub zfs_get_zvol_size
{
264 my ($class, $scfg, $zvol) = @_;
266 my $text = $class->zfs_request($scfg, undef, 'get', '-Hp', 'volsize', "$scfg->{pool}/$zvol");
268 if ($text =~ /volsize\s(\d+)/) {
272 die "Could not get zvol size";
275 sub zfs_create_zvol
{
276 my ($class, $scfg, $zvol, $size) = @_;
278 my $cmd = ['create'];
280 push @$cmd, '-s' if $scfg->{sparse
};
282 push @$cmd, '-b', $scfg->{blocksize
} if $scfg->{blocksize
};
284 push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol";
286 $class->zfs_request($scfg, undef, @$cmd);
289 sub zfs_delete_zvol
{
290 my ($class, $scfg, $zvol) = @_;
292 $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol");
296 my ($class, $scfg) = @_;
298 my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin', '-t', 'volume', '-Hr');
299 my $zvols = zfs_parse_zvol_list
($text);
300 return undef if !$zvols;
303 foreach my $zvol (@$zvols) {
304 my @values = split('/', $zvol->{name
});
306 my $image = pop @values;
307 my $pool = join('/', @values);
309 next if $image !~ m/^((vm|base)-(\d+)-\S+)$/;
312 my $parent = $zvol->{origin
};
313 if($zvol->{origin
} && $zvol->{origin
} =~ m/^$scfg->{pool}\/(\S
+)$/){
317 $list->{$pool}->{$image} = {
319 size
=> $zvol->{size
},
329 sub zfs_find_free_diskname
{
330 my ($class, $storeid, $scfg, $vmid) = @_;
333 my $volumes = $class->zfs_list_zvol($scfg);
336 my $dat = $volumes->{$scfg->{pool
}};
338 foreach my $image (keys %$dat) {
339 my $volname = $dat->{$image}->{name
};
340 if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
345 for (my $i = 1; $i < 100; $i++) {
346 if (!$disk_ids->{$i}) {
347 return "vm-$vmid-disk-$i";
351 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
354 sub zfs_get_latest_snapshot
{
355 my ($class, $scfg, $volname) = @_;
357 # abort rollback if snapshot is not the latest
358 my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
359 my $text = zfs_request
($class, $scfg, undef, 'list', @params);
360 my @snapshots = split(/\n/, $text);
363 foreach (@snapshots) {
364 if (/$scfg->{pool}\/$volname/) {
374 my ($class, $storeid, $scfg, $cache) = @_;
382 ($free, $used) = $class->zfs_get_pool_stats($scfg);
384 $total = $free + $used;
388 return ($total, $free, $used, $active);
391 sub volume_size_info
{
392 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
394 return $class->zfs_get_zvol_size($scfg, $volname);
397 sub volume_snapshot
{
398 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
400 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$volname\@$snap");
403 sub volume_snapshot_delete
{
404 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
406 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$volname\@$snap");
409 sub volume_snapshot_rollback
{
410 my ($class, $scfg, $storeid, $volname, $snap) = @_;
412 zfs_request
($class, $scfg, undef, 'rollback', "$scfg->{pool}/$volname\@$snap");
415 sub volume_rollback_is_possible
{
416 my ($class, $scfg, $storeid, $volname, $snap) = @_;
418 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
419 if ($snap ne $recentsnap) {
420 die "can't rollback, more recent snapshots exist\n";
426 sub activate_storage
{
427 my ($class, $storeid, $scfg, $cache) = @_;
429 my @param = ('-o', 'name', '-H');
431 my $text = zfs_request
($class, $scfg, undef, 'zpool_list', @param);
433 if ($text !~ $scfg->{pool
}) {
434 run_command
("zpool import -d /dev/disk/by-id/ -a");
439 sub deactivate_storage
{
440 my ($class, $storeid, $scfg, $cache) = @_;
444 sub activate_volume
{
445 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
449 sub deactivate_volume
{
450 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
455 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
457 $snap ||= '__base__';
459 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
460 $class->parse_volname($volname);
462 die "clone_image only works on base images\n" if !$isBase;
464 my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid);
466 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
472 my ($class, $storeid, $scfg, $volname) = @_;
474 my $snap = '__base__';
476 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
477 $class->parse_volname($volname);
479 die "create_base not possible with base image\n" if $isBase;
482 $newname =~ s/^vm-/base-/;
484 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
486 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
488 my $running = undef; #fixme : is create_base always offline ?
490 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
496 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
498 my $new_size = ($size/1024);
500 $class->zfs_request($scfg, undef, 'set', 'volsize=' . $new_size . 'k', "$scfg->{pool}/$volname");
505 sub volume_has_feature
{
506 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
509 snapshot
=> { current
=> 1, snap
=> 1},
510 clone
=> { base
=> 1},
511 template
=> { current
=> 1},
512 copy
=> { base
=> 1, current
=> 1},
515 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
516 $class->parse_volname($volname);
523 $key = $isBase ?
'base' : 'current';
526 return 1 if $features->{$feature}->{$key};