]>
git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Storage/Status.pm
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" ,
78 properties
=> { storage
=> { type
=> 'string' } },
80 links
=> [ { rel
=> 'child' , href
=> "{storage}" } ],
85 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
86 my $authuser = $rpcenv -> get_user ();
88 my $localnode = PVE
:: INotify
:: nodename
();
90 my $target = $param ->{ target
};
92 undef $target if $target && ( $target eq $localnode || $target eq 'localhost' );
94 my $cfg = PVE
:: Storage
:: config
();
96 my $info = PVE
:: Storage
:: storage_info
( $cfg, $param ->{ content
}, $param ->{ format
});
98 raise_param_exc
({ storage
=> "No such storage." })
99 if $param ->{ storage
} && ! defined ( $info ->{ $param ->{ storage
}});
102 my @sids = PVE
:: Storage
:: storage_ids
( $cfg );
103 foreach my $storeid ( @sids ) {
104 next if ! $info ->{ $storeid };
105 my $privs = [ 'Datastore.Audit' , 'Datastore.AllocateSpace' ];
106 next if ! $rpcenv -> check_any ( $authuser, "/storage/ $storeid " , $privs, 1 );
107 next if $param ->{ storage
} && $param ->{ storage
} ne $storeid ;
109 my $scfg = PVE
:: Storage
:: storage_config
( $cfg, $storeid );
111 next if $param ->{ enabled
} && $scfg ->{ disable
};
114 # check if storage content is accessible on local node and specified target node
115 # we use this on the Clone GUI
117 next if ! $scfg ->{ shared
};
118 next if ! PVE
:: Storage
:: storage_check_node
( $cfg, $storeid, undef , 1 );
119 next if ! PVE
:: Storage
:: storage_check_node
( $cfg, $storeid, $target, 1 );
122 $res ->{ $storeid } = $info ->{ $storeid };
125 return PVE
:: RESTHandler
:: hash_to_array
( $res, 'storage' );
128 __PACKAGE__-
> register_method ({
134 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.Audit' , 'Datastore.AllocateSpace' ], any
=> 1 ],
137 additionalProperties
=> 0 ,
139 node
=> get_standard_option
( 'pve-node' ),
140 storage
=> get_standard_option
( 'pve-storage-id' ),
148 subdir
=> { type
=> 'string' },
151 links
=> [ { rel
=> 'child' , href
=> "{subdir}" } ],
157 { subdir
=> 'status' },
158 { subdir
=> 'content' },
159 { subdir
=> 'upload' },
161 { subdir
=> 'rrddata' },
167 __PACKAGE__-
> register_method ({
168 name
=> 'read_status' ,
169 path
=> '{storage}/status' ,
171 description
=> "Read storage status." ,
173 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.Audit' , 'Datastore.AllocateSpace' ], any
=> 1 ],
178 additionalProperties
=> 0 ,
180 node
=> get_standard_option
( 'pve-node' ),
181 storage
=> get_standard_option
( 'pve-storage-id' ),
191 my $cfg = PVE
:: Storage
:: config
();
193 my $info = PVE
:: Storage
:: storage_info
( $cfg, $param ->{ content
});
195 my $data = $info ->{ $param ->{ storage
}};
197 raise_param_exc
({ storage
=> "No such storage." })
203 __PACKAGE__-
> register_method ({
205 path
=> '{storage}/rrd' ,
207 description
=> "Read storage RRD statistics (returns PNG)." ,
209 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.Audit' , 'Datastore.AllocateSpace' ], any
=> 1 ],
214 additionalProperties
=> 0 ,
216 node
=> get_standard_option
( 'pve-node' ),
217 storage
=> get_standard_option
( 'pve-storage-id' ),
219 description
=> "Specify the time frame you are interested in." ,
221 enum
=> [ 'hour' , 'day' , 'week' , 'month' , 'year' ],
224 description
=> "The list of datasources you want to display." ,
225 type
=> 'string' , format
=> 'pve-configid-list' ,
228 description
=> "The RRD consolidation function" ,
230 enum
=> [ 'AVERAGE' , 'MAX' ],
238 filename
=> { type
=> 'string' },
244 return PVE
:: Cluster
:: create_rrd_graph
(
245 "pve2-storage/ $param ->{node}/ $param ->{storage}" ,
246 $param ->{ timeframe
}, $param ->{ ds
}, $param ->{ cf
});
250 __PACKAGE__-
> register_method ({
252 path
=> '{storage}/rrddata' ,
254 description
=> "Read storage RRD statistics." ,
256 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.Audit' , 'Datastore.AllocateSpace' ], any
=> 1 ],
261 additionalProperties
=> 0 ,
263 node
=> get_standard_option
( 'pve-node' ),
264 storage
=> get_standard_option
( 'pve-storage-id' ),
266 description
=> "Specify the time frame you are interested in." ,
268 enum
=> [ 'hour' , 'day' , 'week' , 'month' , 'year' ],
271 description
=> "The RRD consolidation function" ,
273 enum
=> [ 'AVERAGE' , 'MAX' ],
288 return PVE
:: Cluster
:: create_rrd_data
(
289 "pve2-storage/ $param ->{node}/ $param ->{storage}" ,
290 $param ->{ timeframe
}, $param ->{ cf
});
293 # makes no sense for big images and backup files (because it
294 # create a copy of the file).
295 __PACKAGE__-
> register_method ({
297 path
=> '{storage}/upload' ,
299 description
=> "Upload templates and ISO images." ,
301 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.AllocateTemplate' ]],
305 additionalProperties
=> 0 ,
307 node
=> get_standard_option
( 'pve-node' ),
308 storage
=> get_standard_option
( 'pve-storage-id' ),
310 description
=> "Content type." ,
311 type
=> 'string' , format
=> 'pve-storage-content' ,
314 description
=> "The name of the file to create." ,
318 description
=> "The source file name. This parameter is usually set by the REST handler. You can only overwrite it when connecting to the trustet port on localhost." ,
324 returns
=> { type
=> "string" },
328 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
330 my $user = $rpcenv -> get_user ();
332 my $cfg = PVE
:: Storage
:: config
();
334 my $node = $param ->{ node
};
335 my $scfg = PVE
:: Storage
:: storage_check_enabled
( $cfg, $param ->{ storage
}, $node );
337 die "can't upload to storage type ' $scfg ->{type}' \n "
338 if ! defined ( $scfg ->{ path
});
340 my $content = $param ->{ content
};
342 my $tmpfilename = $param ->{ tmpfilename
};
343 die "missing temporary file name \n " if ! $tmpfilename ;
345 my $size = - s
$tmpfilename ;
346 die "temporary file ' $tmpfilename ' does not exists \n " if ! defined ( $size );
348 my $filename = $param ->{ filename
};
351 $filename =~ s/^.*[\/\\]/ /;
352 $filename =~ s/[^-a-zA-Z0-9_.]/_/g ;
356 if ( $content eq 'iso' ) {
357 if ( $filename !~ m![^/]+\.[Ii][Ss][Oo]$! ) {
358 raise_param_exc
({ filename
=> "missing '.iso' extension" });
360 $path = PVE
:: Storage
:: get_iso_dir
( $cfg, $param ->{ storage
});
361 } elsif ( $content eq 'vztmpl' ) {
362 if ( $filename !~ m![^/]+\.tar\.[gx]z$! ) {
363 raise_param_exc
({ filename
=> "missing '.tar.gz' or '.tar.xz' extension" });
365 $path = PVE
:: Storage
:: get_vztmpl_dir
( $cfg, $param ->{ storage
});
367 raise_param_exc
({ content
=> "upload content type ' $content ' not allowed" });
370 die "storage ' $param ->{storage}' does not support ' $content ' content \n "
371 if ! $scfg ->{ content
}->{ $content };
373 my $dest = " $path/$filename " ;
374 my $dirname = dirname
( $dest );
376 # we simply overwrite when destination when file already exists
379 if ( $node ne 'localhost' && $node ne PVE
:: INotify
:: nodename
()) {
380 my $remip = PVE
:: Cluster
:: remote_node_ip
( $node );
382 my @ssh_options = ( '-o' , 'BatchMode=yes' );
384 my @remcmd = ( '/usr/bin/ssh' , @ssh_options, $remip, '--' );
387 # activate remote storage
388 PVE
:: Tools
:: run_command
([ @remcmd, '/usr/sbin/pvesm' , 'status' ,
389 '--storage' , $param ->{ storage
}]);
391 die "can't activate storage ' $param ->{storage}' on node ' $node ' \n " if $@ ;
393 PVE
:: Tools
:: run_command
([ @remcmd, '/bin/mkdir' , '-p' , '--' , PVE
:: Tools
:: shell_quote
( $dirname )],
394 errmsg
=> "mkdir failed" );
396 $cmd = [ '/usr/bin/scp' , @ssh_options, '--' , $tmpfilename, "[ $remip ]:" . PVE
:: Tools
:: shell_quote
( $dest )];
398 PVE
:: Storage
:: activate_storage
( $cfg, $param ->{ storage
});
399 File
:: Path
:: make_path
( $dirname );
400 $cmd = [ 'cp' , '--' , $tmpfilename, $dest ];
406 print "starting file import from: $tmpfilename\n " ;
407 print "target node: $node\n " ;
408 print "target file: $dest\n " ;
409 print "file size is: $size\n " ;
410 print "command: " . join ( ' ' , @$cmd ) . " \n " ;
412 eval { PVE
:: Tools
:: run_command
( $cmd, errmsg
=> 'import failed' ); };
417 print "finished file import successfully \n " ;
420 my $upid = $rpcenv -> fork_worker ( 'imgcopy' , undef , $user, $worker );
422 # apache removes the temporary file on return, so we need
423 # to wait here to make sure the worker process starts and
424 # opens the file before it gets removed.