]> git.proxmox.com Git - pve-storage.git/blob - PVE/CLI/pvesm.pm
factor out scan CLI definition to real API module
[pve-storage.git] / PVE / CLI / pvesm.pm
1 package PVE::CLI::pvesm;
2
3 use strict;
4 use warnings;
5
6 use POSIX qw(O_RDONLY O_WRONLY O_CREAT O_TRUNC);
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::Tools qw(extract_param);
16 use PVE::API2::Storage::Config;
17 use PVE::API2::Storage::Content;
18 use PVE::API2::Storage::PruneBackups;
19 use PVE::API2::Storage::Scan;
20 use PVE::API2::Storage::Status;
21 use PVE::JSONSchema qw(get_standard_option);
22 use PVE::PTY;
23
24 use PVE::CLIHandler;
25
26 use base qw(PVE::CLIHandler);
27
28 my $KNOWN_EXPORT_FORMATS = ['raw+size', 'tar+size', 'qcow2+size', 'vmdk+size', 'zfs'];
29
30 my $nodename = PVE::INotify::nodename();
31
32 sub param_mapping {
33 my ($name) = @_;
34
35 my $password_map = PVE::CLIHandler::get_standard_mapping('pve-password', {
36 func => sub {
37 my ($value) = @_;
38 return $value if $value;
39 return PVE::PTY::read_password("Enter Password: ");
40 },
41 });
42
43 my $enc_key_map = {
44 name => 'encryption-key',
45 desc => 'a file containing an encryption key, or the special value "autogen"',
46 func => sub {
47 my ($value) = @_;
48 return $value if $value eq 'autogen';
49 return PVE::Tools::file_get_contents($value);
50 }
51 };
52
53
54 my $mapping = {
55 'cifsscan' => [ $password_map ],
56 'create' => [ $password_map, $enc_key_map ],
57 'update' => [ $password_map, $enc_key_map ],
58 };
59 return $mapping->{$name};
60 }
61
62 sub setup_environment {
63 PVE::RPCEnvironment->setup_default_cli_env();
64 }
65
66 __PACKAGE__->register_method ({
67 name => 'apiinfo',
68 path => 'apiinfo',
69 method => 'GET',
70 description => "Returns APIVER and APIAGE.",
71 parameters => {
72 additionalProperties => 0,
73 properties => {},
74 },
75 returns => {
76 type => 'object',
77 properties => {
78 apiver => { type => 'integer' },
79 apiage => { type => 'integer' },
80 },
81 },
82 code => sub {
83 return {
84 apiver => PVE::Storage::APIVER,
85 apiage => PVE::Storage::APIAGE,
86 };
87 }
88 });
89
90 __PACKAGE__->register_method ({
91 name => 'path',
92 path => 'path',
93 method => 'GET',
94 description => "Get filesystem path for specified volume",
95 parameters => {
96 additionalProperties => 0,
97 properties => {
98 volume => {
99 description => "Volume identifier",
100 type => 'string', format => 'pve-volume-id',
101 completion => \&PVE::Storage::complete_volume,
102 },
103 },
104 },
105 returns => { type => 'null' },
106
107 code => sub {
108 my ($param) = @_;
109
110 my $cfg = PVE::Storage::config();
111
112 my $path = PVE::Storage::path ($cfg, $param->{volume});
113
114 print "$path\n";
115
116 return undef;
117
118 }});
119
120 __PACKAGE__->register_method ({
121 name => 'extractconfig',
122 path => 'extractconfig',
123 method => 'GET',
124 description => "Extract configuration from vzdump backup archive.",
125 permissions => {
126 description => "The user needs 'VM.Backup' permissions on the backed up guest ID, and 'Datastore.AllocateSpace' on the backup storage.",
127 user => 'all',
128 },
129 protected => 1,
130 parameters => {
131 additionalProperties => 0,
132 properties => {
133 volume => {
134 description => "Volume identifier",
135 type => 'string',
136 completion => \&PVE::Storage::complete_volume,
137 },
138 },
139 },
140 returns => { type => 'null' },
141 code => sub {
142 my ($param) = @_;
143 my $volume = $param->{volume};
144
145 my $rpcenv = PVE::RPCEnvironment::get();
146 my $authuser = $rpcenv->get_user();
147
148 my $storage_cfg = PVE::Storage::config();
149 PVE::Storage::check_volume_access($rpcenv, $authuser, $storage_cfg, undef, $volume);
150
151 my $config_raw = PVE::Storage::extract_vzdump_config($storage_cfg, $volume);
152
153 print "$config_raw\n";
154 return;
155 }});
156
157 my $print_content = sub {
158 my ($list) = @_;
159
160 my ($maxlenname, $maxsize) = (0, 0);
161 foreach my $info (@$list) {
162 my $volid = $info->{volid};
163 my $sidlen = length ($volid);
164 $maxlenname = $sidlen if $sidlen > $maxlenname;
165 $maxsize = $info->{size} if ($info->{size} // 0) > $maxsize;
166 }
167 my $sizemaxdigits = length($maxsize);
168
169 my $basefmt = "%-${maxlenname}s %-7s %-9s %${sizemaxdigits}s";
170 printf "$basefmt %s\n", "Volid", "Format", "Type", "Size", "VMID";
171
172 foreach my $info (@$list) {
173 next if !$info->{vmid};
174 my $volid = $info->{volid};
175
176 printf "$basefmt %d\n", $volid, $info->{format}, $info->{content}, $info->{size}, $info->{vmid};
177 }
178
179 foreach my $info (sort { $a->{format} cmp $b->{format} } @$list) {
180 next if $info->{vmid};
181 my $volid = $info->{volid};
182
183 printf "$basefmt\n", $volid, $info->{format}, $info->{content}, $info->{size};
184 }
185 };
186
187 my $print_status = sub {
188 my $res = shift;
189
190 my $maxlen = 0;
191 foreach my $res (@$res) {
192 my $storeid = $res->{storage};
193 $maxlen = length ($storeid) if length ($storeid) > $maxlen;
194 }
195 $maxlen+=1;
196
197 printf "%-${maxlen}s %10s %10s %15s %15s %15s %8s\n", 'Name', 'Type',
198 'Status', 'Total', 'Used', 'Available', '%';
199
200 foreach my $res (sort { $a->{storage} cmp $b->{storage} } @$res) {
201 my $storeid = $res->{storage};
202
203 my $active = $res->{active} ? 'active' : 'inactive';
204 my ($per, $per_fmt) = (0, '% 7.2f%%');
205 $per = ($res->{used}*100)/$res->{total} if $res->{total} > 0;
206
207 if (!$res->{enabled}) {
208 $per = 'N/A';
209 $per_fmt = '% 8s';
210 $active = 'disabled';
211 }
212
213 printf "%-${maxlen}s %10s %10s %15d %15d %15d $per_fmt\n", $storeid,
214 $res->{type}, $active, $res->{total}/1024, $res->{used}/1024,
215 $res->{avail}/1024, $per;
216 }
217 };
218
219 __PACKAGE__->register_method ({
220 name => 'export',
221 path => 'export',
222 method => 'GET',
223 description => "Used internally to export a volume.",
224 protected => 1,
225 parameters => {
226 additionalProperties => 0,
227 properties => {
228 volume => {
229 description => "Volume identifier",
230 type => 'string',
231 completion => \&PVE::Storage::complete_volume,
232 },
233 format => {
234 description => "Export stream format",
235 type => 'string',
236 enum => $KNOWN_EXPORT_FORMATS,
237 },
238 filename => {
239 description => "Destination file name",
240 type => 'string',
241 },
242 base => {
243 description => "Snapshot to start an incremental stream from",
244 type => 'string',
245 pattern => qr/[a-z0-9_\-]{1,40}/,
246 maxLength => 40,
247 optional => 1,
248 },
249 snapshot => {
250 description => "Snapshot to export",
251 type => 'string',
252 pattern => qr/[a-z0-9_\-]{1,40}/,
253 maxLength => 40,
254 optional => 1,
255 },
256 'with-snapshots' => {
257 description =>
258 "Whether to include intermediate snapshots in the stream",
259 type => 'boolean',
260 optional => 1,
261 default => 0,
262 },
263 },
264 },
265 returns => { type => 'null' },
266 code => sub {
267 my ($param) = @_;
268
269 my $filename = $param->{filename};
270
271 my $outfh;
272 if ($filename eq '-') {
273 $outfh = \*STDOUT;
274 } else {
275 sysopen($outfh, $filename, O_CREAT|O_WRONLY|O_TRUNC)
276 or die "open($filename): $!\n";
277 }
278
279 eval {
280 my $cfg = PVE::Storage::config();
281 PVE::Storage::volume_export($cfg, $outfh, $param->{volume}, $param->{format},
282 $param->{snapshot}, $param->{base}, $param->{'with-snapshots'});
283 };
284 my $err = $@;
285 if ($filename ne '-') {
286 close($outfh);
287 unlink($filename) if $err;
288 }
289 die $err if $err;
290 return;
291 }
292 });
293
294 __PACKAGE__->register_method ({
295 name => 'import',
296 path => 'import',
297 method => 'PUT',
298 description => "Used internally to import a volume.",
299 protected => 1,
300 parameters => {
301 additionalProperties => 0,
302 properties => {
303 volume => {
304 description => "Volume identifier",
305 type => 'string',
306 completion => \&PVE::Storage::complete_volume,
307 },
308 format => {
309 description => "Import stream format",
310 type => 'string',
311 enum => $KNOWN_EXPORT_FORMATS,
312 },
313 filename => {
314 description => "Source file name. For '-' stdin is used, the " .
315 "tcp://<IP-or-CIDR> format allows to use a TCP connection as input. " .
316 "Else, the file is treated as common file.",
317 type => 'string',
318 },
319 base => {
320 description => "Base snapshot of an incremental stream",
321 type => 'string',
322 pattern => qr/[a-z0-9_\-]{1,40}/,
323 maxLength => 40,
324 optional => 1,
325 },
326 'with-snapshots' => {
327 description =>
328 "Whether the stream includes intermediate snapshots",
329 type => 'boolean',
330 optional => 1,
331 default => 0,
332 },
333 'delete-snapshot' => {
334 description => "A snapshot to delete on success",
335 type => 'string',
336 pattern => qr/[a-z0-9_\-]{1,80}/,
337 maxLength => 80,
338 optional => 1,
339 },
340 'allow-rename' => {
341 description => "Choose a new volume ID if the requested " .
342 "volume ID already exists, instead of throwing an error.",
343 type => 'boolean',
344 optional => 1,
345 default => 0,
346 },
347 },
348 },
349 returns => { type => 'string' },
350 code => sub {
351 my ($param) = @_;
352
353 my $filename = $param->{filename};
354
355 my $infh;
356 if ($filename eq '-') {
357 $infh = \*STDIN;
358 } elsif ($filename =~ m!^tcp://(([^/]+)(/\d+)?)$!) {
359 my ($cidr, $ip, $subnet) = ($1, $2, $3);
360 if ($subnet) { # got real CIDR notation, not just IP
361 my $ips = PVE::Network::get_local_ip_from_cidr($cidr);
362 die "Unable to get any local IP address in network '$cidr'\n"
363 if scalar(@$ips) < 1;
364 die "Got multiple local IP address in network '$cidr'\n"
365 if scalar(@$ips) > 1;
366
367 $ip = $ips->[0];
368 }
369 my $family = PVE::Tools::get_host_address_family($ip);
370 my $port = PVE::Tools::next_migrate_port($family, $ip);
371
372 my $sock_params = {
373 Listen => 1,
374 ReuseAddr => 1,
375 Proto => &Socket::IPPROTO_TCP,
376 GetAddrInfoFlags => 0,
377 LocalAddr => $ip,
378 LocalPort => $port,
379 };
380 my $socket = IO::Socket::IP->new(%$sock_params)
381 or die "failed to open socket: $!\n";
382
383 print "$ip\n$port\n"; # tell remote where to connect
384 *STDOUT->flush();
385
386 my $prev_alarm = alarm 0;
387 local $SIG{ALRM} = sub { die "timed out waiting for client\n" };
388 alarm 30;
389 my $client = $socket->accept; # Wait for a client
390 alarm $prev_alarm;
391 close($socket);
392
393 $infh = \*$client;
394 } else {
395 sysopen($infh, $filename, O_RDONLY)
396 or die "open($filename): $!\n";
397 }
398
399 my $cfg = PVE::Storage::config();
400 my $volume = $param->{volume};
401 my $delete = $param->{'delete-snapshot'};
402 my $imported_volid = PVE::Storage::volume_import($cfg, $infh, $volume, $param->{format},
403 $param->{base}, $param->{'with-snapshots'}, $param->{'allow-rename'});
404 PVE::Storage::volume_snapshot_delete($cfg, $imported_volid, $delete)
405 if defined($delete);
406 return $imported_volid;
407 }
408 });
409
410 __PACKAGE__->register_method ({
411 name => 'prunebackups',
412 path => 'prunebackups',
413 method => 'GET',
414 description => "Prune backups. Only those using the standard naming scheme are considered. " .
415 "If no keep options are specified, those from the storage configuration are used.",
416 protected => 1,
417 proxyto => 'node',
418 parameters => {
419 additionalProperties => 0,
420 properties => {
421 'dry-run' => {
422 description => "Only show what would be pruned, don't delete anything.",
423 type => 'boolean',
424 optional => 1,
425 },
426 node => get_standard_option('pve-node'),
427 storage => get_standard_option('pve-storage-id', {
428 completion => \&PVE::Storage::complete_storage_enabled,
429 }),
430 %{$PVE::Storage::Plugin::prune_backups_format},
431 type => {
432 description => "Either 'qemu' or 'lxc'. Only consider backups for guests of this type.",
433 type => 'string',
434 optional => 1,
435 enum => ['qemu', 'lxc'],
436 },
437 vmid => get_standard_option('pve-vmid', {
438 description => "Only consider backups for this guest.",
439 optional => 1,
440 completion => \&PVE::Cluster::complete_vmid,
441 }),
442 },
443 },
444 returns => {
445 type => 'object',
446 properties => {
447 dryrun => {
448 description => 'If it was a dry run or not. The list will only be defined in that case.',
449 type => 'boolean',
450 },
451 list => {
452 type => 'array',
453 items => {
454 type => 'object',
455 properties => {
456 volid => {
457 description => "Backup volume ID.",
458 type => 'string',
459 },
460 'ctime' => {
461 description => "Creation time of the backup (seconds since the UNIX epoch).",
462 type => 'integer',
463 },
464 'mark' => {
465 description => "Whether the backup would be kept or removed. For backups that don't " .
466 "use the standard naming scheme, it's 'protected'.",
467 type => 'string',
468 },
469 type => {
470 description => "One of 'qemu', 'lxc', 'openvz' or 'unknown'.",
471 type => 'string',
472 },
473 'vmid' => {
474 description => "The VM the backup belongs to.",
475 type => 'integer',
476 optional => 1,
477 },
478 },
479 },
480 },
481 },
482 },
483 code => sub {
484 my ($param) = @_;
485
486 my $dryrun = extract_param($param, 'dry-run') ? 1 : 0;
487
488 my $keep_opts;
489 foreach my $keep (keys %{$PVE::Storage::Plugin::prune_backups_format}) {
490 $keep_opts->{$keep} = extract_param($param, $keep) if defined($param->{$keep});
491 }
492 $param->{'prune-backups'} = PVE::JSONSchema::print_property_string(
493 $keep_opts, $PVE::Storage::Plugin::prune_backups_format) if $keep_opts;
494
495 my $list = [];
496 if ($dryrun) {
497 $list = PVE::API2::Storage::PruneBackups->dryrun($param);
498 } else {
499 PVE::API2::Storage::PruneBackups->delete($param);
500 }
501
502 return {
503 dryrun => $dryrun,
504 list => $list,
505 };
506 }});
507
508 our $cmddef = {
509 add => [ "PVE::API2::Storage::Config", 'create', ['type', 'storage'] ],
510 set => [ "PVE::API2::Storage::Config", 'update', ['storage'] ],
511 remove => [ "PVE::API2::Storage::Config", 'delete', ['storage'] ],
512 status => [ "PVE::API2::Storage::Status", 'index', [],
513 { node => $nodename }, $print_status ],
514 list => [ "PVE::API2::Storage::Content", 'index', ['storage'],
515 { node => $nodename }, $print_content ],
516 alloc => [ "PVE::API2::Storage::Content", 'create', ['storage', 'vmid', 'filename', 'size'],
517 { node => $nodename }, sub {
518 my $volid = shift;
519 print "successfully created '$volid'\n";
520 }],
521 free => [ "PVE::API2::Storage::Content", 'delete', ['volume'],
522 { node => $nodename } ],
523 scan => {
524 nfs => [ "PVE::API2::Storage::Scan", 'nfsscan', ['server'], { node => $nodename }, sub {
525 my $res = shift;
526
527 my $maxlen = 0;
528 foreach my $rec (@$res) {
529 my $len = length ($rec->{path});
530 $maxlen = $len if $len > $maxlen;
531 }
532 foreach my $rec (@$res) {
533 printf "%-${maxlen}s %s\n", $rec->{path}, $rec->{options};
534 }
535 }],
536 cifs => [ "PVE::API2::Storage::Scan", 'cifsscan', ['server'], { node => $nodename }, sub {
537 my $res = shift;
538
539 my $maxlen = 0;
540 foreach my $rec (@$res) {
541 my $len = length ($rec->{share});
542 $maxlen = $len if $len > $maxlen;
543 }
544 foreach my $rec (@$res) {
545 printf "%-${maxlen}s %s\n", $rec->{share}, $rec->{description};
546 }
547 }],
548 glusterfs => [ "PVE::API2::Storage::Scan", 'glusterfsscan', ['server'], { node => $nodename }, sub {
549 my $res = shift;
550
551 foreach my $rec (@$res) {
552 printf "%s\n", $rec->{volname};
553 }
554 }],
555 iscsi => [ "PVE::API2::Storage::Scan", 'iscsiscan', ['portal'], { node => $nodename }, sub {
556 my $res = shift;
557
558 my $maxlen = 0;
559 foreach my $rec (@$res) {
560 my $len = length ($rec->{target});
561 $maxlen = $len if $len > $maxlen;
562 }
563 foreach my $rec (@$res) {
564 printf "%-${maxlen}s %s\n", $rec->{target}, $rec->{portal};
565 }
566 }],
567 lvm => [ "PVE::API2::Storage::Scan", 'lvmscan', [], { node => $nodename }, sub {
568 my $res = shift;
569 foreach my $rec (@$res) {
570 printf "$rec->{vg}\n";
571 }
572 }],
573 lvmthin => [ "PVE::API2::Storage::Scan", 'lvmthinscan', ['vg'], { node => $nodename }, sub {
574 my $res = shift;
575 foreach my $rec (@$res) {
576 printf "$rec->{lv}\n";
577 }
578 }],
579 zfs => [ "PVE::API2::Storage::Scan", 'zfsscan', [], { node => $nodename }, sub {
580 my $res = shift;
581
582 foreach my $rec (@$res) {
583 printf "$rec->{pool}\n";
584 }
585 }],
586 },
587 nfsscan => { alias => 'scan nfs' },
588 cifsscan => { alias => 'scan cifs' },
589 glusterfsscan => { alias => 'scan glusterfs' },
590 iscsiscan => { alias => 'scan iscsi' },
591 lvmscan => { alias => 'scan lvm' },
592 lvmthinscan => { alias => 'scan lvmthin' },
593 zfsscan => { alias => 'scan zfs' },
594 path => [ __PACKAGE__, 'path', ['volume']],
595 extractconfig => [__PACKAGE__, 'extractconfig', ['volume']],
596 export => [ __PACKAGE__, 'export', ['volume', 'format', 'filename']],
597 import => [ __PACKAGE__, 'import', ['volume', 'format', 'filename'], {}, sub {
598 my $volid = shift;
599 print PVE::Storage::volume_imported_message($volid);
600 }],
601 apiinfo => [ __PACKAGE__, 'apiinfo', [], {}, sub {
602 my $res = shift;
603
604 print "APIVER $res->{apiver}\n";
605 print "APIAGE $res->{apiage}\n";
606 }],
607 'prune-backups' => [ __PACKAGE__, 'prunebackups', ['storage'], { node => $nodename }, sub {
608 my $res = shift;
609
610 my ($dryrun, $list) = ($res->{dryrun}, $res->{list});
611
612 return if !$dryrun;
613
614 if (!scalar(@{$list})) {
615 print "No backups found\n";
616 return;
617 }
618
619 print "NOTE: this is only a preview and might not be what a subsequent\n" .
620 "prune call does if backups are removed/added in the meantime.\n\n";
621
622 my @sorted = sort {
623 my $vmcmp = PVE::Tools::safe_compare($a->{vmid}, $b->{vmid}, sub { $_[0] <=> $_[1] });
624 return $vmcmp if $vmcmp ne 0;
625 return $a->{ctime} <=> $b->{ctime};
626 } @{$list};
627
628 my $maxlen = 0;
629 foreach my $backup (@sorted) {
630 my $volid = $backup->{volid};
631 $maxlen = length($volid) if length($volid) > $maxlen;
632 }
633 $maxlen+=1;
634
635 printf("%-${maxlen}s %15s %10s\n", 'Backup', 'Backup-ID', 'Prune-Mark');
636 foreach my $backup (@sorted) {
637 my $type = $backup->{type};
638 my $vmid = $backup->{vmid};
639 my $backup_id = defined($vmid) ? "$type/$vmid" : "$type";
640 printf("%-${maxlen}s %15s %10s\n", $backup->{volid}, $backup_id, $backup->{mark});
641 }
642 }],
643 };
644
645 1;