]>
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
;
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
=> 'file-restore' },
226 { subdir
=> 'prunebackups' },
228 { subdir
=> 'rrddata' },
229 { subdir
=> 'status' },
230 { subdir
=> 'upload' },
236 __PACKAGE__-
> register_method ({
237 name
=> 'read_status' ,
238 path
=> '{storage}/status' ,
240 description
=> "Read storage status." ,
242 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.Audit' , 'Datastore.AllocateSpace' ], any
=> 1 ],
247 additionalProperties
=> 0 ,
249 node
=> get_standard_option
( 'pve-node' ),
250 storage
=> get_standard_option
( 'pve-storage-id' ),
260 my $cfg = PVE
:: Storage
:: config
();
262 my $info = PVE
:: Storage
:: storage_info
( $cfg, $param ->{ content
});
264 my $data = $info ->{ $param ->{ storage
}};
266 raise_param_exc
({ storage
=> "No such storage." })
272 __PACKAGE__-
> register_method ({
274 path
=> '{storage}/rrd' ,
276 description
=> "Read storage RRD statistics (returns PNG)." ,
278 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.Audit' , 'Datastore.AllocateSpace' ], any
=> 1 ],
283 additionalProperties
=> 0 ,
285 node
=> get_standard_option
( 'pve-node' ),
286 storage
=> get_standard_option
( 'pve-storage-id' ),
288 description
=> "Specify the time frame you are interested in." ,
290 enum
=> [ 'hour' , 'day' , 'week' , 'month' , 'year' ],
293 description
=> "The list of datasources you want to display." ,
294 type
=> 'string' , format
=> 'pve-configid-list' ,
297 description
=> "The RRD consolidation function" ,
299 enum
=> [ 'AVERAGE' , 'MAX' ],
307 filename
=> { type
=> 'string' },
313 return PVE
:: RRD
:: create_rrd_graph
(
314 "pve2-storage/ $param ->{node}/ $param ->{storage}" ,
315 $param ->{ timeframe
}, $param ->{ ds
}, $param ->{ cf
});
318 __PACKAGE__-
> register_method ({
320 path
=> '{storage}/rrddata' ,
322 description
=> "Read storage RRD statistics." ,
324 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.Audit' , 'Datastore.AllocateSpace' ], any
=> 1 ],
329 additionalProperties
=> 0 ,
331 node
=> get_standard_option
( 'pve-node' ),
332 storage
=> get_standard_option
( 'pve-storage-id' ),
334 description
=> "Specify the time frame you are interested in." ,
336 enum
=> [ 'hour' , 'day' , 'week' , 'month' , 'year' ],
339 description
=> "The RRD consolidation function" ,
341 enum
=> [ 'AVERAGE' , 'MAX' ],
356 return PVE
:: RRD
:: create_rrd_data
(
357 "pve2-storage/ $param ->{node}/ $param ->{storage}" ,
358 $param ->{ timeframe
}, $param ->{ cf
});
361 # makes no sense for big images and backup files (because it
362 # create a copy of the file).
363 __PACKAGE__-
> register_method ({
365 path
=> '{storage}/upload' ,
367 description
=> "Upload templates and ISO images." ,
369 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.AllocateTemplate' ]],
373 additionalProperties
=> 0 ,
375 node
=> get_standard_option
( 'pve-node' ),
376 storage
=> get_standard_option
( 'pve-storage-id' ),
378 description
=> "Content type." ,
379 type
=> 'string' , format
=> 'pve-storage-content' ,
382 description
=> "The name of the file to create." ,
386 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." ,
392 returns
=> { type
=> "string" },
396 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
398 my $user = $rpcenv -> get_user ();
400 my $cfg = PVE
:: Storage
:: config
();
402 my $node = $param ->{ node
};
403 my $scfg = PVE
:: Storage
:: storage_check_enabled
( $cfg, $param ->{ storage
}, $node );
405 die "can't upload to storage type ' $scfg ->{type}' \n "
406 if ! defined ( $scfg ->{ path
});
408 my $content = $param ->{ content
};
410 my $tmpfilename = $param ->{ tmpfilename
};
411 die "missing temporary file name \n " if ! $tmpfilename ;
413 my $size = - s
$tmpfilename ;
414 die "temporary file ' $tmpfilename ' does not exist \n " if ! defined ( $size );
416 my $filename = $param ->{ filename
};
419 $filename =~ s/^.*[\/\\]/ /;
420 $filename =~ s/[^-a-zA-Z0-9_.]/_/g ;
424 if ( $content eq 'iso' ) {
425 if ( $filename !~ m![^/]+$PVE::Storage::iso_extension_re$! ) {
426 raise_param_exc
({ filename
=> "wrong file extension" });
428 $path = PVE
:: Storage
:: get_iso_dir
( $cfg, $param ->{ storage
});
429 } elsif ( $content eq 'vztmpl' ) {
430 if ( $filename !~ m![^/]+$PVE::Storage::vztmpl_extension_re$! ) {
431 raise_param_exc
({ filename
=> "wrong file extension" });
433 $path = PVE
:: Storage
:: get_vztmpl_dir
( $cfg, $param ->{ storage
});
435 raise_param_exc
({ content
=> "upload content type ' $content ' not allowed" });
438 die "storage ' $param ->{storage}' does not support ' $content ' content \n "
439 if ! $scfg ->{ content
}->{ $content };
441 my $dest = " $path/$filename " ;
442 my $dirname = dirname
( $dest );
444 # best effort to match apl_download behaviour
445 chmod 0644 , $tmpfilename ;
447 # we simply overwrite the destination file if it already exists
450 if ( $node ne 'localhost' && $node ne PVE
:: INotify
:: nodename
()) {
451 my $remip = PVE
:: Cluster
:: remote_node_ip
( $node );
453 my @ssh_options = ( '-o' , 'BatchMode=yes' );
455 my @remcmd = ( '/usr/bin/ssh' , @ssh_options, $remip, '--' );
458 # activate remote storage
459 PVE
:: Tools
:: run_command
([ @remcmd, '/usr/sbin/pvesm' , 'status' ,
460 '--storage' , $param ->{ storage
}]);
462 die "can't activate storage ' $param ->{storage}' on node ' $node ': $@\n " if $@ ;
464 PVE
:: Tools
:: run_command
([ @remcmd, '/bin/mkdir' , '-p' , '--' , PVE
:: Tools
:: shell_quote
( $dirname )],
465 errmsg
=> "mkdir failed" );
467 $cmd = [ '/usr/bin/scp' , @ssh_options, '-p' , '--' , $tmpfilename, "[ $remip ]:" . PVE
:: Tools
:: shell_quote
( $dest )];
469 PVE
:: Storage
:: activate_storage
( $cfg, $param ->{ storage
});
470 File
:: Path
:: make_path
( $dirname );
471 $cmd = [ 'cp' , '--' , $tmpfilename, $dest ];
477 print "starting file import from: $tmpfilename\n " ;
478 print "target node: $node\n " ;
479 print "target file: $dest\n " ;
480 print "file size is: $size\n " ;
481 print "command: " . join ( ' ' , @$cmd ) . " \n " ;
483 eval { PVE
:: Tools
:: run_command
( $cmd, errmsg
=> 'import failed' ); };
488 print "finished file import successfully \n " ;
491 my $upid = $rpcenv -> fork_worker ( 'imgcopy' , undef , $user, $worker );
493 # apache removes the temporary file on return, so we need
494 # to wait here to make sure the worker process starts and
495 # opens the file before it gets removed.