-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimplyblockPlugin.pm
399 lines (301 loc) · 10 KB
/
SimplyblockPlugin.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package PVE::Storage::Custom::SimplyblockPlugin;
use strict;
use warnings;
use feature qw(fc);
use List::Util qw(max);
use Data::Dumper;
use JSON;
use REST::Client;
use PVE::Tools qw(run_command);
use base qw(PVE::Storage::Plugin);
# Helpers
my $MIN_LVOL_SIZE = 100 * (2 ** 20);
sub _untaint {
my ($value, $type) = @_;
my %patterns = (
num => qr/^(-?\d+)$/,
ip => qr/^((?:\d{1,3}\.){3}\d{1,3})$/,
port => qr/^(\d+)$/,
nqn => qr/^([\w\.\-\:]+)$/
);
die "Unknown validation type: $type" unless exists $patterns{$type};
if ($value =~ $patterns{$type}) {
return $1; # Return the untainted value
} else {
die "Invalid $type value: $value";
}
}
sub _request {
my ($scfg, $method, $path, $body, $expect_failure) = @_;
$expect_failure //= 0;
# TODO: Reuse client, place in $cache
my $client = REST::Client->new({ follow => 1});
$client->addHeader("Authorization", "$scfg->{cluster} $scfg->{secret}");
$client->setHost($scfg->{entrypoint});
if (defined $body) {
$client->addHeader("Content-type", "application/json");
}
$client->request($method, $path, defined $body ? encode_json($body) : "");
my $code = $client->responseCode();
my $content = (fc($client->responseHeader('Content-type')) eq fc('application/json'))
? decode_json($client->responseContent())
: ((200 <= $code or $code < 300) # Ensure we always have response content
? { status => 1, results => 1 }
: { status => 0 }
);
if (($code < 200 or 300 <= $code) or (not $content->{status})) {
my $msg = exists $content->{error} ? $content->{error} : "-";
if ($expect_failure) {
return $msg;
}
warn("Request failed: $code, $msg");
return;
}
return $content->{"results"};
}
sub _list_nvme {
my $json = '';
eval {
run_command(['nvme', 'list', '--output=json'],
outfunc => sub { $json .= shift },
);
};
return decode_json($json);
}
sub _lvol_by_name {
my ($scfg, $volname) = @_;
my $lvols = _request($scfg, "GET", "/lvol") or die("Failed to list volumes\n");
my ($lvol) = grep { $volname eq $_->{lvol_name} } @$lvols;
return ($lvol or die("Volume not found\n"));
};
sub _lvol_id_by_name {
my ($scfg, $volname) = @_;
return _lvol_by_name($scfg, $volname)->{id};
};
sub _snapshot_by_name {
my ($scfg, $snap_name) = @_;
my $snapshots = _request($scfg, "GET", "/snapshot") or die("Failed to list snapshots\n");
my ($snapshot) = grep { $snap_name eq $_->{snap_name} } @$snapshots;
return ($snapshot->{id} or die("Snapshot not found\n"));
}
sub _connect_lvol {
my ($scfg, $id) = @_;
my $connect_info = _request($scfg, "GET", "/lvol/connect/$id");
foreach (@$connect_info) {
run_command([
"nvme", "connect",
"--reconnect-delay=" . _untaint($_->{"reconnect-delay"}, "num"),
"--ctrl-loss-tmo=" . _untaint($_->{"ctrl-loss-tmo"}, "num"),
"--nr-io-queues=" . _untaint($_->{"nr-io-queues"}, "num"),
"--transport=tcp",
"--traddr=" . _untaint($_->{ip}, "ip"),
"--trsvcid=" . _untaint($_->{port}, "port"),
"--nqn=" . _untaint($_->{nqn}, "nqn"),
]);
}
}
sub _disconnect_lvol {
my ($scfg, $id) = @_;
my $info = _request($scfg, "GET", "/lvol/$id")->[0];
run_command("nvme disconnect -n " . _untaint($info->{nqn}, "nqn"));
}
sub _delete_lvol {
my ($scfg, $id) = @_;
_disconnect_lvol($scfg, $id);
_request($scfg, "DELETE", "/lvol/$id");
# Await deletion
for (my $i = 0; $i < 120; $i += 1) {
my $ret = _request($scfg, "GET", "/lvol/$id", undef, 1);
if (ref($ret) eq 'ARRAY' && exists $ret->[0]{status} && ($ret->[0]{status} eq "in_deletion")) {
sleep(1);
} elsif ($ret eq "LVol not found: $id") {
return undef; # Success
} else {
die("Failed to await LVol deletion");
}
}
die ("Timeout waiting for LVol deletion");
}
# Configuration
sub api {
return 10; # Only tested on this version so far.
}
sub type {
return 'simplyblock';
}
sub plugindata {
return {
content => [
{ images => 1, rootdir => 1 },
{ images => 1, rootdir => 1 }
],
format => [
{ raw => 1 },
'raw'
],
};
}
sub properties {
return {
entrypoint => {
description => "Control plane server",
type => 'string',
},
cluster => {
description => "Cluster UUID",
type => 'string',
},
secret => {
description => "Cluster access token",
type => 'string',
},
};
}
sub options {
return {
entrypoint => { optional => 0 },
cluster => { optional => 0 },
pool => { optional => 0 },
secret => { optional => 0 },
};
}
# Storage
sub activate_storage {
my ($class, $storeid, $scfg, $cache) = @_;
_request($scfg, "GET", "/cluster/$scfg->{cluster}") or die("Cluster not responding");
my $lvols = _request($scfg, "GET", "/lvol") or die("Failed to list volumes\n");
my $devices = _list_nvme()->{Devices};
foreach (@$lvols) {
my $lvol = $_;
next if $lvol->{lvol_name} !~ m/^vm-(\d+)-/;
next if $lvol->{status} ne "online";
# Skip already connected
next if grep { $lvol->{id} eq $_->{ModelNumber} } @$devices;
_connect_lvol($scfg, $lvol->{id});
}
return 1;
}
sub deactivate_storage {
my ($class, $storeid, $scfg, $cache) = @_;
# TODO: disconnect volumes?
return 1;
}
sub status {
my ($class, $storeid, $scfg, $cache) = @_;
my $capacity = _request($scfg, "GET", "/cluster/capacity/$scfg->{cluster}")->[0]
or die("Cluster not responding");
return ($capacity->{size_total}, $capacity->{size_free}, $capacity->{size_used}, 1);
}
sub parse_volname {
my ( $class, $volname ) = @_;
if ($volname =~ m/^(vm-(\d+)-\S+)$/) {
return ('images', $1, $2, undef, undef, 0, 'raw');
}
die "unable to parse volume name '$volname'\n";
}
sub filesystem_path {
my ($class, $scfg, $volname, $snapname) = @_;
my ($vtype, $name, $vmid) = $class->parse_volname($volname);
my $id = _lvol_id_by_name($scfg, $volname);
my $devices = _list_nvme()->{Devices};
my ($device) = grep { $id eq $_->{ModelNumber} } @$devices;
my $path = $device->{DevicePath};
return wantarray ? ($path, $vmid, $vtype) : $path;
}
sub create_base {
die "create_base unimplemented";
}
sub alloc_image {
my ( $class, $storeid, $scfg, $vmid, $fmt, $name, $size_kib ) = @_;
die "unsupported format '$fmt'" if $fmt ne 'raw';
$name //= $class->find_free_diskname($storeid, $scfg, $vmid, "raw", 0);
die "illegal name '$name' - should be 'vm-$vmid-*'\n"
if $name && $name !~ m/^vm-$vmid-/;
my $id = _request($scfg, "POST", "/lvol", {
pool => $scfg->{pool},
name => $name,
size => max($size_kib * 1024, $MIN_LVOL_SIZE),
}) or die("Failed to create image");
_connect_lvol($scfg, $id);
return $name;
}
sub free_image {
my ($class, $storeid, $scfg, $volname, $isBase) = @_;
my $lvol = _lvol_by_name($scfg, $volname);
_delete_lvol($scfg, $lvol->{id});
# Delete associated snapshots
my $snapshots = _request($scfg, "GET", "/snapshot") or die("Failed to list snapshots\n");
foreach (@$snapshots) {
next if ($_->{lvol}{id} ne $lvol->{id}) or ($_->{id} ne $lvol->{cloned_from_snap});
_request($scfg, "DELETE", "/snapshot/$_->{id}") or die("Failed to delete snapshot\n");
}
}
sub clone_image {
die "clone_image unimplemented";
}
sub list_images {
my ($class, $storeid, $scfg) = @_;
my $lvols = _request($scfg, "GET", "/lvol") or die("Failed to list volumes\n");
my $res = [];
foreach (@$lvols) {
next if $_->{lvol_name} !~ m/^vm-(\d+)-/;
push @$res, {
volid => "$storeid:$_->{lvol_name}",
format => 'raw',
size => $_->{size},
vmid => $1,
};
}
return $res;
}
sub volume_resize {
my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
my $id = _lvol_id_by_name($scfg, $volname);
_request($scfg, "PUT", "/lvol/resize/$id", {
size => max($size, $MIN_LVOL_SIZE)
}) or die("Failed to resize image");
}
sub volume_snapshot {
my ($class, $scfg, $storeid, $volname, $snap) = @_;
my $id = _lvol_id_by_name($scfg, $volname);
_request($scfg, "POST", "/snapshot", {
snapshot_name => $snap,
lvol_id => $id
}) or die("Failed to create snapshot");
}
sub volume_snapshot_rollback {
my ($class, $scfg, $storeid, $volname, $snap) = @_;
# delete $volname
my $id = _lvol_id_by_name($scfg, $volname);
_delete_lvol($scfg, $id);
# clone $snap
my $snap_id = _snapshot_by_name($scfg, $snap);
my $new_id = _request($scfg, "POST", "/snapshot/clone", {
snapshot_id => $snap_id,
clone_name => $volname
}) or die("Failed to restore snapshot");
_connect_lvol($scfg, $new_id);
}
sub volume_snapshot_delete {
my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
my $snap_id = _snapshot_by_name($scfg, $snap);
_request($scfg, "DELETE", "/snapshot/$snap_id") or die ("Failed to delete snapshot");
}
sub rename_volume {
my ($class, $scfg, $storeid, $source_volname, $target_vmid, $target_volname) = @_;
my $id = _lvol_id_by_name($scfg, $source_volname);
$target_volname = $class->find_free_diskname($storeid, $scfg, $target_vmid, "raw")
if !$target_volname;
_request($scfg, "PUT", "/lvol/resize/$id", {
name => $target_volname
}) or die("Failed to rename image");
}
sub volume_has_feature {
my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running, $opts) = @_;
return 1 if exists({
snapshot => 1,
sparseinit => 1,
}->{$feature});
die "unchecked feature '$feature'";
}
1;