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", {});
73 sub nexenta_create_zvol
{
74 my ($scfg, $zvol, $size) = @_;
76 nexenta_request
($scfg, 'create', 'zvol', "$scfg->{pool}/$zvol", "${size}KB",
77 $scfg->{blocksize
}, 1);
80 sub nexenta_delete_zvol
{
81 my ($scfg, $zvol) = @_;
83 nexenta_request
($scfg, 'destroy', 'zvol', "$scfg->{pool}/$zvol", '-r');
86 sub nexenta_list_zvol
{
89 my $zvols = nexenta_request
($scfg, 'get_names', 'zvol', '');
90 return undef if !$zvols;
93 foreach my $zvol (@$zvols) {
94 my @values = split('/', $zvol);
96 my $pool = $values[0];
97 my $image = $values[1];
99 if ($image =~ m/^(vm-(\d+)-\S+)$/) {
103 $list->{$pool}->{$image} = {
105 size
=> nexenta_get_zvol_size
($scfg, $zvol),
122 content
=> [ {images
=> 1}, { images
=> 1 }],
129 description
=> "login",
133 description
=> "password",
137 description
=> "block size",
141 description
=> "ssl",
149 nodes
=> { optional
=> 1 },
150 disable
=> { optional
=> 1 },
151 target
=> { fixed
=> 1 },
152 portal
=> { fixed
=> 1 },
153 login
=> { fixed
=> 1 },
154 password
=> { fixed
=> 1 },
155 pool
=> { fixed
=> 1 },
156 blocksize
=> { fixed
=> 1 },
157 ssl
=> { optional
=> 1 },
158 content
=> { optional
=> 1 },
162 # Storage implementation
165 my ($class, $volname) = @_;
167 if ($volname =~ m/^(vm-(\d+)-\S+)$/) {
168 return ('images', $1, $2);
171 die "unable to parse nexenta volume name '$volname'\n";
175 my ($class, $scfg, $volname) = @_;
177 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
179 my $target = $scfg->{target
};
180 my $portal = $scfg->{portal
};
182 my $map = nexenta_list_lun_mapping_entries
($scfg, $name);
183 die "could not find lun number" if !$map;
184 my $lun = @$map[0]->{lun
};
185 $lun =~ m/^(\d+)$/ or die "lun is not OK\n";
187 my $path = "iscsi://$portal/$target/$lun";
189 return ($path, $vmid, $vtype);
194 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
196 die "unsupported format '$fmt'" if $fmt ne 'raw';
198 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
199 if $name && $name !~ m/^vm-$vmid-/;
201 my $nexentapool = $scfg->{'pool'};
204 my $volumes = nexenta_list_zvol
($scfg);
205 die "unable de get zvol list" if !$volumes;
207 for (my $i = 1; $i < 100; $i++) {
209 my $tn = "vm-$vmid-disk-$i";
210 if (!defined ($volumes->{$nexentapool}->{$tn})) {
217 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
220 nexenta_create_zvol
($scfg, $name, $size);
221 nexenta_create_lu
($scfg, $name);
222 nexenta_add_lun_mapping_entry
($scfg, $name);
228 my ($class, $storeid, $scfg, $volname) = @_;
230 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
232 nexenta_delete_lu
($scfg, $name);
233 nexenta_delete_zvol
($scfg, $name);
239 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
241 $cache->{nexenta
} = nexenta_list_zvol
($scfg) if !$cache->{nexenta
};
242 my $nexentapool = $scfg->{pool
};
244 if (my $dat = $cache->{nexenta
}->{$nexentapool}) {
245 foreach my $image (keys %$dat) {
247 my $volname = $dat->{$image}->{name
};
249 my $volid = "$storeid:$volname";
252 my $owner = $dat->{$volname}->{vmid
};
254 my $found = grep { $_ eq $volid } @$vollist;
257 next if defined ($vmid) && ($owner ne $vmid);
260 my $info = $dat->{$volname};
261 $info->{volid
} = $volid;
272 my ($class, $storeid, $scfg, $cache) = @_;
278 return ($total,$free,$used,$active);
283 sub activate_storage
{
284 my ($class, $storeid, $scfg, $cache) = @_;
288 sub deactivate_storage
{
289 my ($class, $storeid, $scfg, $cache) = @_;
293 sub activate_volume
{
294 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
298 sub deactivate_volume
{
299 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
303 sub volume_size_info
{
304 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
306 return nexenta_get_zvol_size
($scfg, "$scfg->{pool}/$volname"),
310 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
312 nexenta_request
($scfg, 'set_child_prop', 'zvol', "$scfg->{pool}/$volname", 'volsize', ($size/1024) . 'KB');
315 sub volume_snapshot
{
316 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
318 nexenta_request
($scfg, 'create_snapshot', 'zvol', "$scfg->{pool}/$volname", $snap, '');
321 sub volume_snapshot_rollback
{
322 my ($class, $scfg, $storeid, $volname, $snap) = @_;
324 nexenta_delete_lu
($scfg, $volname);
326 nexenta_request
($scfg, 'rollback', 'snapshot', "$scfg->{pool}/$volname\@$snap", '');
328 nexenta_create_lu
($scfg, $volname);
330 nexenta_add_lun_mapping_entry
($scfg, $volname);
333 sub volume_snapshot_delete
{
334 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
336 nexenta_request
($scfg, 'destroy', 'snapshot', "$scfg->{pool}/$volname\@$snap", '');