]>
git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Storage/Status.pm
91946acf1473fb4a1507ace982e978338342a69a
1 package PVE
:: API2
:: Storage
:: Status
;
12 use PVE
:: API2
:: Storage
:: Content
;
14 use PVE
:: RPCEnvironment
;
15 use PVE
:: JSONSchema
qw(get_standard_option) ;
16 use PVE
:: Exception
qw(raise_param_exc) ;
18 use base
qw(PVE::RESTHandler) ;
20 __PACKAGE__-
> register_method ({
21 subclass
=> "PVE::API2::Storage::Content" ,
22 # set fragment delimiter (no subdirs) - we need that, because volume
23 # IDs may contain a slash '/'
24 fragmentDelimiter
=> '' ,
25 path
=> '{storage}/content' ,
28 __PACKAGE__-
> register_method ({
32 description
=> "Get status for all datastores." ,
34 description
=> "Only list entries where you have 'Datastore.Audit' or 'Datastore.AllocateSpace' permissions on '/storage/<storage>'" ,
40 additionalProperties
=> 0 ,
42 node
=> get_standard_option
( 'pve-node' ),
43 storage
=> get_standard_option
( 'pve-storage-id' , {
44 description
=> "Only list status for specified storage" ,
46 completion
=> \
& PVE
:: Storage
:: complete_storage_enabled
,
49 description
=> "Only list stores which support this content type." ,
50 type
=> 'string' , format
=> 'pve-storage-content-list' ,
52 completion
=> \
& PVE
:: Storage
:: complete_content_type
,
55 description
=> "Only list stores which are enabled (not disabled in config)." ,
60 target
=> get_standard_option
( 'pve-node' , {
61 description
=> "If target is different to 'node', we only lists shared storages which " .
62 "content is accessible on this 'node' and the specified 'target' node." ,
64 completion
=> \
& PVE
:: Cluster
:: get_nodelist
,
67 description
=> "Include information about formats" ,
79 storage
=> get_standard_option
( 'pve-storage-id' ),
81 description
=> "Storage type." ,
85 description
=> "Allowed storage content types." ,
86 type
=> 'string' , format
=> 'pve-storage-content-list' ,
89 description
=> "Set when storage is enabled (not disabled)." ,
94 description
=> "Set when storage is accessible." ,
99 description
=> "Shared flag from storage configuration." ,
104 description
=> "Total storage space in bytes." ,
110 description
=> "Used storage space in bytes." ,
116 description
=> "Available storage space in bytes." ,
122 description
=> "Used fraction (used/total)." ,
124 renderer
=> 'fraction_as_percentage' ,
129 links
=> [ { rel
=> 'child' , href
=> "{storage}" } ],
134 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
135 my $authuser = $rpcenv -> get_user ();
137 my $localnode = PVE
:: INotify
:: nodename
();
139 my $target = $param ->{ target
};
141 undef $target if $target && ( $target eq $localnode || $target eq 'localhost' );
143 my $cfg = PVE
:: Storage
:: config
();
145 my $info = PVE
:: Storage
:: storage_info
( $cfg, $param ->{ content
}, $param ->{ format
});
147 raise_param_exc
({ storage
=> "No such storage." })
148 if $param ->{ storage
} && ! defined ( $info ->{ $param ->{ storage
}});
151 my @sids = PVE
:: Storage
:: storage_ids
( $cfg );
152 foreach my $storeid ( @sids ) {
153 my $data = $info ->{ $storeid };
155 my $privs = [ 'Datastore.Audit' , 'Datastore.AllocateSpace' ];
156 next if ! $rpcenv -> check_any ( $authuser, "/storage/ $storeid " , $privs, 1 );
157 next if $param ->{ storage
} && $param ->{ storage
} ne $storeid ;
159 my $scfg = PVE
:: Storage
:: storage_config
( $cfg, $storeid );
161 next if $param ->{ enabled
} && $scfg ->{ disable
};
164 # check if storage content is accessible on local node and specified target node
165 # we use this on the Clone GUI
167 next if ! $scfg ->{ shared
};
168 next if ! PVE
:: Storage
:: storage_check_node
( $cfg, $storeid, undef , 1 );
169 next if ! PVE
:: Storage
:: storage_check_node
( $cfg, $storeid, $target, 1 );
172 if ( $data ->{ total
}) {
173 $data ->{ used_fraction
} = ( $data ->{ used
} // 0 ) / $data ->{ total
};
176 $res ->{ $storeid } = $data ;
179 return PVE
:: RESTHandler
:: hash_to_array
( $res, 'storage' );
182 __PACKAGE__-
> register_method ({
188 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.Audit' , 'Datastore.AllocateSpace' ], any
=> 1 ],
191 additionalProperties
=> 0 ,
193 node
=> get_standard_option
( 'pve-node' ),
194 storage
=> get_standard_option
( 'pve-storage-id' ),
202 subdir
=> { type
=> 'string' },
205 links
=> [ { rel
=> 'child' , href
=> "{subdir}" } ],
211 { subdir
=> 'status' },
212 { subdir
=> 'content' },
213 { subdir
=> 'upload' },
215 { subdir
=> 'rrddata' },
221 __PACKAGE__-
> register_method ({
222 name
=> 'read_status' ,
223 path
=> '{storage}/status' ,
225 description
=> "Read storage status." ,
227 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.Audit' , 'Datastore.AllocateSpace' ], any
=> 1 ],
232 additionalProperties
=> 0 ,
234 node
=> get_standard_option
( 'pve-node' ),
235 storage
=> get_standard_option
( 'pve-storage-id' ),
245 my $cfg = PVE
:: Storage
:: config
();
247 my $info = PVE
:: Storage
:: storage_info
( $cfg, $param ->{ content
});
249 my $data = $info ->{ $param ->{ storage
}};
251 raise_param_exc
({ storage
=> "No such storage." })
257 __PACKAGE__-
> register_method ({
259 path
=> '{storage}/rrd' ,
261 description
=> "Read storage RRD statistics (returns PNG)." ,
263 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.Audit' , 'Datastore.AllocateSpace' ], any
=> 1 ],
268 additionalProperties
=> 0 ,
270 node
=> get_standard_option
( 'pve-node' ),
271 storage
=> get_standard_option
( 'pve-storage-id' ),
273 description
=> "Specify the time frame you are interested in." ,
275 enum
=> [ 'hour' , 'day' , 'week' , 'month' , 'year' ],
278 description
=> "The list of datasources you want to display." ,
279 type
=> 'string' , format
=> 'pve-configid-list' ,
282 description
=> "The RRD consolidation function" ,
284 enum
=> [ 'AVERAGE' , 'MAX' ],
292 filename
=> { type
=> 'string' },
298 return PVE
:: Cluster
:: create_rrd_graph
(
299 "pve2-storage/ $param ->{node}/ $param ->{storage}" ,
300 $param ->{ timeframe
}, $param ->{ ds
}, $param ->{ cf
});
304 __PACKAGE__-
> register_method ({
306 path
=> '{storage}/rrddata' ,
308 description
=> "Read storage RRD statistics." ,
310 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.Audit' , 'Datastore.AllocateSpace' ], any
=> 1 ],
315 additionalProperties
=> 0 ,
317 node
=> get_standard_option
( 'pve-node' ),
318 storage
=> get_standard_option
( 'pve-storage-id' ),
320 description
=> "Specify the time frame you are interested in." ,
322 enum
=> [ 'hour' , 'day' , 'week' , 'month' , 'year' ],
325 description
=> "The RRD consolidation function" ,
327 enum
=> [ 'AVERAGE' , 'MAX' ],
342 return PVE
:: Cluster
:: create_rrd_data
(
343 "pve2-storage/ $param ->{node}/ $param ->{storage}" ,
344 $param ->{ timeframe
}, $param ->{ cf
});
347 # makes no sense for big images and backup files (because it
348 # create a copy of the file).
349 __PACKAGE__-
> register_method ({
351 path
=> '{storage}/upload' ,
353 description
=> "Upload templates and ISO images." ,
355 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.AllocateTemplate' ]],
359 additionalProperties
=> 0 ,
361 node
=> get_standard_option
( 'pve-node' ),
362 storage
=> get_standard_option
( 'pve-storage-id' ),
364 description
=> "Content type." ,
365 type
=> 'string' , format
=> 'pve-storage-content' ,
368 description
=> "The name of the file to create." ,
372 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." ,
378 returns
=> { type
=> "string" },
382 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
384 my $user = $rpcenv -> get_user ();
386 my $cfg = PVE
:: Storage
:: config
();
388 my $node = $param ->{ node
};
389 my $scfg = PVE
:: Storage
:: storage_check_enabled
( $cfg, $param ->{ storage
}, $node );
391 die "can't upload to storage type ' $scfg ->{type}' \n "
392 if ! defined ( $scfg ->{ path
});
394 my $content = $param ->{ content
};
396 my $tmpfilename = $param ->{ tmpfilename
};
397 die "missing temporary file name \n " if ! $tmpfilename ;
399 my $size = - s
$tmpfilename ;
400 die "temporary file ' $tmpfilename ' does not exists \n " if ! defined ( $size );
402 my $filename = $param ->{ filename
};
405 $filename =~ s/^.*[\/\\]/ /;
406 $filename =~ s/[^-a-zA-Z0-9_.]/_/g ;
410 if ( $content eq 'iso' ) {
411 if ( $filename !~ m![^/]+$PVE::Storage::iso_extension_re$! ) {
412 raise_param_exc
({ filename
=> "missing '.iso' or '.img' extension" });
414 $path = PVE
:: Storage
:: get_iso_dir
( $cfg, $param ->{ storage
});
415 } elsif ( $content eq 'vztmpl' ) {
416 if ( $filename !~ m![^/]+\.tar\.[gx]z$! ) {
417 raise_param_exc
({ filename
=> "missing '.tar.gz' or '.tar.xz' extension" });
419 $path = PVE
:: Storage
:: get_vztmpl_dir
( $cfg, $param ->{ storage
});
421 raise_param_exc
({ content
=> "upload content type ' $content ' not allowed" });
424 die "storage ' $param ->{storage}' does not support ' $content ' content \n "
425 if ! $scfg ->{ content
}->{ $content };
427 my $dest = " $path/$filename " ;
428 my $dirname = dirname
( $dest );
430 # best effort to match apl_download behaviour
431 chmod 0644 , $tmpfilename ;
433 # we simply overwrite the destination file if it already exists
436 if ( $node ne 'localhost' && $node ne PVE
:: INotify
:: nodename
()) {
437 my $remip = PVE
:: Cluster
:: remote_node_ip
( $node );
439 my @ssh_options = ( '-o' , 'BatchMode=yes' );
441 my @remcmd = ( '/usr/bin/ssh' , @ssh_options, $remip, '--' );
444 # activate remote storage
445 PVE
:: Tools
:: run_command
([ @remcmd, '/usr/sbin/pvesm' , 'status' ,
446 '--storage' , $param ->{ storage
}]);
448 die "can't activate storage ' $param ->{storage}' on node ' $node ': $@\n " if $@ ;
450 PVE
:: Tools
:: run_command
([ @remcmd, '/bin/mkdir' , '-p' , '--' , PVE
:: Tools
:: shell_quote
( $dirname )],
451 errmsg
=> "mkdir failed" );
453 $cmd = [ '/usr/bin/scp' , @ssh_options, '-p' , '--' , $tmpfilename, "[ $remip ]:" . PVE
:: Tools
:: shell_quote
( $dest )];
455 PVE
:: Storage
:: activate_storage
( $cfg, $param ->{ storage
});
456 File
:: Path
:: make_path
( $dirname );
457 $cmd = [ 'cp' , '--' , $tmpfilename, $dest ];
463 print "starting file import from: $tmpfilename\n " ;
464 print "target node: $node\n " ;
465 print "target file: $dest\n " ;
466 print "file size is: $size\n " ;
467 print "command: " . join ( ' ' , @$cmd ) . " \n " ;
469 eval { PVE
:: Tools
:: run_command
( $cmd, errmsg
=> 'import failed' ); };
474 print "finished file import successfully \n " ;
477 my $upid = $rpcenv -> fork_worker ( 'imgcopy' , undef , $user, $worker );
479 # apache removes the temporary file on return, so we need
480 # to wait here to make sure the worker process starts and
481 # opens the file before it gets removed.