1 package PVE
::API2
::Storage
::Status
;
13 use PVE
::API2
::Storage
::Content
;
14 use PVE
::API2
::Storage
::PruneBackups
;
15 use PVE
::API2
::Storage
::FileRestore
;
17 use PVE
::RPCEnvironment
;
18 use PVE
::JSONSchema
qw(get_standard_option);
19 use PVE
::Exception
qw(raise_param_exc);
21 use base
qw(PVE::RESTHandler);
23 __PACKAGE__-
>register_method ({
24 subclass
=> "PVE::API2::Storage::PruneBackups",
25 path
=> '{storage}/prunebackups',
28 __PACKAGE__-
>register_method ({
29 subclass
=> "PVE::API2::Storage::Content",
30 # set fragment delimiter (no subdirs) - we need that, because volume
31 # IDs may contain a slash '/'
32 fragmentDelimiter
=> '',
33 path
=> '{storage}/content',
36 __PACKAGE__-
>register_method ({
37 subclass
=> "PVE::API2::Storage::FileRestore",
38 path
=> '{storage}/file-restore',
41 __PACKAGE__-
>register_method ({
45 description
=> "Get status for all datastores.",
47 description
=> "Only list entries where you have 'Datastore.Audit' or 'Datastore.AllocateSpace' permissions on '/storage/<storage>'",
53 additionalProperties
=> 0,
55 node
=> get_standard_option
('pve-node'),
56 storage
=> get_standard_option
('pve-storage-id', {
57 description
=> "Only list status for specified storage",
59 completion
=> \
&PVE
::Storage
::complete_storage_enabled
,
62 description
=> "Only list stores which support this content type.",
63 type
=> 'string', format
=> 'pve-storage-content-list',
65 completion
=> \
&PVE
::Storage
::complete_content_type
,
68 description
=> "Only list stores which are enabled (not disabled in config).",
73 target
=> get_standard_option
('pve-node', {
74 description
=> "If target is different to 'node', we only lists shared storages which " .
75 "content is accessible on this 'node' and the specified 'target' node.",
77 completion
=> \
&PVE
::Cluster
::get_nodelist
,
80 description
=> "Include information about formats",
92 storage
=> get_standard_option
('pve-storage-id'),
94 description
=> "Storage type.",
98 description
=> "Allowed storage content types.",
99 type
=> 'string', format
=> 'pve-storage-content-list',
102 description
=> "Set when storage is enabled (not disabled).",
107 description
=> "Set when storage is accessible.",
112 description
=> "Shared flag from storage configuration.",
117 description
=> "Total storage space in bytes.",
123 description
=> "Used storage space in bytes.",
129 description
=> "Available storage space in bytes.",
135 description
=> "Used fraction (used/total).",
137 renderer
=> 'fraction_as_percentage',
142 links
=> [ { rel
=> 'child', href
=> "{storage}" } ],
147 my $rpcenv = PVE
::RPCEnvironment
::get
();
148 my $authuser = $rpcenv->get_user();
150 my $localnode = PVE
::INotify
::nodename
();
152 my $target = $param->{target
};
154 undef $target if $target && ($target eq $localnode || $target eq 'localhost');
156 my $cfg = PVE
::Storage
::config
();
158 my $info = PVE
::Storage
::storage_info
($cfg, $param->{content
}, $param->{format
});
160 raise_param_exc
({ storage
=> "No such storage." })
161 if $param->{storage
} && !defined($info->{$param->{storage
}});
164 my @sids = PVE
::Storage
::storage_ids
($cfg);
165 foreach my $storeid (@sids) {
166 my $data = $info->{$storeid};
168 my $privs = [ 'Datastore.Audit', 'Datastore.AllocateSpace' ];
169 next if !$rpcenv->check_any($authuser, "/storage/$storeid", $privs, 1);
170 next if $param->{storage
} && $param->{storage
} ne $storeid;
172 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
174 next if $param->{enabled
} && $scfg->{disable
};
177 # check if storage content is accessible on local node and specified target node
178 # we use this on the Clone GUI
180 next if !$scfg->{shared
};
181 next if !PVE
::Storage
::storage_check_node
($cfg, $storeid, undef, 1);
182 next if !PVE
::Storage
::storage_check_node
($cfg, $storeid, $target, 1);
185 if ($data->{total
}) {
186 $data->{used_fraction
} = ($data->{used
} // 0) / $data->{total
};
189 $res->{$storeid} = $data;
192 return PVE
::RESTHandler
::hash_to_array
($res, 'storage');
195 __PACKAGE__-
>register_method ({
201 check
=> ['perm', '/storage/{storage}', ['Datastore.Audit', 'Datastore.AllocateSpace'], any
=> 1],
204 additionalProperties
=> 0,
206 node
=> get_standard_option
('pve-node'),
207 storage
=> get_standard_option
('pve-storage-id'),
215 subdir
=> { type
=> 'string' },
218 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
224 { subdir
=> 'content' },
225 { subdir
=> 'download-url' },
226 { subdir
=> 'file-restore' },
227 { subdir
=> 'prunebackups' },
229 { subdir
=> 'rrddata' },
230 { subdir
=> 'status' },
231 { subdir
=> 'upload' },
237 __PACKAGE__-
>register_method ({
238 name
=> 'read_status',
239 path
=> '{storage}/status',
241 description
=> "Read storage status.",
243 check
=> ['perm', '/storage/{storage}', ['Datastore.Audit', 'Datastore.AllocateSpace'], any
=> 1],
248 additionalProperties
=> 0,
250 node
=> get_standard_option
('pve-node'),
251 storage
=> get_standard_option
('pve-storage-id'),
261 my $cfg = PVE
::Storage
::config
();
263 my $info = PVE
::Storage
::storage_info
($cfg, $param->{content
});
265 my $data = $info->{$param->{storage
}};
267 raise_param_exc
({ storage
=> "No such storage." })
273 __PACKAGE__-
>register_method ({
275 path
=> '{storage}/rrd',
277 description
=> "Read storage RRD statistics (returns PNG).",
279 check
=> ['perm', '/storage/{storage}', ['Datastore.Audit', 'Datastore.AllocateSpace'], any
=> 1],
284 additionalProperties
=> 0,
286 node
=> get_standard_option
('pve-node'),
287 storage
=> get_standard_option
('pve-storage-id'),
289 description
=> "Specify the time frame you are interested in.",
291 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
294 description
=> "The list of datasources you want to display.",
295 type
=> 'string', format
=> 'pve-configid-list',
298 description
=> "The RRD consolidation function",
300 enum
=> [ 'AVERAGE', 'MAX' ],
308 filename
=> { type
=> 'string' },
314 return PVE
::RRD
::create_rrd_graph
(
315 "pve2-storage/$param->{node}/$param->{storage}",
316 $param->{timeframe
}, $param->{ds
}, $param->{cf
});
319 __PACKAGE__-
>register_method ({
321 path
=> '{storage}/rrddata',
323 description
=> "Read storage RRD statistics.",
325 check
=> ['perm', '/storage/{storage}', ['Datastore.Audit', 'Datastore.AllocateSpace'], any
=> 1],
330 additionalProperties
=> 0,
332 node
=> get_standard_option
('pve-node'),
333 storage
=> get_standard_option
('pve-storage-id'),
335 description
=> "Specify the time frame you are interested in.",
337 enum
=> [ 'hour', 'day', 'week', 'month', 'year' ],
340 description
=> "The RRD consolidation function",
342 enum
=> [ 'AVERAGE', 'MAX' ],
357 return PVE
::RRD
::create_rrd_data
(
358 "pve2-storage/$param->{node}/$param->{storage}",
359 $param->{timeframe
}, $param->{cf
});
362 # makes no sense for big images and backup files (because it
363 # create a copy of the file).
364 __PACKAGE__-
>register_method ({
366 path
=> '{storage}/upload',
368 description
=> "Upload templates and ISO images.",
370 check
=> ['perm', '/storage/{storage}', ['Datastore.AllocateTemplate']],
374 additionalProperties
=> 0,
376 node
=> get_standard_option
('pve-node'),
377 storage
=> get_standard_option
('pve-storage-id'),
379 description
=> "Content type.",
380 type
=> 'string', format
=> 'pve-storage-content',
381 enum
=> ['iso', 'vztmpl'],
384 description
=> "The name of the file to create.",
388 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.",
394 returns
=> { type
=> "string" },
398 my $rpcenv = PVE
::RPCEnvironment
::get
();
400 my $user = $rpcenv->get_user();
402 my $cfg = PVE
::Storage
::config
();
404 my $node = $param->{node
};
405 my $scfg = PVE
::Storage
::storage_check_enabled
($cfg, $param->{storage
}, $node);
407 die "can't upload to storage type '$scfg->{type}'\n"
408 if !defined($scfg->{path
});
410 my $content = $param->{content
};
412 my $tmpfilename = $param->{tmpfilename
};
413 die "missing temporary file name\n" if !$tmpfilename;
415 my $size = -s
$tmpfilename;
416 die "temporary file '$tmpfilename' does not exist\n" if !defined($size);
418 my $filename = PVE
::Storage
::normalize_content_filename
($param->{filename
});
422 if ($content eq 'iso') {
423 if ($filename !~ m![^/]+$PVE::Storage::iso_extension_re$!) {
424 raise_param_exc
({ filename
=> "wrong file extension" });
426 $path = PVE
::Storage
::get_iso_dir
($cfg, $param->{storage
});
427 } elsif ($content eq 'vztmpl') {
428 if ($filename !~ m![^/]+$PVE::Storage::vztmpl_extension_re$!) {
429 raise_param_exc
({ filename
=> "wrong file extension" });
431 $path = PVE
::Storage
::get_vztmpl_dir
($cfg, $param->{storage
});
433 raise_param_exc
({ content
=> "upload content type '$content' not allowed" });
436 die "storage '$param->{storage}' does not support '$content' content\n"
437 if !$scfg->{content
}->{$content};
439 my $dest = "$path/$filename";
440 my $dirname = dirname
($dest);
442 # best effort to match apl_download behaviour
443 chmod 0644, $tmpfilename;
445 # we simply overwrite the destination file if it already exists
448 if ($node ne 'localhost' && $node ne PVE
::INotify
::nodename
()) {
449 my $remip = PVE
::Cluster
::remote_node_ip
($node);
451 my @ssh_options = ('-o', 'BatchMode=yes');
453 my @remcmd = ('/usr/bin/ssh', @ssh_options, $remip, '--');
456 # activate remote storage
457 PVE
::Tools
::run_command
([@remcmd, '/usr/sbin/pvesm', 'status',
458 '--storage', $param->{storage
}]);
460 die "can't activate storage '$param->{storage}' on node '$node': $@\n" if $@;
462 PVE
::Tools
::run_command
([@remcmd, '/bin/mkdir', '-p', '--', PVE
::Tools
::shell_quote
($dirname)],
463 errmsg
=> "mkdir failed");
465 $cmd = ['/usr/bin/scp', @ssh_options, '-p', '--', $tmpfilename, "[$remip]:" . PVE
::Tools
::shell_quote
($dest)];
467 PVE
::Storage
::activate_storage
($cfg, $param->{storage
});
468 File
::Path
::make_path
($dirname);
469 $cmd = ['cp', '--', $tmpfilename, $dest];
475 print "starting file import from: $tmpfilename\n";
476 print "target node: $node\n";
477 print "target file: $dest\n";
478 print "file size is: $size\n";
479 print "command: " . join(' ', @$cmd) . "\n";
481 eval { PVE
::Tools
::run_command
($cmd, errmsg
=> 'import failed'); };
486 print "finished file import successfully\n";
489 my $upid = $rpcenv->fork_worker('imgcopy', undef, $user, $worker);
491 # apache removes the temporary file on return, so we need
492 # to wait here to make sure the worker process starts and
493 # opens the file before it gets removed.
499 __PACKAGE__-
>register_method({
500 name
=> 'download_url',
501 path
=> '{storage}/download-url',
503 description
=> "Download templates and ISO images by using an URL.",
507 ['perm', '/storage/{storage}', [ 'Datastore.AllocateTemplate' ]],
508 ['perm', '/', [ 'Sys.Audit', 'Sys.Modify' ]],
513 additionalProperties
=> 0,
515 node
=> get_standard_option
('pve-node'),
516 storage
=> get_standard_option
('pve-storage-id'),
518 description
=> "The URL to download the file from.",
520 pattern
=> 'https?://.*',
523 description
=> "Content type.", # TODO: could be optional & detected in most cases
524 type
=> 'string', format
=> 'pve-storage-content',
525 enum
=> ['iso', 'vztmpl'],
528 description
=> "The name of the file to create. Caution: This will be normalized!",
533 description
=> "The expected checksum of the file.",
535 requires
=> 'checksum-algorithm',
538 'checksum-algorithm' => {
539 description
=> "The algorithm to calculate the checksum of the file.",
541 enum
=> ['md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512'],
542 requires
=> 'checksum',
545 'verify-certificates' => {
546 description
=> "If false, no SSL/TLS certificates will be verified.",
559 my $rpcenv = PVE
::RPCEnvironment
::get
();
560 my $user = $rpcenv->get_user();
562 my $cfg = PVE
::Storage
::config
();
564 my ($node, $storage) = $param->@{'node', 'storage'};
565 my $scfg = PVE
::Storage
::storage_check_enabled
($cfg, $storage, $node);
567 die "can't upload to storage type '$scfg->{type}', not a file based storage!\n"
568 if !defined($scfg->{path
});
570 my ($content, $url) = $param->@{'content', 'url'};
572 die "storage '$storage' is not configured for content-type '$content'\n"
573 if !$scfg->{content
}->{$content};
575 my $filename = PVE
::Storage
::normalize_content_filename
($param->{filename
});
578 if ($content eq 'iso') {
579 if ($filename !~ m![^/]+$PVE::Storage::iso_extension_re$!) {
580 raise_param_exc
({ filename
=> "wrong file extension" });
582 $path = PVE
::Storage
::get_iso_dir
($cfg, $storage);
583 } elsif ($content eq 'vztmpl') {
584 if ($filename !~ m![^/]+$PVE::Storage::vztmpl_extension_re$!) {
585 raise_param_exc
({ filename
=> "wrong file extension" });
587 $path = PVE
::Storage
::get_vztmpl_dir
($cfg, $storage);
589 raise_param_exc
({ content
=> "upload content-type '$content' is not allowed" });
592 PVE
::Storage
::activate_storage
($cfg, $storage);
593 File
::Path
::make_path
($path);
595 my $dccfg = PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
598 verify_certificates
=> $param->{'verify-certificates'} // 1,
599 http_proxy
=> $dccfg->{http_proxy
},
602 my ($checksum, $checksum_algorithm) = $param->@{'checksum', 'checksum-algorithm'};
604 $opts->{"${checksum_algorithm}sum"} = $checksum;
605 $opts->{hash_required
} = 1;
609 PVE
::Tools
::download_file_from_url
("$path/$filename", $url, $opts);
612 my $worker_id = PVE
::Tools
::encode_text
($filename); # must not pass : or the like as w-ID
614 return $rpcenv->fork_worker('download', $worker_id, $user, $worker);