]>
git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Storage/Status.pm
1 package PVE
:: API2
:: Storage
:: Status
;
13 use PVE
:: API2
:: Storage
:: Content
;
14 use PVE
:: API2
:: Storage
:: PruneBackups
;
16 use PVE
:: RPCEnvironment
;
17 use PVE
:: JSONSchema
qw(get_standard_option) ;
18 use PVE
:: Exception
qw(raise_param_exc) ;
20 use base
qw(PVE::RESTHandler) ;
22 __PACKAGE__-
> register_method ({
23 subclass
=> "PVE::API2::Storage::PruneBackups" ,
24 path
=> '{storage}/prunebackups' ,
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' ,
35 __PACKAGE__-
> register_method ({
39 description
=> "Get status for all datastores." ,
41 description
=> "Only list entries where you have 'Datastore.Audit' or 'Datastore.AllocateSpace' permissions on '/storage/<storage>'" ,
47 additionalProperties
=> 0 ,
49 node
=> get_standard_option
( 'pve-node' ),
50 storage
=> get_standard_option
( 'pve-storage-id' , {
51 description
=> "Only list status for specified storage" ,
53 completion
=> \
& PVE
:: Storage
:: complete_storage_enabled
,
56 description
=> "Only list stores which support this content type." ,
57 type
=> 'string' , format
=> 'pve-storage-content-list' ,
59 completion
=> \
& PVE
:: Storage
:: complete_content_type
,
62 description
=> "Only list stores which are enabled (not disabled in config)." ,
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." ,
71 completion
=> \
& PVE
:: Cluster
:: get_nodelist
,
74 description
=> "Include information about formats" ,
86 storage
=> get_standard_option
( 'pve-storage-id' ),
88 description
=> "Storage type." ,
92 description
=> "Allowed storage content types." ,
93 type
=> 'string' , format
=> 'pve-storage-content-list' ,
96 description
=> "Set when storage is enabled (not disabled)." ,
101 description
=> "Set when storage is accessible." ,
106 description
=> "Shared flag from storage configuration." ,
111 description
=> "Total storage space in bytes." ,
117 description
=> "Used storage space in bytes." ,
123 description
=> "Available storage space in bytes." ,
129 description
=> "Used fraction (used/total)." ,
131 renderer
=> 'fraction_as_percentage' ,
136 links
=> [ { rel
=> 'child' , href
=> "{storage}" } ],
141 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
142 my $authuser = $rpcenv -> get_user ();
144 my $localnode = PVE
:: INotify
:: nodename
();
146 my $target = $param ->{ target
};
148 undef $target if $target && ( $target eq $localnode || $target eq 'localhost' );
150 my $cfg = PVE
:: Storage
:: config
();
152 my $info = PVE
:: Storage
:: storage_info
( $cfg, $param ->{ content
}, $param ->{ format
});
154 raise_param_exc
({ storage
=> "No such storage." })
155 if $param ->{ storage
} && ! defined ( $info ->{ $param ->{ storage
}});
158 my @sids = PVE
:: Storage
:: storage_ids
( $cfg );
159 foreach my $storeid ( @sids ) {
160 my $data = $info ->{ $storeid };
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 ;
166 my $scfg = PVE
:: Storage
:: storage_config
( $cfg, $storeid );
168 next if $param ->{ enabled
} && $scfg ->{ disable
};
171 # check if storage content is accessible on local node and specified target node
172 # we use this on the Clone GUI
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 );
179 if ( $data ->{ total
}) {
180 $data ->{ used_fraction
} = ( $data ->{ used
} // 0 ) / $data ->{ total
};
183 $res ->{ $storeid } = $data ;
186 return PVE
:: RESTHandler
:: hash_to_array
( $res, 'storage' );
189 __PACKAGE__-
> register_method ({
195 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.Audit' , 'Datastore.AllocateSpace' ], any
=> 1 ],
198 additionalProperties
=> 0 ,
200 node
=> get_standard_option
( 'pve-node' ),
201 storage
=> get_standard_option
( 'pve-storage-id' ),
209 subdir
=> { type
=> 'string' },
212 links
=> [ { rel
=> 'child' , href
=> "{subdir}" } ],
218 { subdir
=> 'status' },
219 { subdir
=> 'content' },
220 { subdir
=> 'upload' },
222 { subdir
=> 'rrddata' },
223 { subdir
=> 'prunebackups' },
229 __PACKAGE__-
> register_method ({
230 name
=> 'read_status' ,
231 path
=> '{storage}/status' ,
233 description
=> "Read storage status." ,
235 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.Audit' , 'Datastore.AllocateSpace' ], any
=> 1 ],
240 additionalProperties
=> 0 ,
242 node
=> get_standard_option
( 'pve-node' ),
243 storage
=> get_standard_option
( 'pve-storage-id' ),
253 my $cfg = PVE
:: Storage
:: config
();
255 my $info = PVE
:: Storage
:: storage_info
( $cfg, $param ->{ content
});
257 my $data = $info ->{ $param ->{ storage
}};
259 raise_param_exc
({ storage
=> "No such storage." })
265 __PACKAGE__-
> register_method ({
267 path
=> '{storage}/rrd' ,
269 description
=> "Read storage RRD statistics (returns PNG)." ,
271 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.Audit' , 'Datastore.AllocateSpace' ], any
=> 1 ],
276 additionalProperties
=> 0 ,
278 node
=> get_standard_option
( 'pve-node' ),
279 storage
=> get_standard_option
( 'pve-storage-id' ),
281 description
=> "Specify the time frame you are interested in." ,
283 enum
=> [ 'hour' , 'day' , 'week' , 'month' , 'year' ],
286 description
=> "The list of datasources you want to display." ,
287 type
=> 'string' , format
=> 'pve-configid-list' ,
290 description
=> "The RRD consolidation function" ,
292 enum
=> [ 'AVERAGE' , 'MAX' ],
300 filename
=> { type
=> 'string' },
306 return PVE
:: RRD
:: create_rrd_graph
(
307 "pve2-storage/ $param ->{node}/ $param ->{storage}" ,
308 $param ->{ timeframe
}, $param ->{ ds
}, $param ->{ cf
});
311 __PACKAGE__-
> register_method ({
313 path
=> '{storage}/rrddata' ,
315 description
=> "Read storage RRD statistics." ,
317 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.Audit' , 'Datastore.AllocateSpace' ], any
=> 1 ],
322 additionalProperties
=> 0 ,
324 node
=> get_standard_option
( 'pve-node' ),
325 storage
=> get_standard_option
( 'pve-storage-id' ),
327 description
=> "Specify the time frame you are interested in." ,
329 enum
=> [ 'hour' , 'day' , 'week' , 'month' , 'year' ],
332 description
=> "The RRD consolidation function" ,
334 enum
=> [ 'AVERAGE' , 'MAX' ],
349 return PVE
:: RRD
:: create_rrd_data
(
350 "pve2-storage/ $param ->{node}/ $param ->{storage}" ,
351 $param ->{ timeframe
}, $param ->{ cf
});
354 # makes no sense for big images and backup files (because it
355 # create a copy of the file).
356 __PACKAGE__-
> register_method ({
358 path
=> '{storage}/upload' ,
360 description
=> "Upload templates and ISO images." ,
362 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.AllocateTemplate' ]],
366 additionalProperties
=> 0 ,
368 node
=> get_standard_option
( 'pve-node' ),
369 storage
=> get_standard_option
( 'pve-storage-id' ),
371 description
=> "Content type." ,
372 type
=> 'string' , format
=> 'pve-storage-content' ,
375 description
=> "The name of the file to create." ,
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." ,
385 returns
=> { type
=> "string" },
389 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
391 my $user = $rpcenv -> get_user ();
393 my $cfg = PVE
:: Storage
:: config
();
395 my $node = $param ->{ node
};
396 my $scfg = PVE
:: Storage
:: storage_check_enabled
( $cfg, $param ->{ storage
}, $node );
398 die "can't upload to storage type ' $scfg ->{type}' \n "
399 if ! defined ( $scfg ->{ path
});
401 my $content = $param ->{ content
};
403 my $tmpfilename = $param ->{ tmpfilename
};
404 die "missing temporary file name \n " if ! $tmpfilename ;
406 my $size = - s
$tmpfilename ;
407 die "temporary file ' $tmpfilename ' does not exist \n " if ! defined ( $size );
409 my $filename = $param ->{ filename
};
412 $filename =~ s/^.*[\/\\]/ /;
413 $filename =~ s/[^-a-zA-Z0-9_.]/_/g ;
417 if ( $content eq 'iso' ) {
418 if ( $filename !~ m![^/]+$PVE::Storage::iso_extension_re$! ) {
419 raise_param_exc
({ filename
=> "missing '.iso' or '.img' extension" });
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" });
426 $path = PVE
:: Storage
:: get_vztmpl_dir
( $cfg, $param ->{ storage
});
428 raise_param_exc
({ content
=> "upload content type ' $content ' not allowed" });
431 die "storage ' $param ->{storage}' does not support ' $content ' content \n "
432 if ! $scfg ->{ content
}->{ $content };
434 my $dest = " $path/$filename " ;
435 my $dirname = dirname
( $dest );
437 # best effort to match apl_download behaviour
438 chmod 0644 , $tmpfilename ;
440 # we simply overwrite the destination file if it already exists
443 if ( $node ne 'localhost' && $node ne PVE
:: INotify
:: nodename
()) {
444 my $remip = PVE
:: Cluster
:: remote_node_ip
( $node );
446 my @ssh_options = ( '-o' , 'BatchMode=yes' );
448 my @remcmd = ( '/usr/bin/ssh' , @ssh_options, $remip, '--' );
451 # activate remote storage
452 PVE
:: Tools
:: run_command
([ @remcmd, '/usr/sbin/pvesm' , 'status' ,
453 '--storage' , $param ->{ storage
}]);
455 die "can't activate storage ' $param ->{storage}' on node ' $node ': $@\n " if $@ ;
457 PVE
:: Tools
:: run_command
([ @remcmd, '/bin/mkdir' , '-p' , '--' , PVE
:: Tools
:: shell_quote
( $dirname )],
458 errmsg
=> "mkdir failed" );
460 $cmd = [ '/usr/bin/scp' , @ssh_options, '-p' , '--' , $tmpfilename, "[ $remip ]:" . PVE
:: Tools
:: shell_quote
( $dest )];
462 PVE
:: Storage
:: activate_storage
( $cfg, $param ->{ storage
});
463 File
:: Path
:: make_path
( $dirname );
464 $cmd = [ 'cp' , '--' , $tmpfilename, $dest ];
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 " ;
476 eval { PVE
:: Tools
:: run_command
( $cmd, errmsg
=> 'import failed' ); };
481 print "finished file import successfully \n " ;
484 my $upid = $rpcenv -> fork_worker ( 'imgcopy' , undef , $user, $worker );
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.