]>
git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Storage/Content.pm
349231df9fc71d1b6e4eafb1259dfdb5fee4e124
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
=> "If whole backup is encrypted, value is the fingerprint or '1' "
97 . " if encrypted. Only useful for the Proxmox Backup Server storage type." ,
102 description
=> "Last backup verification result, only useful for PBS storages." ,
106 description
=> "Last backup verification state." ,
110 description
=> "Last backup verification UPID." ,
118 links
=> [ { rel
=> 'child' , href
=> "{volid}" } ],
123 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
125 my $authuser = $rpcenv -> get_user ();
127 my $storeid = $param ->{ storage
};
129 my $cfg = PVE
:: Storage
:: config
();
131 my $vollist = PVE
:: Storage
:: volume_list
( $cfg, $storeid, $param ->{ vmid
}, $param ->{ content
});
134 foreach my $item ( @$vollist ) {
135 eval { PVE
:: Storage
:: check_volume_access
( $rpcenv, $authuser, $cfg, undef , $item ->{ volid
}); };
137 $item ->{ vmid
} = int ( $item ->{ vmid
}) if ( defined ( $item ->{ vmid
}));
144 __PACKAGE__-
> register_method ({
148 description
=> "Allocate disk images." ,
150 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.AllocateSpace' ]],
155 additionalProperties
=> 0 ,
157 node
=> get_standard_option
( 'pve-node' ),
158 storage
=> get_standard_option
( 'pve-storage-id' , {
159 completion
=> \
& PVE
:: Storage
:: complete_storage_enabled
,
162 description
=> "The name of the file to create." ,
165 vmid
=> get_standard_option
( 'pve-vmid' , {
166 description
=> "Specify owner VM" ,
167 completion
=> \
& PVE
:: Cluster
:: complete_vmid
,
170 description
=> "Size in kilobyte (1024 bytes). Optional suffixes 'M' (megabyte, 1024K) and 'G' (gigabyte, 1024M)" ,
172 pattern
=> '\d+[MG]?' ,
176 enum
=> [ 'raw' , 'qcow2' , 'subvol' ],
183 description
=> "Volume identifier" ,
189 my $storeid = $param ->{ storage
};
190 my $name = $param ->{ filename
};
191 my $sizestr = $param ->{ size
};
194 if ( $sizestr =~ m/^\d+$/ ) {
196 } elsif ( $sizestr =~ m/^(\d+)M$/ ) {
198 } elsif ( $sizestr =~ m/^(\d+)G$/ ) {
199 $size = $1 * 1024 * 1024 ;
201 raise_param_exc
({ size
=> "unable to parse size ' $sizestr '" });
204 # extract FORMAT from name
205 if ( $name =~ m/\.(raw|qcow2|vmdk)$/ ) {
208 raise_param_exc
({ format
=> "different storage formats ( $param ->{format} != $fmt )" })
209 if $param ->{ format
} && $param ->{ format
} ne $fmt ;
211 $param ->{ format
} = $fmt ;
214 my $cfg = PVE
:: Storage
:: config
();
216 my $volid = PVE
:: Storage
:: vdisk_alloc
( $cfg, $storeid, $param ->{ vmid
},
223 # we allow to pass volume names (without storage prefix) if the storage
224 # is specified as separate parameter.
225 my $real_volume_id = sub {
226 my ( $storeid, $volume ) = @_ ;
230 if ( $volume =~ m/:/ ) {
232 my ( $sid, $volname ) = PVE
:: Storage
:: parse_volume_id
( $volume );
233 die "storage ID missmatch ( $sid != $storeid ) \n "
234 if $storeid && $sid ne $storeid ;
238 raise_param_exc
({ volume
=> $@ }) if $@ ;
241 raise_param_exc
({ volume
=> "no storage speficied - incomplete volume ID" })
244 $volid = " $storeid : $volume " ;
247 return wantarray ?
( $volid, $storeid ) : $volid ;
250 __PACKAGE__-
> register_method ({
254 description
=> "Get volume attributes" ,
256 description
=> "You need read access for the volume." ,
262 additionalProperties
=> 0 ,
264 node
=> get_standard_option
( 'pve-node' ),
265 storage
=> get_standard_option
( 'pve-storage-id' , { optional
=> 1 }),
267 description
=> "Volume identifier" ,
276 description
=> "The Path" ,
280 description
=> "Volume size in bytes." ,
285 description
=> "Used space. Please note that most storage plugins " .
286 "do not report anything useful here." ,
291 description
=> "Format identifier ('raw', 'qcow2', 'subvol', 'iso', 'tgz' ...)" ,
295 description
=> "Optional notes." ,
304 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
305 my $authuser = $rpcenv -> get_user ();
307 my ( $volid, $storeid ) = & $real_volume_id ( $param ->{ storage
}, $param ->{ volume
});
309 my $cfg = PVE
:: Storage
:: config
();
311 PVE
:: Storage
:: check_volume_access
( $rpcenv, $authuser, $cfg, undef , $volid );
313 my $path = PVE
:: Storage
:: path
( $cfg, $volid );
314 my ( $size, $format, $used, $parent ) = PVE
:: Storage
:: volume_size_info
( $cfg, $volid );
315 die "volume_size_info on ' $volid ' failed \n " if !( $format && $size );
324 # not all storages/types support notes, so ignore errors here
326 my $notes = PVE
:: Storage
:: get_volume_notes
( $cfg, $volid );
327 $entry ->{ notes
} = $notes if defined ( $notes );
333 __PACKAGE__-
> register_method ({
334 name
=> 'updateattributes' ,
337 description
=> "Update volume attributes" ,
339 description
=> "You need read access for the volume." ,
345 additionalProperties
=> 0 ,
347 node
=> get_standard_option
( 'pve-node' ),
348 storage
=> get_standard_option
( 'pve-storage-id' , { optional
=> 1 }),
350 description
=> "Volume identifier" ,
354 description
=> "The new notes." ,
360 returns
=> { type
=> 'null' },
364 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
365 my $authuser = $rpcenv -> get_user ();
367 my ( $volid, $storeid ) = & $real_volume_id ( $param ->{ storage
}, $param ->{ volume
});
369 my $cfg = PVE
:: Storage
:: config
();
371 PVE
:: Storage
:: check_volume_access
( $rpcenv, $authuser, $cfg, undef , $volid );
373 if ( my $notes = $param ->{ notes
}) {
374 PVE
:: Storage
:: update_volume_notes
( $cfg, $volid, $notes );
380 __PACKAGE__-
> register_method ({
384 description
=> "Delete volume" ,
386 description
=> "You need 'Datastore.Allocate' privilege on the storage (or 'Datastore.AllocateSpace' for backup volumes if you have VM.Backup privilege on the VM)." ,
392 additionalProperties
=> 0 ,
394 node
=> get_standard_option
( 'pve-node' ),
395 storage
=> get_standard_option
( 'pve-storage-id' , {
397 completion
=> \
& PVE
:: Storage
:: complete_storage
,
400 description
=> "Volume identifier" ,
402 completion
=> \
& PVE
:: Storage
:: complete_volume
,
406 description
=> "Time to wait for the task to finish. We return 'null' if the task finish within that time." ,
413 returns
=> { type
=> 'string' , optional
=> 1 , },
417 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
418 my $authuser = $rpcenv -> get_user ();
420 my $cfg = PVE
:: Storage
:: config
();
422 my ( $volid, $storeid ) = & $real_volume_id ( $param ->{ storage
}, $param ->{ volume
});
424 my ( $path, $ownervm, $vtype ) = PVE
:: Storage
:: path
( $cfg, $volid );
425 if ( $vtype eq 'backup' && $ownervm ) {
426 $rpcenv -> check ( $authuser, "/storage/ $storeid " , [ 'Datastore.AllocateSpace' ]);
427 $rpcenv -> check ( $authuser, "/vms/ $ownervm " , [ 'VM.Backup' ]);
429 $rpcenv -> check ( $authuser, "/storage/ $storeid " , [ 'Datastore.Allocate' ]);
433 PVE
:: Storage
:: vdisk_free
( $cfg, $volid );
434 print "Removed volume ' $volid ' \n " ;
435 if ( $vtype eq 'backup'
436 && $path =~ /(.*\/ vzdump-\w
+- \d
+- \d
{ 4 } _\d
{ 2 } _\d
{ 2 }- \d
{ 2 } _\d
{ 2 } _\d
{ 2 })[^ \
/]+$/ ) {
437 my $logpath = " $1 .log" ;
438 # try to cleanup our backup log file too, if still exisiting, #318
439 unlink ( $logpath ) if - e
$logpath ;
443 my $id = ( defined $ownervm ?
" $ownervm@ " : '' ) . $storeid ;
444 my $upid = $rpcenv -> fork_worker ( 'imgdel' , $id, $authuser, $worker );
445 my $background_delay = $param ->{ delay
};
446 if ( $background_delay ) {
447 my $end_time = time () + $background_delay ;
448 my $currently_deleting ; # not necessarily true, e.g. sequential api call from cli
450 my $task = PVE
:: Tools
:: upid_decode
( $upid );
451 $currently_deleting = PVE
:: ProcFSTools
:: check_process_running
( $task ->{ pid
}, $task ->{ pstart
});
452 sleep 1 if $currently_deleting ;
453 } while ( time () < $end_time && $currently_deleting );
455 if (! $currently_deleting ) {
456 my $status = PVE
:: Tools
:: upid_read_status
( $upid );
457 return undef if $status eq 'OK' ;
464 __PACKAGE__-
> register_method ({
468 description
=> "Copy a volume. This is experimental code - do not use." ,
472 additionalProperties
=> 0 ,
474 node
=> get_standard_option
( 'pve-node' ),
475 storage
=> get_standard_option
( 'pve-storage-id' , { optional
=> 1 }),
477 description
=> "Source volume identifier" ,
481 description
=> "Target volume identifier" ,
484 target_node
=> get_standard_option
( 'pve-node' , {
485 description
=> "Target node. Default is local node." ,
496 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
498 my $user = $rpcenv -> get_user ();
500 my $target_node = $param ->{ target_node
} || PVE
:: INotify
:: nodename
();
502 # cd /nodes/localhost/storage/local/content
503 # pve:/> create local:103/vm-103-disk-1.raw -target local:103/vm-103-disk-2.raw
504 # pve:/> create 103/vm-103-disk-1.raw -target 103/vm-103-disk-3.raw
506 my $src_volid = & $real_volume_id ( $param ->{ storage
}, $param ->{ volume
});
507 my $dst_volid = & $real_volume_id ( $param ->{ storage
}, $param ->{ target
});
509 print "DEBUG: COPY $src_volid TO $dst_volid\n " ;
511 my $cfg = PVE
:: Storage
:: config
();
513 # do all parameter checks first
515 # then do all short running task (to raise errors befor we go to background)
517 # then start the worker task
521 print "DEBUG: starting worker $upid\n " ;
523 my ( $target_sid, $target_volname ) = PVE
:: Storage
:: parse_volume_id
( $dst_volid );
524 #my $target_ip = PVE::Cluster::remote_node_ip($target_node);
526 # you need to get this working (fails currently, because storage_migrate() uses
527 # ssh to connect to local host (which is not needed
528 my $sshinfo = PVE
:: SSHInfo
:: get_ssh_info
( $target_node );
529 PVE
:: Storage
:: storage_migrate
( $cfg, $src_volid, $sshinfo, $target_sid, { 'target_volname' => $target_volname });
531 print "DEBUG: end worker $upid\n " ;
535 return $rpcenv -> fork_worker ( 'imgcopy' , undef , $user, $worker );