]>
git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Storage/Content.pm
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." ,
85 description
=> "Creation time (Unix epoch). Currently only set for backup volumes." ,
92 links
=> [ { rel
=> 'child' , href
=> "{volid}" } ],
97 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
99 my $authuser = $rpcenv -> get_user ();
101 my $storeid = $param ->{ storage
};
103 my $cfg = PVE
:: Storage
:: config
();
105 my $vollist = PVE
:: Storage
:: volume_list
( $cfg, $storeid, $param ->{ vmid
}, $param ->{ content
});
108 foreach my $item ( @$vollist ) {
109 eval { PVE
:: Storage
:: check_volume_access
( $rpcenv, $authuser, $cfg, undef , $item ->{ volid
}); };
111 $item ->{ vmid
} = int ( $item ->{ vmid
}) if ( defined ( $item ->{ vmid
}));
118 __PACKAGE__-
> register_method ({
122 description
=> "Allocate disk images." ,
124 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.AllocateSpace' ]],
129 additionalProperties
=> 0 ,
131 node
=> get_standard_option
( 'pve-node' ),
132 storage
=> get_standard_option
( 'pve-storage-id' , {
133 completion
=> \
& PVE
:: Storage
:: complete_storage_enabled
,
136 description
=> "The name of the file to create." ,
139 vmid
=> get_standard_option
( 'pve-vmid' , {
140 description
=> "Specify owner VM" ,
141 completion
=> \
& PVE
:: Cluster
:: complete_vmid
,
144 description
=> "Size in kilobyte (1024 bytes). Optional suffixes 'M' (megabyte, 1024K) and 'G' (gigabyte, 1024M)" ,
146 pattern
=> '\d+[MG]?' ,
150 enum
=> [ 'raw' , 'qcow2' , 'subvol' ],
157 description
=> "Volume identifier" ,
163 my $storeid = $param ->{ storage
};
164 my $name = $param ->{ filename
};
165 my $sizestr = $param ->{ size
};
168 if ( $sizestr =~ m/^\d+$/ ) {
170 } elsif ( $sizestr =~ m/^(\d+)M$/ ) {
172 } elsif ( $sizestr =~ m/^(\d+)G$/ ) {
173 $size = $1 * 1024 * 1024 ;
175 raise_param_exc
({ size
=> "unable to parse size ' $sizestr '" });
178 # extract FORMAT from name
179 if ( $name =~ m/\.(raw|qcow2|vmdk)$/ ) {
182 raise_param_exc
({ format
=> "different storage formats ( $param ->{format} != $fmt )" })
183 if $param ->{ format
} && $param ->{ format
} ne $fmt ;
185 $param ->{ format
} = $fmt ;
188 my $cfg = PVE
:: Storage
:: config
();
190 my $volid = PVE
:: Storage
:: vdisk_alloc
( $cfg, $storeid, $param ->{ vmid
},
197 # we allow to pass volume names (without storage prefix) if the storage
198 # is specified as separate parameter.
199 my $real_volume_id = sub {
200 my ( $storeid, $volume ) = @_ ;
204 if ( $volume =~ m/:/ ) {
206 my ( $sid, $volname ) = PVE
:: Storage
:: parse_volume_id
( $volume );
207 die "storage ID missmatch ( $sid != $storeid ) \n "
208 if $storeid && $sid ne $storeid ;
212 raise_param_exc
({ volume
=> $@ }) if $@ ;
215 raise_param_exc
({ volume
=> "no storage speficied - incomplete volume ID" })
218 $volid = " $storeid : $volume " ;
221 return wantarray ?
( $volid, $storeid ) : $volid ;
224 __PACKAGE__-
> register_method ({
228 description
=> "Get volume attributes" ,
230 description
=> "You need read access for the volume." ,
236 additionalProperties
=> 0 ,
238 node
=> get_standard_option
( 'pve-node' ),
239 storage
=> get_standard_option
( 'pve-storage-id' , { optional
=> 1 }),
241 description
=> "Volume identifier" ,
246 returns
=> { type
=> 'object' },
250 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
251 my $authuser = $rpcenv -> get_user ();
253 my ( $volid, $storeid ) = & $real_volume_id ( $param ->{ storage
}, $param ->{ volume
});
255 my $cfg = PVE
:: Storage
:: config
();
257 PVE
:: Storage
:: check_volume_access
( $rpcenv, $authuser, $cfg, undef , $volid );
259 my $path = PVE
:: Storage
:: path
( $cfg, $volid );
260 my ( $size, $format, $used, $parent ) = PVE
:: Storage
:: file_size_info
( $path );
261 die "file_size_info on ' $volid ' failed \n " if !( $format && $size );
263 # fixme: return more attributes?
272 __PACKAGE__-
> register_method ({
276 description
=> "Delete volume" ,
278 description
=> "You need 'Datastore.Allocate' privilege on the storage (or 'Datastore.AllocateSpace' for backup volumes if you have VM.Backup privilege on the VM)." ,
284 additionalProperties
=> 0 ,
286 node
=> get_standard_option
( 'pve-node' ),
287 storage
=> get_standard_option
( 'pve-storage-id' , {
289 completion
=> \
& PVE
:: Storage
:: complete_storage
,
292 description
=> "Volume identifier" ,
294 completion
=> \
& PVE
:: Storage
:: complete_volume
,
298 description
=> "Time to wait for the task to finish. We return 'null' if the task finish within that time." ,
305 returns
=> { type
=> 'string' , optional
=> 1 , },
309 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
310 my $authuser = $rpcenv -> get_user ();
312 my $cfg = PVE
:: Storage
:: config
();
314 my ( $volid, $storeid ) = & $real_volume_id ( $param ->{ storage
}, $param ->{ volume
});
316 my ( $path, $ownervm, $vtype ) = PVE
:: Storage
:: path
( $cfg, $volid );
317 if ( $vtype eq 'backup' && $ownervm ) {
318 $rpcenv -> check ( $authuser, "/storage/ $storeid " , [ 'Datastore.AllocateSpace' ]);
319 $rpcenv -> check ( $authuser, "/vms/ $ownervm " , [ 'VM.Backup' ]);
321 $rpcenv -> check ( $authuser, "/storage/ $storeid " , [ 'Datastore.Allocate' ]);
325 PVE
:: Storage
:: vdisk_free
( $cfg, $volid );
326 print "Removed volume ' $volid ' \n " ;
327 if ( $vtype eq 'backup'
328 && $path =~ /(.*\/ vzdump-\w
+- \d
+- \d
{ 4 } _\d
{ 2 } _\d
{ 2 }- \d
{ 2 } _\d
{ 2 } _\d
{ 2 })[^ \
/]+$/ ) {
329 my $logpath = " $1 .log" ;
330 # try to cleanup our backup log file too, if still exisiting, #318
331 unlink ( $logpath ) if - e
$logpath ;
335 my $id = ( defined $ownervm ?
" $ownervm@ " : '' ) . $storeid ;
336 my $upid = $rpcenv -> fork_worker ( 'imgdel' , $id, $authuser, $worker );
337 my $background_delay = $param ->{ delay
};
338 if ( $background_delay ) {
339 my $end_time = time () + $background_delay ;
340 my $currently_deleting ; # not necessarily true, e.g. sequential api call from cli
342 my $task = PVE
:: Tools
:: upid_decode
( $upid );
343 $currently_deleting = PVE
:: ProcFSTools
:: check_process_running
( $task ->{ pid
}, $task ->{ pstart
});
344 sleep 1 if $currently_deleting ;
345 } while ( time () < $end_time && $currently_deleting );
347 if (! $currently_deleting ) {
348 my $status = PVE
:: Tools
:: upid_read_status
( $upid );
349 return undef if $status eq 'OK' ;
356 __PACKAGE__-
> register_method ({
360 description
=> "Copy a volume. This is experimental code - do not use." ,
364 additionalProperties
=> 0 ,
366 node
=> get_standard_option
( 'pve-node' ),
367 storage
=> get_standard_option
( 'pve-storage-id' , { optional
=> 1 }),
369 description
=> "Source volume identifier" ,
373 description
=> "Target volume identifier" ,
376 target_node
=> get_standard_option
( 'pve-node' , {
377 description
=> "Target node. Default is local node." ,
388 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
390 my $user = $rpcenv -> get_user ();
392 my $target_node = $param ->{ target_node
} || PVE
:: INotify
:: nodename
();
394 # cd /nodes/localhost/storage/local/content
395 # pve:/> create local:103/vm-103-disk-1.raw -target local:103/vm-103-disk-2.raw
396 # pve:/> create 103/vm-103-disk-1.raw -target 103/vm-103-disk-3.raw
398 my $src_volid = & $real_volume_id ( $param ->{ storage
}, $param ->{ volume
});
399 my $dst_volid = & $real_volume_id ( $param ->{ storage
}, $param ->{ target
});
401 print "DEBUG: COPY $src_volid TO $dst_volid\n " ;
403 my $cfg = PVE
:: Storage
:: config
();
405 # do all parameter checks first
407 # then do all short running task (to raise errors befor we go to background)
409 # then start the worker task
413 print "DEBUG: starting worker $upid\n " ;
415 my ( $target_sid, $target_volname ) = PVE
:: Storage
:: parse_volume_id
( $dst_volid );
416 #my $target_ip = PVE::Cluster::remote_node_ip($target_node);
418 # you need to get this working (fails currently, because storage_migrate() uses
419 # ssh to connect to local host (which is not needed
420 my $sshinfo = PVE
:: SSHInfo
:: get_ssh_info
( $target_node );
421 PVE
:: Storage
:: storage_migrate
( $cfg, $src_volid, $sshinfo, $target_sid, $target_volname );
423 print "DEBUG: end worker $upid\n " ;
427 return $rpcenv -> fork_worker ( 'imgcopy' , undef , $user, $worker );