]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/ZFSPoolPlugin.pm
490b61c7c101c3c2db708dff50406ace2c306e08
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);
187 run_command
("udevadm trigger --subsystem-match block");
188 run_command
("udevadm settle --timeout 5");
191 last if -e
"/dev/zvol/$scfg->{pool}/$name" ;
192 Time
::HiRes
::usleep
(100);
199 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
201 my (undef, $name, undef) = $class->parse_volname($volname);
203 $class->zfs_delete_zvol($scfg, $name);
209 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
211 $cache->{zfs
} = $class->zfs_list_zvol($scfg) if !$cache->{zfs
};
212 my $zfspool = $scfg->{pool
};
215 if (my $dat = $cache->{zfs
}->{$zfspool}) {
217 foreach my $image (keys %$dat) {
219 my $volname = $dat->{$image}->{name
};
220 my $parent = $dat->{$image}->{parent
};
223 if ($parent && $parent =~ m/^(\S+)@(\S+)$/) {
224 my ($basename) = ($1);
225 $volid = "$storeid:$basename/$volname";
227 $volid = "$storeid:$volname";
230 my $owner = $dat->{$volname}->{vmid
};
232 my $found = grep { $_ eq $volid } @$vollist;
235 next if defined ($vmid) && ($owner ne $vmid);
238 my $info = $dat->{$volname};
239 $info->{volid
} = $volid;
247 sub zfs_get_pool_stats
{
248 my ($class, $scfg) = @_;
253 my $text = $class->zfs_request($scfg, undef, 'get', '-o', 'value', '-Hp',
254 'available,used', $scfg->{pool
});
256 my @lines = split /\n/, $text;
258 if($lines[0] =~ /^(\d+)$/) {
262 if($lines[1] =~ /^(\d+)$/) {
266 return ($available, $used);
269 sub zfs_get_zvol_size
{
270 my ($class, $scfg, $zvol) = @_;
272 my $text = $class->zfs_request($scfg, undef, 'get', '-Hp', 'volsize', "$scfg->{pool}/$zvol");
274 if ($text =~ /volsize\s(\d+)/) {
278 die "Could not get zvol size";
281 sub zfs_create_zvol
{
282 my ($class, $scfg, $zvol, $size) = @_;
284 my $cmd = ['create'];
286 push @$cmd, '-s' if $scfg->{sparse
};
288 push @$cmd, '-b', $scfg->{blocksize
} if $scfg->{blocksize
};
290 push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol";
292 $class->zfs_request($scfg, undef, @$cmd);
295 sub zfs_delete_zvol
{
296 my ($class, $scfg, $zvol) = @_;
298 $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol");
302 my ($class, $scfg) = @_;
304 my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin', '-t', 'volume', '-Hr');
305 my $zvols = zfs_parse_zvol_list
($text);
306 return undef if !$zvols;
309 foreach my $zvol (@$zvols) {
310 my @values = split('/', $zvol->{name
});
312 my $image = pop @values;
313 my $pool = join('/', @values);
315 next if $image !~ m/^((vm|base)-(\d+)-\S+)$/;
318 my $parent = $zvol->{origin
};
319 if($zvol->{origin
} && $zvol->{origin
} =~ m/^$scfg->{pool}\/(\S
+)$/){
323 $list->{$pool}->{$image} = {
325 size
=> $zvol->{size
},
335 sub zfs_find_free_diskname
{
336 my ($class, $storeid, $scfg, $vmid) = @_;
339 my $volumes = $class->zfs_list_zvol($scfg);
342 my $dat = $volumes->{$scfg->{pool
}};
344 foreach my $image (keys %$dat) {
345 my $volname = $dat->{$image}->{name
};
346 if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
351 for (my $i = 1; $i < 100; $i++) {
352 if (!$disk_ids->{$i}) {
353 return "vm-$vmid-disk-$i";
357 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
360 sub zfs_get_latest_snapshot
{
361 my ($class, $scfg, $volname) = @_;
363 # abort rollback if snapshot is not the latest
364 my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
365 my $text = zfs_request
($class, $scfg, undef, 'list', @params);
366 my @snapshots = split(/\n/, $text);
369 foreach (@snapshots) {
370 if (/$scfg->{pool}\/$volname/) {
380 my ($class, $storeid, $scfg, $cache) = @_;
388 ($free, $used) = $class->zfs_get_pool_stats($scfg);
390 $total = $free + $used;
394 return ($total, $free, $used, $active);
397 sub volume_size_info
{
398 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
400 return $class->zfs_get_zvol_size($scfg, $volname);
403 sub volume_snapshot
{
404 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
406 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$volname\@$snap");
409 sub volume_snapshot_delete
{
410 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
412 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$volname\@$snap");
415 sub volume_snapshot_rollback
{
416 my ($class, $scfg, $storeid, $volname, $snap) = @_;
418 # abort rollback if snapshot is not the latest
419 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
420 if ($snap ne $recentsnap) {
421 die "cannot rollback, more recent snapshots exist\n";
424 zfs_request
($class, $scfg, undef, 'rollback', "$scfg->{pool}/$volname\@$snap");
427 sub activate_storage
{
428 my ($class, $storeid, $scfg, $cache) = @_;
432 sub deactivate_storage
{
433 my ($class, $storeid, $scfg, $cache) = @_;
437 sub activate_volume
{
438 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
442 sub deactivate_volume
{
443 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
448 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
450 $snap ||= '__base__';
452 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
453 $class->parse_volname($volname);
455 die "clone_image only works on base images\n" if !$isBase;
457 my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid);
459 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
465 my ($class, $storeid, $scfg, $volname) = @_;
467 my $snap = '__base__';
469 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
470 $class->parse_volname($volname);
472 die "create_base not possible with base image\n" if $isBase;
475 $newname =~ s/^vm-/base-/;
477 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
479 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
481 my $running = undef; #fixme : is create_base always offline ?
483 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
488 sub volume_has_feature
{
489 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
492 snapshot
=> { current
=> 1, snap
=> 1},
493 clone
=> { base
=> 1},
494 template
=> { current
=> 1},
495 copy
=> { base
=> 1, current
=> 1},
498 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
499 $class->parse_volname($volname);
506 $key = $isBase ?
'base' : 'current';
509 return 1 if $features->{$feature}->{$key};