]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/ZFSDirPlugin.pm
9138d2fb240e35fc5287e74a54e79af25070d672
1 package PVE
::Storage
::ZFSDirPlugin
;
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, vztmpl
=> 1, iso
=> 1, backup
=> 1},
27 description
=> "block size",
31 description
=> "use sparse volumes",
39 path
=> { fixed
=> 1 },
40 pool
=> { fixed
=> 1 },
41 blocksize
=> { optional
=> 1 },
42 sparse
=> { optional
=> 1 },
43 nodes
=> { optional
=> 1 },
44 disable
=> { optional
=> 1 },
45 maxfiles
=> { optional
=> 1 },
46 content
=> { optional
=> 1 },
50 # static zfs helper methods
57 if ($text =~ m/^(\d+(\.\d+)?)([TGMK])?$/) {
59 my ($size, $reminder, $unit) = ($1, $2, $3);
64 } elsif ($unit eq 'M') {
66 } elsif ($unit eq 'G') {
67 $size *= 1024*1024*1024;
68 } elsif ($unit eq 'T') {
69 $size *= 1024*1024*1024*1024;
71 die "got unknown zfs size unit '$unit'\n";
83 warn "unable to parse zfs size '$text'\n";
88 sub zfs_parse_zvol_list
{
93 return $list if !$text;
95 my @lines = split /\n/, $text;
96 foreach my $line (@lines) {
97 if ($line =~ /^(.+)\s+([a-zA-Z0-9\.]+|\-)\s+(.+)$/) {
99 my @parts = split /\//, $1;
100 my $name = pop @parts;
101 my $pool = join('/', @parts);
103 if ($pool !~ /^rpool$/) {
104 next unless $name =~ m!^(\w+)-(\d+)-(\w+)-(\d+)$!;
105 $name = $pool . '/' . $name;
110 $zvol->{pool
} = $pool;
111 $zvol->{name
} = $name;
112 $zvol->{size
} = zfs_parse_size
($2);
114 $zvol->{origin
} = $3;
124 my ($class, $volname) = @_;
126 if ($volname =~ m/^(((base|vm)-(\d+)-\S+)\/)?
((base
)?
(vm
)?
-(\d
+)-\S
+)$/) {
127 return ('images', $5, $8, $2, $4, $6);
130 die "unable to parse zfs volume name '$volname'\n";
133 # virtual zfs methods (subclass can overwrite them)
136 my ($class, $scfg, $volname) = @_;
138 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
142 if($vtype eq "images"){
143 $path = "/dev/zvol/$scfg->{pool}/$volname";
145 die "$vtype is not allowed in ZFSDir!";
148 return ($path, $vmid, $vtype);
152 my ($class, $scfg, $timeout, $method, @params) = @_;
154 $timeout = 5 if !$timeout;
158 if ($method eq 'zpool_list') {
159 push @$cmd = 'zpool', 'list';
161 push @$cmd, 'zfs', $method;
173 run_command
($cmd, outfunc
=> $output, timeout
=> $timeout);
179 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
181 die "unsupported format '$fmt'" if $fmt ne 'raw';
183 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
184 if $name && $name !~ m/^vm-$vmid-/;
186 $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid) if !$name;
188 $class->zfs_create_zvol($scfg, $name, $size);
194 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
196 my (undef, $name, undef) = $class->parse_volname($volname);
198 $class->zfs_delete_zvol($scfg, $name);
204 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
206 $cache->{zfs
} = $class->zfs_list_zvol($scfg) if !$cache->{zfs
};
207 my $zfspool = $scfg->{pool
};
210 if (my $dat = $cache->{zfs
}->{$zfspool}) {
212 foreach my $image (keys %$dat) {
214 my $volname = $dat->{$image}->{name
};
215 my $parent = $dat->{$image}->{parent
};
218 if ($parent && $parent =~ m/^(\S+)@(\S+)$/) {
219 my ($basename) = ($1);
220 $volid = "$storeid:$basename/$volname";
222 $volid = "$storeid:$volname";
225 my $owner = $dat->{$volname}->{vmid
};
227 my $found = grep { $_ eq $volid } @$vollist;
230 next if defined ($vmid) && ($owner ne $vmid);
233 my $info = $dat->{$volname};
234 $info->{volid
} = $volid;
242 sub zfs_get_pool_stats
{
243 my ($class, $scfg) = @_;
248 my $text = $class->zfs_request($scfg, undef, 'get', '-o', 'value', '-Hp',
249 'available,used', $scfg->{pool
});
251 my @lines = split /\n/, $text;
253 if($lines[0] =~ /^(\d+)$/) {
257 if($lines[1] =~ /^(\d+)$/) {
261 return ($available, $used);
264 sub zfs_get_zvol_size
{
265 my ($class, $scfg, $zvol) = @_;
267 my $text = $class->zfs_request($scfg, undef, 'get', '-Hp', 'volsize', "$scfg->{pool}/$zvol");
269 if ($text =~ /volsize\s(\d+)/) {
273 die "Could not get zvol size";
276 sub zfs_create_zvol
{
277 my ($class, $scfg, $zvol, $size) = @_;
279 my $cmd = ['create'];
281 push @$cmd, '-s' if $scfg->{sparse
};
283 push @$cmd, '-b', $scfg->{blocksize
} if $scfg->{blocksize
};
285 push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol";
287 $class->zfs_request($scfg, undef, @$cmd);
290 sub zfs_delete_zvol
{
291 my ($class, $scfg, $zvol) = @_;
293 $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol");
297 my ($class, $scfg) = @_;
299 my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin', '-t', 'volume', '-Hr');
300 my $zvols = zfs_parse_zvol_list
($text);
301 return undef if !$zvols;
304 foreach my $zvol (@$zvols) {
305 my @values = split('/', $zvol->{name
});
307 my $image = pop @values;
308 my $pool = join('/', @values);
310 next if $image !~ m/^((vm|base)-(\d+)-\S+)$/;
313 my $parent = $zvol->{origin
};
314 if($zvol->{origin
} && $zvol->{origin
} =~ m/^$scfg->{pool}\/(\S
+)$/){
318 $list->{$pool}->{$image} = {
320 size
=> $zvol->{size
},
330 sub zfs_find_free_diskname
{
331 my ($class, $storeid, $scfg, $vmid) = @_;
334 my $volumes = $class->zfs_list_zvol($scfg);
337 my $dat = $volumes->{$scfg->{pool
}};
339 foreach my $image (keys %$dat) {
340 my $volname = $dat->{$image}->{name
};
341 if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
346 for (my $i = 1; $i < 100; $i++) {
347 if (!$disk_ids->{$i}) {
348 return "vm-$vmid-disk-$i";
352 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
355 sub zfs_get_latest_snapshot
{
356 my ($class, $scfg, $volname) = @_;
358 # abort rollback if snapshot is not the latest
359 my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
360 my $text = zfs_request
($class, $scfg, undef, 'list', @params);
361 my @snapshots = split(/\n/, $text);
364 foreach (@snapshots) {
365 if (/$scfg->{pool}\/$volname/) {
375 my ($class, $storeid, $scfg, $cache) = @_;
383 ($free, $used) = $class->zfs_get_pool_stats($scfg);
385 $total = $free + $used;
389 return ($total, $free, $used, $active);
392 sub volume_size_info
{
393 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
395 return $class->zfs_get_zvol_size($scfg, $volname);
398 sub volume_snapshot
{
399 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
401 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$volname\@$snap");
404 sub volume_snapshot_delete
{
405 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
407 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$volname\@$snap");
410 sub volume_snapshot_rollback
{
411 my ($class, $scfg, $storeid, $volname, $snap) = @_;
413 # abort rollback if snapshot is not the latest
414 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
415 if ($snap ne $recentsnap) {
416 die "cannot rollback, more recent snapshots exist\n";
419 zfs_request
($class, $scfg, undef, 'rollback', "$scfg->{pool}/$volname\@$snap");
422 sub activate_volume
{
423 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
427 sub deactivate_volume
{
428 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
433 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
435 $snap ||= '__base__';
437 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
438 $class->parse_volname($volname);
440 die "clone_image only works on base images\n" if !$isBase;
442 my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid);
444 warn "clone $volname: $basename to $name\n";
446 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
452 my ($class, $storeid, $scfg, $volname) = @_;
454 my $snap = '__base__';
456 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
457 $class->parse_volname($volname);
459 die "create_base not possible with base image\n" if $isBase;
462 $newname =~ s/^vm-/base-/;
464 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
466 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
468 my $running = undef; #fixme : is create_base always offline ?
470 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
475 sub volume_has_feature
{
476 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
479 snapshot
=> { current
=> 1, snap
=> 1},
480 clone
=> { base
=> 1},
481 template
=> { current
=> 1},
482 copy
=> { base
=> 1, current
=> 1},
485 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
486 $class->parse_volname($volname);
493 $key = $isBase ?
'base' : 'current';
496 return 1 if $features->{$feature}->{$key};