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