]>
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 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);
183 run_command
("udevadm trigger --subsystem-match block");
184 run_command
("udevadm settle --timeout 5");
187 last if -e
"/dev/zvol/$scfg->{pool}/$name" ;
188 Time
::HiRes
::usleep
(100);
195 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
197 my (undef, $name, undef) = $class->parse_volname($volname);
199 $class->zfs_delete_zvol($scfg, $name);
205 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
207 $cache->{zfs
} = $class->zfs_list_zvol($scfg) if !$cache->{zfs
};
208 my $zfspool = $scfg->{pool
};
211 if (my $dat = $cache->{zfs
}->{$zfspool}) {
213 foreach my $image (keys %$dat) {
215 my $volname = $dat->{$image}->{name
};
216 my $parent = $dat->{$image}->{parent
};
219 if ($parent && $parent =~ m/^(\S+)@(\S+)$/) {
220 my ($basename) = ($1);
221 $volid = "$storeid:$basename/$volname";
223 $volid = "$storeid:$volname";
226 my $owner = $dat->{$volname}->{vmid
};
228 my $found = grep { $_ eq $volid } @$vollist;
231 next if defined ($vmid) && ($owner ne $vmid);
234 my $info = $dat->{$volname};
235 $info->{volid
} = $volid;
243 sub zfs_get_pool_stats
{
244 my ($class, $scfg) = @_;
249 my $text = $class->zfs_request($scfg, undef, 'get', '-o', 'value', '-Hp',
250 'available,used', $scfg->{pool
});
252 my @lines = split /\n/, $text;
254 if($lines[0] =~ /^(\d+)$/) {
258 if($lines[1] =~ /^(\d+)$/) {
262 return ($available, $used);
265 sub zfs_get_zvol_size
{
266 my ($class, $scfg, $zvol) = @_;
268 my $text = $class->zfs_request($scfg, undef, 'get', '-Hp', 'volsize', "$scfg->{pool}/$zvol");
270 if ($text =~ /volsize\s(\d+)/) {
274 die "Could not get zvol size";
277 sub zfs_create_zvol
{
278 my ($class, $scfg, $zvol, $size) = @_;
280 my $cmd = ['create'];
282 push @$cmd, '-s' if $scfg->{sparse
};
284 push @$cmd, '-b', $scfg->{blocksize
} if $scfg->{blocksize
};
286 push @$cmd, '-V', "${size}k", "$scfg->{pool}/$zvol";
288 $class->zfs_request($scfg, undef, @$cmd);
291 sub zfs_delete_zvol
{
292 my ($class, $scfg, $zvol) = @_;
294 $class->zfs_request($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol");
298 my ($class, $scfg) = @_;
300 my $text = $class->zfs_request($scfg, 10, 'list', '-o', 'name,volsize,origin', '-t', 'volume', '-Hr');
301 my $zvols = zfs_parse_zvol_list
($text);
302 return undef if !$zvols;
305 foreach my $zvol (@$zvols) {
306 my @values = split('/', $zvol->{name
});
308 my $image = pop @values;
309 my $pool = join('/', @values);
311 next if $image !~ m/^((vm|base)-(\d+)-\S+)$/;
314 my $parent = $zvol->{origin
};
315 if($zvol->{origin
} && $zvol->{origin
} =~ m/^$scfg->{pool}\/(\S
+)$/){
319 $list->{$pool}->{$image} = {
321 size
=> $zvol->{size
},
331 sub zfs_find_free_diskname
{
332 my ($class, $storeid, $scfg, $vmid) = @_;
335 my $volumes = $class->zfs_list_zvol($scfg);
338 my $dat = $volumes->{$scfg->{pool
}};
340 foreach my $image (keys %$dat) {
341 my $volname = $dat->{$image}->{name
};
342 if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
347 for (my $i = 1; $i < 100; $i++) {
348 if (!$disk_ids->{$i}) {
349 return "vm-$vmid-disk-$i";
353 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
356 sub zfs_get_latest_snapshot
{
357 my ($class, $scfg, $volname) = @_;
359 # abort rollback if snapshot is not the latest
360 my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
361 my $text = zfs_request
($class, $scfg, undef, 'list', @params);
362 my @snapshots = split(/\n/, $text);
365 foreach (@snapshots) {
366 if (/$scfg->{pool}\/$volname/) {
376 my ($class, $storeid, $scfg, $cache) = @_;
384 ($free, $used) = $class->zfs_get_pool_stats($scfg);
386 $total = $free + $used;
390 return ($total, $free, $used, $active);
393 sub volume_size_info
{
394 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
396 return $class->zfs_get_zvol_size($scfg, $volname);
399 sub volume_snapshot
{
400 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
402 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$volname\@$snap");
405 sub volume_snapshot_delete
{
406 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
408 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$volname\@$snap");
411 sub volume_snapshot_rollback
{
412 my ($class, $scfg, $storeid, $volname, $snap) = @_;
414 # abort rollback if snapshot is not the latest
415 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
416 if ($snap ne $recentsnap) {
417 die "cannot rollback, more recent snapshots exist\n";
420 zfs_request
($class, $scfg, undef, 'rollback', "$scfg->{pool}/$volname\@$snap");
423 sub activate_storage
{
424 my ($class, $storeid, $scfg, $cache) = @_;
428 sub deactivate_storage
{
429 my ($class, $storeid, $scfg, $cache) = @_;
433 sub activate_volume
{
434 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
438 sub deactivate_volume
{
439 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
444 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
446 $snap ||= '__base__';
448 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
449 $class->parse_volname($volname);
451 die "clone_image only works on base images\n" if !$isBase;
453 my $name = $class->zfs_find_free_diskname($storeid, $scfg, $vmid);
455 $class->zfs_request($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
461 my ($class, $storeid, $scfg, $volname) = @_;
463 my $snap = '__base__';
465 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
466 $class->parse_volname($volname);
468 die "create_base not possible with base image\n" if $isBase;
471 $newname =~ s/^vm-/base-/;
473 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
475 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
477 my $running = undef; #fixme : is create_base always offline ?
479 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
484 sub volume_has_feature
{
485 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
488 snapshot
=> { current
=> 1, snap
=> 1},
489 clone
=> { base
=> 1},
490 template
=> { current
=> 1},
491 copy
=> { base
=> 1, current
=> 1},
494 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
495 $class->parse_volname($volname);
502 $key = $isBase ?
'base' : 'current';
505 return 1 if $features->{$feature}->{$key};