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
;
13 use PVE
::Storage
::LunCmd
::Iet
;
15 my @ssh_opts = ('-o', 'BatchMode=yes');
16 my @ssh_cmd = ('/usr/bin/ssh', @ssh_opts);
17 my $id_rsa_path = '/etc/pve/priv/zfs';
29 my $zfs_unknown_scsi_provider = sub {
32 die "$provider: unknown iscsi provider. Available [comstar, istgt, iet]";
35 my $zfs_get_base = sub {
38 if ($scfg->{iscsiprovider
} eq 'comstar') {
39 return PVE
::Storage
::LunCmd
::Comstar
::get_base
;
40 } elsif ($scfg->{iscsiprovider
} eq 'istgt') {
41 return PVE
::Storage
::LunCmd
::Istgt
::get_base
;
42 } elsif ($scfg->{iscsiprovider
} eq 'iet') {
43 return PVE
::Storage
::LunCmd
::Iet
::get_base
;
45 $zfs_unknown_scsi_provider->($scfg->{iscsiprovider
});
50 my ($scfg, $timeout, $method, @params) = @_;
57 $timeout = 5 if !$timeout;
59 if ($lun_cmds->{$method}) {
60 if ($scfg->{iscsiprovider
} eq 'comstar') {
61 $msg = PVE
::Storage
::LunCmd
::Comstar
::run_lun_command
($scfg, $timeout, $method, @params);
62 } elsif ($scfg->{iscsiprovider
} eq 'istgt') {
63 $msg = PVE
::Storage
::LunCmd
::Istgt
::run_lun_command
($scfg, $timeout, $method, @params);
64 } elsif ($scfg->{iscsiprovider
} eq 'iet') {
65 $msg = PVE
::Storage
::LunCmd
::Iet
::run_lun_command
($scfg, $timeout, $method, @params);
67 $zfs_unknown_scsi_provider->($scfg->{iscsiprovider
});
70 if ($method eq 'zpool_list') {
77 $target = 'root@' . $scfg->{portal
};
79 my $cmd = [@ssh_cmd, '-i', "$id_rsa_path/$scfg->{portal}_id_rsa", $target, $zfscmd, $method, @params];
88 run_command
($cmd, outfunc
=> $output, timeout
=> $timeout);
99 if ($text =~ m/^(\d+(\.\d+)?)([TGMK])?$/) {
100 my ($size, $reminder, $unit) = ($1, $2, $3);
101 return $size if !$unit;
104 } elsif ($unit eq 'M') {
106 } elsif ($unit eq 'G') {
107 $size *= 1024*1024*1024;
108 } elsif ($unit eq 'T') {
109 $size *= 1024*1024*1024*1024;
121 sub zfs_get_pool_stats
{
127 my $text = zfs_request
($scfg, undef, 'get', '-o', 'value', '-Hp',
128 'available,used', $scfg->{pool
});
130 my @lines = split /\n/, $text;
132 if($lines[0] =~ /^(\d+)$/) {
136 if($lines[1] =~ /^(\d+)$/) {
140 return ($available, $used);
143 sub zfs_parse_zvol_list
{
148 return $list if !$text;
150 my @lines = split /\n/, $text;
151 foreach my $line (@lines) {
152 if ($line =~ /^(.+)\s+([a-zA-Z0-9\.]+|\-)\s+(.+)$/) {
154 my @parts = split /\//, $1;
155 my $name = pop @parts;
156 my $pool = join('/', @parts);
158 if ($pool !~ /^rpool$/) {
159 next unless $name =~ m!^(\w+)-(\d+)-(\w+)-(\d+)$!;
160 $name = $pool . '/' . $name;
165 $zvol->{pool
} = $pool;
166 $zvol->{name
} = $name;
167 $zvol->{size
} = zfs_parse_size
($2);
169 $zvol->{origin
} = $3;
178 sub zfs_get_lu_name
{
179 my ($scfg, $zvol) = @_;
182 my $base = $zfs_get_base->($scfg);
183 if ($zvol =~ /^.+\/.+/) {
184 $object = "$base/$zvol";
186 $object = "$base/$scfg->{pool}/$zvol";
189 my $lu_name = zfs_request
($scfg, undef, 'list_lu', $object);
191 return $lu_name if $lu_name;
193 die "Could not find lu_name for zvol $zvol";
196 sub zfs_get_zvol_size
{
197 my ($scfg, $zvol) = @_;
199 my $text = zfs_request
($scfg, undef, 'get', '-Hp', 'volsize', "$scfg->{pool}/$zvol");
201 if($text =~ /volsize\s(\d+)/){
205 die "Could not get zvol size";
208 sub zfs_add_lun_mapping_entry
{
209 my ($scfg, $zvol, $guid) = @_;
211 if (! defined($guid)) {
212 $guid = zfs_get_lu_name
($scfg, $zvol);
215 zfs_request
($scfg, undef, 'add_view', $guid);
219 my ($scfg, $zvol) = @_;
221 my $guid = zfs_get_lu_name
($scfg, $zvol);
223 zfs_request
($scfg, undef, 'delete_lu', $guid);
227 my ($scfg, $zvol) = @_;
229 my $base = $zfs_get_base->($scfg);
230 my $guid = zfs_request
($scfg, undef, 'create_lu', "$base/$scfg->{pool}/$zvol");
236 my ($scfg, $zvol) = @_;
238 my $base = $zfs_get_base->($scfg);
239 zfs_request
($scfg, undef, 'import_lu', "$base/$scfg->{pool}/$zvol");
243 my ($scfg, $zvol, $size) = @_;
245 my $guid = zfs_get_lu_name
($scfg, $zvol);
247 zfs_request
($scfg, undef, 'modify_lu', "${size}K", $guid);
250 sub zfs_create_zvol
{
251 my ($scfg, $zvol, $size) = @_;
254 if ($scfg->{sparse
}) {
258 zfs_request
($scfg, undef, 'create', $sparse, '-b', $scfg->{blocksize
}, '-V', "${size}k", "$scfg->{pool}/$zvol");
261 sub zfs_delete_zvol
{
262 my ($scfg, $zvol) = @_;
264 zfs_request
($scfg, undef, 'destroy', '-r', "$scfg->{pool}/$zvol");
267 sub zfs_get_lun_number
{
268 my ($scfg, $guid) = @_;
270 die "could not find lun_number for guid $guid" if !$guid;
272 return zfs_request
($scfg, undef, 'list_view', $guid);
278 my $text = zfs_request
($scfg, 10, 'list', '-o', 'name,volsize,origin', '-t', 'volume', '-Hr');
279 my $zvols = zfs_parse_zvol_list
($text);
280 return undef if !$zvols;
283 foreach my $zvol (@$zvols) {
284 my @values = split('/', $zvol->{name
});
286 my $image = pop @values;
287 my $pool = join('/', @values);
289 next if $image !~ m/^((vm|base)-(\d+)-\S+)$/;
292 my $parent = $zvol->{origin
};
293 if($zvol->{origin
} && $zvol->{origin
} =~ m/^$scfg->{pool}\/(\S
+)$/){
297 $list->{$pool}->{$image} = {
299 size
=> $zvol->{size
},
317 content
=> [ {images
=> 1}, { images
=> 1 }],
324 description
=> "iscsi provider",
328 description
=> "block size",
331 # this will disable write caching on comstar and istgt.
332 # it is not implemented for iet. iet blockio always operates with
333 # writethrough caching when not in readonly mode
335 description
=> "disable write caching on the target",
339 description
=> "use sparse volumes",
347 nodes
=> { optional
=> 1 },
348 disable
=> { optional
=> 1 },
349 portal
=> { fixed
=> 1 },
350 target
=> { fixed
=> 1 },
351 pool
=> { fixed
=> 1 },
352 blocksize
=> { fixed
=> 1 },
353 iscsiprovider
=> { fixed
=> 1 },
354 nowritecache
=> { optional
=> 1 },
355 sparse
=> { optional
=> 1 },
356 content
=> { optional
=> 1 },
360 # Storage implementation
363 my ($class, $volname) = @_;
365 if ($volname =~ m/^(((base|vm)-(\d+)-\S+)\/)?
((base
)?
(vm
)?
-(\d
+)-\S
+)$/) {
366 return ('images', $5, $8, $2, $4, $6);
369 die "unable to parse zfs volume name '$volname'\n";
373 my ($class, $scfg, $volname) = @_;
375 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
377 my $target = $scfg->{target
};
378 my $portal = $scfg->{portal
};
380 my $guid = zfs_get_lu_name
($scfg, $name);
381 my $lun = zfs_get_lun_number
($scfg, $guid);
383 my $path = "iscsi://$portal/$target/$lun";
385 return ($path, $vmid, $vtype);
388 my $find_free_diskname = sub {
389 my ($storeid, $scfg, $vmid) = @_;
392 my $volumes = zfs_list_zvol
($scfg);
395 my $dat = $volumes->{$scfg->{pool
}};
397 foreach my $image (keys %$dat) {
398 my $volname = $dat->{$image}->{name
};
399 if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
404 for (my $i = 1; $i < 100; $i++) {
405 if (!$disk_ids->{$i}) {
406 return "vm-$vmid-disk-$i";
410 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n";
414 my ($class, $storeid, $scfg, $volname) = @_;
416 my $snap = '__base__';
418 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
419 $class->parse_volname($volname);
421 die "create_base not possible with base image\n" if $isBase;
424 $newname =~ s/^vm-/base-/;
426 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
428 zfs_delete_lu
($scfg, $name);
429 zfs_request
($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
431 my $guid = zfs_create_lu
($scfg, $newname);
432 zfs_add_lun_mapping_entry
($scfg, $newname, $guid);
434 my $running = undef; #fixme : is create_base always offline ?
436 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
442 my ($class, $scfg, $storeid, $volname, $vmid) = @_;
444 my $snap = '__base__';
446 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
447 $class->parse_volname($volname);
449 die "clone_image only works on base images\n" if !$isBase;
451 my $name = &$find_free_diskname($storeid, $scfg, $vmid);
453 warn "clone $volname: $basename to $name\n";
455 zfs_request
($scfg, undef, 'clone', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
457 my $guid = zfs_create_lu
($scfg, $name);
458 zfs_add_lun_mapping_entry
($scfg, $name, $guid);
464 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
466 die "unsupported format '$fmt'" if $fmt ne 'raw';
468 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
469 if $name && $name !~ m/^vm-$vmid-/;
471 $name = &$find_free_diskname($storeid, $scfg, $vmid);
473 zfs_create_zvol
($scfg, $name, $size);
474 my $guid = zfs_create_lu
($scfg, $name);
475 zfs_add_lun_mapping_entry
($scfg, $name, $guid);
481 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
483 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
485 zfs_delete_lu
($scfg, $name);
487 zfs_delete_zvol
($scfg, $name);
491 my $guid = zfs_create_lu
($scfg, $name);
492 zfs_add_lun_mapping_entry
($scfg, $name, $guid);
500 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
502 $cache->{zfs
} = zfs_list_zvol
($scfg) if !$cache->{zfs
};
503 my $zfspool = $scfg->{pool
};
506 if (my $dat = $cache->{zfs
}->{$zfspool}) {
508 foreach my $image (keys %$dat) {
510 my $volname = $dat->{$image}->{name
};
511 my $parent = $dat->{$image}->{parent
};
514 if ($parent && $parent =~ m/^(\S+)@(\S+)$/) {
515 my ($basename) = ($1);
516 $volid = "$storeid:$basename/$volname";
518 $volid = "$storeid:$volname";
521 my $owner = $dat->{$volname}->{vmid
};
523 my $found = grep { $_ eq $volid } @$vollist;
526 next if defined ($vmid) && ($owner ne $vmid);
529 my $info = $dat->{$volname};
530 $info->{volid
} = $volid;
539 my ($class, $storeid, $scfg, $cache) = @_;
547 ($free, $used) = zfs_get_pool_stats
($scfg);
549 $total = $free + $used;
553 return ($total, $free, $used, $active);
556 sub activate_storage
{
557 my ($class, $storeid, $scfg, $cache) = @_;
561 sub deactivate_storage
{
562 my ($class, $storeid, $scfg, $cache) = @_;
566 sub activate_volume
{
567 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
571 sub deactivate_volume
{
572 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
576 sub volume_size_info
{
577 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
579 return zfs_get_zvol_size
($scfg, $volname);
583 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
585 my $new_size = ($size/1024);
587 zfs_request
($scfg, undef, 'set', 'volsize=' . $new_size . 'k', "$scfg->{pool}/$volname");
588 zfs_resize_lu
($scfg, $volname, $new_size);
591 sub volume_snapshot
{
592 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
594 zfs_request
($scfg, undef, 'snapshot', "$scfg->{pool}/$volname\@$snap");
597 sub volume_snapshot_rollback
{
598 my ($class, $scfg, $storeid, $volname, $snap) = @_;
600 zfs_delete_lu
($scfg, $volname);
602 zfs_request
($scfg, undef, 'rollback', "$scfg->{pool}/$volname\@$snap");
604 zfs_import_lu
($scfg, $volname);
606 zfs_add_lun_mapping_entry
($scfg, $volname);
609 sub volume_snapshot_delete
{
610 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
612 zfs_request
($scfg, undef, 'destroy', "$scfg->{pool}/$volname\@$snap");
615 sub volume_has_feature
{
616 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
619 snapshot
=> { current
=> 1, snap
=> 1},
620 clone
=> { base
=> 1},
621 template
=> { current
=> 1},
622 copy
=> { base
=> 1, current
=> 1},
625 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
626 $class->parse_volname($volname);
633 $key = $isBase ?
'base' : 'current';
636 return 1 if $features->{$feature}->{$key};