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