1 package PVE
::Storage
::ZFSPlugin
;
7 use PVE
::Tools
qw(run_command);
8 use PVE
::Storage
::Plugin
;
10 use base
qw(PVE::Storage::Plugin);
11 use PVE
::Storage
::LunCmd
::Comstar
;
12 use PVE
::Storage
::LunCmd
::Istgt
;
14 my @ssh_opts = ('-o', 'BatchMode=yes');
15 my @ssh_cmd = ('/usr/bin/ssh', @ssh_opts);
27 my $zfs_unknown_scsi_provider = sub {
30 die "$provider: unknown iscsi provider. Available [comstar, istgt]";
33 my $zfs_get_base = sub {
36 if ($scfg->{iscsiprovider
} eq 'comstar') {
37 return PVE
::Storage
::LunCmd
::Comstar
::get_base
;
38 } elsif ($scfg->{iscsiprovider
} eq 'istgt') {
39 return PVE
::Storage
::LunCmd
::Istgt
::get_base
;
41 $zfs_unknown_scsi_provider->($scfg->{iscsiprovider
});
46 my ($scfg, $timeout, $method, @params) = @_;
53 $timeout = 5 if !$timeout;
55 if ($lun_cmds->{$method}) {
56 if ($scfg->{iscsiprovider
} eq 'comstar') {
57 $msg = PVE
::Storage
::LunCmd
::Comstar
::run_lun_command
($scfg, $timeout, $method, @params);
58 } elsif ($scfg->{iscsiprovider
} eq 'istgt') {
59 $msg = PVE
::Storage
::LunCmd
::Istgt
::run_lun_command
($scfg, $timeout, $method, @params);
61 $zfs_unknown_scsi_provider->($scfg->{iscsiprovider
});
64 if ($method eq 'zpool_list') {
71 $target = 'root@' . $scfg->{portal
};
73 my $cmd = [@ssh_cmd, $target, $zfscmd, $method, @params];
82 run_command
($cmd, outfunc
=> $output, timeout
=> $timeout);
93 if ($text =~ m/^(\d+(\.\d+)?)([TGMK])?$/) {
94 my ($size, $reminder, $unit) = ($1, $2, $3);
95 return $size if !$unit;
98 } elsif ($unit eq 'M') {
100 } elsif ($unit eq 'G') {
101 $size *= 1024*1024*1024;
102 } elsif ($unit eq 'T') {
103 $size *= 1024*1024*1024*1024;
115 sub zfs_get_pool_stats
{
121 my $text = zfs_request
($scfg, undef, 'get', '-o', 'value', '-Hp',
122 'available,used', $scfg->{pool
});
124 my @lines = split /\n/, $text;
126 if($lines[0] =~ /^(\d+)$/) {
130 if($lines[1] =~ /^(\d+)$/) {
134 return ($available, $used);
137 sub zfs_parse_zvol_list
{
142 return $list if !$text;
144 my @lines = split /\n/, $text;
145 foreach my $line (@lines) {
146 if ($line =~ /^(.+)\s+([a-zA-Z0-9\.]+|\-)\s+(.+)$/) {
150 my @zvols = split /\//, $1;
151 my $pool = $zvols[0];
153 if (scalar(@zvols) == 2 && $zvols[0] !~ /^rpool$/) {
155 next unless $disk =~ m!^(\w+)-(\d+)-(\w+)-(\d+)$!;
156 $name = $pool . '/' . $disk;
161 $zvol->{name
} = $name;
162 $zvol->{size
} = zfs_parse_size
($2);
164 $zvol->{origin
} = $3;
173 sub zfs_get_lu_name
{
174 my ($scfg, $zvol) = @_;
177 my $base = $zfs_get_base->($scfg);
178 if ($zvol =~ /^.+\/.+/) {
179 $object = "$base/$zvol";
181 $object = "$base/$scfg->{pool}/$zvol";
184 my $lu_name = zfs_request
($scfg, undef, 'list_lu', $object);
186 return $lu_name if $lu_name;
188 die "Could not find lu_name for zvol $zvol";
191 sub zfs_get_zvol_size
{
192 my ($scfg, $zvol) = @_;
194 my $text = zfs_request
($scfg, undef, 'get', '-Hp', 'volsize', "$scfg->{pool}/$zvol");
196 if($text =~ /volsize\s(\d+)/){
200 die "Could not get zvol size";
203 sub zfs_add_lun_mapping_entry
{
204 my ($scfg, $zvol, $guid) = @_;
206 if (! defined($guid)) {
207 $guid = zfs_get_lu_name
($scfg, $zvol);
210 zfs_request
($scfg, undef, 'add_view', $guid);
214 my ($scfg, $zvol) = @_;
216 my $guid = zfs_get_lu_name
($scfg, $zvol);
218 zfs_request
($scfg, undef, 'delete_lu', $guid);
222 my ($scfg, $zvol) = @_;
224 my $base = $zfs_get_base->($scfg);
225 my $guid = zfs_request
($scfg, undef, 'create_lu', "$base/$scfg->{pool}/$zvol");
231 my ($scfg, $zvol) = @_;
233 my $base = $zfs_get_base->($scfg);
234 zfs_request
($scfg, undef, 'import_lu', "$base/$scfg->{pool}/$zvol");
238 my ($scfg, $zvol, $size) = @_;
240 my $guid = zfs_get_lu_name
($scfg, $zvol);
242 zfs_request
($scfg, undef, 'modify_lu', "${size}K", $guid);
245 sub zfs_create_zvol
{
246 my ($scfg, $zvol, $size) = @_;
248 zfs_request
($scfg, undef, 'create', '-b', $scfg->{blocksize
}, '-V', "${size}k", "$scfg->{pool}/$zvol");
251 sub zfs_delete_zvol
{
252 my ($scfg, $zvol) = @_;
254 zfs_request
($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol");
257 sub zfs_get_lun_number
{
258 my ($scfg, $guid) = @_;
260 die "could not find lun_number for guid $guid" if !$guid;
262 return zfs_request
($scfg, undef, 'list_view', $guid);
268 my $text = zfs_request
($scfg, 10, 'list', '-o', 'name,volsize,origin', '-Hr');
269 my $zvols = zfs_parse_zvol_list
($text);
270 return undef if !$zvols;
273 foreach my $zvol (@$zvols) {
274 my @values = split('/', $zvol->{name
});
276 my $pool = $values[0];
277 my $image = $values[1];
279 next if $image !~ m/^((vm|base)-(\d+)-\S+)$/;
282 my $parent = $zvol->{origin
};
283 if($zvol->{origin
} && $zvol->{origin
} =~ m/^$scfg->{pool}\/(\S
+)$/){
287 $list->{$pool}->{$image} = {
289 size
=> $zvol->{size
},
307 content
=> [ {images
=> 1}, { images
=> 1 }],
314 description
=> "iscsi provider",
318 description
=> "block size",
326 nodes
=> { optional
=> 1 },
327 disable
=> { optional
=> 1 },
328 portal
=> { fixed
=> 1 },
329 target
=> { fixed
=> 1 },
330 pool
=> { fixed
=> 1 },
331 blocksize
=> { fixed
=> 1 },
332 iscsiprovider
=> { fixed
=> 1 },
333 content
=> { optional
=> 1 },
337 # Storage implementation
340 my ($class, $volname) = @_;
342 if ($volname =~ m/^(((base|vm)-(\d+)-\S+)\/)?
((base
)?
(vm
)?
-(\d
+)-\S
+)$/) {
343 return ('images', $5, $8, $2, $4, $6);
346 die "unable to parse zfs volume name '$volname'\n";
350 my ($class, $scfg, $volname) = @_;
352 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
354 my $target = $scfg->{target
};
355 my $portal = $scfg->{portal
};
357 my $guid = zfs_get_lu_name
($scfg, $name);
358 my $lun = zfs_get_lun_number
($scfg, $guid);
360 my $path = "iscsi://$portal/$target/$lun";
362 return ($path, $vmid, $vtype);
365 my $find_free_diskname = sub {
366 my ($storeid, $scfg, $vmid) = @_;
369 my $volumes = zfs_list_zvol
($scfg);
372 my $dat = $volumes->{$scfg->{pool
}};
374 foreach my $image (keys %$dat) {
375 my $volname = $dat->{$image}->{name
};
376 if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
381 for (my $i = 1; $i < 100; $i++) {
382 if (!$disk_ids->{$i}) {
383 return "vm-$vmid-disk-$i";
387 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
391 my ($class, $storeid, $scfg, $volname) = @_;
393 my $snap = '__base__';
395 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
396 $class->parse_volname($volname);
398 die "create_base not possible with base image\n" if $isBase;
401 $newname =~ s/^vm-/base-/;
403 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
405 zfs_delete_lu
($scfg, $name);
406 zfs_request
($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
408 my $guid = zfs_create_lu
($scfg, $newname);
409 zfs_add_lun_mapping_entry
($scfg, $newname, $guid);
411 my $running = undef; #fixme : is create_base always offline ?
413 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
419 my ($class, $scfg, $storeid, $volname, $vmid) = @_;
421 my $snap = '__base__';
423 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
424 $class->parse_volname($volname);
426 die "clone_image only works on base images\n" if !$isBase;
428 my $name = &$find_free_diskname($storeid, $scfg, $vmid);
430 warn "clone $volname: $basename to $name\n";
432 zfs_request
($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
434 my $guid = zfs_create_lu
($scfg, $name);
435 zfs_add_lun_mapping_entry
($scfg, $name, $guid);
441 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
443 die "unsupported format '$fmt'" if $fmt ne 'raw';
445 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
446 if $name && $name !~ m/^vm-$vmid-/;
448 $name = &$find_free_diskname($storeid, $scfg, $vmid);
450 zfs_create_zvol
($scfg, $name, $size);
451 my $guid = zfs_create_lu
($scfg, $name);
452 zfs_add_lun_mapping_entry
($scfg, $name, $guid);
458 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
460 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
462 zfs_delete_lu
($scfg, $name);
464 zfs_delete_zvol
($scfg, $name);
468 my $guid = zfs_create_lu
($scfg, $name);
469 zfs_add_lun_mapping_entry
($scfg, $name, $guid);
477 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
479 $cache->{zfs
} = zfs_list_zvol
($scfg) if !$cache->{zfs
};
480 my $zfspool = $scfg->{pool
};
483 if (my $dat = $cache->{zfs
}->{$zfspool}) {
485 foreach my $image (keys %$dat) {
487 my $volname = $dat->{$image}->{name
};
488 my $parent = $dat->{$image}->{parent
};
491 if ($parent && $parent =~ m/^(\S+)@(\S+)$/) {
492 my ($basename) = ($1);
493 $volid = "$storeid:$basename/$volname";
495 $volid = "$storeid:$volname";
498 my $owner = $dat->{$volname}->{vmid
};
500 my $found = grep { $_ eq $volid } @$vollist;
503 next if defined ($vmid) && ($owner ne $vmid);
506 my $info = $dat->{$volname};
507 $info->{volid
} = $volid;
516 my ($class, $storeid, $scfg, $cache) = @_;
524 ($free, $used) = zfs_get_pool_stats
($scfg);
526 $total = $free + $used;
530 return ($total, $free, $used, $active);
533 sub activate_storage
{
534 my ($class, $storeid, $scfg, $cache) = @_;
538 sub deactivate_storage
{
539 my ($class, $storeid, $scfg, $cache) = @_;
543 sub activate_volume
{
544 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
548 sub deactivate_volume
{
549 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
553 sub volume_size_info
{
554 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
556 return zfs_get_zvol_size
($scfg, $volname);
560 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
562 my $new_size = ($size/1024);
564 zfs_request
($scfg, undef, 'set', 'volsize=' . $new_size . 'k', "$scfg->{pool}/$volname");
565 zfs_resize_lu
($scfg, $volname, $new_size);
568 sub volume_snapshot
{
569 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
571 zfs_request
($scfg, undef, 'snapshot', "$scfg->{pool}/$volname\@$snap");
574 sub volume_snapshot_rollback
{
575 my ($class, $scfg, $storeid, $volname, $snap) = @_;
577 zfs_delete_lu
($scfg, $volname);
579 zfs_request
($scfg, undef, 'rollback', "$scfg->{pool}/$volname\@$snap");
581 zfs_import_lu
($scfg, $volname);
583 zfs_add_lun_mapping_entry
($scfg, $volname);
586 sub volume_snapshot_delete
{
587 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
589 zfs_request
($scfg, undef, 'destroy', "$scfg->{pool}/$volname\@$snap");
592 sub volume_has_feature
{
593 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
596 snapshot
=> { current
=> 1, snap
=> 1},
597 clone
=> { base
=> 1},
598 template
=> { current
=> 1},
599 copy
=> { base
=> 1, current
=> 1},
602 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
603 $class->parse_volname($volname);
610 $key = $isBase ?
'base' : 'current';
613 return 1 if $features->{$feature}->{$key};