]>
git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Storage/Content.pm
1 package PVE
:: API2
:: Storage
:: Content
;
8 use PVE
:: Cluster
qw(cfs_read_file) ;
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' ),
34 description
=> "Only list content of this type." ,
35 type
=> 'string' , format
=> 'pve-storage-content' ,
38 vmid
=> get_standard_option
40 description
=> "Only list images for this VM" ,
55 links
=> [ { rel
=> 'child' , href
=> "{volid}" } ],
60 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
62 my $authuser = $rpcenv -> get_user ();
64 my $storeid = $param ->{ storage
};
66 my $cfg = cfs_read_file
( "storage.cfg" );
68 my $vollist = PVE
:: Storage
:: volume_list
( $cfg, $storeid, $param ->{ vmid
}, $param ->{ content
});
71 foreach my $item ( @$vollist ) {
72 eval { $rpcenv -> check_volume_access ( $authuser, $cfg, undef , $item ->{ volid
}); };
80 __PACKAGE__-
> register_method ({
84 description
=> "Allocate disk images." ,
86 check
=> [ 'perm' , '/storage/{storage}' , [ 'Datastore.AllocateSpace' ]],
91 additionalProperties
=> 0 ,
93 node
=> get_standard_option
( 'pve-node' ),
94 storage
=> get_standard_option
( 'pve-storage-id' , {
95 completion
=> \
& PVE
:: Storage
:: complete_storage_enabled
,
98 description
=> "The name of the file to create." ,
101 vmid
=> get_standard_option
( 'pve-vmid' , {
102 description
=> "Specify owner VM" ,
103 completion
=> \
& PVE
:: Cluster
:: complete_vmid
,
106 description
=> "Size in kilobyte (1024 bytes). Optional suffixes 'M' (megabyte, 1024K) and 'G' (gigabyte, 1024M)" ,
108 pattern
=> '\d+[MG]?' ,
112 enum
=> [ 'raw' , 'qcow2' , 'subvol' ],
119 description
=> "Volume identifier" ,
125 my $storeid = $param ->{ storage
};
126 my $name = $param ->{ filename
};
127 my $sizestr = $param ->{ size
};
130 if ( $sizestr =~ m/^\d+$/ ) {
132 } elsif ( $sizestr =~ m/^(\d+)M$/ ) {
134 } elsif ( $sizestr =~ m/^(\d+)G$/ ) {
135 $size = $1 * 1024 * 1024 ;
137 raise_param_exc
({ size
=> "unable to parse size ' $sizestr '" });
140 # extract FORMAT from name
141 if ( $name =~ m/\.(raw|qcow2|vmdk)$/ ) {
144 raise_param_exc
({ format
=> "different storage formats ( $param ->{format} != $fmt )" })
145 if $param ->{ format
} && $param ->{ format
} ne $fmt ;
147 $param ->{ format
} = $fmt ;
150 my $cfg = cfs_read_file
( 'storage.cfg' );
152 my $volid = PVE
:: Storage
:: vdisk_alloc
( $cfg, $storeid, $param ->{ vmid
},
159 # we allow to pass volume names (without storage prefix) if the storage
160 # is specified as separate parameter.
161 my $real_volume_id = sub {
162 my ( $storeid, $volume ) = @_ ;
166 if ( $volume =~ m/:/ ) {
168 my ( $sid, $volname ) = PVE
:: Storage
:: parse_volume_id
( $volume );
169 die "storage ID missmatch ( $sid != $storeid ) \n "
170 if $storeid && $sid ne $storeid ;
174 raise_param_exc
({ volume
=> $@ }) if $@ ;
177 raise_param_exc
({ volume
=> "no storage speficied - incomplete volume ID" })
180 $volid = " $storeid : $volume " ;
183 return wantarray ?
( $volid, $storeid ) : $volid ;
186 __PACKAGE__-
> register_method ({
190 description
=> "Get volume attributes" ,
192 description
=> "You need read access for the volume." ,
198 additionalProperties
=> 0 ,
200 node
=> get_standard_option
( 'pve-node' ),
201 storage
=> get_standard_option
( 'pve-storage-id' , { optional
=> 1 }),
203 description
=> "Volume identifier" ,
208 returns
=> { type
=> 'object' },
212 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
213 my $authuser = $rpcenv -> get_user ();
215 my ( $volid, $storeid ) = & $real_volume_id ( $param ->{ storage
}, $param ->{ volume
});
217 my $cfg = cfs_read_file
( 'storage.cfg' );
219 $rpcenv -> check_volume_access ( $authuser, $cfg, undef , $volid );
221 my $path = PVE
:: Storage
:: path
( $cfg, $volid );
222 my ( $size, $format, $used, $parent ) = PVE
:: Storage
:: file_size_info
( $path );
223 die "file_size_info on ' $volid ' failed \n " if !( $format && $size );
225 # fixme: return more attributes?
234 __PACKAGE__-
> register_method ({
238 description
=> "Delete volume" ,
240 description
=> "You need 'Datastore.Allocate' privilege on the storage (or 'Datastore.AllocateSpace' for backup volumes if you have VM.Backup privilege on the VM)." ,
246 additionalProperties
=> 0 ,
248 node
=> get_standard_option
( 'pve-node' ),
249 storage
=> get_standard_option
( 'pve-storage-id' , { optional
=> 1 }),
251 description
=> "Volume identifier" ,
256 returns
=> { type
=> 'null' },
260 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
261 my $authuser = $rpcenv -> get_user ();
263 my $cfg = cfs_read_file
( 'storage.cfg' );
265 my ( $volid, $storeid ) = & $real_volume_id ( $param ->{ storage
}, $param ->{ volume
});
267 my ( $path, $ownervm, $vtype ) = PVE
:: Storage
:: path
( $cfg, $volid );
268 if ( $vtype eq 'backup' && $ownervm ) {
269 $rpcenv -> check ( $authuser, "/storage/ $storeid " , [ 'Datastore.AllocateSpace' ]);
270 $rpcenv -> check ( $authuser, "/vms/ $ownervm " , [ 'VM.Backup' ]);
272 $rpcenv -> check ( $authuser, "/storage/ $storeid " , [ 'Datastore.Allocate' ]);
275 PVE
:: Storage
:: vdisk_free
( $cfg, $volid );
280 __PACKAGE__-
> register_method ({
284 description
=> "Copy a volume. This is experimental code - do not use." ,
288 additionalProperties
=> 0 ,
290 node
=> get_standard_option
( 'pve-node' ),
291 storage
=> get_standard_option
( 'pve-storage-id' , { optional
=> 1 }),
293 description
=> "Source volume identifier" ,
297 description
=> "Target volume identifier" ,
300 target_node
=> get_standard_option
( 'pve-node' , {
301 description
=> "Target node. Default is local node." ,
312 my $rpcenv = PVE
:: RPCEnvironment
:: get
();
314 my $user = $rpcenv -> get_user ();
316 my $target_node = $param ->{ target_node
} || PVE
:: INotify
:: nodename
();
318 # cd /nodes/localhost/storage/local/content
319 # pve:/> create local:103/vm-103-disk-1.raw -target local:103/vm-103-disk-2.raw
320 # pve:/> create 103/vm-103-disk-1.raw -target 103/vm-103-disk-3.raw
322 my $src_volid = & $real_volume_id ( $param ->{ storage
}, $param ->{ volume
});
323 my $dst_volid = & $real_volume_id ( $param ->{ storage
}, $param ->{ target
});
325 print "DEBUG: COPY $src_volid TO $dst_volid\n " ;
327 my $cfg = cfs_read_file
( 'storage.cfg' );
329 # do all parameter checks first
331 # then do all short running task (to raise errors befor we go to background)
333 # then start the worker task
337 print "DEBUG: starting worker $upid\n " ;
339 my ( $target_sid, $target_volname ) = PVE
:: Storage
:: parse_volume_id
( $dst_volid );
340 #my $target_ip = PVE::Cluster::remote_node_ip($target_node);
342 # you need to get this working (fails currently, because storage_migrate() uses
343 # ssh to connect to local host (which is not needed
344 PVE
:: Storage
:: storage_migrate
( $cfg, $src_volid, $target_node, $target_sid, $target_volname );
346 print "DEBUG: end worker $upid\n " ;
350 return $rpcenv -> fork_worker ( 'imgcopy' , undef , $user, $worker );