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