]>
git.proxmox.com Git - pve-storage.git/blob - PVE/CLI/pvesm.pm
01ddd23c026787a4ca9f7a6e9d42232c50c73f3b
1 package PVE
::CLI
::pvesm
;
6 use POSIX
qw(O_RDONLY O_WRONLY O_CREAT O_TRUNC);
13 use PVE
::RPCEnvironment
;
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);
23 use base
qw(PVE::CLIHandler);
25 my $KNOWN_EXPORT_FORMATS = ['raw+size', 'tar+size', 'qcow2+size', 'vmdk+size', 'zfs'];
27 my $nodename = PVE
::INotify
::nodename
();
32 my $password_map = PVE
::CLIHandler
::get_standard_mapping
('pve-password', {
35 return $value if $value;
36 return PVE
::PTY
::read_password
("Enter Password: ");
40 'cifsscan' => [ $password_map ],
41 'create' => [ $password_map ],
43 return $mapping->{$name};
46 sub setup_environment
{
47 PVE
::RPCEnvironment-
>setup_default_cli_env();
50 __PACKAGE__-
>register_method ({
54 description
=> "Get filesystem path for specified volume",
56 additionalProperties
=> 0,
59 description
=> "Volume identifier",
60 type
=> 'string', format
=> 'pve-volume-id',
61 completion
=> \
&PVE
::Storage
::complete_volume
,
65 returns
=> { type
=> 'null' },
70 my $cfg = PVE
::Storage
::config
();
72 my $path = PVE
::Storage
::path
($cfg, $param->{volume
});
80 __PACKAGE__-
>register_method ({
81 name
=> 'extractconfig',
82 path
=> 'extractconfig',
84 description
=> "Extract configuration from vzdump backup archive.",
86 description
=> "The user needs 'VM.Backup' permissions on the backed up guest ID, and 'Datastore.AllocateSpace' on the backup storage.",
91 additionalProperties
=> 0,
94 description
=> "Volume identifier",
96 completion
=> \
&PVE
::Storage
::complete_volume
,
100 returns
=> { type
=> 'null' },
103 my $volume = $param->{volume
};
105 my $rpcenv = PVE
::RPCEnvironment
::get
();
106 my $authuser = $rpcenv->get_user();
108 my $storage_cfg = PVE
::Storage
::config
();
109 PVE
::Storage
::check_volume_access
($rpcenv, $authuser, $storage_cfg, undef, $volume);
111 my $config_raw = PVE
::Storage
::extract_vzdump_config
($storage_cfg, $volume);
113 print "$config_raw\n";
117 my $print_content = sub {
121 foreach my $info (@$list) {
123 my $volid = $info->{volid
};
124 my $sidlen = length ($volid);
125 $maxlenname = $sidlen if $sidlen > $maxlenname;
128 foreach my $info (@$list) {
129 next if !$info->{vmid
};
130 my $volid = $info->{volid
};
132 printf "%-${maxlenname}s %5s %10d %d\n", $volid,
133 $info->{format
}, $info->{size
}, $info->{vmid
};
136 foreach my $info (sort { $a->{format
} cmp $b->{format
} } @$list) {
137 next if $info->{vmid
};
138 my $volid = $info->{volid
};
140 printf "%-${maxlenname}s %5s %10d\n", $volid,
141 $info->{format
}, $info->{size
};
145 my $print_status = sub {
149 foreach my $res (@$res) {
150 my $storeid = $res->{storage
};
151 $maxlen = length ($storeid) if length ($storeid) > $maxlen;
155 printf "%-${maxlen}s %10s %10s %15s %15s %15s %8s\n", 'Name', 'Type',
156 'Status', 'Total', 'Used', 'Available', '%';
158 foreach my $res (sort { $a->{storage
} cmp $b->{storage
} } @$res) {
159 my $storeid = $res->{storage
};
161 my $active = $res->{active
} ?
'active' : 'inactive';
162 my ($per, $per_fmt) = (0, '% 7.2f%%');
163 $per = ($res->{used
}*100)/$res->{total
} if $res->{total
} > 0;
165 if (!$res->{enabled
}) {
168 $active = 'disabled';
171 printf "%-${maxlen}s %10s %10s %15d %15d %15d $per_fmt\n", $storeid,
172 $res->{type
}, $active, $res->{total
}/1024, $res->{used}/1024,
173 $res->{avail
}/1024, $per;
177 __PACKAGE__-
>register_method ({
181 description
=> "Export a volume.",
184 additionalProperties
=> 0,
187 description
=> "Volume identifier",
189 completion
=> \
&PVE
::Storage
::complete_volume
,
192 description
=> "Export stream format",
194 enum
=> $KNOWN_EXPORT_FORMATS,
197 description
=> "Destination file name",
201 description
=> "Snapshot to start an incremental stream from",
203 pattern
=> qr/[a-z0-9_\-]{1,40}/,
208 description
=> "Snapshot to export",
210 pattern
=> qr/[a-z0-9_\-]{1,40}/,
214 'with-snapshots' => {
216 "Whether to include intermediate snapshots in the stream",
223 returns
=> { type
=> 'null' },
227 my $filename = $param->{filename
};
230 if ($filename eq '-') {
233 sysopen($outfh, $filename, O_CREAT
|O_WRONLY
|O_TRUNC
)
234 or die "open($filename): $!\n";
238 my $cfg = PVE
::Storage
::config
();
239 PVE
::Storage
::volume_export
($cfg, $outfh, $param->{volume
}, $param->{format
},
240 $param->{snapshot
}, $param->{base
}, $param->{'with-snapshots'});
243 if ($filename ne '-') {
245 unlink($filename) if $err;
252 __PACKAGE__-
>register_method ({
256 description
=> "Import a volume.",
259 additionalProperties
=> 0,
262 description
=> "Volume identifier",
264 completion
=> \
&PVE
::Storage
::complete_volume
,
267 description
=> "Import stream format",
269 enum
=> $KNOWN_EXPORT_FORMATS,
272 description
=> "Source file name. For '-' stdin is used, the " .
273 "tcp://<IP-or-CIDR> format allows to use a TCP connection as input. " .
274 "Else, the file is treated as common file.",
278 description
=> "Base snapshot of an incremental stream",
280 pattern
=> qr/[a-z0-9_\-]{1,40}/,
284 'with-snapshots' => {
286 "Whether the stream includes intermediate snapshots",
291 'delete-snapshot' => {
292 description
=> "A snapshot to delete on success",
294 pattern
=> qr/[a-z0-9_\-]{1,80}/,
300 returns
=> { type
=> 'null' },
304 my $filename = $param->{filename
};
307 if ($filename eq '-') {
309 } elsif ($filename =~ m!^tcp://(([^/]+)(/\d+)?)$!) {
310 my ($cidr, $ip, $subnet) = ($1, $2, $3);
311 if ($subnet) { # got real CIDR notation, not just IP
312 my $ips = PVE
::Network
::get_local_ip_from_cidr
($cidr);
313 die "Unable to get any local IP address in network '$cidr'\n"
314 if scalar(@$ips) < 1;
315 die "Got multiple local IP address in network '$cidr'\n"
316 if scalar(@$ips) > 1;
320 my $family = PVE
::Tools
::get_host_address_family
($ip);
321 my $port = PVE
::Tools
::next_migrate_port
($family, $ip);
326 Proto
=> &Socket
::IPPROTO_TCP
,
327 GetAddrInfoFlags
=> 0,
331 my $socket = IO
::Socket
::IP-
>new(%$sock_params)
332 or die "failed to open socket: $!\n";
334 print "$ip\n$port\n"; # tell remote where to connect
337 my $prev_alarm = alarm 0;
338 local $SIG{ALRM
} = sub { die "timed out waiting for client\n" };
340 my $client = $socket->accept; # Wait for a client
346 sysopen($infh, $filename, O_RDONLY
)
347 or die "open($filename): $!\n";
350 my $cfg = PVE
::Storage
::config
();
351 my $volume = $param->{volume
};
352 my $delete = $param->{'delete-snapshot'};
353 PVE
::Storage
::volume_import
($cfg, $infh, $volume, $param->{format
},
354 $param->{base
}, $param->{'with-snapshots'});
355 PVE
::Storage
::volume_snapshot_delete
($cfg, $volume, $delete)
361 __PACKAGE__-
>register_method ({
365 description
=> "Scan remote NFS server.",
369 check
=> ['perm', '/storage', ['Datastore.Allocate']],
372 additionalProperties
=> 0,
374 node
=> get_standard_option
('pve-node'),
376 description
=> "The server address (name or IP).",
377 type
=> 'string', format
=> 'pve-storage-server',
387 description
=> "The exported path.",
391 description
=> "NFS export options.",
400 my $server = $param->{server
};
401 my $res = PVE
::Storage
::scan_nfs
($server);
404 foreach my $k (keys %$res) {
405 push @$data, { path
=> $k, options
=> $res->{$k} };
410 __PACKAGE__-
>register_method ({
414 description
=> "Scan remote CIFS server.",
418 check
=> ['perm', '/storage', ['Datastore.Allocate']],
421 additionalProperties
=> 0,
423 node
=> get_standard_option
('pve-node'),
425 description
=> "The server address (name or IP).",
426 type
=> 'string', format
=> 'pve-storage-server',
429 description
=> "User name.",
434 description
=> "User password.",
439 description
=> "SMB domain (Workgroup).",
451 description
=> "The cifs share name.",
455 description
=> "Descriptive text from server.",
464 my $server = $param->{server
};
466 my $username = $param->{username
};
467 my $password = $param->{password
};
468 my $domain = $param->{domain
};
470 my $res = PVE
::Storage
::scan_cifs
($server, $username, $password, $domain);
473 foreach my $k (keys %$res) {
474 next if $k =~ m/NT_STATUS_/;
475 push @$data, { share
=> $k, description
=> $res->{$k} };
481 # Note: GlusterFS currently does not have an equivalent of showmount.
482 # As workaround, we simply use nfs showmount.
483 # see http://www.gluster.org/category/volumes/
485 __PACKAGE__-
>register_method ({
486 name
=> 'glusterfsscan',
489 description
=> "Scan remote GlusterFS server.",
493 check
=> ['perm', '/storage', ['Datastore.Allocate']],
496 additionalProperties
=> 0,
498 node
=> get_standard_option
('pve-node'),
500 description
=> "The server address (name or IP).",
501 type
=> 'string', format
=> 'pve-storage-server',
511 description
=> "The volume name.",
520 my $server = $param->{server
};
521 my $res = PVE
::Storage
::scan_nfs
($server);
524 foreach my $path (keys %$res) {
525 if ($path =~ m!^/([^\s/]+)$!) {
526 push @$data, { volname
=> $1 };
532 __PACKAGE__-
>register_method ({
536 description
=> "Scan remote iSCSI server.",
540 check
=> ['perm', '/storage', ['Datastore.Allocate']],
543 additionalProperties
=> 0,
545 node
=> get_standard_option
('pve-node'),
547 description
=> "The iSCSI portal (IP or DNS name with optional port).",
548 type
=> 'string', format
=> 'pve-storage-portal-dns',
558 description
=> "The iSCSI target name.",
562 description
=> "The iSCSI portal name.",
571 my $res = PVE
::Storage
::scan_iscsi
($param->{portal
});
574 foreach my $k (keys %$res) {
575 push @$data, { target
=> $k, portal
=> join(',', @{$res->{$k}}) };
581 __PACKAGE__-
>register_method ({
585 description
=> "List local LVM volume groups.",
589 check
=> ['perm', '/storage', ['Datastore.Allocate']],
592 additionalProperties
=> 0,
594 node
=> get_standard_option
('pve-node'),
603 description
=> "The LVM logical volume group name.",
612 my $res = PVE
::Storage
::LVMPlugin
::lvm_vgs
();
613 return PVE
::RESTHandler
::hash_to_array
($res, 'vg');
616 __PACKAGE__-
>register_method ({
617 name
=> 'lvmthinscan',
620 description
=> "List local LVM Thin Pools.",
624 check
=> ['perm', '/storage', ['Datastore.Allocate']],
627 additionalProperties
=> 0,
629 node
=> get_standard_option
('pve-node'),
632 pattern
=> '[a-zA-Z0-9\.\+\_][a-zA-Z0-9\.\+\_\-]+', # see lvm(8) manpage
643 description
=> "The LVM Thin Pool name (LVM logical volume).",
652 return PVE
::Storage
::LvmThinPlugin
::list_thinpools
($param->{vg
});
655 __PACKAGE__-
>register_method ({
659 description
=> "Scan zfs pool list on local node.",
663 check
=> ['perm', '/storage', ['Datastore.Allocate']],
666 additionalProperties
=> 0,
668 node
=> get_standard_option
('pve-node'),
677 description
=> "ZFS pool name.",
686 return PVE
::Storage
::scan_zfs
();
690 add
=> [ "PVE::API2::Storage::Config", 'create', ['type', 'storage'] ],
691 set
=> [ "PVE::API2::Storage::Config", 'update', ['storage'] ],
692 remove
=> [ "PVE::API2::Storage::Config", 'delete', ['storage'] ],
693 status
=> [ "PVE::API2::Storage::Status", 'index', [],
694 { node
=> $nodename }, $print_status ],
695 list
=> [ "PVE::API2::Storage::Content", 'index', ['storage'],
696 { node
=> $nodename }, $print_content ],
697 alloc
=> [ "PVE::API2::Storage::Content", 'create', ['storage', 'vmid', 'filename', 'size'],
698 { node
=> $nodename }, sub {
700 print "successfully created '$volid'\n";
702 free
=> [ "PVE::API2::Storage::Content", 'delete', ['volume'],
703 { node
=> $nodename } ],
705 nfs
=> [ __PACKAGE__
, 'nfsscan', ['server'], { node
=> $nodename }, sub {
709 foreach my $rec (@$res) {
710 my $len = length ($rec->{path
});
711 $maxlen = $len if $len > $maxlen;
713 foreach my $rec (@$res) {
714 printf "%-${maxlen}s %s\n", $rec->{path
}, $rec->{options
};
717 cifs
=> [ __PACKAGE__
, 'cifsscan', ['server'], { node
=> $nodename }, sub {
721 foreach my $rec (@$res) {
722 my $len = length ($rec->{share
});
723 $maxlen = $len if $len > $maxlen;
725 foreach my $rec (@$res) {
726 printf "%-${maxlen}s %s\n", $rec->{share
}, $rec->{description
};
729 glusterfs
=> [ __PACKAGE__
, 'glusterfsscan', ['server'], { node
=> $nodename }, sub {
732 foreach my $rec (@$res) {
733 printf "%s\n", $rec->{volname
};
736 iscsi
=> [ __PACKAGE__
, 'iscsiscan', ['portal'], { node
=> $nodename }, sub {
740 foreach my $rec (@$res) {
741 my $len = length ($rec->{target
});
742 $maxlen = $len if $len > $maxlen;
744 foreach my $rec (@$res) {
745 printf "%-${maxlen}s %s\n", $rec->{target
}, $rec->{portal
};
748 lvm
=> [ __PACKAGE__
, 'lvmscan', [], { node
=> $nodename }, sub {
750 foreach my $rec (@$res) {
751 printf "$rec->{vg}\n";
754 lvmthin
=> [ __PACKAGE__
, 'lvmthinscan', ['vg'], { node
=> $nodename }, sub {
756 foreach my $rec (@$res) {
757 printf "$rec->{lv}\n";
760 zfs
=> [ __PACKAGE__
, 'zfsscan', [], { node
=> $nodename }, sub {
763 foreach my $rec (@$res) {
764 printf "$rec->{pool}\n";
768 nfsscan
=> { alias
=> 'scan nfs' },
769 cifsscan
=> { alias
=> 'scan cifs' },
770 glusterfsscan
=> { alias
=> 'scan glusterfs' },
771 iscsiscan
=> { alias
=> 'scan iscsi' },
772 lvmscan
=> { alias
=> 'scan lvm' },
773 lvmthinscan
=> { alias
=> 'scan lvmthin' },
774 zfsscan
=> { alias
=> 'scan zfs' },
775 path
=> [ __PACKAGE__
, 'path', ['volume']],
776 extractconfig
=> [__PACKAGE__
, 'extractconfig', ['volume']],
777 export
=> [ __PACKAGE__
, 'export', ['volume', 'format', 'filename']],
778 import
=> [ __PACKAGE__
, 'import', ['volume', 'format', 'filename']],