]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/ZFSDirPlugin.pm
11ee78339fc58274825d8b95d4d59bab30c4ad6f
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";
356 my ($class, $storeid, $scfg, $cache) = @_;
364 ($free, $used) = $class->zfs_get_pool_stats($scfg);
366 $total = $free + $used;
370 return ($total, $free, $used, $active);
373 sub volume_size_info
{
374 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
376 return $class->zfs_get_zvol_size($scfg, $volname);
379 sub volume_snapshot
{
380 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
382 $class->zfs_request($scfg, undef, 'snapshot', "$scfg->{pool}/$volname\@$snap");
385 sub volume_snapshot_delete
{
386 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
388 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$volname\@$snap");
391 sub volume_snapshot_rollback
{
392 my ($class, $scfg, $storeid, $volname, $snap) = @_;
394 # abort rollback if snapshot is not the latest
395 my @params = ('-t', 'snapshot', '-o', 'name', '-s', 'creation');
396 my $text = zfs_request
($class, $scfg, undef,'list', @params);
397 my @snapshots = split(/\n/, $text);
398 my $recentsnap = undef;
399 foreach (@snapshots) {
400 if (/$scfg->{pool}\/$volname/) {
405 if ($snap ne $recentsnap) {
406 die "cannot rollback, more recent snapshots exist\n";
409 zfs_request
($class, $scfg, undef, 'rollback', "$scfg->{pool}/$volname\@$snap");
413 sub activate_volume
{
414 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
418 sub deactivate_volume
{
419 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
423 sub volume_has_feature
{
424 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
427 snapshot
=> { current
=> 1, snap
=> 1},
428 clone
=> { base
=> 1},
429 template
=> { current
=> 1},
430 copy
=> { base
=> 1, current
=> 1},
433 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
434 $class->parse_volname($volname);
441 $key = $isBase ?
'base' : 'current';
444 return 1 if $features->{$feature}->{$key};