]>
Commit | Line | Data |
---|---|---|
b6cf0a66 DM |
1 | package PVE::API2::Storage::Content; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
1ccae449 | 5 | use Data::Dumper; |
b6cf0a66 DM |
6 | |
7 | use PVE::SafeSyslog; | |
8 | use PVE::Cluster qw(cfs_read_file); | |
9 | use PVE::Storage; | |
10 | use PVE::INotify; | |
11 | use PVE::Exception qw(raise_param_exc); | |
12 | use PVE::RPCEnvironment; | |
13 | use PVE::RESTHandler; | |
14 | use PVE::JSONSchema qw(get_standard_option); | |
15 | ||
16 | use base qw(PVE::RESTHandler); | |
17 | ||
b6cf0a66 DM |
18 | __PACKAGE__->register_method ({ |
19 | name => 'index', | |
20 | path => '', | |
21 | method => 'GET', | |
22 | description => "List storage content.", | |
5f642f73 DM |
23 | permissions => { |
24 | check => ['perm', '/storage/{storage}', ['Datastore.Audit', 'Datastore.AllocateSpace'], any => 1], | |
25 | }, | |
b6cf0a66 DM |
26 | protected => 1, |
27 | proxyto => 'node', | |
28 | parameters => { | |
29 | additionalProperties => 0, | |
30 | properties => { | |
31 | node => get_standard_option('pve-node'), | |
32 | storage => get_standard_option('pve-storage-id'), | |
33 | content => { | |
34 | description => "Only list content of this type.", | |
35 | type => 'string', format => 'pve-storage-content', | |
36 | optional => 1, | |
98437f4c | 37 | completion => \&PVE::Storage::complete_content_type, |
b6cf0a66 DM |
38 | }, |
39 | vmid => get_standard_option | |
40 | ('pve-vmid', { | |
41 | description => "Only list images for this VM", | |
42 | optional => 1, | |
43 | }), | |
44 | }, | |
45 | }, | |
46 | returns => { | |
47 | type => 'array', | |
48 | items => { | |
49 | type => "object", | |
50 | properties => { | |
51 | volid => { | |
52 | type => 'string' | |
53 | } | |
54 | }, | |
55 | }, | |
56 | links => [ { rel => 'child', href => "{volid}" } ], | |
57 | }, | |
58 | code => sub { | |
59 | my ($param) = @_; | |
60 | ||
b8744249 DM |
61 | my $rpcenv = PVE::RPCEnvironment::get(); |
62 | ||
63 | my $authuser = $rpcenv->get_user(); | |
64 | ||
b6cf0a66 DM |
65 | my $storeid = $param->{storage}; |
66 | ||
67 | my $cfg = cfs_read_file("storage.cfg"); | |
68 | ||
37ba0aea | 69 | my $vollist = PVE::Storage::volume_list($cfg, $storeid, $param->{vmid}, $param->{content}); |
b6cf0a66 DM |
70 | |
71 | my $res = []; | |
37ba0aea DM |
72 | foreach my $item (@$vollist) { |
73 | eval { $rpcenv->check_volume_access($authuser, $cfg, undef, $item->{volid}); }; | |
74 | next if $@; | |
75 | push @$res, $item; | |
b6cf0a66 DM |
76 | } |
77 | ||
78 | return $res; | |
79 | }}); | |
80 | ||
81 | __PACKAGE__->register_method ({ | |
82 | name => 'create', | |
83 | path => '', | |
84 | method => 'POST', | |
85 | description => "Allocate disk images.", | |
5f642f73 DM |
86 | permissions => { |
87 | check => ['perm', '/storage/{storage}', ['Datastore.AllocateSpace']], | |
88 | }, | |
b6cf0a66 DM |
89 | protected => 1, |
90 | proxyto => 'node', | |
91 | parameters => { | |
92 | additionalProperties => 0, | |
93 | properties => { | |
94 | node => get_standard_option('pve-node'), | |
f7621c01 DM |
95 | storage => get_standard_option('pve-storage-id', { |
96 | completion => \&PVE::Storage::complete_storage_enabled, | |
97 | }), | |
b6cf0a66 | 98 | filename => { |
03f03009 | 99 | description => "The name of the file to create.", |
b6cf0a66 DM |
100 | type => 'string', |
101 | }, | |
f7621c01 DM |
102 | vmid => get_standard_option('pve-vmid', { |
103 | description => "Specify owner VM", | |
104 | completion => \&PVE::Cluster::complete_vmid, | |
105 | }), | |
b6cf0a66 DM |
106 | size => { |
107 | description => "Size in kilobyte (1024 bytes). Optional suffixes 'M' (megabyte, 1024K) and 'G' (gigabyte, 1024M)", | |
108 | type => 'string', | |
109 | pattern => '\d+[MG]?', | |
110 | }, | |
111 | 'format' => { | |
112 | type => 'string', | |
1ccae449 | 113 | enum => ['raw', 'qcow2', 'subvol'], |
b6cf0a66 DM |
114 | requires => 'size', |
115 | optional => 1, | |
116 | }, | |
117 | }, | |
118 | }, | |
119 | returns => { | |
120 | description => "Volume identifier", | |
121 | type => 'string', | |
122 | }, | |
123 | code => sub { | |
124 | my ($param) = @_; | |
125 | ||
126 | my $storeid = $param->{storage}; | |
127 | my $name = $param->{filename}; | |
128 | my $sizestr = $param->{size}; | |
129 | ||
130 | my $size; | |
131 | if ($sizestr =~ m/^\d+$/) { | |
132 | $size = $sizestr; | |
133 | } elsif ($sizestr =~ m/^(\d+)M$/) { | |
134 | $size = $1 * 1024; | |
135 | } elsif ($sizestr =~ m/^(\d+)G$/) { | |
136 | $size = $1 * 1024 * 1024; | |
137 | } else { | |
138 | raise_param_exc({ size => "unable to parse size '$sizestr'" }); | |
139 | } | |
140 | ||
141 | # extract FORMAT from name | |
8e87d6ee | 142 | if ($name =~ m/\.(raw|qcow2|vmdk)$/) { |
b6cf0a66 DM |
143 | my $fmt = $1; |
144 | ||
145 | raise_param_exc({ format => "different storage formats ($param->{format} != $fmt)" }) | |
146 | if $param->{format} && $param->{format} ne $fmt; | |
147 | ||
148 | $param->{format} = $fmt; | |
149 | } | |
150 | ||
151 | my $cfg = cfs_read_file('storage.cfg'); | |
152 | ||
153 | my $volid = PVE::Storage::vdisk_alloc ($cfg, $storeid, $param->{vmid}, | |
154 | $param->{format}, | |
155 | $name, $size); | |
156 | ||
157 | return $volid; | |
158 | }}); | |
159 | ||
160 | # we allow to pass volume names (without storage prefix) if the storage | |
161 | # is specified as separate parameter. | |
162 | my $real_volume_id = sub { | |
163 | my ($storeid, $volume) = @_; | |
164 | ||
165 | my $volid; | |
166 | ||
167 | if ($volume =~ m/:/) { | |
168 | eval { | |
169 | my ($sid, $volname) = PVE::Storage::parse_volume_id ($volume); | |
5f25af2f | 170 | die "storage ID missmatch ($sid != $storeid)\n" |
b6cf0a66 DM |
171 | if $storeid && $sid ne $storeid; |
172 | $volid = $volume; | |
b755bdb0 | 173 | $storeid = $sid; |
b6cf0a66 | 174 | }; |
5f25af2f | 175 | raise_param_exc({ volume => $@ }) if $@; |
b6cf0a66 DM |
176 | |
177 | } else { | |
178 | raise_param_exc({ volume => "no storage speficied - incomplete volume ID" }) | |
179 | if !$storeid; | |
180 | ||
181 | $volid = "$storeid:$volume"; | |
182 | } | |
183 | ||
b755bdb0 | 184 | return wantarray ? ($volid, $storeid) : $volid; |
b6cf0a66 DM |
185 | }; |
186 | ||
187 | __PACKAGE__->register_method ({ | |
188 | name => 'info', | |
189 | path => '{volume}', | |
190 | method => 'GET', | |
191 | description => "Get volume attributes", | |
5f642f73 | 192 | permissions => { |
b8744249 | 193 | description => "You need read access for the volume.", |
b755bdb0 | 194 | user => 'all', |
5f642f73 | 195 | }, |
b6cf0a66 DM |
196 | protected => 1, |
197 | proxyto => 'node', | |
198 | parameters => { | |
199 | additionalProperties => 0, | |
200 | properties => { | |
201 | node => get_standard_option('pve-node'), | |
202 | storage => get_standard_option('pve-storage-id', { optional => 1 }), | |
203 | volume => { | |
204 | description => "Volume identifier", | |
205 | type => 'string', | |
206 | }, | |
207 | }, | |
208 | }, | |
209 | returns => { type => 'object' }, | |
210 | code => sub { | |
211 | my ($param) = @_; | |
212 | ||
b755bdb0 DM |
213 | my $rpcenv = PVE::RPCEnvironment::get(); |
214 | my $authuser = $rpcenv->get_user(); | |
215 | ||
216 | my ($volid, $storeid) = &$real_volume_id($param->{storage}, $param->{volume}); | |
217 | ||
b6cf0a66 DM |
218 | my $cfg = cfs_read_file('storage.cfg'); |
219 | ||
b8744249 DM |
220 | $rpcenv->check_volume_access($authuser, $cfg, undef, $volid); |
221 | ||
b6cf0a66 | 222 | my $path = PVE::Storage::path($cfg, $volid); |
a18f7740 DM |
223 | my ($size, $format, $used, $parent) = PVE::Storage::file_size_info($path); |
224 | die "file_size_info on '$volid' failed\n" if !($format && $size); | |
b6cf0a66 DM |
225 | |
226 | # fixme: return more attributes? | |
227 | return { | |
228 | path => $path, | |
229 | size => $size, | |
230 | used => $used, | |
a18f7740 | 231 | format => $format, |
b6cf0a66 DM |
232 | }; |
233 | }}); | |
234 | ||
235 | __PACKAGE__->register_method ({ | |
236 | name => 'delete', | |
237 | path => '{volume}', | |
238 | method => 'DELETE', | |
239 | description => "Delete volume", | |
5f642f73 | 240 | permissions => { |
df6b79c8 | 241 | description => "You need 'Datastore.Allocate' privilege on the storage (or 'Datastore.AllocateSpace' for backup volumes if you have VM.Backup privilege on the VM).", |
b755bdb0 | 242 | user => 'all', |
5f642f73 | 243 | }, |
b6cf0a66 DM |
244 | protected => 1, |
245 | proxyto => 'node', | |
246 | parameters => { | |
247 | additionalProperties => 0, | |
248 | properties => { | |
249 | node => get_standard_option('pve-node'), | |
250 | storage => get_standard_option('pve-storage-id', { optional => 1}), | |
251 | volume => { | |
252 | description => "Volume identifier", | |
253 | type => 'string', | |
254 | }, | |
255 | }, | |
256 | }, | |
257 | returns => { type => 'null' }, | |
258 | code => sub { | |
259 | my ($param) = @_; | |
260 | ||
b755bdb0 DM |
261 | my $rpcenv = PVE::RPCEnvironment::get(); |
262 | my $authuser = $rpcenv->get_user(); | |
263 | ||
df6b79c8 DM |
264 | my $cfg = cfs_read_file('storage.cfg'); |
265 | ||
b755bdb0 | 266 | my ($volid, $storeid) = &$real_volume_id($param->{storage}, $param->{volume}); |
b755bdb0 | 267 | |
df6b79c8 DM |
268 | my ($path, $ownervm, $vtype) = PVE::Storage::path($cfg, $volid); |
269 | if ($vtype eq 'backup' && $ownervm) { | |
270 | $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']); | |
271 | $rpcenv->check($authuser, "/vms/$ownervm", ['VM.Backup']); | |
272 | } else { | |
273 | $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.Allocate']); | |
274 | } | |
b6cf0a66 DM |
275 | |
276 | PVE::Storage::vdisk_free ($cfg, $volid); | |
277 | ||
278 | return undef; | |
279 | }}); | |
280 | ||
883eeea6 DM |
281 | __PACKAGE__->register_method ({ |
282 | name => 'copy', | |
283 | path => '{volume}', | |
284 | method => 'POST', | |
5f642f73 | 285 | description => "Copy a volume. This is experimental code - do not use.", |
883eeea6 DM |
286 | protected => 1, |
287 | proxyto => 'node', | |
288 | parameters => { | |
289 | additionalProperties => 0, | |
290 | properties => { | |
291 | node => get_standard_option('pve-node'), | |
292 | storage => get_standard_option('pve-storage-id', { optional => 1}), | |
293 | volume => { | |
294 | description => "Source volume identifier", | |
295 | type => 'string', | |
296 | }, | |
297 | target => { | |
298 | description => "Target volume identifier", | |
299 | type => 'string', | |
300 | }, | |
301 | target_node => get_standard_option('pve-node', { | |
302 | description => "Target node. Default is local node.", | |
303 | optional => 1, | |
304 | }), | |
305 | }, | |
306 | }, | |
307 | returns => { | |
308 | type => 'string', | |
309 | }, | |
310 | code => sub { | |
311 | my ($param) = @_; | |
312 | ||
313 | my $rpcenv = PVE::RPCEnvironment::get(); | |
314 | ||
315 | my $user = $rpcenv->get_user(); | |
316 | ||
317 | my $target_node = $param->{target_node} || PVE::INotify::nodename(); | |
318 | # pvesh examples | |
319 | # cd /nodes/localhost/storage/local/content | |
320 | # pve:/> create local:103/vm-103-disk-1.raw -target local:103/vm-103-disk-2.raw | |
321 | # pve:/> create 103/vm-103-disk-1.raw -target 103/vm-103-disk-3.raw | |
322 | ||
323 | my $src_volid = &$real_volume_id($param->{storage}, $param->{volume}); | |
324 | my $dst_volid = &$real_volume_id($param->{storage}, $param->{target}); | |
325 | ||
326 | print "DEBUG: COPY $src_volid TO $dst_volid\n"; | |
327 | ||
328 | my $cfg = cfs_read_file('storage.cfg'); | |
329 | ||
330 | # do all parameter checks first | |
331 | ||
332 | # then do all short running task (to raise errors befor we go to background) | |
333 | ||
334 | # then start the worker task | |
335 | my $worker = sub { | |
336 | my $upid = shift; | |
337 | ||
338 | print "DEBUG: starting worker $upid\n"; | |
339 | ||
340 | my ($target_sid, $target_volname) = PVE::Storage::parse_volume_id($dst_volid); | |
341 | #my $target_ip = PVE::Cluster::remote_node_ip($target_node); | |
342 | ||
343 | # you need to get this working (fails currently, because storage_migrate() uses | |
344 | # ssh to connect to local host (which is not needed | |
345 | PVE::Storage::storage_migrate($cfg, $src_volid, $target_node, $target_sid, $target_volname); | |
346 | ||
347 | print "DEBUG: end worker $upid\n"; | |
348 | ||
349 | }; | |
350 | ||
351 | return $rpcenv->fork_worker('imgcopy', undef, $user, $worker); | |
352 | }}); | |
353 | ||
b6cf0a66 | 354 | 1; |