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