]>
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
;
15 use PVE
:: RPCEnvironment
;
16 use PVE
:: JSONSchema
qw(get_standard_option) ;
17 use PVE
:: Exception
qw(raise_param_exc) ;
19 use base
qw(PVE::RESTHandler) ;
21 __PACKAGE__-
> register_method ({
22 subclass
=> "PVE::API2::Storage::Content" ,
23 # set fragment delimiter (no subdirs) - we need that, because volume
24 # IDs may contain a slash '/'
25 fragmentDelimiter
=> '' ,
26 path
=> '{storage}/content' ,
29 __PACKAGE__-
> register_method ({
33 description
=> "Get status for all datastores." ,
35 description
=> "Only list entries where you have 'Datastore.Audit' or 'Datastore.AllocateSpace' permissions on '/storage/<storage>'" ,
41 additionalProperties
=> 0 ,
43 node
=> get_standard_option
( 'pve-node' ),
44 storage
=> get_standard_option
( 'pve-storage-id' , {
45 description
=> "Only list status for specified storage" ,
47 completion
=> \
& PVE
:: Storage
:: complete_storage_enabled
,
50 description
=> "Only list stores which support this content type." ,
51 type
=> 'string' , format
=> 'pve-storage-content-list' ,
53 completion
=> \
& PVE
:: Storage
:: complete_content_type
,
56 description
=> "Only list stores which are enabled (not disabled in config)." ,
61 target
=> get_standard_option
( 'pve-node' , {
62 description
=> "If target is different to 'node', we only lists shared storages which " .
63 "content is accessible on this 'node' and the specified 'target' node." ,
65 completion
=> \
& PVE
:: Cluster
:: get_nodelist
,
68 description
=> "Include information about formats" ,
80 storage
=> get_standard_option
( 'pve-storage-id' ),
82 description
=> "Storage type." ,
86 description
=> "Allowed storage content types." ,
87 type
=> 'string' , format
=> 'pve-storage-content-list' ,
90 description
=> "Set when storage is enabled (not disabled)." ,
95 description
=> "Set when storage is accessible." ,
100 description
=> "Shared flag from storage configuration." ,
105 description
=> "Total storage space in bytes." ,
111 description
=> "Used storage space in bytes." ,
117 description
=> "Available storage space in bytes." ,
123 description
=> "Used fraction (used/total)." ,
125 renderer
=> 'fraction_as_percentage' ,
130 links
=> [ { rel
=> 'child' , href
=> "{storage}" } ],
135 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
136 my $authuser = $rpcenv -> get_user ();
138 my $localnode = PVE
:: INotify
:: nodename
();
140 my $target = $param ->{ target
};
142 undef $target if $target && ( $target eq $localnode || $target eq 'localhost' );
144 my $cfg = PVE
:: Storage
:: config
();
146 my $info = PVE
:: Storage
:: storage_info
( $cfg, $param ->{ content
}, $param ->{ format
});
148 raise_param_exc
({ storage
=> "No such storage." })
149 if $param ->{ storage
} && ! defined ( $info ->{ $param ->{ storage
}});
152 my @sids = PVE
:: Storage
:: storage_ids
( $cfg );
153 foreach my $storeid ( @sids ) {
154 my $data = $info ->{ $storeid };
156 my $privs = [ 'Datastore.Audit' , 'Datastore.AllocateSpace' ];
157 next if ! $rpcenv -> check_any ( $authuser, "/storage/ $storeid " , $privs, 1 );
158 next if $param ->{ storage
} && $param ->{ storage
} ne $storeid ;
160 my $scfg = PVE
:: Storage
:: storage_config
( $cfg, $storeid );
162 next if $param ->{ enabled
} && $scfg ->{ disable
};
165 # check if storage content is accessible on local node and specified target node
166 # we use this on the Clone GUI
168 next if ! $scfg ->{ shared
};
169 next if ! PVE
:: Storage
:: storage_check_node
( $cfg, $storeid, undef , 1 );
170 next if ! PVE
:: Storage
:: storage_check_node
( $cfg, $storeid, $target, 1 );
173 if ( $data ->{ total
}) {
174 $data ->{ used_fraction
} = ( $data ->{ used
} // 0 ) / $data ->{ total
};
177 $res ->{ $storeid } = $data ;
180 return PVE
:: RESTHandler
:: hash_to_array
( $res, 'storage' );
183 __PACKAGE__-
> register_method ({
189 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.Audit' , 'Datastore.AllocateSpace' ], any
=> 1 ],
192 additionalProperties
=> 0 ,
194 node
=> get_standard_option
( 'pve-node' ),
195 storage
=> get_standard_option
( 'pve-storage-id' ),
203 subdir
=> { type
=> 'string' },
206 links
=> [ { rel
=> 'child' , href
=> "{subdir}" } ],
212 { subdir
=> 'status' },
213 { subdir
=> 'content' },
214 { subdir
=> 'upload' },
216 { subdir
=> 'rrddata' },
222 __PACKAGE__-
> register_method ({
223 name
=> 'read_status' ,
224 path
=> '{storage}/status' ,
226 description
=> "Read storage status." ,
228 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.Audit' , 'Datastore.AllocateSpace' ], any
=> 1 ],
233 additionalProperties
=> 0 ,
235 node
=> get_standard_option
( 'pve-node' ),
236 storage
=> get_standard_option
( 'pve-storage-id' ),
246 my $cfg = PVE
:: Storage
:: config
();
248 my $info = PVE
:: Storage
:: storage_info
( $cfg, $param ->{ content
});
250 my $data = $info ->{ $param ->{ storage
}};
252 raise_param_exc
({ storage
=> "No such storage." })
258 __PACKAGE__-
> register_method ({
260 path
=> '{storage}/rrd' ,
262 description
=> "Read storage RRD statistics (returns PNG)." ,
264 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.Audit' , 'Datastore.AllocateSpace' ], any
=> 1 ],
269 additionalProperties
=> 0 ,
271 node
=> get_standard_option
( 'pve-node' ),
272 storage
=> get_standard_option
( 'pve-storage-id' ),
274 description
=> "Specify the time frame you are interested in." ,
276 enum
=> [ 'hour' , 'day' , 'week' , 'month' , 'year' ],
279 description
=> "The list of datasources you want to display." ,
280 type
=> 'string' , format
=> 'pve-configid-list' ,
283 description
=> "The RRD consolidation function" ,
285 enum
=> [ 'AVERAGE' , 'MAX' ],
293 filename
=> { type
=> 'string' },
299 return PVE
:: RRD
:: create_rrd_graph
(
300 "pve2-storage/ $param ->{node}/ $param ->{storage}" ,
301 $param ->{ timeframe
}, $param ->{ ds
}, $param ->{ cf
});
305 __PACKAGE__-
> register_method ({
307 path
=> '{storage}/rrddata' ,
309 description
=> "Read storage RRD statistics." ,
311 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.Audit' , 'Datastore.AllocateSpace' ], any
=> 1 ],
316 additionalProperties
=> 0 ,
318 node
=> get_standard_option
( 'pve-node' ),
319 storage
=> get_standard_option
( 'pve-storage-id' ),
321 description
=> "Specify the time frame you are interested in." ,
323 enum
=> [ 'hour' , 'day' , 'week' , 'month' , 'year' ],
326 description
=> "The RRD consolidation function" ,
328 enum
=> [ 'AVERAGE' , 'MAX' ],
343 return PVE
:: RRD
:: create_rrd_data
(
344 "pve2-storage/ $param ->{node}/ $param ->{storage}" ,
345 $param ->{ timeframe
}, $param ->{ cf
});
348 # makes no sense for big images and backup files (because it
349 # create a copy of the file).
350 __PACKAGE__-
> register_method ({
352 path
=> '{storage}/upload' ,
354 description
=> "Upload templates and ISO images." ,
356 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.AllocateTemplate' ]],
360 additionalProperties
=> 0 ,
362 node
=> get_standard_option
( 'pve-node' ),
363 storage
=> get_standard_option
( 'pve-storage-id' ),
365 description
=> "Content type." ,
366 type
=> 'string' , format
=> 'pve-storage-content' ,
369 description
=> "The name of the file to create." ,
373 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." ,
379 returns
=> { type
=> "string" },
383 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
385 my $user = $rpcenv -> get_user ();
387 my $cfg = PVE
:: Storage
:: config
();
389 my $node = $param ->{ node
};
390 my $scfg = PVE
:: Storage
:: storage_check_enabled
( $cfg, $param ->{ storage
}, $node );
392 die "can't upload to storage type ' $scfg ->{type}' \n "
393 if ! defined ( $scfg ->{ path
});
395 my $content = $param ->{ content
};
397 my $tmpfilename = $param ->{ tmpfilename
};
398 die "missing temporary file name \n " if ! $tmpfilename ;
400 my $size = - s
$tmpfilename ;
401 die "temporary file ' $tmpfilename ' does not exist \n " if ! defined ( $size );
403 my $filename = $param ->{ filename
};
406 $filename =~ s/^.*[\/\\]/ /;
407 $filename =~ s/[^-a-zA-Z0-9_.]/_/g ;
411 if ( $content eq 'iso' ) {
412 if ( $filename !~ m![^/]+$PVE::Storage::iso_extension_re$! ) {
413 raise_param_exc
({ filename
=> "missing '.iso' or '.img' extension" });
415 $path = PVE
:: Storage
:: get_iso_dir
( $cfg, $param ->{ storage
});
416 } elsif ( $content eq 'vztmpl' ) {
417 if ( $filename !~ m![^/]+\.tar\.[gx]z$! ) {
418 raise_param_exc
({ filename
=> "missing '.tar.gz' or '.tar.xz' extension" });
420 $path = PVE
:: Storage
:: get_vztmpl_dir
( $cfg, $param ->{ storage
});
422 raise_param_exc
({ content
=> "upload content type ' $content ' not allowed" });
425 die "storage ' $param ->{storage}' does not support ' $content ' content \n "
426 if ! $scfg ->{ content
}->{ $content };
428 my $dest = " $path/$filename " ;
429 my $dirname = dirname
( $dest );
431 # best effort to match apl_download behaviour
432 chmod 0644 , $tmpfilename ;
434 # we simply overwrite the destination file if it already exists
437 if ( $node ne 'localhost' && $node ne PVE
:: INotify
:: nodename
()) {
438 my $remip = PVE
:: Cluster
:: remote_node_ip
( $node );
440 my @ssh_options = ( '-o' , 'BatchMode=yes' );
442 my @remcmd = ( '/usr/bin/ssh' , @ssh_options, $remip, '--' );
445 # activate remote storage
446 PVE
:: Tools
:: run_command
([ @remcmd, '/usr/sbin/pvesm' , 'status' ,
447 '--storage' , $param ->{ storage
}]);
449 die "can't activate storage ' $param ->{storage}' on node ' $node ': $@\n " if $@ ;
451 PVE
:: Tools
:: run_command
([ @remcmd, '/bin/mkdir' , '-p' , '--' , PVE
:: Tools
:: shell_quote
( $dirname )],
452 errmsg
=> "mkdir failed" );
454 $cmd = [ '/usr/bin/scp' , @ssh_options, '-p' , '--' , $tmpfilename, "[ $remip ]:" . PVE
:: Tools
:: shell_quote
( $dest )];
456 PVE
:: Storage
:: activate_storage
( $cfg, $param ->{ storage
});
457 File
:: Path
:: make_path
( $dirname );
458 $cmd = [ 'cp' , '--' , $tmpfilename, $dest ];
464 print "starting file import from: $tmpfilename\n " ;
465 print "target node: $node\n " ;
466 print "target file: $dest\n " ;
467 print "file size is: $size\n " ;
468 print "command: " . join ( ' ' , @$cmd ) . " \n " ;
470 eval { PVE
:: Tools
:: run_command
( $cmd, errmsg
=> 'import failed' ); };
475 print "finished file import successfully \n " ;
478 my $upid = $rpcenv -> fork_worker ( 'imgcopy' , undef , $user, $worker );
480 # apache removes the temporary file on return, so we need
481 # to wait here to make sure the worker process starts and
482 # opens the file before it gets removed.