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, $json) = @_;
19 my $uri = ( $scfg->{ssl
} ?
"https" : "http" ) . "://" . $scfg->{portal
} . ":2000/rest/nms/";
20 my $req = HTTP
::Request-
>new( 'POST', $uri );
22 $req->header( 'Content-Type' => 'application/json' );
23 $req->content( $json );
24 my $token = encode_base64
("$scfg->{login}:$scfg->{password}");
25 $req->header( Authorization
=> "Basic $token" );
27 my $ua = LWP
::UserAgent-
>new; # You might want some options here
28 my $res = $ua->request($req);
29 if (!$res->is_success) {
32 my $obj = eval { from_json
($res->content); };
33 die "JSON not valid. Content: " . $res->content if ($@);
34 die "Nexenta API Error: " . $obj->{error
}->{message
} if $obj->{error
}->{message
};
35 return $obj->{result
} if $obj->{result
};
39 sub nexenta_get_zvol_size
{
40 my ($zvol, $scfg) = @_;
42 my $json = '{"method": "get_child_prop", "object": "zvol", "params": ["' . $zvol . '", "volsize"]}';
43 my $volsize = nexenta_request
($scfg, $json);
44 if ($volsize =~ /^(\d+)([KMGT])$/) {
45 my ($size, $unit) = ($1, $2);
48 } elsif ($unit eq 'M') {
50 } elsif ($unit eq 'G') {
51 $size *= 1024*1024*1024;
52 } elsif ($unit eq 'T') {
53 $size *= 1024*1024*1024*1024;
57 die "got undefined size '$volsize'\n";
60 sub nexenta_list_lun_mapping_entries
{
61 my ($zvol, $scfg) = @_;
63 my $json = '{"method": "list_lun_mapping_entries","object" : "scsidisk","params": ["'.$scfg->{pool
}.'/'.$zvol.'"]}';
64 my $map = nexenta_request
($scfg,$json);
69 sub nexenta_add_lun_mapping_entry
{
70 my ($zvol, $scfg) = @_;
72 my $json = '{"method": "add_lun_mapping_entry","object" : "scsidisk","params": ["'.$scfg->{pool
}.'/'.$zvol.'",{"target_group": "All"}]}';
74 nexenta_request
($scfg, $json);
78 sub nexenta_delete_lu
{
79 my ($zvol, $scfg) = @_;
81 my $json = '{"method": "delete_lu","object" : "scsidisk","params": ["'.$scfg->{pool
}.'/'.$zvol.'"]}';
82 nexenta_request
($scfg, $json);
86 sub nexenta_create_lu
{
87 my ($zvol, $scfg) = @_;
89 my $json = '{"method": "create_lu","object" : "scsidisk","params": ["'.$scfg->{pool
}.'/'.$zvol.'",{}]}';
91 nexenta_request
($scfg, $json);
95 sub nexenta_create_zvol
{
96 my ($zvol, $size, $scfg) = @_;
98 my $blocksize = $scfg->{blocksize
};
99 my $nexentapool = $scfg->{pool
};
101 my $json = '{"method": "create","object" : "zvol","params": ["'.$nexentapool.'/'.$zvol.'", "'.$size.'KB", "'.$blocksize.'", "1"]}';
103 nexenta_request
($scfg, $json);
107 sub nexenta_delete_zvol
{
108 my ($zvol, $scfg) = @_;
111 my $json = '{"method": "destroy","object" : "zvol","params": ["'.$scfg->{pool
}.'/'.$zvol.'", ""]}';
112 nexenta_request
($scfg, $json);
116 sub nexenta_list_zvol
{
119 my $json = '{"method": "get_names","object" : "zvol","params": [""]}';
122 my $zvols = nexenta_request
($scfg, $json);
123 return undef if !$zvols;
127 foreach my $zvol (@$zvols) {
128 my @values = split('/', $zvol);
129 #$volumes->{$values[0]}->{$values[1]}->{volname} = $values[1];
131 my $pool = $values[0];
132 my $image = $values[1];
134 if ($image =~ m/^(vm-(\d+)-\S+)$/) {
138 $list->{$pool}->{$image} = {
140 size
=> nexenta_get_zvol_size
($zvol, $scfg),
158 content
=> [ {images
=> 1}, { images
=> 1 }],
165 description
=> "login",
169 description
=> "password",
173 description
=> "block size",
177 description
=> "ssl",
185 nodes
=> { optional
=> 1 },
186 disable
=> { optional
=> 1 },
187 target
=> { fixed
=> 1 },
188 portal
=> { fixed
=> 1 },
189 login
=> { fixed
=> 1 },
190 password
=> { fixed
=> 1 },
191 pool
=> { fixed
=> 1 },
192 blocksize
=> { fixed
=> 1 },
193 ssl
=> { optional
=> 1 },
194 content
=> { optional
=> 1 },
199 # Storage implementation
202 my ($class, $volname) = @_;
204 if ($volname =~ m/^(vm-(\d+)-\S+)$/) {
205 return ('images', $1, $2);
208 return('images',$volname,'');
209 #die "unable to parse lvm volume name '$volname'\n";
213 my ($class, $scfg, $volname) = @_;
215 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
217 my $target = $scfg->{target
};
218 my $portal = $scfg->{portal
};
220 my $map = nexenta_list_lun_mapping_entries
($name,$scfg);
221 die "could not find lun number" if !$map;
222 my $lun = @$map[0]->{lun
};
223 $lun =~ m/^(\d+)$/ or die "lun is not OK\n";
225 my $path = "iscsi://$portal/$target/$lun";
227 return ($path, $vmid, $vtype);
232 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
234 die "unsupported format '$fmt'" if $fmt ne 'raw';
236 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
237 if $name && $name !~ m/^vm-$vmid-/;
239 my $nexentapool = $scfg->{'pool'};
242 my $volumes = nexenta_list_zvol
($scfg);
243 die "unable de get zvol list" if !$volumes;
245 for (my $i = 1; $i < 100; $i++) {
247 my $tn = "vm-$vmid-disk-$i";
248 if (!defined ($volumes->{$nexentapool}->{$tn})) {
255 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
258 eval { nexenta_create_zvol
($name, $size, $scfg); };
260 eval { nexenta_create_lu
($name, $scfg); };
262 nexenta_add_lun_mapping_entry
($name, $scfg);
268 my ($class, $storeid, $scfg, $volname) = @_;
270 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
272 eval { nexenta_delete_lu
($name, $scfg); };
274 nexenta_delete_zvol
($name, $scfg);
281 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
283 $cache->{nexenta
} = nexenta_list_zvol
($scfg) if !$cache->{nexenta
};
284 my $nexentapool = $scfg->{pool
};
286 if (my $dat = $cache->{nexenta
}->{$nexentapool}) {
287 foreach my $image (keys %$dat) {
289 my $volname = $dat->{$image}->{name
};
291 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;
314 my ($class, $storeid, $scfg, $cache) = @_;
320 return ($total,$free,$used,$active);
325 sub activate_storage
{
326 my ($class, $storeid, $scfg, $cache) = @_;
330 sub deactivate_storage
{
331 my ($class, $storeid, $scfg, $cache) = @_;
335 sub activate_volume
{
336 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
340 sub deactivate_volume
{
341 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
345 sub volume_size_info
{
346 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
348 my $json = '{"method": "get_child_prop","object" : "zvol","params": ["'.$scfg->{pool
}.'/'.$volname.'", "size_bytes"]}';
349 my $size = nexenta_request
($scfg, $json);
354 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
356 my $json = '{"method": "set_child_prop","object" : "zvol","params": ["'.$scfg->{pool
}.'/'.$volname.'", "volsize", "'.($size/1024).'KB"]}';
357 nexenta_request
($scfg, $json);
361 sub volume_snapshot
{
362 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
364 my $json = '{"method": "create_snapshot","object" : "zvol","params": ["'.$scfg->{pool
}.'/'.$volname.'", "'.$snap.'", ""]}';
365 nexenta_request
($scfg, $json);
369 sub volume_snapshot_rollback
{
370 my ($class, $scfg, $storeid, $volname, $snap) = @_;
372 eval { nexenta_delete_lu
($volname, $scfg); };
374 my $json = '{"method": "rollback","object" : "snapshot","params": ["'.$scfg->{pool
}.'/'.$volname.'@'.$snap.'", ""]}';
375 nexenta_request
($scfg, $json);
377 eval { nexenta_create_lu
($volname, $scfg); };
379 nexenta_add_lun_mapping_entry
($volname, $scfg);
382 sub volume_snapshot_delete
{
383 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
385 my $json = '{"method": "destroy","object" : "snapshot","params": ["'.$scfg->{pool
}.'/'.$volname.'@'.$snap.'"]}';
386 nexenta_request
($scfg, $json);