]> git.proxmox.com Git - pve-storage.git/blame - src/PVE/API2/Storage/Status.pm
upload: use SSH helper to get ssh/scp options
[pve-storage.git] / src / PVE / API2 / Storage / Status.pm
CommitLineData
b6cf0a66
DM
1package PVE::API2::Storage::Status;
2
3use strict;
4use warnings;
5
7814e05f 6use File::Basename;
0a65c237 7use File::Path;
b11d054b 8use POSIX qw(ENOENT);
0a65c237 9
83d7192f 10use PVE::Cluster;
0a65c237
TL
11use PVE::Exception qw(raise_param_exc);
12use PVE::INotify;
13use PVE::JSONSchema qw(get_standard_option);
14use PVE::RESTHandler;
15use PVE::RPCEnvironment;
0ce8cadd 16use PVE::RRD;
a84804c4 17use PVE::Tools qw(run_command);
0a65c237 18
b6cf0a66 19use PVE::API2::Storage::Content;
f1a3ce3b 20use PVE::API2::Storage::FileRestore;
0a65c237
TL
21use PVE::API2::Storage::PruneBackups;
22use PVE::Storage;
b6cf0a66
DM
23
24use base qw(PVE::RESTHandler);
25
25a95836
FE
26__PACKAGE__->register_method ({
27 subclass => "PVE::API2::Storage::PruneBackups",
28 path => '{storage}/prunebackups',
29});
30
b6cf0a66 31__PACKAGE__->register_method ({
7dd31e68 32 subclass => "PVE::API2::Storage::Content",
b6cf0a66 33 # set fragment delimiter (no subdirs) - we need that, because volume
7dd31e68
FE
34 # IDs may contain a slash '/'
35 fragmentDelimiter => '',
b6cf0a66
DM
36 path => '{storage}/content',
37});
38
f1a3ce3b
SR
39__PACKAGE__->register_method ({
40 subclass => "PVE::API2::Storage::FileRestore",
41 path => '{storage}/file-restore',
42});
43
b6cf0a66 44__PACKAGE__->register_method ({
7dd31e68 45 name => 'index',
b6cf0a66
DM
46 path => '',
47 method => 'GET',
48 description => "Get status for all datastores.",
7dd31e68 49 permissions => {
5f642f73
DM
50 description => "Only list entries where you have 'Datastore.Audit' or 'Datastore.AllocateSpace' permissions on '/storage/<storage>'",
51 user => 'all',
52 },
b6cf0a66
DM
53 protected => 1,
54 proxyto => 'node',
55 parameters => {
618bdfd9 56 additionalProperties => 0,
b6cf0a66
DM
57 properties => {
58 node => get_standard_option('pve-node'),
f3bd890d
DM
59 storage => get_standard_option('pve-storage-id', {
60 description => "Only list status for specified storage",
61 optional => 1,
62 completion => \&PVE::Storage::complete_storage_enabled,
63 }),
7dd31e68 64 content => {
b6cf0a66 65 description => "Only list stores which support this content type.",
583c2802 66 type => 'string', format => 'pve-storage-content-list',
b6cf0a66 67 optional => 1,
98437f4c 68 completion => \&PVE::Storage::complete_content_type,
b6cf0a66 69 },
283608f3
DM
70 enabled => {
71 description => "Only list stores which are enabled (not disabled in config).",
72 type => 'boolean',
12c2fe32
DM
73 optional => 1,
74 default => 0,
283608f3
DM
75 },
76 target => get_standard_option('pve-node', {
77 description => "If target is different to 'node', we only lists shared storages which " .
78 "content is accessible on this 'node' and the specified 'target' node.",
79 optional => 1,
f3bd890d 80 completion => \&PVE::Cluster::get_nodelist,
283608f3 81 }),
d347322a 82 'format' => {
856c54bd
DC
83 description => "Include information about formats",
84 type => 'boolean',
85 optional => 1,
86 default => 0,
87 },
b6cf0a66
DM
88 },
89 },
90 returns => {
91 type => 'array',
92 items => {
93 type => "object",
d347322a
DM
94 properties => {
95 storage => get_standard_option('pve-storage-id'),
96 type => {
97 description => "Storage type.",
98 type => 'string',
99 },
100 content => {
101 description => "Allowed storage content types.",
102 type => 'string', format => 'pve-storage-content-list',
103 },
104 enabled => {
105 description => "Set when storage is enabled (not disabled).",
106 type => 'boolean',
107 optional => 1,
108 },
109 active => {
110 description => "Set when storage is accessible.",
111 type => 'boolean',
112 optional => 1,
113 },
114 shared => {
115 description => "Shared flag from storage configuration.",
116 type => 'boolean',
117 optional => 1,
118 },
119 total => {
120 description => "Total storage space in bytes.",
121 type => 'integer',
122 renderer => 'bytes',
123 optional => 1,
124 },
125 used => {
126 description => "Used storage space in bytes.",
127 type => 'integer',
128 renderer => 'bytes',
129 optional => 1,
130 },
131 avail => {
132 description => "Available storage space in bytes.",
133 type => 'integer',
134 renderer => 'bytes',
135 optional => 1,
136 },
137 used_fraction => {
138 description => "Used fraction (used/total).",
139 type => 'number',
140 renderer => 'fraction_as_percentage',
141 optional => 1,
142 },
143 },
b6cf0a66
DM
144 },
145 links => [ { rel => 'child', href => "{storage}" } ],
146 },
147 code => sub {
148 my ($param) = @_;
149
5f642f73
DM
150 my $rpcenv = PVE::RPCEnvironment::get();
151 my $authuser = $rpcenv->get_user();
152
283608f3
DM
153 my $localnode = PVE::INotify::nodename();
154
155 my $target = $param->{target};
156
157 undef $target if $target && ($target eq $localnode || $target eq 'localhost');
f6c03d36 158
83d7192f 159 my $cfg = PVE::Storage::config();
b6cf0a66 160
856c54bd 161 my $info = PVE::Storage::storage_info($cfg, $param->{content}, $param->{format});
b6cf0a66 162
5f642f73
DM
163 raise_param_exc({ storage => "No such storage." })
164 if $param->{storage} && !defined($info->{$param->{storage}});
f6c03d36 165
5f642f73
DM
166 my $res = {};
167 my @sids = PVE::Storage::storage_ids($cfg);
168 foreach my $storeid (@sids) {
d347322a
DM
169 my $data = $info->{$storeid};
170 next if !$data;
5f642f73
DM
171 my $privs = [ 'Datastore.Audit', 'Datastore.AllocateSpace' ];
172 next if !$rpcenv->check_any($authuser, "/storage/$storeid", $privs, 1);
173 next if $param->{storage} && $param->{storage} ne $storeid;
283608f3
DM
174
175 my $scfg = PVE::Storage::storage_config($cfg, $storeid);
176
177 next if $param->{enabled} && $scfg->{disable};
f6c03d36 178
283608f3
DM
179 if ($target) {
180 # check if storage content is accessible on local node and specified target node
181 # we use this on the Clone GUI
182
183 next if !$scfg->{shared};
184 next if !PVE::Storage::storage_check_node($cfg, $storeid, undef, 1);
185 next if !PVE::Storage::storage_check_node($cfg, $storeid, $target, 1);
186 }
187
d347322a
DM
188 if ($data->{total}) {
189 $data->{used_fraction} = ($data->{used} // 0) / $data->{total};
190 }
191
192 $res->{$storeid} = $data;
b6cf0a66 193 }
5f642f73
DM
194
195 return PVE::RESTHandler::hash_to_array($res, 'storage');
b6cf0a66
DM
196 }});
197
198__PACKAGE__->register_method ({
199 name => 'diridx',
7dd31e68 200 path => '{storage}',
b6cf0a66
DM
201 method => 'GET',
202 description => "",
7dd31e68 203 permissions => {
5f642f73
DM
204 check => ['perm', '/storage/{storage}', ['Datastore.Audit', 'Datastore.AllocateSpace'], any => 1],
205 },
b6cf0a66 206 parameters => {
618bdfd9 207 additionalProperties => 0,
b6cf0a66
DM
208 properties => {
209 node => get_standard_option('pve-node'),
210 storage => get_standard_option('pve-storage-id'),
211 },
212 },
213 returns => {
214 type => 'array',
215 items => {
216 type => "object",
217 properties => {
218 subdir => { type => 'string' },
219 },
220 },
221 links => [ { rel => 'child', href => "{subdir}" } ],
222 },
223 code => sub {
224 my ($param) = @_;
225
226 my $res = [
b6cf0a66 227 { subdir => 'content' },
837b1942 228 { subdir => 'download-url' },
ead6be93 229 { subdir => 'file-restore' },
e72845ef 230 { subdir => 'import-metadata' },
ead6be93 231 { subdir => 'prunebackups' },
b6cf0a66
DM
232 { subdir => 'rrd' },
233 { subdir => 'rrddata' },
ead6be93
TL
234 { subdir => 'status' },
235 { subdir => 'upload' },
7dd31e68
FE
236 ];
237
b6cf0a66
DM
238 return $res;
239 }});
240
241__PACKAGE__->register_method ({
242 name => 'read_status',
7dd31e68 243 path => '{storage}/status',
b6cf0a66
DM
244 method => 'GET',
245 description => "Read storage status.",
7dd31e68 246 permissions => {
5f642f73
DM
247 check => ['perm', '/storage/{storage}', ['Datastore.Audit', 'Datastore.AllocateSpace'], any => 1],
248 },
b6cf0a66
DM
249 protected => 1,
250 proxyto => 'node',
251 parameters => {
618bdfd9 252 additionalProperties => 0,
b6cf0a66
DM
253 properties => {
254 node => get_standard_option('pve-node'),
255 storage => get_standard_option('pve-storage-id'),
256 },
257 },
258 returns => {
259 type => "object",
260 properties => {},
261 },
262 code => sub {
263 my ($param) = @_;
264
83d7192f 265 my $cfg = PVE::Storage::config();
b6cf0a66
DM
266
267 my $info = PVE::Storage::storage_info($cfg, $param->{content});
268
269 my $data = $info->{$param->{storage}};
270
271 raise_param_exc({ storage => "No such storage." })
272 if !defined($data);
7dd31e68 273
b6cf0a66
DM
274 return $data;
275 }});
276
277__PACKAGE__->register_method ({
278 name => 'rrd',
7dd31e68 279 path => '{storage}/rrd',
b6cf0a66
DM
280 method => 'GET',
281 description => "Read storage RRD statistics (returns PNG).",
7dd31e68 282 permissions => {
5f642f73
DM
283 check => ['perm', '/storage/{storage}', ['Datastore.Audit', 'Datastore.AllocateSpace'], any => 1],
284 },
b6cf0a66
DM
285 protected => 1,
286 proxyto => 'node',
287 parameters => {
618bdfd9 288 additionalProperties => 0,
b6cf0a66
DM
289 properties => {
290 node => get_standard_option('pve-node'),
291 storage => get_standard_option('pve-storage-id'),
292 timeframe => {
293 description => "Specify the time frame you are interested in.",
294 type => 'string',
295 enum => [ 'hour', 'day', 'week', 'month', 'year' ],
296 },
297 ds => {
298 description => "The list of datasources you want to display.",
618bdfd9 299 type => 'string', format => 'pve-configid-list',
b6cf0a66
DM
300 },
301 cf => {
302 description => "The RRD consolidation function",
618bdfd9 303 type => 'string',
b6cf0a66
DM
304 enum => [ 'AVERAGE', 'MAX' ],
305 optional => 1,
306 },
307 },
308 },
309 returns => {
310 type => "object",
311 properties => {
312 filename => { type => 'string' },
313 },
314 },
315 code => sub {
316 my ($param) = @_;
317
0ce8cadd 318 return PVE::RRD::create_rrd_graph(
7dd31e68 319 "pve2-storage/$param->{node}/$param->{storage}",
b6cf0a66 320 $param->{timeframe}, $param->{ds}, $param->{cf});
b6cf0a66
DM
321 }});
322
323__PACKAGE__->register_method ({
324 name => 'rrddata',
7dd31e68 325 path => '{storage}/rrddata',
b6cf0a66
DM
326 method => 'GET',
327 description => "Read storage RRD statistics.",
7dd31e68 328 permissions => {
5f642f73
DM
329 check => ['perm', '/storage/{storage}', ['Datastore.Audit', 'Datastore.AllocateSpace'], any => 1],
330 },
b6cf0a66
DM
331 protected => 1,
332 proxyto => 'node',
333 parameters => {
618bdfd9 334 additionalProperties => 0,
b6cf0a66
DM
335 properties => {
336 node => get_standard_option('pve-node'),
337 storage => get_standard_option('pve-storage-id'),
338 timeframe => {
339 description => "Specify the time frame you are interested in.",
340 type => 'string',
341 enum => [ 'hour', 'day', 'week', 'month', 'year' ],
342 },
343 cf => {
344 description => "The RRD consolidation function",
618bdfd9 345 type => 'string',
b6cf0a66
DM
346 enum => [ 'AVERAGE', 'MAX' ],
347 optional => 1,
348 },
349 },
350 },
351 returns => {
352 type => "array",
353 items => {
354 type => "object",
355 properties => {},
356 },
357 },
358 code => sub {
359 my ($param) = @_;
360
0ce8cadd 361 return PVE::RRD::create_rrd_data(
7dd31e68
FE
362 "pve2-storage/$param->{node}/$param->{storage}",
363 $param->{timeframe}, $param->{cf});
b6cf0a66 364 }});
7814e05f 365
7dd31e68 366# makes no sense for big images and backup files (because it
1f6610f3 367# create a copy of the file).
7814e05f
DM
368__PACKAGE__->register_method ({
369 name => 'upload',
7dd31e68 370 path => '{storage}/upload',
7814e05f 371 method => 'POST',
1f6610f3 372 description => "Upload templates and ISO images.",
7dd31e68 373 permissions => {
1f6610f3 374 check => ['perm', '/storage/{storage}', ['Datastore.AllocateTemplate']],
5f642f73 375 },
7814e05f
DM
376 protected => 1,
377 parameters => {
618bdfd9 378 additionalProperties => 0,
7814e05f
DM
379 properties => {
380 node => get_standard_option('pve-node'),
381 storage => get_standard_option('pve-storage-id'),
7dd31e68 382 content => {
7814e05f
DM
383 description => "Content type.",
384 type => 'string', format => 'pve-storage-content',
1e96ffc9 385 enum => ['iso', 'vztmpl'],
7814e05f 386 },
7dd31e68 387 filename => {
ca8c8658
LS
388 description => "The name of the file to create. Caution: This will be normalized!",
389 maxLength => 255,
7814e05f
DM
390 type => 'string',
391 },
0246225c
LS
392 checksum => {
393 description => "The expected checksum of the file.",
394 type => 'string',
395 requires => 'checksum-algorithm',
396 optional => 1,
397 },
398 'checksum-algorithm' => {
399 description => "The algorithm to calculate the checksum of the file.",
400 type => 'string',
401 enum => ['md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512'],
402 requires => 'checksum',
403 optional => 1,
404 },
7dd31e68 405 tmpfilename => {
5c0720d6 406 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.",
7814e05f
DM
407 type => 'string',
408 optional => 1,
4e55684c 409 pattern => '/var/tmp/pveupload-[0-9a-f]+',
7814e05f
DM
410 },
411 },
412 },
413 returns => { type => "string" },
414 code => sub {
415 my ($param) = @_;
416
417 my $rpcenv = PVE::RPCEnvironment::get();
418
419 my $user = $rpcenv->get_user();
420
83d7192f 421 my $cfg = PVE::Storage::config();
7814e05f
DM
422
423 my $node = $param->{node};
424 my $scfg = PVE::Storage::storage_check_enabled($cfg, $param->{storage}, $node);
425
d68c7bca 426 die "can't upload to storage type '$scfg->{type}'\n"
ee8c176d 427 if !defined($scfg->{path});
7814e05f
DM
428
429 my $content = $param->{content};
430
431 my $tmpfilename = $param->{tmpfilename};
432 die "missing temporary file name\n" if !$tmpfilename;
433
434 my $size = -s $tmpfilename;
481f6177 435 die "temporary file '$tmpfilename' does not exist\n" if !defined($size);
7814e05f 436
edda43ed 437 my $filename = PVE::Storage::normalize_content_filename($param->{filename});
7814e05f
DM
438
439 my $path;
440
441 if ($content eq 'iso') {
cd461a50 442 if ($filename !~ m![^/]+$PVE::Storage::ISO_EXT_RE_0$!) {
bba10cf4 443 raise_param_exc({ filename => "wrong file extension" });
7814e05f
DM
444 }
445 $path = PVE::Storage::get_iso_dir($cfg, $param->{storage});
446 } elsif ($content eq 'vztmpl') {
cd461a50 447 if ($filename !~ m![^/]+$PVE::Storage::VZTMPL_EXT_RE_1$!) {
bba10cf4 448 raise_param_exc({ filename => "wrong file extension" });
7814e05f 449 }
4ea5bca4 450 $path = PVE::Storage::get_vztmpl_dir($cfg, $param->{storage});
7814e05f 451 } else {
1f6610f3 452 raise_param_exc({ content => "upload content type '$content' not allowed" });
7814e05f
DM
453 }
454
d68c7bca 455 die "storage '$param->{storage}' does not support '$content' content\n"
7814e05f
DM
456 if !$scfg->{content}->{$content};
457
458 my $dest = "$path/$filename";
459 my $dirname = dirname($dest);
460
5c0720d6
SR
461 # best effort to match apl_download behaviour
462 chmod 0644, $tmpfilename;
463
855915b6 464 my $err_cleanup = sub { unlink $dest; die "cleanup failed: $!\n" if $! && $! != ENOENT };
7814e05f
DM
465
466 my $cmd;
467 if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
468 my $remip = PVE::Cluster::remote_node_ip($node);
469
0cb758c9 470 my $ssh_options = PVE::SSHInfo::ssh_info_to_ssh_opts({ ip => $remip, name => $node });
7814e05f 471
0cb758c9 472 my @remcmd = ('/usr/bin/ssh', $ssh_options->@*, $remip, '--');
7814e05f 473
a84804c4
TL
474 eval { # activate remote storage
475 run_command([@remcmd, '/usr/sbin/pvesm', 'status', '--storage', $param->{storage}]);
7814e05f 476 };
4af77132 477 die "can't activate storage '$param->{storage}' on node '$node': $@\n" if $@;
7814e05f 478
a84804c4
TL
479 run_command(
480 [@remcmd, '/bin/mkdir', '-p', '--', PVE::Tools::shell_quote($dirname)],
481 errmsg => "mkdir failed",
482 );
7814e05f 483
0cb758c9 484 $cmd = ['/usr/bin/scp', $ssh_options->@*, '-p', '--', $tmpfilename, "[$remip]:" . PVE::Tools::shell_quote($dest)];
b11d054b 485
855915b6 486 $err_cleanup = sub { run_command([@remcmd, 'rm', '-f', '--', $dest]) };
7814e05f
DM
487 } else {
488 PVE::Storage::activate_storage($cfg, $param->{storage});
4ea5bca4 489 File::Path::make_path($dirname);
53ec90e2 490 $cmd = ['cp', '--', $tmpfilename, $dest];
7814e05f
DM
491 }
492
b11d054b 493 # NOTE: we simply overwrite the destination file if it already exists
7dd31e68 494 my $worker = sub {
7814e05f 495 my $upid = shift;
7dd31e68 496
7814e05f 497 print "starting file import from: $tmpfilename\n";
0246225c
LS
498
499 eval {
500 my ($checksum, $checksum_algorithm) = $param->@{'checksum', 'checksum-algorithm'};
501 if ($checksum_algorithm) {
502 print "calculating checksum...";
503
504 my $checksum_got = PVE::Tools::get_file_hash($checksum_algorithm, $tmpfilename);
505
506 if (lc($checksum_got) eq lc($checksum)) {
507 print "OK, checksum verified\n";
508 } else {
509 print "\n"; # the front end expects the error to reside at the last line without any noise
510 die "checksum mismatch: got '$checksum_got' != expect '$checksum'\n";
511 }
512 }
513 };
514 if (my $err = $@) {
515 # unlinks only the temporary file from the http server
516 unlink $tmpfilename;
517 warn "unable to clean up temporory file '$tmpfilename' - $!\n"
518 if $! && $! != ENOENT;
519 die $err;
520 }
521
7814e05f
DM
522 print "target node: $node\n";
523 print "target file: $dest\n";
524 print "file size is: $size\n";
525 print "command: " . join(' ', @$cmd) . "\n";
526
a84804c4 527 eval { run_command($cmd, errmsg => 'import failed'); };
f6aeefff 528
3de1c50a
TL
529 unlink $tmpfilename; # the temporary file got only uploaded locally, no need to rm remote
530 warn "unable to clean up temporary file '$tmpfilename' - $!\n" if $! && $! != ENOENT;
f6aeefff 531
7814e05f 532 if (my $err = $@) {
b11d054b
TL
533 eval { $err_cleanup->() };
534 warn "$@" if $@;
7814e05f
DM
535 die $err;
536 }
537 print "finished file import successfully\n";
538 };
539
e4d56f09 540 return $rpcenv->fork_worker('imgcopy', undef, $user, $worker);
7814e05f 541 }});
7dd31e68 542
837b1942
LS
543__PACKAGE__->register_method({
544 name => 'download_url',
545 path => '{storage}/download-url',
546 method => 'POST',
547 description => "Download templates and ISO images by using an URL.",
548 proxyto => 'node',
549 permissions => {
17e0b01b
TL
550 description => 'Requires allocation access on the storage and as this allows one to probe'
551 .' the (local!) host network indirectly it also requires one of Sys.Modify on / (for'
552 .' backwards compatibility) or the newer Sys.AccessNetwork privilege on the node.',
837b1942
LS
553 check => [ 'and',
554 ['perm', '/storage/{storage}', [ 'Datastore.AllocateTemplate' ]],
17e0b01b
TL
555 [ 'or',
556 ['perm', '/', [ 'Sys.Audit', 'Sys.Modify' ]],
557 ['perm', '/nodes/{node}', [ 'Sys.AccessNetwork' ]],
558 ],
837b1942
LS
559 ],
560 },
561 protected => 1,
562 parameters => {
563 additionalProperties => 0,
564 properties => {
565 node => get_standard_option('pve-node'),
566 storage => get_standard_option('pve-storage-id'),
567 url => {
568 description => "The URL to download the file from.",
569 type => 'string',
570 pattern => 'https?://.*',
571 },
572 content => {
d0a3db1b 573 description => "Content type.", # TODO: could be optional & detected in most cases
837b1942
LS
574 type => 'string', format => 'pve-storage-content',
575 enum => ['iso', 'vztmpl'],
576 },
577 filename => {
578 description => "The name of the file to create. Caution: This will be normalized!",
fc015f3d 579 maxLength => 255,
837b1942
LS
580 type => 'string',
581 },
582 checksum => {
583 description => "The expected checksum of the file.",
584 type => 'string',
585 requires => 'checksum-algorithm',
586 optional => 1,
587 },
2197ff97 588 compression => {
d99d4593
WB
589 description =>
590 "Decompress the downloaded file using the specified compression algorithm.",
2197ff97
PH
591 type => 'string',
592 enum => $PVE::Storage::Plugin::KNOWN_COMPRESSION_FORMATS,
593 optional => 1,
594 },
837b1942
LS
595 'checksum-algorithm' => {
596 description => "The algorithm to calculate the checksum of the file.",
597 type => 'string',
598 enum => ['md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512'],
599 requires => 'checksum',
600 optional => 1,
601 },
602 'verify-certificates' => {
603 description => "If false, no SSL/TLS certificates will be verified.",
604 type => 'boolean',
605 optional => 1,
606 default => 1,
d0a3db1b 607 },
837b1942
LS
608 },
609 },
610 returns => {
611 type => "string"
612 },
613 code => sub {
614 my ($param) = @_;
615
616 my $rpcenv = PVE::RPCEnvironment::get();
617 my $user = $rpcenv->get_user();
618
619 my $cfg = PVE::Storage::config();
620
d99d4593 621 my ($node, $storage, $compression) = $param->@{qw(node storage compression)};
837b1942
LS
622 my $scfg = PVE::Storage::storage_check_enabled($cfg, $storage, $node);
623
624 die "can't upload to storage type '$scfg->{type}', not a file based storage!\n"
625 if !defined($scfg->{path});
626
627 my ($content, $url) = $param->@{'content', 'url'};
628
629 die "storage '$storage' is not configured for content-type '$content'\n"
630 if !$scfg->{content}->{$content};
631
632 my $filename = PVE::Storage::normalize_content_filename($param->{filename});
633
634 my $path;
635 if ($content eq 'iso') {
cd461a50 636 if ($filename !~ m![^/]+$PVE::Storage::ISO_EXT_RE_0$!) {
837b1942
LS
637 raise_param_exc({ filename => "wrong file extension" });
638 }
639 $path = PVE::Storage::get_iso_dir($cfg, $storage);
640 } elsif ($content eq 'vztmpl') {
cd461a50 641 if ($filename !~ m![^/]+$PVE::Storage::VZTMPL_EXT_RE_1$!) {
837b1942
LS
642 raise_param_exc({ filename => "wrong file extension" });
643 }
644 $path = PVE::Storage::get_vztmpl_dir($cfg, $storage);
645 } else {
646 raise_param_exc({ content => "upload content-type '$content' is not allowed" });
647 }
648
649 PVE::Storage::activate_storage($cfg, $storage);
650 File::Path::make_path($path);
651
652 my $dccfg = PVE::Cluster::cfs_read_file('datacenter.cfg');
653 my $opts = {
654 hash_required => 0,
655 verify_certificates => $param->{'verify-certificates'} // 1,
656 http_proxy => $dccfg->{http_proxy},
657 };
658
659 my ($checksum, $checksum_algorithm) = $param->@{'checksum', 'checksum-algorithm'};
660 if ($checksum) {
661 $opts->{"${checksum_algorithm}sum"} = $checksum;
662 $opts->{hash_required} = 1;
663 }
664
665 my $worker = sub {
2197ff97
PH
666 if ($compression) {
667 die "decompression not supported for $content\n" if $content ne 'iso';
668 my $info = PVE::Storage::decompressor_info('iso', $compression);
d99d4593 669 die "no decompression method found\n" if !$info->{decompressor};
2197ff97
PH
670 $opts->{decompression_command} = $info->{decompressor};
671 }
837b1942
LS
672 PVE::Tools::download_file_from_url("$path/$filename", $url, $opts);
673 };
674
fc015f3d
TL
675 my $worker_id = PVE::Tools::encode_text($filename); # must not pass : or the like as w-ID
676
677 return $rpcenv->fork_worker('download', $worker_id, $user, $worker);
837b1942
LS
678 }});
679
e72845ef
WB
680__PACKAGE__->register_method({
681 name => 'get_import_metadata',
682 path => '{storage}/import-metadata',
683 method => 'GET',
684 description =>
685 "Get the base parameters for creating a guest which imports data from a foreign importable"
686 ." guest, like an ESXi VM",
687 proxyto => 'node',
688 permissions => {
689 description => "You need read access for the volume.",
690 user => 'all',
691 },
692 protected => 1,
693 parameters => {
694 additionalProperties => 0,
695 properties => {
696 node => get_standard_option('pve-node'),
697 storage => get_standard_option('pve-storage-id'),
698 volume => {
b6289642 699 description => "Volume identifier for the guest archive/entry.",
e72845ef
WB
700 type => 'string',
701 },
e72845ef
WB
702 },
703 },
704 returns => {
705 type => "object",
706 description => 'Information about how to import a guest.',
707 additionalProperties => 0,
708 properties => {
709 type => {
710 type => 'string',
711 enum => [ 'vm' ],
712 description => 'The type of guest this is going to produce.',
713 },
b6289642
TL
714 source => {
715 type => 'string',
716 enum => [ 'esxi' ],
717 description => 'The type of the import-source of this guest volume.',
718 },
e72845ef
WB
719 'create-args' => {
720 type => 'object',
721 additionalProperties => 1,
722 description => 'Parameters which can be used in a call to create a VM or container.',
723 },
b6289642
TL
724 'disks' => {
725 type => 'object',
726 additionalProperties => 1,
727 optional => 1,
728 description => 'Recognised disk volumes as `$bus$id` => `$storeid:$path` map.',
729 },
730 'net' => {
731 type => 'object',
732 additionalProperties => 1,
733 optional => 1,
734 description => 'Recognised network interfaces as `net$id` => { ...params } object.',
735 },
b6289642
TL
736 'warnings' => {
737 type => 'array',
738 description => 'List of known issues that can affect the import of a guest.'
739 .' Note that lack of warning does not imply that there cannot be any problems.',
740 optional => 1,
741 items => {
742 type => "object",
743 additionalProperties => 1,
744 properties => {
1a94dceb
TL
745 'type' => {
746 description => 'What this warning is about.',
747 enum => [
748 'cdrom-image-ignored',
0f940f10 749 'efi-state-lost',
11281005 750 'guest-is-running',
1a94dceb
TL
751 'nvme-unsupported',
752 'ovmf-with-lsi-unsupported',
753 'serial-port-socket-only',
754 ],
755 type => 'string',
756 },
757 'key' => {
758 description => 'Related subject (config) key of warning.',
759 optional => 1,
760 type => 'string',
761 },
762 'value' => {
763 description => 'Related subject (config) value of warning.',
764 optional => 1,
b6289642
TL
765 type => 'string',
766 },
767 },
768 },
769 },
e72845ef
WB
770 },
771 },
772 code => sub {
773 my ($param) = @_;
774
775 my $rpcenv = PVE::RPCEnvironment::get();
776 my $authuser = $rpcenv->get_user();
777
c92d8100 778 my ($storeid, $volume) = $param->@{qw(storage volume)};
e72845ef
WB
779 my $volid = "$storeid:$volume";
780
781 my $cfg = PVE::Storage::config();
782
783 PVE::Storage::check_volume_access($rpcenv, $authuser, $cfg, undef, $volid);
784
763a2292 785 return PVE::Tools::run_with_timeout(30, sub {
4f50a578 786 return PVE::Storage::get_import_metadata($cfg, $volid);
e72845ef 787 });
e72845ef
WB
788 }});
789
b6cf0a66 7901;