]> git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Storage/Status.pm
lvmthin: import: improve readability
[pve-storage.git] / PVE / API2 / Storage / Status.pm
1 package PVE::API2::Storage::Status;
2
3 use strict;
4 use warnings;
5
6 use File::Path;
7 use File::Basename;
8 use PVE::Tools;
9 use PVE::INotify;
10 use PVE::Cluster;
11 use PVE::Storage;
12 use PVE::API2::Storage::Content;
13 use PVE::RESTHandler;
14 use PVE::RPCEnvironment;
15 use PVE::JSONSchema qw(get_standard_option);
16 use PVE::Exception qw(raise_param_exc);
17
18 use base qw(PVE::RESTHandler);
19
20 __PACKAGE__->register_method ({
21 subclass => "PVE::API2::Storage::Content",
22 # set fragment delimiter (no subdirs) - we need that, because volume
23 # IDs may contain a slash '/'
24 fragmentDelimiter => '',
25 path => '{storage}/content',
26 });
27
28 __PACKAGE__->register_method ({
29 name => 'index',
30 path => '',
31 method => 'GET',
32 description => "Get status for all datastores.",
33 permissions => {
34 description => "Only list entries where you have 'Datastore.Audit' or 'Datastore.AllocateSpace' permissions on '/storage/<storage>'",
35 user => 'all',
36 },
37 protected => 1,
38 proxyto => 'node',
39 parameters => {
40 additionalProperties => 0,
41 properties => {
42 node => get_standard_option('pve-node'),
43 storage => get_standard_option('pve-storage-id', {
44 description => "Only list status for specified storage",
45 optional => 1,
46 completion => \&PVE::Storage::complete_storage_enabled,
47 }),
48 content => {
49 description => "Only list stores which support this content type.",
50 type => 'string', format => 'pve-storage-content-list',
51 optional => 1,
52 completion => \&PVE::Storage::complete_content_type,
53 },
54 enabled => {
55 description => "Only list stores which are enabled (not disabled in config).",
56 type => 'boolean',
57 optional => 1,
58 default => 0,
59 },
60 target => get_standard_option('pve-node', {
61 description => "If target is different to 'node', we only lists shared storages which " .
62 "content is accessible on this 'node' and the specified 'target' node.",
63 optional => 1,
64 completion => \&PVE::Cluster::get_nodelist,
65 }),
66 'format' => {
67 description => "Include information about formats",
68 type => 'boolean',
69 optional => 1,
70 default => 0,
71 },
72 },
73 },
74 returns => {
75 type => 'array',
76 items => {
77 type => "object",
78 properties => {
79 storage => get_standard_option('pve-storage-id'),
80 type => {
81 description => "Storage type.",
82 type => 'string',
83 },
84 content => {
85 description => "Allowed storage content types.",
86 type => 'string', format => 'pve-storage-content-list',
87 },
88 enabled => {
89 description => "Set when storage is enabled (not disabled).",
90 type => 'boolean',
91 optional => 1,
92 },
93 active => {
94 description => "Set when storage is accessible.",
95 type => 'boolean',
96 optional => 1,
97 },
98 shared => {
99 description => "Shared flag from storage configuration.",
100 type => 'boolean',
101 optional => 1,
102 },
103 total => {
104 description => "Total storage space in bytes.",
105 type => 'integer',
106 renderer => 'bytes',
107 optional => 1,
108 },
109 used => {
110 description => "Used storage space in bytes.",
111 type => 'integer',
112 renderer => 'bytes',
113 optional => 1,
114 },
115 avail => {
116 description => "Available storage space in bytes.",
117 type => 'integer',
118 renderer => 'bytes',
119 optional => 1,
120 },
121 used_fraction => {
122 description => "Used fraction (used/total).",
123 type => 'number',
124 renderer => 'fraction_as_percentage',
125 optional => 1,
126 },
127 },
128 },
129 links => [ { rel => 'child', href => "{storage}" } ],
130 },
131 code => sub {
132 my ($param) = @_;
133
134 my $rpcenv = PVE::RPCEnvironment::get();
135 my $authuser = $rpcenv->get_user();
136
137 my $localnode = PVE::INotify::nodename();
138
139 my $target = $param->{target};
140
141 undef $target if $target && ($target eq $localnode || $target eq 'localhost');
142
143 my $cfg = PVE::Storage::config();
144
145 my $info = PVE::Storage::storage_info($cfg, $param->{content}, $param->{format});
146
147 raise_param_exc({ storage => "No such storage." })
148 if $param->{storage} && !defined($info->{$param->{storage}});
149
150 my $res = {};
151 my @sids = PVE::Storage::storage_ids($cfg);
152 foreach my $storeid (@sids) {
153 my $data = $info->{$storeid};
154 next if !$data;
155 my $privs = [ 'Datastore.Audit', 'Datastore.AllocateSpace' ];
156 next if !$rpcenv->check_any($authuser, "/storage/$storeid", $privs, 1);
157 next if $param->{storage} && $param->{storage} ne $storeid;
158
159 my $scfg = PVE::Storage::storage_config($cfg, $storeid);
160
161 next if $param->{enabled} && $scfg->{disable};
162
163 if ($target) {
164 # check if storage content is accessible on local node and specified target node
165 # we use this on the Clone GUI
166
167 next if !$scfg->{shared};
168 next if !PVE::Storage::storage_check_node($cfg, $storeid, undef, 1);
169 next if !PVE::Storage::storage_check_node($cfg, $storeid, $target, 1);
170 }
171
172 if ($data->{total}) {
173 $data->{used_fraction} = ($data->{used} // 0) / $data->{total};
174 }
175
176 $res->{$storeid} = $data;
177 }
178
179 return PVE::RESTHandler::hash_to_array($res, 'storage');
180 }});
181
182 __PACKAGE__->register_method ({
183 name => 'diridx',
184 path => '{storage}',
185 method => 'GET',
186 description => "",
187 permissions => {
188 check => ['perm', '/storage/{storage}', ['Datastore.Audit', 'Datastore.AllocateSpace'], any => 1],
189 },
190 parameters => {
191 additionalProperties => 0,
192 properties => {
193 node => get_standard_option('pve-node'),
194 storage => get_standard_option('pve-storage-id'),
195 },
196 },
197 returns => {
198 type => 'array',
199 items => {
200 type => "object",
201 properties => {
202 subdir => { type => 'string' },
203 },
204 },
205 links => [ { rel => 'child', href => "{subdir}" } ],
206 },
207 code => sub {
208 my ($param) = @_;
209
210 my $res = [
211 { subdir => 'status' },
212 { subdir => 'content' },
213 { subdir => 'upload' },
214 { subdir => 'rrd' },
215 { subdir => 'rrddata' },
216 ];
217
218 return $res;
219 }});
220
221 __PACKAGE__->register_method ({
222 name => 'read_status',
223 path => '{storage}/status',
224 method => 'GET',
225 description => "Read storage status.",
226 permissions => {
227 check => ['perm', '/storage/{storage}', ['Datastore.Audit', 'Datastore.AllocateSpace'], any => 1],
228 },
229 protected => 1,
230 proxyto => 'node',
231 parameters => {
232 additionalProperties => 0,
233 properties => {
234 node => get_standard_option('pve-node'),
235 storage => get_standard_option('pve-storage-id'),
236 },
237 },
238 returns => {
239 type => "object",
240 properties => {},
241 },
242 code => sub {
243 my ($param) = @_;
244
245 my $cfg = PVE::Storage::config();
246
247 my $info = PVE::Storage::storage_info($cfg, $param->{content});
248
249 my $data = $info->{$param->{storage}};
250
251 raise_param_exc({ storage => "No such storage." })
252 if !defined($data);
253
254 return $data;
255 }});
256
257 __PACKAGE__->register_method ({
258 name => 'rrd',
259 path => '{storage}/rrd',
260 method => 'GET',
261 description => "Read storage RRD statistics (returns PNG).",
262 permissions => {
263 check => ['perm', '/storage/{storage}', ['Datastore.Audit', 'Datastore.AllocateSpace'], any => 1],
264 },
265 protected => 1,
266 proxyto => 'node',
267 parameters => {
268 additionalProperties => 0,
269 properties => {
270 node => get_standard_option('pve-node'),
271 storage => get_standard_option('pve-storage-id'),
272 timeframe => {
273 description => "Specify the time frame you are interested in.",
274 type => 'string',
275 enum => [ 'hour', 'day', 'week', 'month', 'year' ],
276 },
277 ds => {
278 description => "The list of datasources you want to display.",
279 type => 'string', format => 'pve-configid-list',
280 },
281 cf => {
282 description => "The RRD consolidation function",
283 type => 'string',
284 enum => [ 'AVERAGE', 'MAX' ],
285 optional => 1,
286 },
287 },
288 },
289 returns => {
290 type => "object",
291 properties => {
292 filename => { type => 'string' },
293 },
294 },
295 code => sub {
296 my ($param) = @_;
297
298 return PVE::Cluster::create_rrd_graph(
299 "pve2-storage/$param->{node}/$param->{storage}",
300 $param->{timeframe}, $param->{ds}, $param->{cf});
301
302 }});
303
304 __PACKAGE__->register_method ({
305 name => 'rrddata',
306 path => '{storage}/rrddata',
307 method => 'GET',
308 description => "Read storage RRD statistics.",
309 permissions => {
310 check => ['perm', '/storage/{storage}', ['Datastore.Audit', 'Datastore.AllocateSpace'], any => 1],
311 },
312 protected => 1,
313 proxyto => 'node',
314 parameters => {
315 additionalProperties => 0,
316 properties => {
317 node => get_standard_option('pve-node'),
318 storage => get_standard_option('pve-storage-id'),
319 timeframe => {
320 description => "Specify the time frame you are interested in.",
321 type => 'string',
322 enum => [ 'hour', 'day', 'week', 'month', 'year' ],
323 },
324 cf => {
325 description => "The RRD consolidation function",
326 type => 'string',
327 enum => [ 'AVERAGE', 'MAX' ],
328 optional => 1,
329 },
330 },
331 },
332 returns => {
333 type => "array",
334 items => {
335 type => "object",
336 properties => {},
337 },
338 },
339 code => sub {
340 my ($param) = @_;
341
342 return PVE::Cluster::create_rrd_data(
343 "pve2-storage/$param->{node}/$param->{storage}",
344 $param->{timeframe}, $param->{cf});
345 }});
346
347 # makes no sense for big images and backup files (because it
348 # create a copy of the file).
349 __PACKAGE__->register_method ({
350 name => 'upload',
351 path => '{storage}/upload',
352 method => 'POST',
353 description => "Upload templates and ISO images.",
354 permissions => {
355 check => ['perm', '/storage/{storage}', ['Datastore.AllocateTemplate']],
356 },
357 protected => 1,
358 parameters => {
359 additionalProperties => 0,
360 properties => {
361 node => get_standard_option('pve-node'),
362 storage => get_standard_option('pve-storage-id'),
363 content => {
364 description => "Content type.",
365 type => 'string', format => 'pve-storage-content',
366 },
367 filename => {
368 description => "The name of the file to create.",
369 type => 'string',
370 },
371 tmpfilename => {
372 description => "The source file name. This parameter is usually set by the REST handler. You can only overwrite it when connecting to the trustet port on localhost.",
373 type => 'string',
374 optional => 1,
375 },
376 },
377 },
378 returns => { type => "string" },
379 code => sub {
380 my ($param) = @_;
381
382 my $rpcenv = PVE::RPCEnvironment::get();
383
384 my $user = $rpcenv->get_user();
385
386 my $cfg = PVE::Storage::config();
387
388 my $node = $param->{node};
389 my $scfg = PVE::Storage::storage_check_enabled($cfg, $param->{storage}, $node);
390
391 die "can't upload to storage type '$scfg->{type}'\n"
392 if !defined($scfg->{path});
393
394 my $content = $param->{content};
395
396 my $tmpfilename = $param->{tmpfilename};
397 die "missing temporary file name\n" if !$tmpfilename;
398
399 my $size = -s $tmpfilename;
400 die "temporary file '$tmpfilename' does not exists\n" if !defined($size);
401
402 my $filename = $param->{filename};
403
404 chomp $filename;
405 $filename =~ s/^.*[\/\\]//;
406 $filename =~ s/[^-a-zA-Z0-9_.]/_/g;
407
408 my $path;
409
410 if ($content eq 'iso') {
411 if ($filename !~ m![^/]+\.[Ii][Ss][Oo]$!) {
412 raise_param_exc({ filename => "missing '.iso' extension" });
413 }
414 $path = PVE::Storage::get_iso_dir($cfg, $param->{storage});
415 } elsif ($content eq 'vztmpl') {
416 if ($filename !~ m![^/]+\.tar\.[gx]z$!) {
417 raise_param_exc({ filename => "missing '.tar.gz' or '.tar.xz' extension" });
418 }
419 $path = PVE::Storage::get_vztmpl_dir($cfg, $param->{storage});
420 } else {
421 raise_param_exc({ content => "upload content type '$content' not allowed" });
422 }
423
424 die "storage '$param->{storage}' does not support '$content' content\n"
425 if !$scfg->{content}->{$content};
426
427 my $dest = "$path/$filename";
428 my $dirname = dirname($dest);
429
430 # we simply overwrite when destination when file already exists
431
432 my $cmd;
433 if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
434 my $remip = PVE::Cluster::remote_node_ip($node);
435
436 my @ssh_options = ('-o', 'BatchMode=yes');
437
438 my @remcmd = ('/usr/bin/ssh', @ssh_options, $remip, '--');
439
440 eval {
441 # activate remote storage
442 PVE::Tools::run_command([@remcmd, '/usr/sbin/pvesm', 'status',
443 '--storage', $param->{storage}]);
444 };
445 die "can't activate storage '$param->{storage}' on node '$node': $@\n" if $@;
446
447 PVE::Tools::run_command([@remcmd, '/bin/mkdir', '-p', '--', PVE::Tools::shell_quote($dirname)],
448 errmsg => "mkdir failed");
449
450 $cmd = ['/usr/bin/scp', @ssh_options, '--', $tmpfilename, "[$remip]:" . PVE::Tools::shell_quote($dest)];
451 } else {
452 PVE::Storage::activate_storage($cfg, $param->{storage});
453 File::Path::make_path($dirname);
454 $cmd = ['cp', '--', $tmpfilename, $dest];
455 }
456
457 my $worker = sub {
458 my $upid = shift;
459
460 print "starting file import from: $tmpfilename\n";
461 print "target node: $node\n";
462 print "target file: $dest\n";
463 print "file size is: $size\n";
464 print "command: " . join(' ', @$cmd) . "\n";
465
466 eval { PVE::Tools::run_command($cmd, errmsg => 'import failed'); };
467 if (my $err = $@) {
468 unlink $dest;
469 die $err;
470 }
471 print "finished file import successfully\n";
472 };
473
474 my $upid = $rpcenv->fork_worker('imgcopy', undef, $user, $worker);
475
476 # apache removes the temporary file on return, so we need
477 # to wait here to make sure the worker process starts and
478 # opens the file before it gets removed.
479 sleep(1);
480
481 return $upid;
482 }});
483
484 1;