]>
Commit | Line | Data |
---|---|---|
b6cf0a66 DM |
1 | package PVE::API2::Storage::Content; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use PVE::SafeSyslog; | |
7 | use PVE::Cluster qw(cfs_read_file); | |
8 | use PVE::Storage; | |
9 | use PVE::INotify; | |
10 | use PVE::Exception qw(raise_param_exc); | |
11 | use PVE::RPCEnvironment; | |
12 | use PVE::RESTHandler; | |
13 | use PVE::JSONSchema qw(get_standard_option); | |
14 | ||
15 | use base qw(PVE::RESTHandler); | |
16 | ||
17 | my @ctypes = qw(images vztmpl iso backup); | |
18 | ||
19 | __PACKAGE__->register_method ({ | |
20 | name => 'index', | |
21 | path => '', | |
22 | method => 'GET', | |
23 | description => "List storage content.", | |
24 | protected => 1, | |
25 | proxyto => 'node', | |
26 | parameters => { | |
27 | additionalProperties => 0, | |
28 | properties => { | |
29 | node => get_standard_option('pve-node'), | |
30 | storage => get_standard_option('pve-storage-id'), | |
31 | content => { | |
32 | description => "Only list content of this type.", | |
33 | type => 'string', format => 'pve-storage-content', | |
34 | optional => 1, | |
35 | }, | |
36 | vmid => get_standard_option | |
37 | ('pve-vmid', { | |
38 | description => "Only list images for this VM", | |
39 | optional => 1, | |
40 | }), | |
41 | }, | |
42 | }, | |
43 | returns => { | |
44 | type => 'array', | |
45 | items => { | |
46 | type => "object", | |
47 | properties => { | |
48 | volid => { | |
49 | type => 'string' | |
50 | } | |
51 | }, | |
52 | }, | |
53 | links => [ { rel => 'child', href => "{volid}" } ], | |
54 | }, | |
55 | code => sub { | |
56 | my ($param) = @_; | |
57 | ||
58 | my $cts = $param->{content} ? [ $param->{content} ] : [ @ctypes ]; | |
59 | ||
60 | my $storeid = $param->{storage}; | |
61 | ||
62 | my $cfg = cfs_read_file("storage.cfg"); | |
63 | ||
64 | my $scfg = PVE::Storage::storage_config ($cfg, $storeid); | |
65 | ||
66 | my $res = []; | |
67 | foreach my $ct (@$cts) { | |
68 | my $data; | |
69 | if ($ct eq 'images') { | |
70 | $data = PVE::Storage::vdisk_list ($cfg, $storeid, $param->{vmid}); | |
71 | } elsif ($ct eq 'iso') { | |
72 | $data = PVE::Storage::template_list ($cfg, $storeid, 'iso') | |
73 | if !$param->{vmid}; | |
74 | } elsif ($ct eq 'vztmpl') { | |
75 | $data = PVE::Storage::template_list ($cfg, $storeid, 'vztmpl') | |
76 | if !$param->{vmid}; | |
77 | } elsif ($ct eq 'backup') { | |
78 | $data = PVE::Storage::template_list ($cfg, $storeid, 'backup') | |
79 | if !$param->{vmid}; | |
80 | } | |
81 | ||
82 | next if !$data || !$data->{$storeid}; | |
83 | ||
84 | foreach my $item (@{$data->{$storeid}}) { | |
85 | push @$res, $item; | |
86 | } | |
87 | } | |
88 | ||
89 | return $res; | |
90 | }}); | |
91 | ||
92 | __PACKAGE__->register_method ({ | |
93 | name => 'create', | |
94 | path => '', | |
95 | method => 'POST', | |
96 | description => "Allocate disk images.", | |
97 | protected => 1, | |
98 | proxyto => 'node', | |
99 | parameters => { | |
100 | additionalProperties => 0, | |
101 | properties => { | |
102 | node => get_standard_option('pve-node'), | |
103 | storage => get_standard_option('pve-storage-id'), | |
104 | filename => { | |
105 | description => "The name of the file to create/upload.", | |
106 | type => 'string', | |
107 | }, | |
108 | vmid => get_standard_option('pve-vmid', { description => "Specify owner VM" } ), | |
109 | size => { | |
110 | description => "Size in kilobyte (1024 bytes). Optional suffixes 'M' (megabyte, 1024K) and 'G' (gigabyte, 1024M)", | |
111 | type => 'string', | |
112 | pattern => '\d+[MG]?', | |
113 | }, | |
114 | 'format' => { | |
115 | type => 'string', | |
116 | enum => ['raw', 'qcow2'], | |
117 | requires => 'size', | |
118 | optional => 1, | |
119 | }, | |
120 | }, | |
121 | }, | |
122 | returns => { | |
123 | description => "Volume identifier", | |
124 | type => 'string', | |
125 | }, | |
126 | code => sub { | |
127 | my ($param) = @_; | |
128 | ||
129 | my $storeid = $param->{storage}; | |
130 | my $name = $param->{filename}; | |
131 | my $sizestr = $param->{size}; | |
132 | ||
133 | my $size; | |
134 | if ($sizestr =~ m/^\d+$/) { | |
135 | $size = $sizestr; | |
136 | } elsif ($sizestr =~ m/^(\d+)M$/) { | |
137 | $size = $1 * 1024; | |
138 | } elsif ($sizestr =~ m/^(\d+)G$/) { | |
139 | $size = $1 * 1024 * 1024; | |
140 | } else { | |
141 | raise_param_exc({ size => "unable to parse size '$sizestr'" }); | |
142 | } | |
143 | ||
144 | # extract FORMAT from name | |
145 | if ($name =~ m/\.(raw|qcow2)$/) { | |
146 | my $fmt = $1; | |
147 | ||
148 | raise_param_exc({ format => "different storage formats ($param->{format} != $fmt)" }) | |
149 | if $param->{format} && $param->{format} ne $fmt; | |
150 | ||
151 | $param->{format} = $fmt; | |
152 | } | |
153 | ||
154 | my $cfg = cfs_read_file('storage.cfg'); | |
155 | ||
156 | my $volid = PVE::Storage::vdisk_alloc ($cfg, $storeid, $param->{vmid}, | |
157 | $param->{format}, | |
158 | $name, $size); | |
159 | ||
160 | return $volid; | |
161 | }}); | |
162 | ||
163 | # we allow to pass volume names (without storage prefix) if the storage | |
164 | # is specified as separate parameter. | |
165 | my $real_volume_id = sub { | |
166 | my ($storeid, $volume) = @_; | |
167 | ||
168 | my $volid; | |
169 | ||
170 | if ($volume =~ m/:/) { | |
171 | eval { | |
172 | my ($sid, $volname) = PVE::Storage::parse_volume_id ($volume); | |
173 | raise_param_exc({ storage => "storage ID missmatch" }) | |
174 | if $storeid && $sid ne $storeid; | |
175 | $volid = $volume; | |
176 | }; | |
177 | raise_param_exc({ volume => $@}) if $@; | |
178 | ||
179 | } else { | |
180 | raise_param_exc({ volume => "no storage speficied - incomplete volume ID" }) | |
181 | if !$storeid; | |
182 | ||
183 | $volid = "$storeid:$volume"; | |
184 | } | |
185 | ||
186 | return $volid; | |
187 | }; | |
188 | ||
189 | __PACKAGE__->register_method ({ | |
190 | name => 'info', | |
191 | path => '{volume}', | |
192 | method => 'GET', | |
193 | description => "Get volume attributes", | |
194 | protected => 1, | |
195 | proxyto => 'node', | |
196 | parameters => { | |
197 | additionalProperties => 0, | |
198 | properties => { | |
199 | node => get_standard_option('pve-node'), | |
200 | storage => get_standard_option('pve-storage-id', { optional => 1 }), | |
201 | volume => { | |
202 | description => "Volume identifier", | |
203 | type => 'string', | |
204 | }, | |
205 | }, | |
206 | }, | |
207 | returns => { type => 'object' }, | |
208 | code => sub { | |
209 | my ($param) = @_; | |
210 | ||
211 | my $volid = &$real_volume_id($param->{storage}, $param->{volume}); | |
212 | ||
213 | my $cfg = cfs_read_file('storage.cfg'); | |
214 | ||
215 | my $path = PVE::Storage::path($cfg, $volid); | |
216 | my ($size, $format, $used) = PVE::Storage::file_size_info ($path); | |
217 | ||
218 | # fixme: return more attributes? | |
219 | return { | |
220 | path => $path, | |
221 | size => $size, | |
222 | used => $used, | |
223 | }; | |
224 | }}); | |
225 | ||
226 | __PACKAGE__->register_method ({ | |
227 | name => 'delete', | |
228 | path => '{volume}', | |
229 | method => 'DELETE', | |
230 | description => "Delete volume", | |
231 | protected => 1, | |
232 | proxyto => 'node', | |
233 | parameters => { | |
234 | additionalProperties => 0, | |
235 | properties => { | |
236 | node => get_standard_option('pve-node'), | |
237 | storage => get_standard_option('pve-storage-id', { optional => 1}), | |
238 | volume => { | |
239 | description => "Volume identifier", | |
240 | type => 'string', | |
241 | }, | |
242 | }, | |
243 | }, | |
244 | returns => { type => 'null' }, | |
245 | code => sub { | |
246 | my ($param) = @_; | |
247 | ||
248 | my $volid = &$real_volume_id($param->{storage}, $param->{volume}); | |
249 | ||
250 | my $cfg = cfs_read_file('storage.cfg'); | |
251 | ||
252 | PVE::Storage::vdisk_free ($cfg, $volid); | |
253 | ||
254 | return undef; | |
255 | }}); | |
256 | ||
257 | 1; |