]>
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) ;
16 use base
qw(PVE::RESTHandler) ;
18 __PACKAGE__-
> register_method ({
22 description
=> "List storage content." ,
24 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.Audit' , 'Datastore.AllocateSpace' ], any
=> 1 ],
29 additionalProperties
=> 0 ,
31 node
=> get_standard_option
( 'pve-node' ),
32 storage
=> get_standard_option
( 'pve-storage-id' , {
33 completion
=> \
& PVE
:: Storage
:: complete_storage_enabled
,
36 description
=> "Only list content of this type." ,
37 type
=> 'string' , format
=> 'pve-storage-content' ,
39 completion
=> \
& PVE
:: Storage
:: complete_content_type
,
41 vmid
=> get_standard_option
( 'pve-vmid' , {
42 description
=> "Only list images for this VM" ,
44 completion
=> \
& PVE
:: Cluster
:: complete_vmid
,
54 description
=> "Volume identifier." ,
58 description
=> "Associated Owner VMID." ,
63 description
=> "Volume identifier of parent (for linked cloned)." ,
68 description
=> "Format identifier ('raw', 'qcow2', 'subvol', 'iso', 'tgz' ...)" ,
72 description
=> "Volume size in bytes." ,
77 description
=> "Used space. Please note that most storage plugins " .
78 "does not report anything useful here." ,
85 links
=> [ { rel
=> 'child' , href
=> "{volid}" } ],
90 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
92 my $authuser = $rpcenv -> get_user ();
94 my $storeid = $param ->{ storage
};
96 my $cfg = PVE
:: Storage
:: config
();
98 my $vollist = PVE
:: Storage
:: volume_list
( $cfg, $storeid, $param ->{ vmid
}, $param ->{ content
});
101 foreach my $item ( @$vollist ) {
102 eval { PVE
:: Storage
:: check_volume_access
( $rpcenv, $authuser, $cfg, undef , $item ->{ volid
}); };
104 $item ->{ vmid
} = int ( $item ->{ vmid
}) if ( defined ( $item ->{ vmid
}));
111 __PACKAGE__-
> register_method ({
115 description
=> "Allocate disk images." ,
117 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.AllocateSpace' ]],
122 additionalProperties
=> 0 ,
124 node
=> get_standard_option
( 'pve-node' ),
125 storage
=> get_standard_option
( 'pve-storage-id' , {
126 completion
=> \
& PVE
:: Storage
:: complete_storage_enabled
,
129 description
=> "The name of the file to create." ,
132 vmid
=> get_standard_option
( 'pve-vmid' , {
133 description
=> "Specify owner VM" ,
134 completion
=> \
& PVE
:: Cluster
:: complete_vmid
,
137 description
=> "Size in kilobyte (1024 bytes). Optional suffixes 'M' (megabyte, 1024K) and 'G' (gigabyte, 1024M)" ,
139 pattern
=> '\d+[MG]?' ,
143 enum
=> [ 'raw' , 'qcow2' , 'subvol' ],
150 description
=> "Volume identifier" ,
156 my $storeid = $param ->{ storage
};
157 my $name = $param ->{ filename
};
158 my $sizestr = $param ->{ size
};
161 if ( $sizestr =~ m/^\d+$/ ) {
163 } elsif ( $sizestr =~ m/^(\d+)M$/ ) {
165 } elsif ( $sizestr =~ m/^(\d+)G$/ ) {
166 $size = $1 * 1024 * 1024 ;
168 raise_param_exc
({ size
=> "unable to parse size ' $sizestr '" });
171 # extract FORMAT from name
172 if ( $name =~ m/\.(raw|qcow2|vmdk)$/ ) {
175 raise_param_exc
({ format
=> "different storage formats ( $param ->{format} != $fmt )" })
176 if $param ->{ format
} && $param ->{ format
} ne $fmt ;
178 $param ->{ format
} = $fmt ;
181 my $cfg = PVE
:: Storage
:: config
();
183 my $volid = PVE
:: Storage
:: vdisk_alloc
( $cfg, $storeid, $param ->{ vmid
},
190 # we allow to pass volume names (without storage prefix) if the storage
191 # is specified as separate parameter.
192 my $real_volume_id = sub {
193 my ( $storeid, $volume ) = @_ ;
197 if ( $volume =~ m/:/ ) {
199 my ( $sid, $volname ) = PVE
:: Storage
:: parse_volume_id
( $volume );
200 die "storage ID missmatch ( $sid != $storeid ) \n "
201 if $storeid && $sid ne $storeid ;
205 raise_param_exc
({ volume
=> $@ }) if $@ ;
208 raise_param_exc
({ volume
=> "no storage speficied - incomplete volume ID" })
211 $volid = " $storeid : $volume " ;
214 return wantarray ?
( $volid, $storeid ) : $volid ;
217 __PACKAGE__-
> register_method ({
221 description
=> "Get volume attributes" ,
223 description
=> "You need read access for the volume." ,
229 additionalProperties
=> 0 ,
231 node
=> get_standard_option
( 'pve-node' ),
232 storage
=> get_standard_option
( 'pve-storage-id' , { optional
=> 1 }),
234 description
=> "Volume identifier" ,
239 returns
=> { type
=> 'object' },
243 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
244 my $authuser = $rpcenv -> get_user ();
246 my ( $volid, $storeid ) = & $real_volume_id ( $param ->{ storage
}, $param ->{ volume
});
248 my $cfg = PVE
:: Storage
:: config
();
250 PVE
:: Storage
:: check_volume_access
( $rpcenv, $authuser, $cfg, undef , $volid );
252 my $path = PVE
:: Storage
:: path
( $cfg, $volid );
253 my ( $size, $format, $used, $parent ) = PVE
:: Storage
:: file_size_info
( $path );
254 die "file_size_info on ' $volid ' failed \n " if !( $format && $size );
256 # fixme: return more attributes?
265 __PACKAGE__-
> register_method ({
269 description
=> "Delete volume" ,
271 description
=> "You need 'Datastore.Allocate' privilege on the storage (or 'Datastore.AllocateSpace' for backup volumes if you have VM.Backup privilege on the VM)." ,
277 additionalProperties
=> 0 ,
279 node
=> get_standard_option
( 'pve-node' ),
280 storage
=> get_standard_option
( 'pve-storage-id' , {
282 completion
=> \
& PVE
:: Storage
:: complete_storage
,
285 description
=> "Volume identifier" ,
287 completion
=> \
& PVE
:: Storage
:: complete_volume
,
291 description
=> "Time to wait for the task to finish. We return 'null' if the task finish within that time." ,
298 returns
=> { type
=> 'string' , optional
=> 1 , },
302 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
303 my $authuser = $rpcenv -> get_user ();
305 my $cfg = PVE
:: Storage
:: config
();
307 my ( $volid, $storeid ) = & $real_volume_id ( $param ->{ storage
}, $param ->{ volume
});
309 my ( $path, $ownervm, $vtype ) = PVE
:: Storage
:: path
( $cfg, $volid );
310 if ( $vtype eq 'backup' && $ownervm ) {
311 $rpcenv -> check ( $authuser, "/storage/ $storeid " , [ 'Datastore.AllocateSpace' ]);
312 $rpcenv -> check ( $authuser, "/vms/ $ownervm " , [ 'VM.Backup' ]);
314 $rpcenv -> check ( $authuser, "/storage/ $storeid " , [ 'Datastore.Allocate' ]);
318 PVE
:: Storage
:: vdisk_free
( $cfg, $volid );
319 print "Removed volume ' $volid ' \n " ;
320 if ( $vtype eq 'backup'
321 && $path =~ /(.*\/ vzdump-\w
+- \d
+- \d
{ 4 } _\d
{ 2 } _\d
{ 2 }- \d
{ 2 } _\d
{ 2 } _\d
{ 2 })[^ \
/]+$/ ) {
322 my $logpath = " $1 .log" ;
323 # try to cleanup our backup log file too, if still exisiting, #318
324 unlink ( $logpath ) if - e
$logpath ;
328 my $id = ( defined $ownervm ?
" $ownervm@ " : '' ) . $storeid ;
329 my $upid = $rpcenv -> fork_worker ( 'imgdel' , $id, $authuser, $worker );
330 my $background_delay = $param ->{ delay
};
331 if ( $background_delay ) {
332 my $end_time = time () + $background_delay ;
333 my $currently_deleting ; # not necessarily true, e.g. sequential api call from cli
335 my $task = PVE
:: Tools
:: upid_decode
( $upid );
336 $currently_deleting = PVE
:: ProcFSTools
:: check_process_running
( $task ->{ pid
}, $task ->{ pstart
});
337 sleep 1 if $currently_deleting ;
338 } while ( time () < $end_time && $currently_deleting );
340 if (! $currently_deleting ) {
341 my $status = PVE
:: Tools
:: upid_read_status
( $upid );
342 return undef if $status eq 'OK' ;
349 __PACKAGE__-
> register_method ({
353 description
=> "Copy a volume. This is experimental code - do not use." ,
357 additionalProperties
=> 0 ,
359 node
=> get_standard_option
( 'pve-node' ),
360 storage
=> get_standard_option
( 'pve-storage-id' , { optional
=> 1 }),
362 description
=> "Source volume identifier" ,
366 description
=> "Target volume identifier" ,
369 target_node
=> get_standard_option
( 'pve-node' , {
370 description
=> "Target node. Default is local node." ,
381 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
383 my $user = $rpcenv -> get_user ();
385 my $target_node = $param ->{ target_node
} || PVE
:: INotify
:: nodename
();
387 # cd /nodes/localhost/storage/local/content
388 # pve:/> create local:103/vm-103-disk-1.raw -target local:103/vm-103-disk-2.raw
389 # pve:/> create 103/vm-103-disk-1.raw -target 103/vm-103-disk-3.raw
391 my $src_volid = & $real_volume_id ( $param ->{ storage
}, $param ->{ volume
});
392 my $dst_volid = & $real_volume_id ( $param ->{ storage
}, $param ->{ target
});
394 print "DEBUG: COPY $src_volid TO $dst_volid\n " ;
396 my $cfg = PVE
:: Storage
:: config
();
398 # do all parameter checks first
400 # then do all short running task (to raise errors befor we go to background)
402 # then start the worker task
406 print "DEBUG: starting worker $upid\n " ;
408 my ( $target_sid, $target_volname ) = PVE
:: Storage
:: parse_volume_id
( $dst_volid );
409 #my $target_ip = PVE::Cluster::remote_node_ip($target_node);
411 # you need to get this working (fails currently, because storage_migrate() uses
412 # ssh to connect to local host (which is not needed
413 my $sshinfo = PVE
:: Cluster
:: get_ssh_info
( $target_node );
414 PVE
:: Storage
:: storage_migrate
( $cfg, $src_volid, $sshinfo, $target_sid, $target_volname );
416 print "DEBUG: end worker $upid\n " ;
420 return $rpcenv -> fork_worker ( 'imgcopy' , undef , $user, $worker );