]>
git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Storage/Content.pm
d4e23926b9dc8f1d4333173132551e3770c6d446
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 "do not report anything useful here." ,
85 description
=> "Creation time (seconds since the UNIX Epoch)." ,
91 description
=> "Optional notes. If they contain multiple lines, only the first one is returned here." ,
96 description
=> "Last backup verification result, only useful for PBS storages." ,
100 description
=> "Last backup verification state." ,
104 description
=> "Last backup verification UPID." ,
112 links
=> [ { rel
=> 'child' , href
=> "{volid}" } ],
117 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
119 my $authuser = $rpcenv -> get_user ();
121 my $storeid = $param ->{ storage
};
123 my $cfg = PVE
:: Storage
:: config
();
125 my $vollist = PVE
:: Storage
:: volume_list
( $cfg, $storeid, $param ->{ vmid
}, $param ->{ content
});
128 foreach my $item ( @$vollist ) {
129 eval { PVE
:: Storage
:: check_volume_access
( $rpcenv, $authuser, $cfg, undef , $item ->{ volid
}); };
131 $item ->{ vmid
} = int ( $item ->{ vmid
}) if ( defined ( $item ->{ vmid
}));
138 __PACKAGE__-
> register_method ({
142 description
=> "Allocate disk images." ,
144 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.AllocateSpace' ]],
149 additionalProperties
=> 0 ,
151 node
=> get_standard_option
( 'pve-node' ),
152 storage
=> get_standard_option
( 'pve-storage-id' , {
153 completion
=> \
& PVE
:: Storage
:: complete_storage_enabled
,
156 description
=> "The name of the file to create." ,
159 vmid
=> get_standard_option
( 'pve-vmid' , {
160 description
=> "Specify owner VM" ,
161 completion
=> \
& PVE
:: Cluster
:: complete_vmid
,
164 description
=> "Size in kilobyte (1024 bytes). Optional suffixes 'M' (megabyte, 1024K) and 'G' (gigabyte, 1024M)" ,
166 pattern
=> '\d+[MG]?' ,
170 enum
=> [ 'raw' , 'qcow2' , 'subvol' ],
177 description
=> "Volume identifier" ,
183 my $storeid = $param ->{ storage
};
184 my $name = $param ->{ filename
};
185 my $sizestr = $param ->{ size
};
188 if ( $sizestr =~ m/^\d+$/ ) {
190 } elsif ( $sizestr =~ m/^(\d+)M$/ ) {
192 } elsif ( $sizestr =~ m/^(\d+)G$/ ) {
193 $size = $1 * 1024 * 1024 ;
195 raise_param_exc
({ size
=> "unable to parse size ' $sizestr '" });
198 # extract FORMAT from name
199 if ( $name =~ m/\.(raw|qcow2|vmdk)$/ ) {
202 raise_param_exc
({ format
=> "different storage formats ( $param ->{format} != $fmt )" })
203 if $param ->{ format
} && $param ->{ format
} ne $fmt ;
205 $param ->{ format
} = $fmt ;
208 my $cfg = PVE
:: Storage
:: config
();
210 my $volid = PVE
:: Storage
:: vdisk_alloc
( $cfg, $storeid, $param ->{ vmid
},
217 # we allow to pass volume names (without storage prefix) if the storage
218 # is specified as separate parameter.
219 my $real_volume_id = sub {
220 my ( $storeid, $volume ) = @_ ;
224 if ( $volume =~ m/:/ ) {
226 my ( $sid, $volname ) = PVE
:: Storage
:: parse_volume_id
( $volume );
227 die "storage ID missmatch ( $sid != $storeid ) \n "
228 if $storeid && $sid ne $storeid ;
232 raise_param_exc
({ volume
=> $@ }) if $@ ;
235 raise_param_exc
({ volume
=> "no storage speficied - incomplete volume ID" })
238 $volid = " $storeid : $volume " ;
241 return wantarray ?
( $volid, $storeid ) : $volid ;
244 __PACKAGE__-
> register_method ({
248 description
=> "Get volume attributes" ,
250 description
=> "You need read access for the volume." ,
256 additionalProperties
=> 0 ,
258 node
=> get_standard_option
( 'pve-node' ),
259 storage
=> get_standard_option
( 'pve-storage-id' , { optional
=> 1 }),
261 description
=> "Volume identifier" ,
266 returns
=> { type
=> 'object' },
270 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
271 my $authuser = $rpcenv -> get_user ();
273 my ( $volid, $storeid ) = & $real_volume_id ( $param ->{ storage
}, $param ->{ volume
});
275 my $cfg = PVE
:: Storage
:: config
();
277 PVE
:: Storage
:: check_volume_access
( $rpcenv, $authuser, $cfg, undef , $volid );
279 my $path = PVE
:: Storage
:: path
( $cfg, $volid );
280 my ( $size, $format, $used, $parent ) = PVE
:: Storage
:: file_size_info
( $path );
281 die "file_size_info on ' $volid ' failed \n " if !( $format && $size );
283 # fixme: return more attributes?
292 __PACKAGE__-
> register_method ({
296 description
=> "Delete volume" ,
298 description
=> "You need 'Datastore.Allocate' privilege on the storage (or 'Datastore.AllocateSpace' for backup volumes if you have VM.Backup privilege on the VM)." ,
304 additionalProperties
=> 0 ,
306 node
=> get_standard_option
( 'pve-node' ),
307 storage
=> get_standard_option
( 'pve-storage-id' , {
309 completion
=> \
& PVE
:: Storage
:: complete_storage
,
312 description
=> "Volume identifier" ,
314 completion
=> \
& PVE
:: Storage
:: complete_volume
,
318 description
=> "Time to wait for the task to finish. We return 'null' if the task finish within that time." ,
325 returns
=> { type
=> 'string' , optional
=> 1 , },
329 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
330 my $authuser = $rpcenv -> get_user ();
332 my $cfg = PVE
:: Storage
:: config
();
334 my ( $volid, $storeid ) = & $real_volume_id ( $param ->{ storage
}, $param ->{ volume
});
336 my ( $path, $ownervm, $vtype ) = PVE
:: Storage
:: path
( $cfg, $volid );
337 if ( $vtype eq 'backup' && $ownervm ) {
338 $rpcenv -> check ( $authuser, "/storage/ $storeid " , [ 'Datastore.AllocateSpace' ]);
339 $rpcenv -> check ( $authuser, "/vms/ $ownervm " , [ 'VM.Backup' ]);
341 $rpcenv -> check ( $authuser, "/storage/ $storeid " , [ 'Datastore.Allocate' ]);
345 PVE
:: Storage
:: vdisk_free
( $cfg, $volid );
346 print "Removed volume ' $volid ' \n " ;
347 if ( $vtype eq 'backup'
348 && $path =~ /(.*\/ vzdump-\w
+- \d
+- \d
{ 4 } _\d
{ 2 } _\d
{ 2 }- \d
{ 2 } _\d
{ 2 } _\d
{ 2 })[^ \
/]+$/ ) {
349 my $logpath = " $1 .log" ;
350 # try to cleanup our backup log file too, if still exisiting, #318
351 unlink ( $logpath ) if - e
$logpath ;
355 my $id = ( defined $ownervm ?
" $ownervm@ " : '' ) . $storeid ;
356 my $upid = $rpcenv -> fork_worker ( 'imgdel' , $id, $authuser, $worker );
357 my $background_delay = $param ->{ delay
};
358 if ( $background_delay ) {
359 my $end_time = time () + $background_delay ;
360 my $currently_deleting ; # not necessarily true, e.g. sequential api call from cli
362 my $task = PVE
:: Tools
:: upid_decode
( $upid );
363 $currently_deleting = PVE
:: ProcFSTools
:: check_process_running
( $task ->{ pid
}, $task ->{ pstart
});
364 sleep 1 if $currently_deleting ;
365 } while ( time () < $end_time && $currently_deleting );
367 if (! $currently_deleting ) {
368 my $status = PVE
:: Tools
:: upid_read_status
( $upid );
369 return undef if $status eq 'OK' ;
376 __PACKAGE__-
> register_method ({
380 description
=> "Copy a volume. This is experimental code - do not use." ,
384 additionalProperties
=> 0 ,
386 node
=> get_standard_option
( 'pve-node' ),
387 storage
=> get_standard_option
( 'pve-storage-id' , { optional
=> 1 }),
389 description
=> "Source volume identifier" ,
393 description
=> "Target volume identifier" ,
396 target_node
=> get_standard_option
( 'pve-node' , {
397 description
=> "Target node. Default is local node." ,
408 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
410 my $user = $rpcenv -> get_user ();
412 my $target_node = $param ->{ target_node
} || PVE
:: INotify
:: nodename
();
414 # cd /nodes/localhost/storage/local/content
415 # pve:/> create local:103/vm-103-disk-1.raw -target local:103/vm-103-disk-2.raw
416 # pve:/> create 103/vm-103-disk-1.raw -target 103/vm-103-disk-3.raw
418 my $src_volid = & $real_volume_id ( $param ->{ storage
}, $param ->{ volume
});
419 my $dst_volid = & $real_volume_id ( $param ->{ storage
}, $param ->{ target
});
421 print "DEBUG: COPY $src_volid TO $dst_volid\n " ;
423 my $cfg = PVE
:: Storage
:: config
();
425 # do all parameter checks first
427 # then do all short running task (to raise errors befor we go to background)
429 # then start the worker task
433 print "DEBUG: starting worker $upid\n " ;
435 my ( $target_sid, $target_volname ) = PVE
:: Storage
:: parse_volume_id
( $dst_volid );
436 #my $target_ip = PVE::Cluster::remote_node_ip($target_node);
438 # you need to get this working (fails currently, because storage_migrate() uses
439 # ssh to connect to local host (which is not needed
440 my $sshinfo = PVE
:: SSHInfo
:: get_ssh_info
( $target_node );
441 PVE
:: Storage
:: storage_migrate
( $cfg, $src_volid, $sshinfo, $target_sid, { 'target_volname' => $target_volname });
443 print "DEBUG: end worker $upid\n " ;
447 return $rpcenv -> fork_worker ( 'imgcopy' , undef , $user, $worker );