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',
383 description
=> "The name of the file to create.",
387 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.",
393 returns
=> { type
=> "string" },
397 my $rpcenv = PVE
::RPCEnvironment
::get
();
399 my $user = $rpcenv->get_user();
401 my $cfg = PVE
::Storage
::config
();
403 my $node = $param->{node
};
404 my $scfg = PVE
::Storage
::storage_check_enabled
($cfg, $param->{storage
}, $node);
406 die "can't upload to storage type '$scfg->{type}'\n"
407 if !defined($scfg->{path
});
409 my $content = $param->{content
};
411 my $tmpfilename = $param->{tmpfilename
};
412 die "missing temporary file name\n" if !$tmpfilename;
414 my $size = -s
$tmpfilename;
415 die "temporary file '$tmpfilename' does not exist\n" if !defined($size);
417 my $filename = PVE
::Storage
::normalize_content_filename
($param->{filename
});
421 if ($content eq 'iso') {
422 if ($filename !~ m![^/]+$PVE::Storage::iso_extension_re$!) {
423 raise_param_exc
({ filename
=> "wrong file extension" });
425 $path = PVE
::Storage
::get_iso_dir
($cfg, $param->{storage
});
426 } elsif ($content eq 'vztmpl') {
427 if ($filename !~ m![^/]+$PVE::Storage::vztmpl_extension_re$!) {
428 raise_param_exc
({ filename
=> "wrong file extension" });
430 $path = PVE
::Storage
::get_vztmpl_dir
($cfg, $param->{storage
});
432 raise_param_exc
({ content
=> "upload content type '$content' not allowed" });
435 die "storage '$param->{storage}' does not support '$content' content\n"
436 if !$scfg->{content
}->{$content};
438 my $dest = "$path/$filename";
439 my $dirname = dirname
($dest);
441 # best effort to match apl_download behaviour
442 chmod 0644, $tmpfilename;
444 # we simply overwrite the destination file if it already exists
447 if ($node ne 'localhost' && $node ne PVE
::INotify
::nodename
()) {
448 my $remip = PVE
::Cluster
::remote_node_ip
($node);
450 my @ssh_options = ('-o', 'BatchMode=yes');
452 my @remcmd = ('/usr/bin/ssh', @ssh_options, $remip, '--');
455 # activate remote storage
456 PVE
::Tools
::run_command
([@remcmd, '/usr/sbin/pvesm', 'status',
457 '--storage', $param->{storage
}]);
459 die "can't activate storage '$param->{storage}' on node '$node': $@\n" if $@;
461 PVE
::Tools
::run_command
([@remcmd, '/bin/mkdir', '-p', '--', PVE
::Tools
::shell_quote
($dirname)],
462 errmsg
=> "mkdir failed");
464 $cmd = ['/usr/bin/scp', @ssh_options, '-p', '--', $tmpfilename, "[$remip]:" . PVE
::Tools
::shell_quote
($dest)];
466 PVE
::Storage
::activate_storage
($cfg, $param->{storage
});
467 File
::Path
::make_path
($dirname);
468 $cmd = ['cp', '--', $tmpfilename, $dest];
474 print "starting file import from: $tmpfilename\n";
475 print "target node: $node\n";
476 print "target file: $dest\n";
477 print "file size is: $size\n";
478 print "command: " . join(' ', @$cmd) . "\n";
480 eval { PVE
::Tools
::run_command
($cmd, errmsg
=> 'import failed'); };
485 print "finished file import successfully\n";
488 my $upid = $rpcenv->fork_worker('imgcopy', undef, $user, $worker);
490 # apache removes the temporary file on return, so we need
491 # to wait here to make sure the worker process starts and
492 # opens the file before it gets removed.
498 __PACKAGE__-
>register_method({
499 name
=> 'download_url',
500 path
=> '{storage}/download-url',
502 description
=> "Download templates and ISO images by using an URL.",
506 ['perm', '/storage/{storage}', [ 'Datastore.AllocateTemplate' ]],
507 ['perm', '/', [ 'Sys.Audit', 'Sys.Modify' ]],
512 additionalProperties
=> 0,
514 node
=> get_standard_option
('pve-node'),
515 storage
=> get_standard_option
('pve-storage-id'),
517 description
=> "The URL to download the file from.",
519 pattern
=> 'https?://.*',
522 description
=> "Content type.", # TODO: could be optional & detected in most cases
523 type
=> 'string', format
=> 'pve-storage-content',
524 enum
=> ['iso', 'vztmpl'],
527 description
=> "The name of the file to create. Caution: This will be normalized!",
531 description
=> "The expected checksum of the file.",
533 requires
=> 'checksum-algorithm',
536 'checksum-algorithm' => {
537 description
=> "The algorithm to calculate the checksum of the file.",
539 enum
=> ['md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512'],
540 requires
=> 'checksum',
543 'verify-certificates' => {
544 description
=> "If false, no SSL/TLS certificates will be verified.",
557 my $rpcenv = PVE
::RPCEnvironment
::get
();
558 my $user = $rpcenv->get_user();
560 my $cfg = PVE
::Storage
::config
();
562 my ($node, $storage) = $param->@{'node', 'storage'};
563 my $scfg = PVE
::Storage
::storage_check_enabled
($cfg, $storage, $node);
565 die "can't upload to storage type '$scfg->{type}', not a file based storage!\n"
566 if !defined($scfg->{path
});
568 my ($content, $url) = $param->@{'content', 'url'};
570 die "storage '$storage' is not configured for content-type '$content'\n"
571 if !$scfg->{content
}->{$content};
573 my $filename = PVE
::Storage
::normalize_content_filename
($param->{filename
});
576 if ($content eq 'iso') {
577 if ($filename !~ m![^/]+$PVE::Storage::iso_extension_re$!) {
578 raise_param_exc
({ filename
=> "wrong file extension" });
580 $path = PVE
::Storage
::get_iso_dir
($cfg, $storage);
581 } elsif ($content eq 'vztmpl') {
582 if ($filename !~ m![^/]+$PVE::Storage::vztmpl_extension_re$!) {
583 raise_param_exc
({ filename
=> "wrong file extension" });
585 $path = PVE
::Storage
::get_vztmpl_dir
($cfg, $storage);
587 raise_param_exc
({ content
=> "upload content-type '$content' is not allowed" });
590 PVE
::Storage
::activate_storage
($cfg, $storage);
591 File
::Path
::make_path
($path);
593 my $dccfg = PVE
::Cluster
::cfs_read_file
('datacenter.cfg');
596 verify_certificates
=> $param->{'verify-certificates'} // 1,
597 http_proxy
=> $dccfg->{http_proxy
},
600 my ($checksum, $checksum_algorithm) = $param->@{'checksum', 'checksum-algorithm'};
602 $opts->{"${checksum_algorithm}sum"} = $checksum;
603 $opts->{hash_required
} = 1;
607 PVE
::Tools
::download_file_from_url
("$path/$filename", $url, $opts);
610 return $rpcenv->fork_worker('download', $filename, $user, $worker);