1 package PVE
::Storage
::NexentaPlugin
;
10 use PVE
::Tools
qw(run_command file_read_firstline trim dir_glob_regex dir_glob_foreach);
11 use PVE
::Storage
::Plugin
;
12 use PVE
::JSONSchema
qw(get_standard_option);
14 use base
qw(PVE::Storage::Plugin);
17 my ($scfg, $method, $object, @params) = @_;
19 my $apicall = { method => $method, object
=> $object, params
=> [ @params ] };
21 my $json = encode_json
($apicall);
23 my $uri = ($scfg->{ssl
} ?
"https" : "http") . "://" . $scfg->{portal
} . ":2000/rest/nms/";
24 my $req = HTTP
::Request-
>new('POST', $uri);
26 $req->header('Content-Type' => 'application/json');
28 my $token = encode_base64
("$scfg->{login}:$scfg->{password}");
29 $req->header(Authorization
=> "Basic $token");
31 my $ua = LWP
::UserAgent-
>new(ssl_opts
=> { verify_hostname
=> 0 });
32 my $res = $ua->request($req);
33 die $res->content if !$res->is_success;
35 my $obj = eval { from_json
($res->content); };
36 die "JSON not valid. Content: " . $res->content if ($@);
37 die "Nexenta API Error: $obj->{error}->{message}\n" if $obj->{error
}->{message
};
38 return $obj->{result
};
42 sub nexenta_get_zvol_size
{
43 my ($scfg, $zvol) = @_;
45 my $ret = nexenta_request
($scfg, 'get_child_prop', 'zvol', $zvol, 'size_bytes');
46 $ret =~ m/^(\d+)$/ or die "size is not valid";
51 sub nexenta_get_zvol_props
{
52 my ($scfg, $zvol) = @_;
54 my $props = nexenta_request
($scfg, 'get_child_props', 'zvol', $zvol, '');
58 sub nexenta_list_lun_mapping_entries
{
59 my ($scfg, $zvol) = @_;
61 return nexenta_request
($scfg, 'list_lun_mapping_entries', 'scsidisk', "$scfg->{pool}/$zvol");
64 sub nexenta_add_lun_mapping_entry
{
65 my ($scfg, $zvol) = @_;
67 nexenta_request
($scfg, 'add_lun_mapping_entry', 'scsidisk',
68 "$scfg->{pool}/$zvol", { target_group
=> "All" });
71 sub nexenta_delete_lu
{
72 my ($scfg, $zvol) = @_;
74 nexenta_request
($scfg, 'delete_lu', 'scsidisk', "$scfg->{pool}/$zvol");
77 sub nexenta_create_lu
{
78 my ($scfg, $zvol) = @_;
80 nexenta_request
($scfg, 'create_lu', 'scsidisk', "$scfg->{pool}/$zvol", {});
83 sub nexenta_import_lu
{
84 my ($scfg, $zvol) = @_;
86 nexenta_request
($scfg, 'import_lu', 'scsidisk', "$scfg->{pool}/$zvol");
89 sub nexenta_create_zvol
{
90 my ($scfg, $zvol, $size) = @_;
92 nexenta_request
($scfg, 'create', 'zvol', "$scfg->{pool}/$zvol", "${size}KB",
93 $scfg->{blocksize
}, 1);
96 sub nexenta_delete_zvol
{
97 my ($scfg, $zvol) = @_;
99 nexenta_request
($scfg, 'destroy', 'zvol', "$scfg->{pool}/$zvol", '-r');
102 sub nexenta_list_zvol
{
105 my $zvols = nexenta_request
($scfg, 'get_names', 'zvol', '');
106 return undef if !$zvols;
109 foreach my $zvol (@$zvols) {
110 my @values = split('/', $zvol);
112 my $pool = $values[0];
113 my $image = $values[1];
116 if ($image =~ m/^((vm|base)-(\d+)-\S+)$/) {
122 my $props = nexenta_get_zvol_props
($scfg, $zvol);
123 my $parent = $props->{origin
};
124 if($parent && $parent =~ m/^$scfg->{pool}\/(\S
+)$/){
128 $list->{$pool}->{$image} = {
130 size
=> $props->{size_bytes
},
148 content
=> [ {images
=> 1}, { images
=> 1 }],
155 description
=> "login",
159 description
=> "password",
163 description
=> "block size",
167 description
=> "ssl",
175 nodes
=> { optional
=> 1 },
176 disable
=> { optional
=> 1 },
177 target
=> { fixed
=> 1 },
178 portal
=> { fixed
=> 1 },
179 login
=> { fixed
=> 1 },
180 password
=> { fixed
=> 1 },
181 pool
=> { fixed
=> 1 },
182 blocksize
=> { fixed
=> 1 },
183 ssl
=> { optional
=> 1 },
184 content
=> { optional
=> 1 },
188 # Storage implementation
191 my ($class, $volname) = @_;
193 if ($volname =~ m/^(((base|vm)-(\d+)-\S+)\/)?
((base
)?
(vm
)?
-(\d
+)-\S
+)$/) {
194 return ('images', $5, $8, $2, $4, $6);
197 die "unable to parse nexenta volume name '$volname'\n";
201 my ($class, $scfg, $volname) = @_;
203 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
205 my $target = $scfg->{target
};
206 my $portal = $scfg->{portal
};
208 my $map = nexenta_list_lun_mapping_entries
($scfg, $name);
209 die "could not find lun number" if !$map;
210 my $lun = @$map[0]->{lun
};
211 $lun =~ m/^(\d+)$/ or die "lun is not OK\n";
213 my $path = "iscsi://$portal/$target/$lun";
215 return ($path, $vmid, $vtype);
218 my $find_free_diskname = sub {
219 my ($storeid, $scfg, $vmid) = @_;
222 my $volumes = nexenta_list_zvol
($scfg);
223 die "unable de get zvol list" if !$volumes;
226 my $dat = $volumes->{$scfg->{pool
}};
228 foreach my $image (keys %$dat) {
229 my $volname = $dat->{$image}->{name
};
230 if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
235 #fix: can we search in $rbd hash key with a regex to find (vm|base) ?
236 for (my $i = 1; $i < 100; $i++) {
237 if (!$disk_ids->{$i}) {
238 return "vm-$vmid-disk-$i";
242 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
247 my ($class, $storeid, $scfg, $volname) = @_;
249 my $snap = '__base__';
251 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
252 $class->parse_volname($volname);
254 die "create_base not possible with base image\n" if $isBase;
256 # die "volname '$volname' contains wrong information about parent $parent $basename\n"
257 # if $basename && (!$parent || $parent ne $basename."@".$snap);
260 $newname =~ s/^vm-/base-/;
262 my $newvolname = $basename ?
"$basename/$newname" : "$newname";
264 #we can't rename a nexenta volume, so clone it to a new volname
265 nexenta_request
($scfg, 'create_snapshot', 'zvol', "$scfg->{pool}/$name", $snap, '');
266 nexenta_request
($scfg, 'clone', 'zvol', "$scfg->{pool}/$name\@$snap", "$scfg->{pool}/$newname");
267 nexenta_create_lu
($scfg, $newname);
268 nexenta_add_lun_mapping_entry
($scfg, $newname);
270 my $running = undef; #fixme : is create_base always offline ?
272 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
278 my ($class, $scfg, $storeid, $volname, $vmid) = @_;
280 my $snap = '__base__';
282 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
283 $class->parse_volname($volname);
285 die "clone_image only works on base images\n" if !$isBase;
287 my $name = &$find_free_diskname($storeid, $scfg, $vmid);
289 warn "clone $volname: $basename to $name\n";
291 my $newvol = "$basename/$name";
293 nexenta_request
($scfg, 'clone', 'zvol', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
295 nexenta_create_lu
($scfg, $name);
296 nexenta_add_lun_mapping_entry
($scfg, $name);
302 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
304 die "unsupported format '$fmt'" if $fmt ne 'raw';
306 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
307 if $name && $name !~ m/^vm-$vmid-/;
309 $name = &$find_free_diskname($storeid, $scfg, $vmid);
311 nexenta_create_zvol
($scfg, $name, $size);
312 nexenta_create_lu
($scfg, $name);
313 nexenta_add_lun_mapping_entry
($scfg, $name);
319 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
321 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
323 nexenta_delete_lu
($scfg, $name);
324 nexenta_delete_zvol
($scfg, $name);
326 #if base volume, we delete also the original cloned volume
328 $name =~ s/^base-/vm-/;
329 nexenta_delete_lu
($scfg, $name);
330 nexenta_delete_zvol
($scfg, $name);
337 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
339 $cache->{nexenta
} = nexenta_list_zvol
($scfg) if !$cache->{nexenta
};
340 my $nexentapool = $scfg->{pool
};
342 if (my $dat = $cache->{nexenta
}->{$nexentapool}) {
343 foreach my $image (keys %$dat) {
345 my $volname = $dat->{$image}->{name
};
346 my $parent = $dat->{$image}->{parent
};
349 if ($parent && $parent =~ m/^(\S+)@(\S+)$/) {
350 my ($basename) = ($1);
351 $volid = "$storeid:$basename/$volname";
353 $volid = "$storeid:$volname";
356 my $owner = $dat->{$volname}->{vmid
};
358 my $found = grep { $_ eq $volid } @$vollist;
361 next if defined ($vmid) && ($owner ne $vmid);
364 my $info = $dat->{$volname};
365 $info->{volid
} = $volid;
375 sub nexenta_parse_size
{
380 if ($text =~ m/^(\d+)([TGMK])?$/) {
381 my ($size, $unit) = ($1, $2);
382 return $size if !$unit;
385 } elsif ($unit eq 'M') {
387 } elsif ($unit eq 'G') {
388 $size *= 1024*1024*1024;
389 } elsif ($unit eq 'T') {
390 $size *= 1024*1024*1024*1024;
398 my ($class, $storeid, $scfg, $cache) = @_;
406 my $map = nexenta_request
($scfg, 'get_child_props', 'volume', $scfg->{pool
}, '');
408 $total = nexenta_parse_size
($map->{size
});
409 $used = nexenta_parse_size
($map->{used
});
410 $free = $total - $used;
414 return ($total, $free, $used, $active);
417 sub activate_storage
{
418 my ($class, $storeid, $scfg, $cache) = @_;
422 sub deactivate_storage
{
423 my ($class, $storeid, $scfg, $cache) = @_;
427 sub activate_volume
{
428 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
432 sub deactivate_volume
{
433 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
437 sub volume_size_info
{
438 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
440 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
442 return nexenta_get_zvol_size
($scfg, "$scfg->{pool}/$name"),
446 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
448 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
450 nexenta_request
($scfg, 'set_child_prop', 'zvol', "$scfg->{pool}/$name", 'volsize', ($size/1024) . 'KB');
453 sub volume_snapshot
{
454 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
456 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
458 nexenta_request
($scfg, 'create_snapshot', 'zvol', "$scfg->{pool}/$name", $snap, '');
461 sub volume_snapshot_rollback
{
462 my ($class, $scfg, $storeid, $volname, $snap) = @_;
464 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
466 nexenta_delete_lu
($scfg, $name);
468 nexenta_request
($scfg, 'rollback', 'snapshot', "$scfg->{pool}/$name\@$snap", '');
470 nexenta_import_lu
($scfg, $name);
472 nexenta_add_lun_mapping_entry
($scfg, $name);
475 sub volume_snapshot_delete
{
476 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
478 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
480 nexenta_request
($scfg, 'destroy', 'snapshot', "$scfg->{pool}/$name\@$snap", '');
483 sub volume_has_feature
{
484 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
487 snapshot
=> { current
=> 1, snap
=> 1},
488 clone
=> { base
=> 1},
489 template
=> { current
=> 1},
490 copy
=> { base
=> 1, current
=> 1},
493 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
494 $class->parse_volname($volname);
500 $key = $isBase ?
'base' : 'current';
502 return 1 if $features->{$feature}->{$key};