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; # You might want some options here
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 return nexenta_request
($scfg, 'get_child_prop', 'zvol', $zvol, 'size_bytes');
48 sub nexenta_get_zvol_props
{
49 my ($scfg, $zvol) = @_;
51 my $props = nexenta_request
($scfg, 'get_child_props', 'zvol', $zvol, '');
55 sub nexenta_list_lun_mapping_entries
{
56 my ($scfg, $zvol) = @_;
58 return nexenta_request
($scfg, 'list_lun_mapping_entries', 'scsidisk', "$scfg->{pool}/$zvol");
61 sub nexenta_add_lun_mapping_entry
{
62 my ($scfg, $zvol) = @_;
64 nexenta_request
($scfg, 'add_lun_mapping_entry', 'scsidisk',
65 "$scfg->{pool}/$zvol", { target_group
=> "All" });
68 sub nexenta_delete_lu
{
69 my ($scfg, $zvol) = @_;
71 nexenta_request
($scfg, 'delete_lu', 'scsidisk', "$scfg->{pool}/$zvol");
74 sub nexenta_create_lu
{
75 my ($scfg, $zvol) = @_;
77 nexenta_request
($scfg, 'create_lu', 'scsidisk', "$scfg->{pool}/$zvol", {});
80 sub nexenta_import_lu
{
81 my ($scfg, $zvol) = @_;
83 nexenta_request
($scfg, 'import_lu', 'scsidisk', "$scfg->{pool}/$zvol");
86 sub nexenta_create_zvol
{
87 my ($scfg, $zvol, $size) = @_;
89 nexenta_request
($scfg, 'create', 'zvol', "$scfg->{pool}/$zvol", "${size}KB",
90 $scfg->{blocksize
}, 1);
93 sub nexenta_delete_zvol
{
94 my ($scfg, $zvol) = @_;
96 nexenta_request
($scfg, 'destroy', 'zvol', "$scfg->{pool}/$zvol", '-r');
99 sub nexenta_list_zvol
{
102 my $zvols = nexenta_request
($scfg, 'get_names', 'zvol', '');
103 return undef if !$zvols;
106 foreach my $zvol (@$zvols) {
107 my @values = split('/', $zvol);
109 my $pool = $values[0];
110 my $image = $values[1];
113 if ($image =~ m/^((vm|base)-(\d+)-\S+)$/) {
117 my $props = nexenta_get_zvol_props
($scfg, $zvol);
118 my $parent = $props->{origin
};
119 if($parent && $parent =~ m/^$scfg->{pool}\/(\S
+)$/){
123 $list->{$pool}->{$image} = {
125 size
=> $props->{size_bytes
},
143 content
=> [ {images
=> 1}, { images
=> 1 }],
150 description
=> "login",
154 description
=> "password",
158 description
=> "block size",
162 description
=> "ssl",
170 nodes
=> { optional
=> 1 },
171 disable
=> { optional
=> 1 },
172 target
=> { fixed
=> 1 },
173 portal
=> { fixed
=> 1 },
174 login
=> { fixed
=> 1 },
175 password
=> { fixed
=> 1 },
176 pool
=> { fixed
=> 1 },
177 blocksize
=> { fixed
=> 1 },
178 ssl
=> { optional
=> 1 },
179 content
=> { optional
=> 1 },
183 # Storage implementation
186 my ($class, $volname) = @_;
188 if ($volname =~ m/^(((base|vm)-(\d+)-\S+)\/)?
((base
)?
(vm
)?
-(\d
+)-\S
+)$/) {
189 return ('images', $5, $8, $2, $4, $6);
192 die "unable to parse nexenta volume name '$volname'\n";
196 my ($class, $scfg, $volname) = @_;
198 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
200 my $target = $scfg->{target
};
201 my $portal = $scfg->{portal
};
203 my $map = nexenta_list_lun_mapping_entries
($scfg, $name);
204 die "could not find lun number" if !$map;
205 my $lun = @$map[0]->{lun
};
206 $lun =~ m/^(\d+)$/ or die "lun is not OK\n";
208 my $path = "iscsi://$portal/$target/$lun";
210 return ($path, $vmid, $vtype);
213 my $find_free_diskname = sub {
214 my ($storeid, $scfg, $vmid) = @_;
217 my $volumes = nexenta_list_zvol
($scfg);
218 die "unable de get zvol list" if !$volumes;
221 my $dat = $volumes->{$scfg->{pool
}};
223 foreach my $image (keys %$dat) {
224 my $volname = $dat->{$image}->{name
};
225 if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
230 #fix: can we search in $rbd hash key with a regex to find (vm|base) ?
231 for (my $i = 1; $i < 100; $i++) {
232 if (!$disk_ids->{$i}) {
233 return "vm-$vmid-disk-$i";
237 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
242 my ($class, $storeid, $scfg, $volname) = @_;
244 die "not implemented";
248 my ($class, $scfg, $storeid, $volname, $vmid) = @_;
250 die "not implemented";
254 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
256 die "unsupported format '$fmt'" if $fmt ne 'raw';
258 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
259 if $name && $name !~ m/^vm-$vmid-/;
261 $name = &$find_free_diskname($storeid, $scfg, $vmid);
263 nexenta_create_zvol
($scfg, $name, $size);
264 nexenta_create_lu
($scfg, $name);
265 nexenta_add_lun_mapping_entry
($scfg, $name);
271 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
273 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
275 nexenta_delete_lu
($scfg, $name);
276 nexenta_delete_zvol
($scfg, $name);
282 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
284 $cache->{nexenta
} = nexenta_list_zvol
($scfg) if !$cache->{nexenta
};
285 my $nexentapool = $scfg->{pool
};
287 if (my $dat = $cache->{nexenta
}->{$nexentapool}) {
288 foreach my $image (keys %$dat) {
290 my $volname = $dat->{$image}->{name
};
292 my $volid = "$storeid:$volname";
294 my $owner = $dat->{$volname}->{vmid
};
296 my $found = grep { $_ eq $volid } @$vollist;
299 next if defined ($vmid) && ($owner ne $vmid);
302 my $info = $dat->{$volname};
303 $info->{volid
} = $volid;
313 sub nexenta_parse_size
{
318 if ($text =~ m/^(\d+)([TGMK])?$/) {
319 my ($size, $unit) = ($1, $2);
320 return $size if !$unit;
323 } elsif ($unit eq 'M') {
325 } elsif ($unit eq 'G') {
326 $size *= 1024*1024*1024;
327 } elsif ($unit eq 'T') {
328 $size *= 1024*1024*1024*1024;
336 my ($class, $storeid, $scfg, $cache) = @_;
344 my $map = nexenta_request
($scfg, 'get_child_props', 'volume', $scfg->{pool
}, '');
346 $total = nexenta_parse_size
($map->{size
});
347 $used = nexenta_parse_size
($map->{used
});
348 $free = $total - $used;
352 return ($total, $free, $used, $active);
355 sub activate_storage
{
356 my ($class, $storeid, $scfg, $cache) = @_;
360 sub deactivate_storage
{
361 my ($class, $storeid, $scfg, $cache) = @_;
365 sub activate_volume
{
366 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
370 sub deactivate_volume
{
371 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
375 sub volume_size_info
{
376 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
378 return nexenta_get_zvol_size
($scfg, "$scfg->{pool}/$volname"),
382 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
384 nexenta_request
($scfg, 'set_child_prop', 'zvol', "$scfg->{pool}/$volname", 'volsize', ($size/1024) . 'KB');
387 sub volume_snapshot
{
388 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
390 nexenta_request
($scfg, 'create_snapshot', 'zvol', "$scfg->{pool}/$volname", $snap, '');
393 sub volume_snapshot_rollback
{
394 my ($class, $scfg, $storeid, $volname, $snap) = @_;
396 nexenta_delete_lu
($scfg, $volname);
398 nexenta_request
($scfg, 'rollback', 'snapshot', "$scfg->{pool}/$volname\@$snap", '');
400 nexenta_import_lu
($scfg, $volname);
402 nexenta_add_lun_mapping_entry
($scfg, $volname);
405 sub volume_snapshot_delete
{
406 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
408 nexenta_request
($scfg, 'destroy', 'snapshot', "$scfg->{pool}/$volname\@$snap", '');
411 sub volume_has_feature
{
412 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
415 snapshot
=> { current
=> 1, snap
=> 1},
416 clone
=> { snap
=> 1},
419 my $snap = $snapname ?
'snap' : 'current';
420 return 1 if $features->{$feature}->{$snap};