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