]>
git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Storage/Content.pm
ce89ec5dccdc595a4618f816079ff3b210483249
1 package PVE
:: API2
:: Storage
:: Content
;
11 use PVE
:: Exception
qw(raise_param_exc) ;
12 use PVE
:: RPCEnvironment
;
14 use PVE
:: JSONSchema
qw(get_standard_option) ;
17 use base
qw(PVE::RESTHandler) ;
19 __PACKAGE__-
> register_method ({
23 description
=> "List storage content." ,
25 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.Audit' , 'Datastore.AllocateSpace' ], any
=> 1 ],
30 additionalProperties
=> 0 ,
32 node
=> get_standard_option
( 'pve-node' ),
33 storage
=> get_standard_option
( 'pve-storage-id' , {
34 completion
=> \
& PVE
:: Storage
:: complete_storage_enabled
,
37 description
=> "Only list content of this type." ,
38 type
=> 'string' , format
=> 'pve-storage-content' ,
40 completion
=> \
& PVE
:: Storage
:: complete_content_type
,
42 vmid
=> get_standard_option
( 'pve-vmid' , {
43 description
=> "Only list images for this VM" ,
45 completion
=> \
& PVE
:: Cluster
:: complete_vmid
,
55 description
=> "Volume identifier." ,
59 description
=> "Associated Owner VMID." ,
64 description
=> "Volume identifier of parent (for linked cloned)." ,
69 description
=> "Format identifier ('raw', 'qcow2', 'subvol', 'iso', 'tgz' ...)" ,
73 description
=> "Volume size in bytes." ,
78 description
=> "Used space. Please note that most storage plugins " .
79 "does not report anything useful here." ,
86 links
=> [ { rel
=> 'child' , href
=> "{volid}" } ],
91 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
93 my $authuser = $rpcenv -> get_user ();
95 my $storeid = $param ->{ storage
};
97 my $cfg = PVE
:: Storage
:: config
();
99 my $vollist = PVE
:: Storage
:: volume_list
( $cfg, $storeid, $param ->{ vmid
}, $param ->{ content
});
102 foreach my $item ( @$vollist ) {
103 eval { PVE
:: Storage
:: check_volume_access
( $rpcenv, $authuser, $cfg, undef , $item ->{ volid
}); };
105 $item ->{ vmid
} = int ( $item ->{ vmid
}) if ( defined ( $item ->{ vmid
}));
112 __PACKAGE__-
> register_method ({
116 description
=> "Allocate disk images." ,
118 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.AllocateSpace' ]],
123 additionalProperties
=> 0 ,
125 node
=> get_standard_option
( 'pve-node' ),
126 storage
=> get_standard_option
( 'pve-storage-id' , {
127 completion
=> \
& PVE
:: Storage
:: complete_storage_enabled
,
130 description
=> "The name of the file to create." ,
133 vmid
=> get_standard_option
( 'pve-vmid' , {
134 description
=> "Specify owner VM" ,
135 completion
=> \
& PVE
:: Cluster
:: complete_vmid
,
138 description
=> "Size in kilobyte (1024 bytes). Optional suffixes 'M' (megabyte, 1024K) and 'G' (gigabyte, 1024M)" ,
140 pattern
=> '\d+[MG]?' ,
144 enum
=> [ 'raw' , 'qcow2' , 'subvol' ],
151 description
=> "Volume identifier" ,
157 my $storeid = $param ->{ storage
};
158 my $name = $param ->{ filename
};
159 my $sizestr = $param ->{ size
};
162 if ( $sizestr =~ m/^\d+$/ ) {
164 } elsif ( $sizestr =~ m/^(\d+)M$/ ) {
166 } elsif ( $sizestr =~ m/^(\d+)G$/ ) {
167 $size = $1 * 1024 * 1024 ;
169 raise_param_exc
({ size
=> "unable to parse size ' $sizestr '" });
172 # extract FORMAT from name
173 if ( $name =~ m/\.(raw|qcow2|vmdk)$/ ) {
176 raise_param_exc
({ format
=> "different storage formats ( $param ->{format} != $fmt )" })
177 if $param ->{ format
} && $param ->{ format
} ne $fmt ;
179 $param ->{ format
} = $fmt ;
182 my $cfg = PVE
:: Storage
:: config
();
184 my $volid = PVE
:: Storage
:: vdisk_alloc
( $cfg, $storeid, $param ->{ vmid
},
191 # we allow to pass volume names (without storage prefix) if the storage
192 # is specified as separate parameter.
193 my $real_volume_id = sub {
194 my ( $storeid, $volume ) = @_ ;
198 if ( $volume =~ m/:/ ) {
200 my ( $sid, $volname ) = PVE
:: Storage
:: parse_volume_id
( $volume );
201 die "storage ID missmatch ( $sid != $storeid ) \n "
202 if $storeid && $sid ne $storeid ;
206 raise_param_exc
({ volume
=> $@ }) if $@ ;
209 raise_param_exc
({ volume
=> "no storage speficied - incomplete volume ID" })
212 $volid = " $storeid : $volume " ;
215 return wantarray ?
( $volid, $storeid ) : $volid ;
218 __PACKAGE__-
> register_method ({
222 description
=> "Get volume attributes" ,
224 description
=> "You need read access for the volume." ,
230 additionalProperties
=> 0 ,
232 node
=> get_standard_option
( 'pve-node' ),
233 storage
=> get_standard_option
( 'pve-storage-id' , { optional
=> 1 }),
235 description
=> "Volume identifier" ,
240 returns
=> { type
=> 'object' },
244 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
245 my $authuser = $rpcenv -> get_user ();
247 my ( $volid, $storeid ) = & $real_volume_id ( $param ->{ storage
}, $param ->{ volume
});
249 my $cfg = PVE
:: Storage
:: config
();
251 PVE
:: Storage
:: check_volume_access
( $rpcenv, $authuser, $cfg, undef , $volid );
253 my $path = PVE
:: Storage
:: path
( $cfg, $volid );
254 my ( $size, $format, $used, $parent ) = PVE
:: Storage
:: file_size_info
( $path );
255 die "file_size_info on ' $volid ' failed \n " if !( $format && $size );
257 # fixme: return more attributes?
266 __PACKAGE__-
> register_method ({
270 description
=> "Delete volume" ,
272 description
=> "You need 'Datastore.Allocate' privilege on the storage (or 'Datastore.AllocateSpace' for backup volumes if you have VM.Backup privilege on the VM)." ,
278 additionalProperties
=> 0 ,
280 node
=> get_standard_option
( 'pve-node' ),
281 storage
=> get_standard_option
( 'pve-storage-id' , {
283 completion
=> \
& PVE
:: Storage
:: complete_storage
,
286 description
=> "Volume identifier" ,
288 completion
=> \
& PVE
:: Storage
:: complete_volume
,
292 description
=> "Time to wait for the task to finish. We return 'null' if the task finish within that time." ,
299 returns
=> { type
=> 'string' , optional
=> 1 , },
303 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
304 my $authuser = $rpcenv -> get_user ();
306 my $cfg = PVE
:: Storage
:: config
();
308 my ( $volid, $storeid ) = & $real_volume_id ( $param ->{ storage
}, $param ->{ volume
});
310 my ( $path, $ownervm, $vtype ) = PVE
:: Storage
:: path
( $cfg, $volid );
311 if ( $vtype eq 'backup' && $ownervm ) {
312 $rpcenv -> check ( $authuser, "/storage/ $storeid " , [ 'Datastore.AllocateSpace' ]);
313 $rpcenv -> check ( $authuser, "/vms/ $ownervm " , [ 'VM.Backup' ]);
315 $rpcenv -> check ( $authuser, "/storage/ $storeid " , [ 'Datastore.Allocate' ]);
319 PVE
:: Storage
:: vdisk_free
( $cfg, $volid );
320 print "Removed volume ' $volid ' \n " ;
321 if ( $vtype eq 'backup'
322 && $path =~ /(.*\/ vzdump-\w
+- \d
+- \d
{ 4 } _\d
{ 2 } _\d
{ 2 }- \d
{ 2 } _\d
{ 2 } _\d
{ 2 })[^ \
/]+$/ ) {
323 my $logpath = " $1 .log" ;
324 # try to cleanup our backup log file too, if still exisiting, #318
325 unlink ( $logpath ) if - e
$logpath ;
329 my $id = ( defined $ownervm ?
" $ownervm@ " : '' ) . $storeid ;
330 my $upid = $rpcenv -> fork_worker ( 'imgdel' , $id, $authuser, $worker );
331 my $background_delay = $param ->{ delay
};
332 if ( $background_delay ) {
333 my $end_time = time () + $background_delay ;
334 my $currently_deleting ; # not necessarily true, e.g. sequential api call from cli
336 my $task = PVE
:: Tools
:: upid_decode
( $upid );
337 $currently_deleting = PVE
:: ProcFSTools
:: check_process_running
( $task ->{ pid
}, $task ->{ pstart
});
338 sleep 1 if $currently_deleting ;
339 } while ( time () < $end_time && $currently_deleting );
341 if (! $currently_deleting ) {
342 my $status = PVE
:: Tools
:: upid_read_status
( $upid );
343 return undef if $status eq 'OK' ;
350 __PACKAGE__-
> register_method ({
354 description
=> "Copy a volume. This is experimental code - do not use." ,
358 additionalProperties
=> 0 ,
360 node
=> get_standard_option
( 'pve-node' ),
361 storage
=> get_standard_option
( 'pve-storage-id' , { optional
=> 1 }),
363 description
=> "Source volume identifier" ,
367 description
=> "Target volume identifier" ,
370 target_node
=> get_standard_option
( 'pve-node' , {
371 description
=> "Target node. Default is local node." ,
382 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
384 my $user = $rpcenv -> get_user ();
386 my $target_node = $param ->{ target_node
} || PVE
:: INotify
:: nodename
();
388 # cd /nodes/localhost/storage/local/content
389 # pve:/> create local:103/vm-103-disk-1.raw -target local:103/vm-103-disk-2.raw
390 # pve:/> create 103/vm-103-disk-1.raw -target 103/vm-103-disk-3.raw
392 my $src_volid = & $real_volume_id ( $param ->{ storage
}, $param ->{ volume
});
393 my $dst_volid = & $real_volume_id ( $param ->{ storage
}, $param ->{ target
});
395 print "DEBUG: COPY $src_volid TO $dst_volid\n " ;
397 my $cfg = PVE
:: Storage
:: config
();
399 # do all parameter checks first
401 # then do all short running task (to raise errors befor we go to background)
403 # then start the worker task
407 print "DEBUG: starting worker $upid\n " ;
409 my ( $target_sid, $target_volname ) = PVE
:: Storage
:: parse_volume_id
( $dst_volid );
410 #my $target_ip = PVE::Cluster::remote_node_ip($target_node);
412 # you need to get this working (fails currently, because storage_migrate() uses
413 # ssh to connect to local host (which is not needed
414 my $sshinfo = PVE
:: SSHInfo
:: get_ssh_info
( $target_node );
415 PVE
:: Storage
:: storage_migrate
( $cfg, $src_volid, $sshinfo, $target_sid, $target_volname );
417 print "DEBUG: end worker $upid\n " ;
421 return $rpcenv -> fork_worker ( 'imgcopy' , undef , $user, $worker );