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_list_lun_mapping_entries
{
49 my ($scfg, $zvol) = @_;
51 return nexenta_request
($scfg, 'list_lun_mapping_entries', 'scsidisk', "$scfg->{pool}/$zvol");
54 sub nexenta_add_lun_mapping_entry
{
55 my ($scfg, $zvol) = @_;
57 nexenta_request
($scfg, 'add_lun_mapping_entry', 'scsidisk',
58 "$scfg->{pool}/$zvol", { target_group
=> "All" });
61 sub nexenta_delete_lu
{
62 my ($scfg, $zvol) = @_;
64 nexenta_request
($scfg, 'delete_lu', 'scsidisk', "$scfg->{pool}/$zvol");
67 sub nexenta_create_lu
{
68 my ($scfg, $zvol) = @_;
70 nexenta_request
($scfg, 'create_lu', 'scsidisk', "$scfg->{pool}/$zvol", {});
75 sub nexenta_create_zvol
{
76 my ($scfg, $zvol, $size) = @_;
78 nexenta_request
($scfg, 'create', 'zvol', "$scfg->{pool}/$zvol", "${size}KB",
79 $scfg->{blocksize
}, 1);
84 sub nexenta_delete_zvol
{
85 my ($scfg, $zvol) = @_;
87 nexenta_request
($scfg, 'destroy', 'zvol', "$scfg->{pool}/$zvol", '');
90 sub nexenta_list_zvol
{
93 my $zvols = nexenta_request
($scfg, 'get_names', 'zvol', '');
94 return undef if !$zvols;
97 foreach my $zvol (@$zvols) {
98 my @values = split('/', $zvol);
100 my $pool = $values[0];
101 my $image = $values[1];
103 if ($image =~ m/^(vm-(\d+)-\S+)$/) {
107 $list->{$pool}->{$image} = {
109 size
=> nexenta_get_zvol_size
($scfg, $zvol),
126 content
=> [ {images
=> 1}, { images
=> 1 }],
133 description
=> "login",
137 description
=> "password",
141 description
=> "block size",
145 description
=> "ssl",
153 nodes
=> { optional
=> 1 },
154 disable
=> { optional
=> 1 },
155 target
=> { fixed
=> 1 },
156 portal
=> { fixed
=> 1 },
157 login
=> { fixed
=> 1 },
158 password
=> { fixed
=> 1 },
159 pool
=> { fixed
=> 1 },
160 blocksize
=> { fixed
=> 1 },
161 ssl
=> { optional
=> 1 },
162 content
=> { optional
=> 1 },
167 # Storage implementation
170 my ($class, $volname) = @_;
172 if ($volname =~ m/^(vm-(\d+)-\S+)$/) {
173 return ('images', $1, $2);
176 return('images',$volname,'');
177 #die "unable to parse lvm volume name '$volname'\n";
181 my ($class, $scfg, $volname) = @_;
183 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
185 my $target = $scfg->{target
};
186 my $portal = $scfg->{portal
};
188 my $map = nexenta_list_lun_mapping_entries
($scfg, $name);
189 die "could not find lun number" if !$map;
190 my $lun = @$map[0]->{lun
};
191 $lun =~ m/^(\d+)$/ or die "lun is not OK\n";
193 my $path = "iscsi://$portal/$target/$lun";
195 return ($path, $vmid, $vtype);
200 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
202 die "unsupported format '$fmt'" if $fmt ne 'raw';
204 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
205 if $name && $name !~ m/^vm-$vmid-/;
207 my $nexentapool = $scfg->{'pool'};
210 my $volumes = nexenta_list_zvol
($scfg);
211 die "unable de get zvol list" if !$volumes;
213 for (my $i = 1; $i < 100; $i++) {
215 my $tn = "vm-$vmid-disk-$i";
216 if (!defined ($volumes->{$nexentapool}->{$tn})) {
223 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
226 nexenta_create_zvol
($scfg, $name, $size);
227 nexenta_create_lu
($scfg, $name);
228 nexenta_add_lun_mapping_entry
($scfg, $name);
234 my ($class, $storeid, $scfg, $volname) = @_;
236 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
238 nexenta_delete_lu
($scfg, $name);
239 nexenta_delete_zvol
($scfg, $name);
245 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
247 $cache->{nexenta
} = nexenta_list_zvol
($scfg) if !$cache->{nexenta
};
248 my $nexentapool = $scfg->{pool
};
250 if (my $dat = $cache->{nexenta
}->{$nexentapool}) {
251 foreach my $image (keys %$dat) {
253 my $volname = $dat->{$image}->{name
};
255 my $volid = "$storeid:$volname";
258 my $owner = $dat->{$volname}->{vmid
};
260 my $found = grep { $_ eq $volid } @$vollist;
263 next if defined ($vmid) && ($owner ne $vmid);
266 my $info = $dat->{$volname};
267 $info->{volid
} = $volid;
278 my ($class, $storeid, $scfg, $cache) = @_;
284 return ($total,$free,$used,$active);
289 sub activate_storage
{
290 my ($class, $storeid, $scfg, $cache) = @_;
294 sub deactivate_storage
{
295 my ($class, $storeid, $scfg, $cache) = @_;
299 sub activate_volume
{
300 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
304 sub deactivate_volume
{
305 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
309 sub volume_size_info
{
310 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
312 return nexenta_get_zvol_size
($scfg, "$scfg->{pool}/$volname"),
316 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
318 nexenta_request
($scfg, 'set_child_prop', 'zvol', "$scfg->{pool}/$volname", 'volsize', ($size/1024) . 'KB');
321 sub volume_snapshot
{
322 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
324 nexenta_request
($scfg, 'create_snapshot', 'zvol', "$scfg->{pool}/$volname", $snap, 0);
327 sub volume_snapshot_rollback
{
328 my ($class, $scfg, $storeid, $volname, $snap) = @_;
330 nexenta_delete_lu
($scfg, $volname);
332 nexenta_request
($scfg, 'rollback', 'snapshot', "$scfg->{pool}/$volname\@$snap", '');
334 nexenta_create_lu
($scfg, $volname);
336 nexenta_add_lun_mapping_entry
($scfg, $volname);
339 sub volume_snapshot_delete
{
340 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
342 nexenta_request
($scfg, 'destroy', 'snapshot', "$scfg->{pool}/$volname\@$snap", '');