]> git.proxmox.com Git - pve-storage.git/blob - PVE/CLI/pvesm.pm
Add apiinfo helper to pvesm
[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::API2::Storage::Config;
16 use PVE::API2::Storage::Content;
17 use PVE::API2::Storage::Status;
18 use PVE::JSONSchema qw(get_standard_option);
19 use PVE::PTY;
20
21 use PVE::CLIHandler;
22
23 use base qw(PVE::CLIHandler);
24
25 my $KNOWN_EXPORT_FORMATS = ['raw+size', 'tar+size', 'qcow2+size', 'vmdk+size', 'zfs'];
26
27 my $nodename = PVE::INotify::nodename();
28
29 sub param_mapping {
30 my ($name) = @_;
31
32 my $password_map = PVE::CLIHandler::get_standard_mapping('pve-password', {
33 func => sub {
34 my ($value) = @_;
35 return $value if $value;
36 return PVE::PTY::read_password("Enter Password: ");
37 },
38 });
39 my $mapping = {
40 'cifsscan' => [ $password_map ],
41 'create' => [ $password_map ],
42 };
43 return $mapping->{$name};
44 }
45
46 sub setup_environment {
47 PVE::RPCEnvironment->setup_default_cli_env();
48 }
49
50 __PACKAGE__->register_method ({
51 name => 'apiinfo',
52 path => 'apiinfo',
53 method => 'GET',
54 description => "Returns APIVER and APIAGE.",
55 parameters => {
56 additionalProperties => 0,
57 properties => {},
58 },
59 returns => {
60 type => 'object',
61 properties => {
62 apiver => { type => 'integer' },
63 apiage => { type => 'integer' },
64 },
65 },
66 code => sub {
67 return {
68 apiver => PVE::Storage::APIVER,
69 apiage => PVE::Storage::APIAGE,
70 };
71 }
72 });
73
74 __PACKAGE__->register_method ({
75 name => 'path',
76 path => 'path',
77 method => 'GET',
78 description => "Get filesystem path for specified volume",
79 parameters => {
80 additionalProperties => 0,
81 properties => {
82 volume => {
83 description => "Volume identifier",
84 type => 'string', format => 'pve-volume-id',
85 completion => \&PVE::Storage::complete_volume,
86 },
87 },
88 },
89 returns => { type => 'null' },
90
91 code => sub {
92 my ($param) = @_;
93
94 my $cfg = PVE::Storage::config();
95
96 my $path = PVE::Storage::path ($cfg, $param->{volume});
97
98 print "$path\n";
99
100 return undef;
101
102 }});
103
104 __PACKAGE__->register_method ({
105 name => 'extractconfig',
106 path => 'extractconfig',
107 method => 'GET',
108 description => "Extract configuration from vzdump backup archive.",
109 permissions => {
110 description => "The user needs 'VM.Backup' permissions on the backed up guest ID, and 'Datastore.AllocateSpace' on the backup storage.",
111 user => 'all',
112 },
113 protected => 1,
114 parameters => {
115 additionalProperties => 0,
116 properties => {
117 volume => {
118 description => "Volume identifier",
119 type => 'string',
120 completion => \&PVE::Storage::complete_volume,
121 },
122 },
123 },
124 returns => { type => 'null' },
125 code => sub {
126 my ($param) = @_;
127 my $volume = $param->{volume};
128
129 my $rpcenv = PVE::RPCEnvironment::get();
130 my $authuser = $rpcenv->get_user();
131
132 my $storage_cfg = PVE::Storage::config();
133 PVE::Storage::check_volume_access($rpcenv, $authuser, $storage_cfg, undef, $volume);
134
135 my $config_raw = PVE::Storage::extract_vzdump_config($storage_cfg, $volume);
136
137 print "$config_raw\n";
138 return;
139 }});
140
141 my $print_content = sub {
142 my ($list) = @_;
143
144 my ($maxlenname, $maxsize) = (0, 0);
145 foreach my $info (@$list) {
146 my $volid = $info->{volid};
147 my $sidlen = length ($volid);
148 $maxlenname = $sidlen if $sidlen > $maxlenname;
149 $maxsize = $info->{size} if ($info->{size} // 0) > $maxsize;
150 }
151 my $sizemaxdigits = length($maxsize);
152
153 my $basefmt = "%-${maxlenname}s %-7s %-9s %${sizemaxdigits}s";
154 printf "$basefmt %s\n", "Volid", "Format", "Type", "Size", "VMID";
155
156 foreach my $info (@$list) {
157 next if !$info->{vmid};
158 my $volid = $info->{volid};
159
160 printf "$basefmt %d\n", $volid, $info->{format}, $info->{content}, $info->{size}, $info->{vmid};
161 }
162
163 foreach my $info (sort { $a->{format} cmp $b->{format} } @$list) {
164 next if $info->{vmid};
165 my $volid = $info->{volid};
166
167 printf "$basefmt\n", $volid, $info->{format}, $info->{content}, $info->{size};
168 }
169 };
170
171 my $print_status = sub {
172 my $res = shift;
173
174 my $maxlen = 0;
175 foreach my $res (@$res) {
176 my $storeid = $res->{storage};
177 $maxlen = length ($storeid) if length ($storeid) > $maxlen;
178 }
179 $maxlen+=1;
180
181 printf "%-${maxlen}s %10s %10s %15s %15s %15s %8s\n", 'Name', 'Type',
182 'Status', 'Total', 'Used', 'Available', '%';
183
184 foreach my $res (sort { $a->{storage} cmp $b->{storage} } @$res) {
185 my $storeid = $res->{storage};
186
187 my $active = $res->{active} ? 'active' : 'inactive';
188 my ($per, $per_fmt) = (0, '% 7.2f%%');
189 $per = ($res->{used}*100)/$res->{total} if $res->{total} > 0;
190
191 if (!$res->{enabled}) {
192 $per = 'N/A';
193 $per_fmt = '% 8s';
194 $active = 'disabled';
195 }
196
197 printf "%-${maxlen}s %10s %10s %15d %15d %15d $per_fmt\n", $storeid,
198 $res->{type}, $active, $res->{total}/1024, $res->{used}/1024,
199 $res->{avail}/1024, $per;
200 }
201 };
202
203 __PACKAGE__->register_method ({
204 name => 'export',
205 path => 'export',
206 method => 'GET',
207 description => "Export a volume.",
208 protected => 1,
209 parameters => {
210 additionalProperties => 0,
211 properties => {
212 volume => {
213 description => "Volume identifier",
214 type => 'string',
215 completion => \&PVE::Storage::complete_volume,
216 },
217 format => {
218 description => "Export stream format",
219 type => 'string',
220 enum => $KNOWN_EXPORT_FORMATS,
221 },
222 filename => {
223 description => "Destination file name",
224 type => 'string',
225 },
226 base => {
227 description => "Snapshot to start an incremental stream from",
228 type => 'string',
229 pattern => qr/[a-z0-9_\-]{1,40}/,
230 maxLength => 40,
231 optional => 1,
232 },
233 snapshot => {
234 description => "Snapshot to export",
235 type => 'string',
236 pattern => qr/[a-z0-9_\-]{1,40}/,
237 maxLength => 40,
238 optional => 1,
239 },
240 'with-snapshots' => {
241 description =>
242 "Whether to include intermediate snapshots in the stream",
243 type => 'boolean',
244 optional => 1,
245 default => 0,
246 },
247 },
248 },
249 returns => { type => 'null' },
250 code => sub {
251 my ($param) = @_;
252
253 my $filename = $param->{filename};
254
255 my $outfh;
256 if ($filename eq '-') {
257 $outfh = \*STDOUT;
258 } else {
259 sysopen($outfh, $filename, O_CREAT|O_WRONLY|O_TRUNC)
260 or die "open($filename): $!\n";
261 }
262
263 eval {
264 my $cfg = PVE::Storage::config();
265 PVE::Storage::volume_export($cfg, $outfh, $param->{volume}, $param->{format},
266 $param->{snapshot}, $param->{base}, $param->{'with-snapshots'});
267 };
268 my $err = $@;
269 if ($filename ne '-') {
270 close($outfh);
271 unlink($filename) if $err;
272 }
273 die $err if $err;
274 return;
275 }
276 });
277
278 __PACKAGE__->register_method ({
279 name => 'import',
280 path => 'import',
281 method => 'PUT',
282 description => "Import a volume.",
283 protected => 1,
284 parameters => {
285 additionalProperties => 0,
286 properties => {
287 volume => {
288 description => "Volume identifier",
289 type => 'string',
290 completion => \&PVE::Storage::complete_volume,
291 },
292 format => {
293 description => "Import stream format",
294 type => 'string',
295 enum => $KNOWN_EXPORT_FORMATS,
296 },
297 filename => {
298 description => "Source file name. For '-' stdin is used, the " .
299 "tcp://<IP-or-CIDR> format allows to use a TCP connection as input. " .
300 "Else, the file is treated as common file.",
301 type => 'string',
302 },
303 base => {
304 description => "Base snapshot of an incremental stream",
305 type => 'string',
306 pattern => qr/[a-z0-9_\-]{1,40}/,
307 maxLength => 40,
308 optional => 1,
309 },
310 'with-snapshots' => {
311 description =>
312 "Whether the stream includes intermediate snapshots",
313 type => 'boolean',
314 optional => 1,
315 default => 0,
316 },
317 'delete-snapshot' => {
318 description => "A snapshot to delete on success",
319 type => 'string',
320 pattern => qr/[a-z0-9_\-]{1,80}/,
321 maxLength => 80,
322 optional => 1,
323 },
324 },
325 },
326 returns => { type => 'null' },
327 code => sub {
328 my ($param) = @_;
329
330 my $filename = $param->{filename};
331
332 my $infh;
333 if ($filename eq '-') {
334 $infh = \*STDIN;
335 } elsif ($filename =~ m!^tcp://(([^/]+)(/\d+)?)$!) {
336 my ($cidr, $ip, $subnet) = ($1, $2, $3);
337 if ($subnet) { # got real CIDR notation, not just IP
338 my $ips = PVE::Network::get_local_ip_from_cidr($cidr);
339 die "Unable to get any local IP address in network '$cidr'\n"
340 if scalar(@$ips) < 1;
341 die "Got multiple local IP address in network '$cidr'\n"
342 if scalar(@$ips) > 1;
343
344 $ip = $ips->[0];
345 }
346 my $family = PVE::Tools::get_host_address_family($ip);
347 my $port = PVE::Tools::next_migrate_port($family, $ip);
348
349 my $sock_params = {
350 Listen => 1,
351 ReuseAddr => 1,
352 Proto => &Socket::IPPROTO_TCP,
353 GetAddrInfoFlags => 0,
354 LocalAddr => $ip,
355 LocalPort => $port,
356 };
357 my $socket = IO::Socket::IP->new(%$sock_params)
358 or die "failed to open socket: $!\n";
359
360 print "$ip\n$port\n"; # tell remote where to connect
361 *STDOUT->flush();
362
363 my $prev_alarm = alarm 0;
364 local $SIG{ALRM} = sub { die "timed out waiting for client\n" };
365 alarm 30;
366 my $client = $socket->accept; # Wait for a client
367 alarm $prev_alarm;
368 close($socket);
369
370 $infh = \*$client;
371 } else {
372 sysopen($infh, $filename, O_RDONLY)
373 or die "open($filename): $!\n";
374 }
375
376 my $cfg = PVE::Storage::config();
377 my $volume = $param->{volume};
378 my $delete = $param->{'delete-snapshot'};
379 PVE::Storage::volume_import($cfg, $infh, $volume, $param->{format},
380 $param->{base}, $param->{'with-snapshots'});
381 PVE::Storage::volume_snapshot_delete($cfg, $volume, $delete)
382 if defined($delete);
383 return;
384 }
385 });
386
387 __PACKAGE__->register_method ({
388 name => 'nfsscan',
389 path => 'nfs',
390 method => 'GET',
391 description => "Scan remote NFS server.",
392 protected => 1,
393 proxyto => "node",
394 permissions => {
395 check => ['perm', '/storage', ['Datastore.Allocate']],
396 },
397 parameters => {
398 additionalProperties => 0,
399 properties => {
400 node => get_standard_option('pve-node'),
401 server => {
402 description => "The server address (name or IP).",
403 type => 'string', format => 'pve-storage-server',
404 },
405 },
406 },
407 returns => {
408 type => 'array',
409 items => {
410 type => "object",
411 properties => {
412 path => {
413 description => "The exported path.",
414 type => 'string',
415 },
416 options => {
417 description => "NFS export options.",
418 type => 'string',
419 },
420 },
421 },
422 },
423 code => sub {
424 my ($param) = @_;
425
426 my $server = $param->{server};
427 my $res = PVE::Storage::scan_nfs($server);
428
429 my $data = [];
430 foreach my $k (keys %$res) {
431 push @$data, { path => $k, options => $res->{$k} };
432 }
433 return $data;
434 }});
435
436 __PACKAGE__->register_method ({
437 name => 'cifsscan',
438 path => 'cifs',
439 method => 'GET',
440 description => "Scan remote CIFS server.",
441 protected => 1,
442 proxyto => "node",
443 permissions => {
444 check => ['perm', '/storage', ['Datastore.Allocate']],
445 },
446 parameters => {
447 additionalProperties => 0,
448 properties => {
449 node => get_standard_option('pve-node'),
450 server => {
451 description => "The server address (name or IP).",
452 type => 'string', format => 'pve-storage-server',
453 },
454 username => {
455 description => "User name.",
456 type => 'string',
457 optional => 1,
458 },
459 password => {
460 description => "User password.",
461 type => 'string',
462 optional => 1,
463 },
464 domain => {
465 description => "SMB domain (Workgroup).",
466 type => 'string',
467 optional => 1,
468 },
469 },
470 },
471 returns => {
472 type => 'array',
473 items => {
474 type => "object",
475 properties => {
476 share => {
477 description => "The cifs share name.",
478 type => 'string',
479 },
480 description => {
481 description => "Descriptive text from server.",
482 type => 'string',
483 },
484 },
485 },
486 },
487 code => sub {
488 my ($param) = @_;
489
490 my $server = $param->{server};
491
492 my $username = $param->{username};
493 my $password = $param->{password};
494 my $domain = $param->{domain};
495
496 my $res = PVE::Storage::scan_cifs($server, $username, $password, $domain);
497
498 my $data = [];
499 foreach my $k (keys %$res) {
500 next if $k =~ m/NT_STATUS_/;
501 push @$data, { share => $k, description => $res->{$k} };
502 }
503
504 return $data;
505 }});
506
507 # Note: GlusterFS currently does not have an equivalent of showmount.
508 # As workaround, we simply use nfs showmount.
509 # see http://www.gluster.org/category/volumes/
510
511 __PACKAGE__->register_method ({
512 name => 'glusterfsscan',
513 path => 'glusterfs',
514 method => 'GET',
515 description => "Scan remote GlusterFS server.",
516 protected => 1,
517 proxyto => "node",
518 permissions => {
519 check => ['perm', '/storage', ['Datastore.Allocate']],
520 },
521 parameters => {
522 additionalProperties => 0,
523 properties => {
524 node => get_standard_option('pve-node'),
525 server => {
526 description => "The server address (name or IP).",
527 type => 'string', format => 'pve-storage-server',
528 },
529 },
530 },
531 returns => {
532 type => 'array',
533 items => {
534 type => "object",
535 properties => {
536 volname => {
537 description => "The volume name.",
538 type => 'string',
539 },
540 },
541 },
542 },
543 code => sub {
544 my ($param) = @_;
545
546 my $server = $param->{server};
547 my $res = PVE::Storage::scan_nfs($server);
548
549 my $data = [];
550 foreach my $path (keys %$res) {
551 if ($path =~ m!^/([^\s/]+)$!) {
552 push @$data, { volname => $1 };
553 }
554 }
555 return $data;
556 }});
557
558 __PACKAGE__->register_method ({
559 name => 'iscsiscan',
560 path => 'iscsi',
561 method => 'GET',
562 description => "Scan remote iSCSI server.",
563 protected => 1,
564 proxyto => "node",
565 permissions => {
566 check => ['perm', '/storage', ['Datastore.Allocate']],
567 },
568 parameters => {
569 additionalProperties => 0,
570 properties => {
571 node => get_standard_option('pve-node'),
572 portal => {
573 description => "The iSCSI portal (IP or DNS name with optional port).",
574 type => 'string', format => 'pve-storage-portal-dns',
575 },
576 },
577 },
578 returns => {
579 type => 'array',
580 items => {
581 type => "object",
582 properties => {
583 target => {
584 description => "The iSCSI target name.",
585 type => 'string',
586 },
587 portal => {
588 description => "The iSCSI portal name.",
589 type => 'string',
590 },
591 },
592 },
593 },
594 code => sub {
595 my ($param) = @_;
596
597 my $res = PVE::Storage::scan_iscsi($param->{portal});
598
599 my $data = [];
600 foreach my $k (keys %$res) {
601 push @$data, { target => $k, portal => join(',', @{$res->{$k}}) };
602 }
603
604 return $data;
605 }});
606
607 __PACKAGE__->register_method ({
608 name => 'lvmscan',
609 path => 'lvm',
610 method => 'GET',
611 description => "List local LVM volume groups.",
612 protected => 1,
613 proxyto => "node",
614 permissions => {
615 check => ['perm', '/storage', ['Datastore.Allocate']],
616 },
617 parameters => {
618 additionalProperties => 0,
619 properties => {
620 node => get_standard_option('pve-node'),
621 },
622 },
623 returns => {
624 type => 'array',
625 items => {
626 type => "object",
627 properties => {
628 vg => {
629 description => "The LVM logical volume group name.",
630 type => 'string',
631 },
632 },
633 },
634 },
635 code => sub {
636 my ($param) = @_;
637
638 my $res = PVE::Storage::LVMPlugin::lvm_vgs();
639 return PVE::RESTHandler::hash_to_array($res, 'vg');
640 }});
641
642 __PACKAGE__->register_method ({
643 name => 'lvmthinscan',
644 path => 'lvmthin',
645 method => 'GET',
646 description => "List local LVM Thin Pools.",
647 protected => 1,
648 proxyto => "node",
649 permissions => {
650 check => ['perm', '/storage', ['Datastore.Allocate']],
651 },
652 parameters => {
653 additionalProperties => 0,
654 properties => {
655 node => get_standard_option('pve-node'),
656 vg => {
657 type => 'string',
658 pattern => '[a-zA-Z0-9\.\+\_][a-zA-Z0-9\.\+\_\-]+', # see lvm(8) manpage
659 maxLength => 100,
660 },
661 },
662 },
663 returns => {
664 type => 'array',
665 items => {
666 type => "object",
667 properties => {
668 lv => {
669 description => "The LVM Thin Pool name (LVM logical volume).",
670 type => 'string',
671 },
672 },
673 },
674 },
675 code => sub {
676 my ($param) = @_;
677
678 return PVE::Storage::LvmThinPlugin::list_thinpools($param->{vg});
679 }});
680
681 __PACKAGE__->register_method ({
682 name => 'zfsscan',
683 path => 'zfs',
684 method => 'GET',
685 description => "Scan zfs pool list on local node.",
686 protected => 1,
687 proxyto => "node",
688 permissions => {
689 check => ['perm', '/storage', ['Datastore.Allocate']],
690 },
691 parameters => {
692 additionalProperties => 0,
693 properties => {
694 node => get_standard_option('pve-node'),
695 },
696 },
697 returns => {
698 type => 'array',
699 items => {
700 type => "object",
701 properties => {
702 pool => {
703 description => "ZFS pool name.",
704 type => 'string',
705 },
706 },
707 },
708 },
709 code => sub {
710 my ($param) = @_;
711
712 return PVE::Storage::scan_zfs();
713 }});
714
715 our $cmddef = {
716 add => [ "PVE::API2::Storage::Config", 'create', ['type', 'storage'] ],
717 set => [ "PVE::API2::Storage::Config", 'update', ['storage'] ],
718 remove => [ "PVE::API2::Storage::Config", 'delete', ['storage'] ],
719 status => [ "PVE::API2::Storage::Status", 'index', [],
720 { node => $nodename }, $print_status ],
721 list => [ "PVE::API2::Storage::Content", 'index', ['storage'],
722 { node => $nodename }, $print_content ],
723 alloc => [ "PVE::API2::Storage::Content", 'create', ['storage', 'vmid', 'filename', 'size'],
724 { node => $nodename }, sub {
725 my $volid = shift;
726 print "successfully created '$volid'\n";
727 }],
728 free => [ "PVE::API2::Storage::Content", 'delete', ['volume'],
729 { node => $nodename } ],
730 scan => {
731 nfs => [ __PACKAGE__, 'nfsscan', ['server'], { node => $nodename }, sub {
732 my $res = shift;
733
734 my $maxlen = 0;
735 foreach my $rec (@$res) {
736 my $len = length ($rec->{path});
737 $maxlen = $len if $len > $maxlen;
738 }
739 foreach my $rec (@$res) {
740 printf "%-${maxlen}s %s\n", $rec->{path}, $rec->{options};
741 }
742 }],
743 cifs => [ __PACKAGE__, 'cifsscan', ['server'], { node => $nodename }, sub {
744 my $res = shift;
745
746 my $maxlen = 0;
747 foreach my $rec (@$res) {
748 my $len = length ($rec->{share});
749 $maxlen = $len if $len > $maxlen;
750 }
751 foreach my $rec (@$res) {
752 printf "%-${maxlen}s %s\n", $rec->{share}, $rec->{description};
753 }
754 }],
755 glusterfs => [ __PACKAGE__, 'glusterfsscan', ['server'], { node => $nodename }, sub {
756 my $res = shift;
757
758 foreach my $rec (@$res) {
759 printf "%s\n", $rec->{volname};
760 }
761 }],
762 iscsi => [ __PACKAGE__, 'iscsiscan', ['portal'], { node => $nodename }, sub {
763 my $res = shift;
764
765 my $maxlen = 0;
766 foreach my $rec (@$res) {
767 my $len = length ($rec->{target});
768 $maxlen = $len if $len > $maxlen;
769 }
770 foreach my $rec (@$res) {
771 printf "%-${maxlen}s %s\n", $rec->{target}, $rec->{portal};
772 }
773 }],
774 lvm => [ __PACKAGE__, 'lvmscan', [], { node => $nodename }, sub {
775 my $res = shift;
776 foreach my $rec (@$res) {
777 printf "$rec->{vg}\n";
778 }
779 }],
780 lvmthin => [ __PACKAGE__, 'lvmthinscan', ['vg'], { node => $nodename }, sub {
781 my $res = shift;
782 foreach my $rec (@$res) {
783 printf "$rec->{lv}\n";
784 }
785 }],
786 zfs => [ __PACKAGE__, 'zfsscan', [], { node => $nodename }, sub {
787 my $res = shift;
788
789 foreach my $rec (@$res) {
790 printf "$rec->{pool}\n";
791 }
792 }],
793 },
794 nfsscan => { alias => 'scan nfs' },
795 cifsscan => { alias => 'scan cifs' },
796 glusterfsscan => { alias => 'scan glusterfs' },
797 iscsiscan => { alias => 'scan iscsi' },
798 lvmscan => { alias => 'scan lvm' },
799 lvmthinscan => { alias => 'scan lvmthin' },
800 zfsscan => { alias => 'scan zfs' },
801 path => [ __PACKAGE__, 'path', ['volume']],
802 extractconfig => [__PACKAGE__, 'extractconfig', ['volume']],
803 export => [ __PACKAGE__, 'export', ['volume', 'format', 'filename']],
804 import => [ __PACKAGE__, 'import', ['volume', 'format', 'filename']],
805 apiinfo => [ __PACKAGE__, 'apiinfo', [], {}, sub {
806 my $res = shift;
807
808 print "APIVER $res->{apiver}\n";
809 print "APIAGE $res->{apiage}\n";
810 }],
811 };
812
813 1;