]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/ZFSPoolPlugin.pm
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 if ($pool !~ /^rpool$/) {
102 next unless $name =~ m!^(\w+)-(\d+)-(\w+)-(\d+)$!;
103 $name = $pool . '/' . $name;
108 $zvol->{pool
} = $pool;
109 $zvol->{name
} = $name;
110 $zvol->{size
} = zfs_parse_size
($2);
112 $zvol->{origin
} = $3;
122 my ($class, $volname) = @_;
124 if ($volname =~ m/^(((base|vm)-(\d+)-\S+)\/)?
((base
)?
(vm
)?
-(\d
+)-\S
+)$/) {
125 return ('images', $5, $8, $2, $4, $6);
128 die "unable to parse zfs volume name '$volname'\n";
131 # virtual zfs methods (subclass can overwrite them)
134 my ($class, $scfg, $volname) = @_;
136 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
140 if($vtype eq "images"){
141 $path = "/dev/zvol/$scfg->{pool}/$volname";
143 die "$vtype is not allowed in ZFSPool!";
146 return ($path, $vmid, $vtype);
150 my ($class, $scfg, $timeout, $method, @params) = @_;
152 $timeout = 5 if !$timeout;
156 if ($method eq 'zpool_list') {
157 push @$cmd = 'zpool', 'list';
159 push @$cmd, 'zfs', $method;
171 run_command
($cmd, outfunc
=> $output, timeout
=> $timeout);
177 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
179 die "unsupported format '$fmt'" if $fmt ne 'raw';
181 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
182 if $name && $name !~ m/^vm-$vmid-/;
184 $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid) if !$name;
186 $class->zfs_create_zvol($scfg, $name, $size);
192 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
194 my (undef, $name, undef) = $class->parse_volname($volname);
196 $class->zfs_delete_zvol($scfg, $name);
202 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
204 $cache->{zfs
} = $class->zfs_list_zvol($scfg) if !$cache->{zfs
};
205 my $zfspool = $scfg->{pool
};
208 if (my $dat = $cache->{zfs
}->{$zfspool}) {
210 foreach my $image (keys %$dat) {
212 my $volname = $dat->{$image}->{name
};
213 my $parent = $dat->{$image}->{parent
};
216 if ($parent && $parent =~ m/^(\S+)@(\S+)$/) {
217 my ($basename) = ($1);
218 $volid = "$storeid:$basename/$volname";
220 $volid = "$storeid:$volname";
223 my $owner = $dat->{$volname}->{vmid
};
225 my $found = grep { $_ eq $volid } @$vollist;
228 next if defined ($vmid) && ($owner ne $vmid);
231 my $info = $dat->{$volname};
232 $info->{volid
} = $volid;
240 sub zfs_get_pool_stats
{
241 my ($class, $scfg) = @_;
246 my $text = $class->zfs_request($scfg, undef, 'get', '-o', 'value', '-Hp',
247 'available,used', $scfg->{pool
});
249 my @lines = split /\n/, $text;
251 if($lines[0] =~ /^(\d+)$/) {
255 if($lines[1] =~ /^(\d+)$/) {
259 return ($available, $used);
262 sub zfs_get_zvol_size
{
263 my ($class, $scfg, $zvol) = @_;
265 my $text = $class->zfs_request($scfg, undef, 'get', '-Hp', 'volsize', "$scfg->{pool}/$zvol");
267 if ($text =~ /volsize\s(\d+)/) {
271 die "Could not get zvol size";
274 sub zfs_create_zvol
{
275 my ($class, $scfg, $zvol, $size) = @_;
277 my $cmd = ['create'];
279 push @$cmd, '-s' if $scfg->{sparse
};
281 push @$cmd, '-b', $scfg->{blocksize
} if $scfg->{blocksize
};
283 push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol";
285 $class->zfs_request($scfg, undef, @$cmd);
288 sub zfs_delete_zvol
{
289 my ($class, $scfg, $zvol) = @_;
291 $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol");
295 my ($class, $scfg) = @_;
297 my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin', '-t', 'volume', '-Hr');
298 my $zvols = zfs_parse_zvol_list
($text);
299 return undef if !$zvols;
302 foreach my $zvol (@$zvols) {
303 my @values = split('/', $zvol->{name
});
305 my $image = pop @values;
306 my $pool = join('/', @values);
308 next if $image !~ m/^((vm|base)-(\d+)-\S+)$/;
311 my $parent = $zvol->{origin
};
312 if($zvol->{origin
} && $zvol->{origin
} =~ m/^$scfg->{pool}\/(\S
+)$/){
316 $list->{$pool}->{$image} = {
318 size
=> $zvol->{size
},
328 sub zfs_find_free_diskname
{
329 my ($class, $storeid, $scfg, $vmid) = @_;
332 my $volumes = $class->zfs_list_zvol($scfg);
335 my $dat = $volumes->{$scfg->{pool
}};
337 foreach my $image (keys %$dat) {
338 my $volname = $dat->{$image}->{name
};
339 if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
344 for (my $i = 1; $i < 100; $i++) {
345 if (!$disk_ids->{$i}) {
346 return "vm-$vmid-disk-$i";
350 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
353 sub zfs_get_latest_snapshot
{
354 my ($class, $scfg, $volname) = @_;
356 # abort rollback if snapshot is not the latest
357 my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
358 my $text = zfs_request
($class, $scfg, undef, 'list', @params);
359 my @snapshots = split(/\n/, $text);
362 foreach (@snapshots) {
363 if (/$scfg->{pool}\/$volname/) {
373 my ($class, $storeid, $scfg, $cache) = @_;
381 ($free, $used) = $class->zfs_get_pool_stats($scfg);
383 $total = $free + $used;
387 return ($total, $free, $used, $active);
390 sub volume_size_info
{
391 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
393 return $class->zfs_get_zvol_size($scfg, $volname);
396 sub volume_snapshot
{
397 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
399 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$volname\@$snap");
402 sub volume_snapshot_delete
{
403 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
405 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$volname\@$snap");
408 sub volume_snapshot_rollback
{
409 my ($class, $scfg, $storeid, $volname, $snap) = @_;
411 # abort rollback if snapshot is not the latest
412 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
413 if ($snap ne $recentsnap) {
414 die "cannot rollback, more recent snapshots exist\n";
417 zfs_request
($class, $scfg, undef, 'rollback', "$scfg->{pool}/$volname\@$snap");
420 sub activate_volume
{
421 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
425 sub deactivate_volume
{
426 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
431 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
433 $snap ||= '__base__';
435 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
436 $class->parse_volname($volname);
438 die "clone_image only works on base images\n" if !$isBase;
440 my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid);
442 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
448 my ($class, $storeid, $scfg, $volname) = @_;
450 my $snap = '__base__';
452 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
453 $class->parse_volname($volname);
455 die "create_base not possible with base image\n" if $isBase;
458 $newname =~ s/^vm-/base-/;
460 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
462 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
464 my $running = undef; #fixme : is create_base always offline ?
466 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
471 sub volume_has_feature
{
472 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
475 snapshot
=> { current
=> 1, snap
=> 1},
476 clone
=> { base
=> 1},
477 template
=> { current
=> 1},
478 copy
=> { base
=> 1, current
=> 1},
481 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
482 $class->parse_volname($volname);
489 $key = $isBase ?
'base' : 'current';
492 return 1 if $features->{$feature}->{$key};