]>
Commit | Line | Data |
---|---|---|
c669f42d DM |
1 | package PVE::CLI::pvesm; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use Fcntl ':flock'; | |
7 | use File::Path; | |
8 | ||
9 | use PVE::SafeSyslog; | |
10 | use PVE::Cluster; | |
11 | use PVE::INotify; | |
12 | use PVE::RPCEnvironment; | |
13 | use PVE::Storage; | |
14 | use PVE::API2::Storage::Config; | |
15 | use PVE::API2::Storage::Content; | |
16 | use PVE::API2::Storage::Status; | |
17 | use PVE::API2::Storage::Scan; | |
18 | use PVE::JSONSchema qw(get_standard_option); | |
19 | ||
20 | use PVE::CLIHandler; | |
21 | ||
22 | use base qw(PVE::CLIHandler); | |
23 | ||
47f37b53 WB |
24 | my $KNOWN_EXPORT_FORMATS = ['zfs']; |
25 | ||
c669f42d DM |
26 | my $nodename = PVE::INotify::nodename(); |
27 | ||
f984732e DM |
28 | sub setup_environment { |
29 | PVE::RPCEnvironment->setup_default_cli_env(); | |
30 | } | |
31 | ||
c669f42d DM |
32 | __PACKAGE__->register_method ({ |
33 | name => 'path', | |
34 | path => 'path', | |
35 | method => 'GET', | |
36 | description => "Get filesystem path for specified volume", | |
37 | parameters => { | |
38 | additionalProperties => 0, | |
39 | properties => { | |
40 | volume => { | |
41 | description => "Volume identifier", | |
42 | type => 'string', format => 'pve-volume-id', | |
f3bd890d | 43 | completion => \&PVE::Storage::complete_volume, |
c669f42d DM |
44 | }, |
45 | }, | |
46 | }, | |
47 | returns => { type => 'null' }, | |
48 | ||
49 | code => sub { | |
50 | my ($param) = @_; | |
51 | ||
52 | my $cfg = PVE::Storage::config(); | |
53 | ||
54 | my $path = PVE::Storage::path ($cfg, $param->{volume}); | |
55 | ||
56 | print "$path\n"; | |
57 | ||
58 | return undef; | |
59 | ||
60 | }}); | |
61 | ||
fa017b96 FG |
62 | __PACKAGE__->register_method ({ |
63 | name => 'extractconfig', | |
64 | path => 'extractconfig', | |
65 | method => 'GET', | |
66 | description => "Extract configuration from vzdump backup archive.", | |
67 | permissions => { | |
68 | description => "The user needs 'VM.Backup' permissions on the backed up guest ID, and 'Datastore.AllocateSpace' on the backup storage.", | |
69 | user => 'all', | |
70 | }, | |
71 | protected => 1, | |
72 | parameters => { | |
73 | additionalProperties => 0, | |
74 | properties => { | |
75 | volume => { | |
76 | description => "Volume identifier", | |
77 | type => 'string', | |
78 | completion => \&PVE::Storage::complete_volume, | |
79 | }, | |
80 | }, | |
81 | }, | |
82 | returns => { type => 'null' }, | |
83 | code => sub { | |
84 | my ($param) = @_; | |
85 | my $volume = $param->{volume}; | |
86 | ||
87 | my $rpcenv = PVE::RPCEnvironment::get(); | |
88 | my $authuser = $rpcenv->get_user(); | |
89 | ||
90 | my $storage_cfg = PVE::Storage::config(); | |
04a13668 | 91 | PVE::Storage::check_volume_access($rpcenv, $authuser, $storage_cfg, undef, $volume); |
fa017b96 FG |
92 | |
93 | my $config_raw = PVE::Storage::extract_vzdump_config($storage_cfg, $volume); | |
94 | ||
95 | print "$config_raw\n"; | |
96 | return; | |
97 | }}); | |
98 | ||
c669f42d DM |
99 | my $print_content = sub { |
100 | my ($list) = @_; | |
101 | ||
102 | my $maxlenname = 0; | |
103 | foreach my $info (@$list) { | |
104 | ||
105 | my $volid = $info->{volid}; | |
106 | my $sidlen = length ($volid); | |
107 | $maxlenname = $sidlen if $sidlen > $maxlenname; | |
108 | } | |
109 | ||
110 | foreach my $info (@$list) { | |
111 | next if !$info->{vmid}; | |
112 | my $volid = $info->{volid}; | |
113 | ||
114 | printf "%-${maxlenname}s %5s %10d %d\n", $volid, | |
115 | $info->{format}, $info->{size}, $info->{vmid}; | |
116 | } | |
117 | ||
118 | foreach my $info (sort { $a->{format} cmp $b->{format} } @$list) { | |
119 | next if $info->{vmid}; | |
120 | my $volid = $info->{volid}; | |
121 | ||
122 | printf "%-${maxlenname}s %5s %10d\n", $volid, | |
123 | $info->{format}, $info->{size}; | |
124 | } | |
125 | }; | |
126 | ||
127 | my $print_status = sub { | |
128 | my $res = shift; | |
129 | ||
130 | my $maxlen = 0; | |
131 | foreach my $res (@$res) { | |
132 | my $storeid = $res->{storage}; | |
133 | $maxlen = length ($storeid) if length ($storeid) > $maxlen; | |
134 | } | |
135 | $maxlen+=1; | |
136 | ||
137 | foreach my $res (sort { $a->{storage} cmp $b->{storage} } @$res) { | |
138 | my $storeid = $res->{storage}; | |
139 | ||
140 | my $sum = $res->{used} + $res->{avail}; | |
141 | my $per = $sum ? (0.5 + ($res->{used}*100)/$sum) : 100; | |
142 | ||
143 | printf "%-${maxlen}s %5s %1d %15d %15d %15d %.2f%%\n", $storeid, | |
144 | $res->{type}, $res->{active}, | |
145 | $res->{total}/1024, $res->{used}/1024, $res->{avail}/1024, $per; | |
146 | } | |
147 | }; | |
148 | ||
47f37b53 WB |
149 | __PACKAGE__->register_method ({ |
150 | name => 'export', | |
151 | path => 'export', | |
152 | method => 'GET', | |
153 | description => "Export a volume.", | |
154 | protected => 1, | |
155 | parameters => { | |
156 | additionalProperties => 0, | |
157 | properties => { | |
158 | volume => { | |
159 | description => "Volume identifier", | |
160 | type => 'string', | |
161 | completion => \&PVE::Storage::complete_volume, | |
162 | }, | |
163 | format => { | |
164 | description => "Export stream format", | |
165 | type => 'string', | |
166 | enum => $KNOWN_EXPORT_FORMATS, | |
167 | }, | |
168 | filename => { | |
169 | description => "Destination file name", | |
170 | type => 'string', | |
171 | }, | |
172 | base => { | |
173 | description => "Snapshot to start an incremental stream from", | |
174 | type => 'string', | |
175 | pattern => qr/[a-z0-9_\-]{1,40}/, | |
176 | maxLength => 40, | |
177 | optional => 1, | |
178 | }, | |
179 | snapshot => { | |
180 | description => "Snapshot to export", | |
181 | type => 'string', | |
182 | pattern => qr/[a-z0-9_\-]{1,40}/, | |
183 | maxLength => 40, | |
184 | optional => 1, | |
185 | }, | |
186 | 'with-snapshots' => { | |
187 | description => | |
188 | "Whether to include intermediate snapshots in the stream", | |
189 | type => 'boolean', | |
190 | optional => 1, | |
191 | default => 0, | |
192 | }, | |
193 | }, | |
194 | }, | |
195 | returns => { type => 'null' }, | |
196 | code => sub { | |
197 | my ($param) = @_; | |
198 | ||
199 | my $filename = $param->{filename}; | |
200 | ||
201 | my $outfh; | |
202 | if ($filename eq '-') { | |
203 | $outfh = \*STDOUT; | |
204 | } else { | |
205 | open($outfh, '>', $filename) | |
206 | or die "open($filename): $!\n"; | |
207 | } | |
208 | ||
209 | eval { | |
210 | my $cfg = PVE::Storage::config(); | |
211 | PVE::Storage::volume_export($cfg, $outfh, $param->{volume}, $param->{format}, | |
212 | $param->{snapshot}, $param->{base}, $param->{'with-snapshots'}); | |
213 | }; | |
214 | my $err = $@; | |
215 | if ($filename ne '-') { | |
216 | close($outfh); | |
217 | unlink($filename) if $err; | |
218 | } | |
219 | die $err if $err; | |
220 | return; | |
221 | } | |
222 | }); | |
223 | ||
224 | __PACKAGE__->register_method ({ | |
225 | name => 'import', | |
226 | path => 'import', | |
227 | method => 'PUT', | |
228 | description => "Import a volume.", | |
229 | protected => 1, | |
230 | parameters => { | |
231 | additionalProperties => 0, | |
232 | properties => { | |
233 | volume => { | |
234 | description => "Volume identifier", | |
235 | type => 'string', | |
236 | completion => \&PVE::Storage::complete_volume, | |
237 | }, | |
238 | format => { | |
239 | description => "Import stream format", | |
240 | type => 'string', | |
241 | enum => $KNOWN_EXPORT_FORMATS, | |
242 | }, | |
243 | filename => { | |
244 | description => "Source file name", | |
245 | type => 'string', | |
246 | }, | |
247 | base => { | |
248 | description => "Base snapshot of an incremental stream", | |
249 | type => 'string', | |
250 | pattern => qr/[a-z0-9_\-]{1,40}/, | |
251 | maxLength => 40, | |
252 | optional => 1, | |
253 | }, | |
254 | 'with-snapshots' => { | |
255 | description => | |
256 | "Whether the stream includes intermediate snapshots", | |
257 | type => 'boolean', | |
258 | optional => 1, | |
259 | default => 0, | |
260 | }, | |
261 | }, | |
262 | }, | |
263 | returns => { type => 'null' }, | |
264 | code => sub { | |
265 | my ($param) = @_; | |
266 | ||
267 | my $filename = $param->{filename}; | |
268 | ||
269 | my $infh; | |
270 | if ($filename eq '-') { | |
271 | $infh = \*STDIN; | |
272 | } else { | |
273 | open($infh, '<', $filename) | |
274 | or die "open($filename): $!\n"; | |
275 | } | |
276 | ||
277 | my $cfg = PVE::Storage::config(); | |
278 | PVE::Storage::volume_import($cfg, $infh, $param->{volume}, $param->{format}, | |
279 | $param->{base}, $param->{'with-snapshots'}); | |
280 | return; | |
281 | } | |
282 | }); | |
283 | ||
c669f42d DM |
284 | our $cmddef = { |
285 | add => [ "PVE::API2::Storage::Config", 'create', ['type', 'storage'] ], | |
286 | set => [ "PVE::API2::Storage::Config", 'update', ['storage'] ], | |
287 | remove => [ "PVE::API2::Storage::Config", 'delete', ['storage'] ], | |
288 | status => [ "PVE::API2::Storage::Status", 'index', [], | |
289 | { node => $nodename }, $print_status ], | |
290 | list => [ "PVE::API2::Storage::Content", 'index', ['storage'], | |
291 | { node => $nodename }, $print_content ], | |
292 | alloc => [ "PVE::API2::Storage::Content", 'create', ['storage', 'vmid', 'filename', 'size'], | |
293 | { node => $nodename }, sub { | |
294 | my $volid = shift; | |
e967e0ef | 295 | print "successfully created '$volid'\n"; |
c669f42d DM |
296 | }], |
297 | free => [ "PVE::API2::Storage::Content", 'delete', ['volume'], | |
298 | { node => $nodename } ], | |
299 | nfsscan => [ "PVE::API2::Storage::Scan", 'nfsscan', ['server'], | |
300 | { node => $nodename }, sub { | |
301 | my $res = shift; | |
302 | ||
303 | my $maxlen = 0; | |
304 | foreach my $rec (@$res) { | |
305 | my $len = length ($rec->{path}); | |
306 | $maxlen = $len if $len > $maxlen; | |
307 | } | |
308 | foreach my $rec (@$res) { | |
309 | printf "%-${maxlen}s %s\n", $rec->{path}, $rec->{options}; | |
310 | } | |
311 | }], | |
312 | glusterfsscan => [ "PVE::API2::Storage::Scan", 'glusterfsscan', ['server'], | |
313 | { node => $nodename }, sub { | |
314 | my $res = shift; | |
315 | ||
316 | foreach my $rec (@$res) { | |
317 | printf "%s\n", $rec->{volname}; | |
318 | } | |
319 | }], | |
320 | iscsiscan => [ "PVE::API2::Storage::Scan", 'iscsiscan', ['server'], | |
321 | { node => $nodename }, sub { | |
322 | my $res = shift; | |
323 | ||
324 | my $maxlen = 0; | |
325 | foreach my $rec (@$res) { | |
326 | my $len = length ($rec->{target}); | |
327 | $maxlen = $len if $len > $maxlen; | |
328 | } | |
329 | foreach my $rec (@$res) { | |
330 | printf "%-${maxlen}s %s\n", $rec->{target}, $rec->{portal}; | |
331 | } | |
332 | }], | |
333 | lvmscan => [ "PVE::API2::Storage::Scan", 'lvmscan', [], | |
334 | { node => $nodename }, sub { | |
335 | my $res = shift; | |
336 | foreach my $rec (@$res) { | |
337 | printf "$rec->{vg}\n"; | |
338 | } | |
339 | }], | |
668f6d9f DM |
340 | lvmthinscan => [ "PVE::API2::Storage::Scan", 'lvmthinscan', ['vg'], |
341 | { node => $nodename }, sub { | |
342 | my $res = shift; | |
343 | foreach my $rec (@$res) { | |
344 | printf "$rec->{lv}\n"; | |
345 | } | |
346 | }], | |
c669f42d DM |
347 | zfsscan => [ "PVE::API2::Storage::Scan", 'zfsscan', [], |
348 | { node => $nodename }, sub { | |
349 | my $res = shift; | |
350 | ||
351 | foreach my $rec (@$res) { | |
352 | printf "$rec->{pool}\n"; | |
353 | } | |
354 | }], | |
355 | path => [ __PACKAGE__, 'path', ['volume']], | |
fa017b96 | 356 | extractconfig => [__PACKAGE__, 'extractconfig', ['volume']], |
47f37b53 WB |
357 | export => [ __PACKAGE__, 'export', ['volume', 'format', 'filename']], |
358 | import => [ __PACKAGE__, 'import', ['volume', 'format', 'filename']], | |
c669f42d DM |
359 | }; |
360 | ||
361 | 1; |