]>
git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Storage/Status.pm
fda54bb04e85b32e63f522e068109c553107581c
1 package PVE
:: API2
:: Storage
:: Status
;
10 use PVE
:: Cluster
qw(cfs_read_file) ;
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
45 description
=> "Only list status for specified storage" ,
49 description
=> "Only list stores which support this content type." ,
50 type
=> 'string' , format
=> 'pve-storage-content' ,
59 properties
=> { storage
=> { type
=> 'string' } },
61 links
=> [ { rel
=> 'child' , href
=> "{storage}" } ],
66 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
67 my $authuser = $rpcenv -> get_user ();
69 my $cfg = cfs_read_file
( "storage.cfg" );
71 my $info = PVE
:: Storage
:: storage_info
( $cfg, $param ->{ content
});
73 raise_param_exc
({ storage
=> "No such storage." })
74 if $param ->{ storage
} && ! defined ( $info ->{ $param ->{ storage
}});
77 my @sids = PVE
:: Storage
:: storage_ids
( $cfg );
78 foreach my $storeid ( @sids ) {
79 next if ! $info ->{ $storeid };
80 my $privs = [ 'Datastore.Audit' , 'Datastore.AllocateSpace' ];
81 next if ! $rpcenv -> check_any ( $authuser, "/storage/ $storeid " , $privs, 1 );
82 next if $param ->{ storage
} && $param ->{ storage
} ne $storeid ;
83 $res ->{ $storeid } = $info ->{ $storeid };
86 return PVE
:: RESTHandler
:: hash_to_array
( $res, 'storage' );
89 __PACKAGE__-
> register_method ({
95 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.Audit' , 'Datastore.AllocateSpace' ], any
=> 1 ],
98 additionalProperties
=> 0 ,
100 node
=> get_standard_option
( 'pve-node' ),
101 storage
=> get_standard_option
( 'pve-storage-id' ),
109 subdir
=> { type
=> 'string' },
112 links
=> [ { rel
=> 'child' , href
=> "{subdir}" } ],
118 { subdir
=> 'status' },
119 { subdir
=> 'content' },
120 { subdir
=> 'upload' },
122 { subdir
=> 'rrddata' },
128 __PACKAGE__-
> register_method ({
129 name
=> 'read_status' ,
130 path
=> '{storage}/status' ,
132 description
=> "Read storage status." ,
134 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.Audit' , 'Datastore.AllocateSpace' ], any
=> 1 ],
139 additionalProperties
=> 0 ,
141 node
=> get_standard_option
( 'pve-node' ),
142 storage
=> get_standard_option
( 'pve-storage-id' ),
152 my $cfg = cfs_read_file
( "storage.cfg" );
154 my $info = PVE
:: Storage
:: storage_info
( $cfg, $param ->{ content
});
156 my $data = $info ->{ $param ->{ storage
}};
158 raise_param_exc
({ storage
=> "No such storage." })
164 __PACKAGE__-
> register_method ({
166 path
=> '{storage}/rrd' ,
168 description
=> "Read storage RRD statistics (returns PNG)." ,
170 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.Audit' , 'Datastore.AllocateSpace' ], any
=> 1 ],
175 additionalProperties
=> 0 ,
177 node
=> get_standard_option
( 'pve-node' ),
178 storage
=> get_standard_option
( 'pve-storage-id' ),
180 description
=> "Specify the time frame you are interested in." ,
182 enum
=> [ 'hour' , 'day' , 'week' , 'month' , 'year' ],
185 description
=> "The list of datasources you want to display." ,
186 type
=> 'string' , format
=> 'pve-configid-list' ,
189 description
=> "The RRD consolidation function" ,
191 enum
=> [ 'AVERAGE' , 'MAX' ],
199 filename
=> { type
=> 'string' },
205 return PVE
:: Cluster
:: create_rrd_graph
(
206 "pve2-storage/ $param ->{node}/ $param ->{storage}" ,
207 $param ->{ timeframe
}, $param ->{ ds
}, $param ->{ cf
});
211 __PACKAGE__-
> register_method ({
213 path
=> '{storage}/rrddata' ,
215 description
=> "Read storage RRD statistics." ,
217 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.Audit' , 'Datastore.AllocateSpace' ], any
=> 1 ],
222 additionalProperties
=> 0 ,
224 node
=> get_standard_option
( 'pve-node' ),
225 storage
=> get_standard_option
( 'pve-storage-id' ),
227 description
=> "Specify the time frame you are interested in." ,
229 enum
=> [ 'hour' , 'day' , 'week' , 'month' , 'year' ],
232 description
=> "The RRD consolidation function" ,
234 enum
=> [ 'AVERAGE' , 'MAX' ],
249 return PVE
:: Cluster
:: create_rrd_data
(
250 "pve2-storage/ $param ->{node}/ $param ->{storage}" ,
251 $param ->{ timeframe
}, $param ->{ cf
});
254 # makes no sense for big images and backup files (because it
255 # create a copy of the file).
256 __PACKAGE__-
> register_method ({
258 path
=> '{storage}/upload' ,
260 description
=> "Upload templates and ISO images." ,
262 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.AllocateTemplate' ]],
266 additionalProperties
=> 0 ,
268 node
=> get_standard_option
( 'pve-node' ),
269 storage
=> get_standard_option
( 'pve-storage-id' ),
271 description
=> "Content type." ,
272 type
=> 'string' , format
=> 'pve-storage-content' ,
275 description
=> "The name of the file to create." ,
279 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." ,
285 returns
=> { type
=> "string" },
289 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
291 my $user = $rpcenv -> get_user ();
293 my $cfg = cfs_read_file
( "storage.cfg" );
295 my $node = $param ->{ node
};
296 my $scfg = PVE
:: Storage
:: storage_check_enabled
( $cfg, $param ->{ storage
}, $node );
298 die "cant upload to storage type ' $scfg ->{type}' \n "
299 if !( $scfg ->{ type
} eq 'dir' || $scfg ->{ type
} eq 'nfs' );
301 my $content = $param ->{ content
};
303 my $tmpfilename = $param ->{ tmpfilename
};
304 die "missing temporary file name \n " if ! $tmpfilename ;
306 my $size = - s
$tmpfilename ;
307 die "temporary file ' $tmpfilename ' does not exists \n " if ! defined ( $size );
309 my $filename = $param ->{ filename
};
312 $filename =~ s/^.*[\/\\]/ /;
313 $filename =~ s/\s/_/g ;
317 if ( $content eq 'iso' ) {
318 if ( $filename !~ m![^/]+\.[Ii][Ss][Oo]$! ) {
319 raise_param_exc
({ filename
=> "missing '.iso' extension" });
321 $path = PVE
:: Storage
:: get_iso_dir
( $cfg, $param ->{ storage
});
322 } elsif ( $content eq 'vztmpl' ) {
323 if ( $filename !~ m![^/]+\.tar\.gz$! ) {
324 raise_param_exc
({ filename
=> "missing '.tar.gz' extension" });
326 $path = PVE
:: Storage
:: get_vztmpl_dir
( $cfg, $param ->{ storage
});
328 raise_param_exc
({ content
=> "upload content type ' $content ' not allowed" });
331 die "storage ' $param ->{storage}' does not support ' $content ' content \n "
332 if ! $scfg ->{ content
}->{ $content };
334 my $dest = " $path/$filename " ;
335 my $dirname = dirname
( $dest );
337 # we simply overwrite when destination when file already exists
340 if ( $node ne 'localhost' && $node ne PVE
:: INotify
:: nodename
()) {
341 my $remip = PVE
:: Cluster
:: remote_node_ip
( $node );
343 my @ssh_options = ( '-o' , 'BatchMode=yes' );
345 my @remcmd = ( '/usr/bin/ssh' , @ssh_options, $remip );
348 # activate remote storage
349 PVE
:: Tools
:: run_command
([ @remcmd, '/usr/sbin/pvesm' , 'status' ,
350 '--storage' , $param ->{ storage
}]);
352 die "can't activate storage ' $param ->{storage}' on node ' $node ' \n " if $@ ;
354 PVE
:: Tools
:: run_command
([ @remcmd, '/bin/mkdir' , '-p' , $dirname ],
355 errmsg
=> "mkdir failed" );
357 $cmd = [ '/usr/bin/scp' , @ssh_options, $tmpfilename, " $remip : $dest " ];
359 PVE
:: Storage
:: activate_storage
( $cfg, $param ->{ storage
});
360 File
:: Path
:: make_path
( $dirname );
361 $cmd = [ 'cp' , $tmpfilename, $dest ];
367 print "starting file import from: $tmpfilename\n " ;
368 print "target node: $node\n " ;
369 print "target file: $dest\n " ;
370 print "file size is: $size\n " ;
371 print "command: " . join ( ' ' , @$cmd ) . " \n " ;
373 eval { PVE
:: Tools
:: run_command
( $cmd, errmsg
=> 'import failed' ); };
378 print "finished file import successfully \n " ;
381 return $rpcenv -> fork_worker ( 'imgcopy' , undef , $user, $worker );