]>
git.proxmox.com Git - pve-storage.git/blob - PVE/CLI/pvesm.pm
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 $ip = PVE
::Cluster
::get_local_migration_ip
($cidr);
314 my $family = PVE
::Tools
::get_host_address_family
($ip);
315 my $port = PVE
::Tools
::next_migrate_port
($family, $ip);
320 Proto
=> &Socket
::IPPROTO_TCP
,
321 GetAddrInfoFlags
=> 0,
325 my $socket = IO
::Socket
::IP-
>new(%$sock_params)
326 or die "failed to open socket: $!\n";
328 print "$ip\n$port\n"; # tell remote where to connect
331 my $prev_alarm = alarm 0;
332 local $SIG{ALRM
} = sub { die "timed out waiting for client\n" };
334 my $client = $socket->accept; # Wait for a client
340 sysopen($infh, $filename, O_RDONLY
)
341 or die "open($filename): $!\n";
344 my $cfg = PVE
::Storage
::config
();
345 my $volume = $param->{volume
};
346 my $delete = $param->{'delete-snapshot'};
347 PVE
::Storage
::volume_import
($cfg, $infh, $volume, $param->{format
},
348 $param->{base
}, $param->{'with-snapshots'});
349 PVE
::Storage
::volume_snapshot_delete
($cfg, $volume, $delete)
355 __PACKAGE__-
>register_method ({
359 description
=> "Scan remote NFS server.",
363 check
=> ['perm', '/storage', ['Datastore.Allocate']],
366 additionalProperties
=> 0,
368 node
=> get_standard_option
('pve-node'),
370 description
=> "The server address (name or IP).",
371 type
=> 'string', format
=> 'pve-storage-server',
381 description
=> "The exported path.",
385 description
=> "NFS export options.",
394 my $server = $param->{server
};
395 my $res = PVE
::Storage
::scan_nfs
($server);
398 foreach my $k (keys %$res) {
399 push @$data, { path
=> $k, options
=> $res->{$k} };
404 __PACKAGE__-
>register_method ({
408 description
=> "Scan remote CIFS server.",
412 check
=> ['perm', '/storage', ['Datastore.Allocate']],
415 additionalProperties
=> 0,
417 node
=> get_standard_option
('pve-node'),
419 description
=> "The server address (name or IP).",
420 type
=> 'string', format
=> 'pve-storage-server',
423 description
=> "User name.",
428 description
=> "User password.",
433 description
=> "SMB domain (Workgroup).",
445 description
=> "The cifs share name.",
449 description
=> "Descriptive text from server.",
458 my $server = $param->{server
};
460 my $username = $param->{username
};
461 my $password = $param->{password
};
462 my $domain = $param->{domain
};
464 my $res = PVE
::Storage
::scan_cifs
($server, $username, $password, $domain);
467 foreach my $k (keys %$res) {
468 next if $k =~ m/NT_STATUS_/;
469 push @$data, { share
=> $k, description
=> $res->{$k} };
475 # Note: GlusterFS currently does not have an equivalent of showmount.
476 # As workaround, we simply use nfs showmount.
477 # see http://www.gluster.org/category/volumes/
479 __PACKAGE__-
>register_method ({
480 name
=> 'glusterfsscan',
483 description
=> "Scan remote GlusterFS server.",
487 check
=> ['perm', '/storage', ['Datastore.Allocate']],
490 additionalProperties
=> 0,
492 node
=> get_standard_option
('pve-node'),
494 description
=> "The server address (name or IP).",
495 type
=> 'string', format
=> 'pve-storage-server',
505 description
=> "The volume name.",
514 my $server = $param->{server
};
515 my $res = PVE
::Storage
::scan_nfs
($server);
518 foreach my $path (keys %$res) {
519 if ($path =~ m!^/([^\s/]+)$!) {
520 push @$data, { volname
=> $1 };
526 __PACKAGE__-
>register_method ({
530 description
=> "Scan remote iSCSI server.",
534 check
=> ['perm', '/storage', ['Datastore.Allocate']],
537 additionalProperties
=> 0,
539 node
=> get_standard_option
('pve-node'),
541 description
=> "The iSCSI portal (IP or DNS name with optional port).",
542 type
=> 'string', format
=> 'pve-storage-portal-dns',
552 description
=> "The iSCSI target name.",
556 description
=> "The iSCSI portal name.",
565 my $res = PVE
::Storage
::scan_iscsi
($param->{portal
});
568 foreach my $k (keys %$res) {
569 push @$data, { target
=> $k, portal
=> join(',', @{$res->{$k}}) };
575 __PACKAGE__-
>register_method ({
579 description
=> "List local LVM volume groups.",
583 check
=> ['perm', '/storage', ['Datastore.Allocate']],
586 additionalProperties
=> 0,
588 node
=> get_standard_option
('pve-node'),
597 description
=> "The LVM logical volume group name.",
606 my $res = PVE
::Storage
::LVMPlugin
::lvm_vgs
();
607 return PVE
::RESTHandler
::hash_to_array
($res, 'vg');
610 __PACKAGE__-
>register_method ({
611 name
=> 'lvmthinscan',
614 description
=> "List local LVM Thin Pools.",
618 check
=> ['perm', '/storage', ['Datastore.Allocate']],
621 additionalProperties
=> 0,
623 node
=> get_standard_option
('pve-node'),
626 pattern
=> '[a-zA-Z0-9\.\+\_][a-zA-Z0-9\.\+\_\-]+', # see lvm(8) manpage
637 description
=> "The LVM Thin Pool name (LVM logical volume).",
646 return PVE
::Storage
::LvmThinPlugin
::list_thinpools
($param->{vg
});
649 __PACKAGE__-
>register_method ({
653 description
=> "Scan zfs pool list on local node.",
657 check
=> ['perm', '/storage', ['Datastore.Allocate']],
660 additionalProperties
=> 0,
662 node
=> get_standard_option
('pve-node'),
671 description
=> "ZFS pool name.",
680 return PVE
::Storage
::scan_zfs
();
684 add
=> [ "PVE::API2::Storage::Config", 'create', ['type', 'storage'] ],
685 set
=> [ "PVE::API2::Storage::Config", 'update', ['storage'] ],
686 remove
=> [ "PVE::API2::Storage::Config", 'delete', ['storage'] ],
687 status
=> [ "PVE::API2::Storage::Status", 'index', [],
688 { node
=> $nodename }, $print_status ],
689 list
=> [ "PVE::API2::Storage::Content", 'index', ['storage'],
690 { node
=> $nodename }, $print_content ],
691 alloc
=> [ "PVE::API2::Storage::Content", 'create', ['storage', 'vmid', 'filename', 'size'],
692 { node
=> $nodename }, sub {
694 print "successfully created '$volid'\n";
696 free
=> [ "PVE::API2::Storage::Content", 'delete', ['volume'],
697 { node
=> $nodename } ],
699 nfs
=> [ __PACKAGE__
, 'nfsscan', ['server'], { node
=> $nodename }, sub {
703 foreach my $rec (@$res) {
704 my $len = length ($rec->{path
});
705 $maxlen = $len if $len > $maxlen;
707 foreach my $rec (@$res) {
708 printf "%-${maxlen}s %s\n", $rec->{path
}, $rec->{options
};
711 cifs
=> [ __PACKAGE__
, 'cifsscan', ['server'], { node
=> $nodename }, sub {
715 foreach my $rec (@$res) {
716 my $len = length ($rec->{share
});
717 $maxlen = $len if $len > $maxlen;
719 foreach my $rec (@$res) {
720 printf "%-${maxlen}s %s\n", $rec->{share
}, $rec->{description
};
723 glusterfs
=> [ __PACKAGE__
, 'glusterfsscan', ['server'], { node
=> $nodename }, sub {
726 foreach my $rec (@$res) {
727 printf "%s\n", $rec->{volname
};
730 iscsi
=> [ __PACKAGE__
, 'iscsiscan', ['portal'], { node
=> $nodename }, sub {
734 foreach my $rec (@$res) {
735 my $len = length ($rec->{target
});
736 $maxlen = $len if $len > $maxlen;
738 foreach my $rec (@$res) {
739 printf "%-${maxlen}s %s\n", $rec->{target
}, $rec->{portal
};
742 lvm
=> [ __PACKAGE__
, 'lvmscan', [], { node
=> $nodename }, sub {
744 foreach my $rec (@$res) {
745 printf "$rec->{vg}\n";
748 lvmthin
=> [ __PACKAGE__
, 'lvmthinscan', ['vg'], { node
=> $nodename }, sub {
750 foreach my $rec (@$res) {
751 printf "$rec->{lv}\n";
754 zfs
=> [ __PACKAGE__
, 'zfsscan', [], { node
=> $nodename }, sub {
757 foreach my $rec (@$res) {
758 printf "$rec->{pool}\n";
762 nfsscan
=> { alias
=> 'scan nfs' },
763 cifsscan
=> { alias
=> 'scan cifs' },
764 glusterfsscan
=> { alias
=> 'scan glusterfs' },
765 iscsiscan
=> { alias
=> 'scan iscsi' },
766 lvmscan
=> { alias
=> 'scan lvm' },
767 lvmthinscan
=> { alias
=> 'scan lvmthin' },
768 zfsscan
=> { alias
=> 'scan zfs' },
769 path
=> [ __PACKAGE__
, 'path', ['volume']],
770 extractconfig
=> [__PACKAGE__
, 'extractconfig', ['volume']],
771 export
=> [ __PACKAGE__
, 'export', ['volume', 'format', 'filename']],
772 import
=> [ __PACKAGE__
, 'import', ['volume', 'format', 'filename']],