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