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_list_lun_mapping_entries
{
40 my ($zvol, $scfg) = @_;
42 my $json = '{"method": "list_lun_mapping_entries","object" : "scsidisk","params": ["'.$scfg->{pool
}.'/'.$zvol.'"]}';
43 my $map = nexenta_request
($scfg,$json);
48 sub nexenta_add_lun_mapping_entry
{
49 my ($zvol, $scfg) = @_;
51 my $json = '{"method": "add_lun_mapping_entry","object" : "scsidisk","params": ["'.$scfg->{pool
}.'/'.$zvol.'",{"target_group": "All"}]}';
53 nexenta_request
($scfg, $json);
57 sub nexenta_delete_lu
{
58 my ($zvol, $scfg) = @_;
60 my $json = '{"method": "delete_lu","object" : "scsidisk","params": ["'.$scfg->{pool
}.'/'.$zvol.'"]}';
61 nexenta_request
($scfg, $json);
65 sub nexenta_create_lu
{
66 my ($zvol, $scfg) = @_;
68 my $json = '{"method": "create_lu","object" : "scsidisk","params": ["'.$scfg->{pool
}.'/'.$zvol.'",{}]}';
70 nexenta_request
($scfg, $json);
74 sub nexenta_create_zvol
{
75 my ($zvol, $size, $scfg) = @_;
77 my $blocksize = $scfg->{blocksize
};
78 my $nexentapool = $scfg->{pool
};
80 my $json = '{"method": "create","object" : "zvol","params": ["'.$nexentapool.'/'.$zvol.'", "'.$size.'KB", "'.$blocksize.'", "1"]}';
82 nexenta_request
($scfg, $json);
86 sub nexenta_delete_zvol
{
87 my ($zvol, $scfg) = @_;
90 my $json = '{"method": "destroy","object" : "zvol","params": ["'.$scfg->{pool
}.'/'.$zvol.'", ""]}';
91 nexenta_request
($scfg, $json);
95 sub nexenta_list_zvol
{
98 my $json = '{"method": "get_names","object" : "zvol","params": [""]}';
101 my $zvols = nexenta_request
($scfg, $json);
102 return undef if !$zvols;
106 foreach my $zvol (@$zvols) {
107 my @values = split('/', $zvol);
108 #$volumes->{$values[0]}->{$values[1]}->{volname} = $values[1];
110 my $pool = $values[0];
111 my $image = $values[1];
113 if ($image =~ m/^(vm-(\d+)-\S+)$/) {
117 $list->{$pool}->{$image} = {
136 content
=> [ {images
=> 1}, { images
=> 1 }],
143 description
=> "login",
147 description
=> "password",
151 description
=> "block size",
155 description
=> "ssl",
163 nodes
=> { optional
=> 1 },
164 disable
=> { optional
=> 1 },
165 target
=> { fixed
=> 1 },
166 portal
=> { fixed
=> 1 },
167 login
=> { fixed
=> 1 },
168 password
=> { fixed
=> 1 },
169 pool
=> { fixed
=> 1 },
170 blocksize
=> { fixed
=> 1 },
171 ssl
=> { optional
=> 1 },
172 content
=> { optional
=> 1 },
177 # Storage implementation
180 my ($class, $volname) = @_;
182 if ($volname =~ m/^(vm-(\d+)-\S+)$/) {
183 return ('images', $1, $2);
186 return('images',$volname,'');
187 #die "unable to parse lvm volume name '$volname'\n";
191 my ($class, $scfg, $volname) = @_;
193 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
195 my $target = $scfg->{target
};
196 my $portal = $scfg->{portal
};
198 my $map = nexenta_list_lun_mapping_entries
($name,$scfg);
199 die "could not find lun number" if !$map;
200 my $lun = @$map[0]->{lun
};
201 $lun =~ m/^(\d+)$/ or die "lun is not OK\n";
203 my $path = "iscsi://$portal/$target/$lun";
205 return ($path, $vmid, $vtype);
210 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
212 die "unsupported format '$fmt'" if $fmt ne 'raw';
214 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
215 if $name && $name !~ m/^vm-$vmid-/;
217 my $nexentapool = $scfg->{'pool'};
220 my $volumes = nexenta_list_zvol
($scfg);
221 die "unable de get zvol list" if !$volumes;
223 for (my $i = 1; $i < 100; $i++) {
225 my $tn = "vm-$vmid-disk-$i";
226 if (!defined ($volumes->{$nexentapool}->{$tn})) {
233 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
236 eval { nexenta_create_zvol
($name, $size, $scfg); };
238 eval { nexenta_create_lu
($name, $scfg); };
240 nexenta_add_lun_mapping_entry
($name, $scfg);
246 my ($class, $storeid, $scfg, $volname) = @_;
248 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
250 eval { nexenta_delete_lu
($name, $scfg); };
252 nexenta_delete_zvol
($name, $scfg);
259 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
261 $cache->{nexenta
} = nexenta_list_zvol
($scfg) if !$cache->{nexenta
};
262 my $nexentapool = $scfg->{pool
};
264 if (my $dat = $cache->{nexenta
}->{$nexentapool}) {
265 foreach my $image (keys %$dat) {
267 my $volname = $dat->{$image}->{name
};
269 my $volid = "$storeid:$volname";
272 my $owner = $dat->{$volname}->{vmid
};
274 my $found = grep { $_ eq $volid } @$vollist;
277 next if defined ($vmid) && ($owner ne $vmid);
280 my $info = $dat->{$volname};
281 $info->{volid
} = $volid;
292 my ($class, $storeid, $scfg, $cache) = @_;
298 return ($total,$free,$used,$active);
303 sub activate_storage
{
304 my ($class, $storeid, $scfg, $cache) = @_;
308 sub deactivate_storage
{
309 my ($class, $storeid, $scfg, $cache) = @_;
313 sub activate_volume
{
314 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
318 sub deactivate_volume
{
319 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
323 sub volume_size_info
{
324 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
326 my $json = '{"method": "get_child_prop","object" : "zvol","params": ["'.$scfg->{pool
}.'/'.$volname.'", "size_bytes"]}';
327 my $size = nexenta_request
($scfg, $json);
332 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
334 my $json = '{"method": "set_child_prop","object" : "zvol","params": ["'.$scfg->{pool
}.'/'.$volname.'", "volsize", "'.($size/1024).'KB"]}';
335 nexenta_request
($scfg, $json);
339 sub volume_snapshot
{
340 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
342 my $json = '{"method": "create_snapshot","object" : "zvol","params": ["'.$scfg->{pool
}.'/'.$volname.'", "'.$snap.'", ""]}';
343 nexenta_request
($scfg, $json);
347 sub volume_snapshot_rollback
{
348 my ($class, $scfg, $storeid, $volname, $snap) = @_;
350 eval { nexenta_delete_lu
($volname, $scfg); };
352 my $json = '{"method": "rollback","object" : "snapshot","params": ["'.$scfg->{pool
}.'/'.$volname.'@'.$snap.'", ""]}';
353 nexenta_request
($scfg, $json);
355 eval { nexenta_create_lu
($volname, $scfg); };
357 nexenta_add_lun_mapping_entry
($volname, $scfg);