]>
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
,
58 links
=> [ { rel
=> 'child' , href
=> "{volid}" } ],
63 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
65 my $authuser = $rpcenv -> get_user ();
67 my $storeid = $param ->{ storage
};
69 my $cfg = PVE
:: Storage
:: config
();
71 my $vollist = PVE
:: Storage
:: volume_list
( $cfg, $storeid, $param ->{ vmid
}, $param ->{ content
});
74 foreach my $item ( @$vollist ) {
75 eval { PVE
:: Storage
:: check_volume_access
( $rpcenv, $authuser, $cfg, undef , $item ->{ volid
}); };
83 __PACKAGE__-
> register_method ({
87 description
=> "Allocate disk images." ,
89 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.AllocateSpace' ]],
94 additionalProperties
=> 0 ,
96 node
=> get_standard_option
( 'pve-node' ),
97 storage
=> get_standard_option
( 'pve-storage-id' , {
98 completion
=> \
& PVE
:: Storage
:: complete_storage_enabled
,
101 description
=> "The name of the file to create." ,
104 vmid
=> get_standard_option
( 'pve-vmid' , {
105 description
=> "Specify owner VM" ,
106 completion
=> \
& PVE
:: Cluster
:: complete_vmid
,
109 description
=> "Size in kilobyte (1024 bytes). Optional suffixes 'M' (megabyte, 1024K) and 'G' (gigabyte, 1024M)" ,
111 pattern
=> '\d+[MG]?' ,
115 enum
=> [ 'raw' , 'qcow2' , 'subvol' ],
122 description
=> "Volume identifier" ,
128 my $storeid = $param ->{ storage
};
129 my $name = $param ->{ filename
};
130 my $sizestr = $param ->{ size
};
133 if ( $sizestr =~ m/^\d+$/ ) {
135 } elsif ( $sizestr =~ m/^(\d+)M$/ ) {
137 } elsif ( $sizestr =~ m/^(\d+)G$/ ) {
138 $size = $1 * 1024 * 1024 ;
140 raise_param_exc
({ size
=> "unable to parse size ' $sizestr '" });
143 # extract FORMAT from name
144 if ( $name =~ m/\.(raw|qcow2|vmdk)$/ ) {
147 raise_param_exc
({ format
=> "different storage formats ( $param ->{format} != $fmt )" })
148 if $param ->{ format
} && $param ->{ format
} ne $fmt ;
150 $param ->{ format
} = $fmt ;
153 my $cfg = PVE
:: Storage
:: config
();
155 my $volid = PVE
:: Storage
:: vdisk_alloc
( $cfg, $storeid, $param ->{ vmid
},
162 # we allow to pass volume names (without storage prefix) if the storage
163 # is specified as separate parameter.
164 my $real_volume_id = sub {
165 my ( $storeid, $volume ) = @_ ;
169 if ( $volume =~ m/:/ ) {
171 my ( $sid, $volname ) = PVE
:: Storage
:: parse_volume_id
( $volume );
172 die "storage ID missmatch ( $sid != $storeid ) \n "
173 if $storeid && $sid ne $storeid ;
177 raise_param_exc
({ volume
=> $@ }) if $@ ;
180 raise_param_exc
({ volume
=> "no storage speficied - incomplete volume ID" })
183 $volid = " $storeid : $volume " ;
186 return wantarray ?
( $volid, $storeid ) : $volid ;
189 __PACKAGE__-
> register_method ({
193 description
=> "Get volume attributes" ,
195 description
=> "You need read access for the volume." ,
201 additionalProperties
=> 0 ,
203 node
=> get_standard_option
( 'pve-node' ),
204 storage
=> get_standard_option
( 'pve-storage-id' , { optional
=> 1 }),
206 description
=> "Volume identifier" ,
211 returns
=> { type
=> 'object' },
215 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
216 my $authuser = $rpcenv -> get_user ();
218 my ( $volid, $storeid ) = & $real_volume_id ( $param ->{ storage
}, $param ->{ volume
});
220 my $cfg = PVE
:: Storage
:: config
();
222 PVE
:: Storage
:: check_volume_access
( $rpcenv, $authuser, $cfg, undef , $volid );
224 my $path = PVE
:: Storage
:: path
( $cfg, $volid );
225 my ( $size, $format, $used, $parent ) = PVE
:: Storage
:: file_size_info
( $path );
226 die "file_size_info on ' $volid ' failed \n " if !( $format && $size );
228 # fixme: return more attributes?
237 __PACKAGE__-
> register_method ({
241 description
=> "Delete volume" ,
243 description
=> "You need 'Datastore.Allocate' privilege on the storage (or 'Datastore.AllocateSpace' for backup volumes if you have VM.Backup privilege on the VM)." ,
249 additionalProperties
=> 0 ,
251 node
=> get_standard_option
( 'pve-node' ),
252 storage
=> get_standard_option
( 'pve-storage-id' , {
254 completion
=> \
& PVE
:: Storage
:: complete_storage
,
257 description
=> "Volume identifier" ,
259 completion
=> \
& PVE
:: Storage
:: complete_volume
,
263 returns
=> { type
=> 'null' },
267 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
268 my $authuser = $rpcenv -> get_user ();
270 my $cfg = PVE
:: Storage
:: config
();
272 my ( $volid, $storeid ) = & $real_volume_id ( $param ->{ storage
}, $param ->{ volume
});
274 my ( $path, $ownervm, $vtype ) = PVE
:: Storage
:: path
( $cfg, $volid );
275 if ( $vtype eq 'backup' && $ownervm ) {
276 $rpcenv -> check ( $authuser, "/storage/ $storeid " , [ 'Datastore.AllocateSpace' ]);
277 $rpcenv -> check ( $authuser, "/vms/ $ownervm " , [ 'VM.Backup' ]);
279 $rpcenv -> check ( $authuser, "/storage/ $storeid " , [ 'Datastore.Allocate' ]);
282 PVE
:: Storage
:: vdisk_free
( $cfg, $volid );
287 __PACKAGE__-
> register_method ({
291 description
=> "Copy a volume. This is experimental code - do not use." ,
295 additionalProperties
=> 0 ,
297 node
=> get_standard_option
( 'pve-node' ),
298 storage
=> get_standard_option
( 'pve-storage-id' , { optional
=> 1 }),
300 description
=> "Source volume identifier" ,
304 description
=> "Target volume identifier" ,
307 target_node
=> get_standard_option
( 'pve-node' , {
308 description
=> "Target node. Default is local node." ,
319 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
321 my $user = $rpcenv -> get_user ();
323 my $target_node = $param ->{ target_node
} || PVE
:: INotify
:: nodename
();
325 # cd /nodes/localhost/storage/local/content
326 # pve:/> create local:103/vm-103-disk-1.raw -target local:103/vm-103-disk-2.raw
327 # pve:/> create 103/vm-103-disk-1.raw -target 103/vm-103-disk-3.raw
329 my $src_volid = & $real_volume_id ( $param ->{ storage
}, $param ->{ volume
});
330 my $dst_volid = & $real_volume_id ( $param ->{ storage
}, $param ->{ target
});
332 print "DEBUG: COPY $src_volid TO $dst_volid\n " ;
334 my $cfg = PVE
:: Storage
:: config
();
336 # do all parameter checks first
338 # then do all short running task (to raise errors befor we go to background)
340 # then start the worker task
344 print "DEBUG: starting worker $upid\n " ;
346 my ( $target_sid, $target_volname ) = PVE
:: Storage
:: parse_volume_id
( $dst_volid );
347 #my $target_ip = PVE::Cluster::remote_node_ip($target_node);
349 # you need to get this working (fails currently, because storage_migrate() uses
350 # ssh to connect to local host (which is not needed
351 PVE
:: Storage
:: storage_migrate
( $cfg, $src_volid, $target_node, $target_sid, $target_volname );
353 print "DEBUG: end worker $upid\n " ;
357 return $rpcenv -> fork_worker ( 'imgcopy' , undef , $user, $worker );