]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/NexentaPlugin.pm
nexenta: nexenta_get_zvol_size : parse result to avoid tainted value
[pve-storage.git] / PVE / Storage / NexentaPlugin.pm
1 package PVE::Storage::NexentaPlugin;
2
3 use strict;
4 use warnings;
5 use IO::File;
6 use HTTP::Request;
7 use LWP::UserAgent;
8 use MIME::Base64;
9 use JSON;
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);
13
14 use base qw(PVE::Storage::Plugin);
15
16 sub nexenta_request {
17 my ($scfg, $method, $object, @params) = @_;
18
19 my $apicall = { method => $method, object => $object, params => [ @params ] };
20
21 my $json = encode_json($apicall);
22
23 my $uri = ($scfg->{ssl} ? "https" : "http") . "://" . $scfg->{portal} . ":2000/rest/nms/";
24 my $req = HTTP::Request->new('POST', $uri);
25
26 $req->header('Content-Type' => 'application/json');
27 $req->content($json);
28 my $token = encode_base64("$scfg->{login}:$scfg->{password}");
29 $req->header(Authorization => "Basic $token");
30
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;
34
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};
39 }
40
41
42 sub nexenta_get_zvol_size {
43 my ($scfg, $zvol) = @_;
44
45 my $ret = nexenta_request($scfg, 'get_child_prop', 'zvol', $zvol, 'size_bytes');
46 $ret =~ m/^(\d+)$/ or die "size is not valid";
47 my $size = $1;
48 return $size;
49 }
50
51 sub nexenta_get_zvol_props {
52 my ($scfg, $zvol) = @_;
53
54 my $props = nexenta_request($scfg, 'get_child_props', 'zvol', $zvol, '');
55 return $props;
56 }
57
58 sub nexenta_list_lun_mapping_entries {
59 my ($scfg, $zvol) = @_;
60
61 return nexenta_request($scfg, 'list_lun_mapping_entries', 'scsidisk', "$scfg->{pool}/$zvol");
62 }
63
64 sub nexenta_add_lun_mapping_entry {
65 my ($scfg, $zvol) = @_;
66
67 nexenta_request($scfg, 'add_lun_mapping_entry', 'scsidisk',
68 "$scfg->{pool}/$zvol", { target_group => "All" });
69 }
70
71 sub nexenta_delete_lu {
72 my ($scfg, $zvol) = @_;
73
74 nexenta_request($scfg, 'delete_lu', 'scsidisk', "$scfg->{pool}/$zvol");
75 }
76
77 sub nexenta_create_lu {
78 my ($scfg, $zvol) = @_;
79
80 nexenta_request($scfg, 'create_lu', 'scsidisk', "$scfg->{pool}/$zvol", {});
81 }
82
83 sub nexenta_import_lu {
84 my ($scfg, $zvol) = @_;
85
86 nexenta_request($scfg, 'import_lu', 'scsidisk', "$scfg->{pool}/$zvol");
87 }
88
89 sub nexenta_create_zvol {
90 my ($scfg, $zvol, $size) = @_;
91
92 nexenta_request($scfg, 'create', 'zvol', "$scfg->{pool}/$zvol", "${size}KB",
93 $scfg->{blocksize}, 1);
94 }
95
96 sub nexenta_delete_zvol {
97 my ($scfg, $zvol) = @_;
98
99 nexenta_request($scfg, 'destroy', 'zvol', "$scfg->{pool}/$zvol", '-r');
100 }
101
102 sub nexenta_list_zvol {
103 my ($scfg) = @_;
104
105 my $zvols = nexenta_request($scfg, 'get_names', 'zvol', '');
106 return undef if !$zvols;
107
108 my $list = {};
109 foreach my $zvol (@$zvols) {
110 my @values = split('/', $zvol);
111
112 my $pool = $values[0];
113 my $image = $values[1];
114 my $owner;
115
116 if ($image =~ m/^((vm|base)-(\d+)-\S+)$/) {
117 $owner = $3;
118 } else {
119 next;
120 }
121
122 my $props = nexenta_get_zvol_props($scfg, $zvol);
123 my $parent = $props->{origin};
124 if($parent && $parent =~ m/^$scfg->{pool}\/(\S+)$/){
125 $parent = $1;
126 }
127
128 $list->{$pool}->{$image} = {
129 name => $image,
130 size => $props->{size_bytes},
131 parent => $parent,
132 format => 'raw',
133 vmid => $owner
134 };
135 }
136
137 return $list;
138 }
139
140 # Configuration
141
142 sub type {
143 return 'nexenta';
144 }
145
146 sub plugindata {
147 return {
148 content => [ {images => 1}, { images => 1 }],
149 };
150 }
151
152 sub properties {
153 return {
154 login => {
155 description => "login",
156 type => 'string',
157 },
158 password => {
159 description => "password",
160 type => 'string',
161 },
162 blocksize => {
163 description => "block size",
164 type => 'string',
165 },
166 ssl => {
167 description => "ssl",
168 type => 'boolean',
169 },
170 };
171 }
172
173 sub options {
174 return {
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 },
185 };
186 }
187
188 # Storage implementation
189
190 sub parse_volname {
191 my ($class, $volname) = @_;
192
193 if ($volname =~ m/^(((base|vm)-(\d+)-\S+)\/)?((base)?(vm)?-(\d+)-\S+)$/) {
194 return ('images', $5, $8, $2, $4, $6);
195 }
196
197 die "unable to parse nexenta volume name '$volname'\n";
198 }
199
200 sub path {
201 my ($class, $scfg, $volname) = @_;
202
203 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
204
205 my $target = $scfg->{target};
206 my $portal = $scfg->{portal};
207
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";
212 $lun = $1;
213 my $path = "iscsi://$portal/$target/$lun";
214
215 return ($path, $vmid, $vtype);
216 }
217
218 my $find_free_diskname = sub {
219 my ($storeid, $scfg, $vmid) = @_;
220
221 my $name = undef;
222 my $volumes = nexenta_list_zvol($scfg);
223 die "unable de get zvol list" if !$volumes;
224
225 my $disk_ids = {};
226 my $dat = $volumes->{$scfg->{pool}};
227
228 foreach my $image (keys %$dat) {
229 my $volname = $dat->{$image}->{name};
230 if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){
231 $disk_ids->{$2} = 1;
232 }
233 }
234
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";
239 }
240 }
241
242 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
243
244 };
245
246 sub create_base {
247 my ($class, $storeid, $scfg, $volname) = @_;
248
249 my $snap = '__base__';
250
251 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
252 $class->parse_volname($volname);
253
254 die "create_base not possible with base image\n" if $isBase;
255
256 # die "volname '$volname' contains wrong information about parent $parent $basename\n"
257 # if $basename && (!$parent || $parent ne $basename."@".$snap);
258
259 my $newname = $name;
260 $newname =~ s/^vm-/base-/;
261
262 my $newvolname = $basename ? "$basename/$newname" : "$newname";
263
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);
269
270 my $running = undef; #fixme : is create_base always offline ?
271
272 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
273
274 return $newvolname;
275 }
276
277 sub clone_image {
278 my ($class, $scfg, $storeid, $volname, $vmid) = @_;
279
280 my $snap = '__base__';
281
282 my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
283 $class->parse_volname($volname);
284
285 die "clone_image only works on base images\n" if !$isBase;
286
287 my $name = &$find_free_diskname($storeid, $scfg, $vmid);
288
289 warn "clone $volname: $basename to $name\n";
290
291 my $newvol = "$basename/$name";
292
293 nexenta_request($scfg, 'clone', 'zvol', "$scfg->{pool}/$basename\@$snap", "$scfg->{pool}/$name");
294
295 nexenta_create_lu($scfg, $name);
296 nexenta_add_lun_mapping_entry($scfg, $name);
297
298 return $newvol;
299 }
300
301 sub alloc_image {
302 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
303
304 die "unsupported format '$fmt'" if $fmt ne 'raw';
305
306 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
307 if $name && $name !~ m/^vm-$vmid-/;
308
309 $name = &$find_free_diskname($storeid, $scfg, $vmid);
310
311 nexenta_create_zvol($scfg, $name, $size);
312 nexenta_create_lu($scfg, $name);
313 nexenta_add_lun_mapping_entry($scfg, $name);
314
315 return $name;
316 }
317
318 sub free_image {
319 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
320
321 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
322
323 nexenta_delete_lu($scfg, $name);
324 nexenta_delete_zvol($scfg, $name);
325
326 #if base volume, we delete also the original cloned volume
327 if ($isBase) {
328 $name =~ s/^base-/vm-/;
329 nexenta_delete_lu($scfg, $name);
330 nexenta_delete_zvol($scfg, $name);
331 }
332
333 return undef;
334 }
335
336 sub list_images {
337 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
338
339 $cache->{nexenta} = nexenta_list_zvol($scfg) if !$cache->{nexenta};
340 my $nexentapool = $scfg->{pool};
341 my $res = [];
342 if (my $dat = $cache->{nexenta}->{$nexentapool}) {
343 foreach my $image (keys %$dat) {
344
345 my $volname = $dat->{$image}->{name};
346 my $parent = $dat->{$image}->{parent};
347
348 my $volid = undef;
349 if ($parent && $parent =~ m/^(\S+)@(\S+)$/) {
350 my ($basename) = ($1);
351 $volid = "$storeid:$basename/$volname";
352 } else {
353 $volid = "$storeid:$volname";
354 }
355
356 my $owner = $dat->{$volname}->{vmid};
357 if ($vollist) {
358 my $found = grep { $_ eq $volid } @$vollist;
359 next if !$found;
360 } else {
361 next if defined ($vmid) && ($owner ne $vmid);
362 }
363
364 my $info = $dat->{$volname};
365 $info->{volid} = $volid;
366
367 push @$res, $info;
368
369 }
370 }
371
372 return $res;
373 }
374
375 sub nexenta_parse_size {
376 my ($text) = @_;
377
378 return 0 if !$text;
379
380 if ($text =~ m/^(\d+)([TGMK])?$/) {
381 my ($size, $unit) = ($1, $2);
382 return $size if !$unit;
383 if ($unit eq 'K') {
384 $size *= 1024;
385 } elsif ($unit eq 'M') {
386 $size *= 1024*1024;
387 } elsif ($unit eq 'G') {
388 $size *= 1024*1024*1024;
389 } elsif ($unit eq 'T') {
390 $size *= 1024*1024*1024*1024;
391 }
392 return $size;
393 } else {
394 return 0;
395 }
396 }
397 sub status {
398 my ($class, $storeid, $scfg, $cache) = @_;
399
400 my $total = 0;
401 my $free = 0;
402 my $used = 0;
403 my $active = 0;
404
405 eval {
406 my $map = nexenta_request($scfg, 'get_child_props', 'volume', $scfg->{pool}, '');
407 $active = 1;
408 $total = nexenta_parse_size($map->{size});
409 $used = nexenta_parse_size($map->{used});
410 $free = $total - $used;
411 };
412 warn $@ if $@;
413
414 return ($total, $free, $used, $active);
415 }
416
417 sub activate_storage {
418 my ($class, $storeid, $scfg, $cache) = @_;
419 return 1;
420 }
421
422 sub deactivate_storage {
423 my ($class, $storeid, $scfg, $cache) = @_;
424 return 1;
425 }
426
427 sub activate_volume {
428 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
429 return 1;
430 }
431
432 sub deactivate_volume {
433 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
434 return 1;
435 }
436
437 sub volume_size_info {
438 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
439
440 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
441
442 return nexenta_get_zvol_size($scfg, "$scfg->{pool}/$name"),
443 }
444
445 sub volume_resize {
446 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
447
448 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
449
450 nexenta_request($scfg, 'set_child_prop', 'zvol', "$scfg->{pool}/$name", 'volsize', ($size/1024) . 'KB');
451 }
452
453 sub volume_snapshot {
454 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
455
456 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
457
458 nexenta_request($scfg, 'create_snapshot', 'zvol', "$scfg->{pool}/$name", $snap, '');
459 }
460
461 sub volume_snapshot_rollback {
462 my ($class, $scfg, $storeid, $volname, $snap) = @_;
463
464 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
465
466 nexenta_delete_lu($scfg, $name);
467
468 nexenta_request($scfg, 'rollback', 'snapshot', "$scfg->{pool}/$name\@$snap", '');
469
470 nexenta_import_lu($scfg, $name);
471
472 nexenta_add_lun_mapping_entry($scfg, $name);
473 }
474
475 sub volume_snapshot_delete {
476 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
477
478 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
479
480 nexenta_request($scfg, 'destroy', 'snapshot', "$scfg->{pool}/$name\@$snap", '');
481 }
482
483 sub volume_has_feature {
484 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
485
486 my $features = {
487 snapshot => { current => 1, snap => 1},
488 clone => { base => 1},
489 template => { current => 1},
490 copy => { base => 1, current => 1},
491 };
492
493 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
494 $class->parse_volname($volname);
495
496 my $key = undef;
497 if($snapname){
498 $key = 'snap';
499 }else{
500 $key = $isBase ? 'base' : 'current';
501 }
502 return 1 if $features->{$feature}->{$key};
503
504 return undef;
505 }
506
507 1;