]>
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 my $ips = PVE
::Network
::get_local_ip_from_cidr
($cidr);
313 die "Unable to get single local IP address in network '$cidr'\n"
314 if scalar(@$ips) != 1;
317 my $family = PVE
::Tools
::get_host_address_family
($ip);
318 my $port = PVE
::Tools
::next_migrate_port
($family, $ip);
323 Proto
=> &Socket
::IPPROTO_TCP
,
324 GetAddrInfoFlags
=> 0,
328 my $socket = IO
::Socket
::IP-
>new(%$sock_params)
329 or die "failed to open socket: $!\n";
331 print "$ip\n$port\n"; # tell remote where to connect
334 my $prev_alarm = alarm 0;
335 local $SIG{ALRM
} = sub { die "timed out waiting for client\n" };
337 my $client = $socket->accept; # Wait for a client
343 sysopen($infh, $filename, O_RDONLY
)
344 or die "open($filename): $!\n";
347 my $cfg = PVE
::Storage
::config
();
348 my $volume = $param->{volume
};
349 my $delete = $param->{'delete-snapshot'};
350 PVE
::Storage
::volume_import
($cfg, $infh, $volume, $param->{format
},
351 $param->{base
}, $param->{'with-snapshots'});
352 PVE
::Storage
::volume_snapshot_delete
($cfg, $volume, $delete)
358 __PACKAGE__-
>register_method ({
362 description
=> "Scan remote NFS server.",
366 check
=> ['perm', '/storage', ['Datastore.Allocate']],
369 additionalProperties
=> 0,
371 node
=> get_standard_option
('pve-node'),
373 description
=> "The server address (name or IP).",
374 type
=> 'string', format
=> 'pve-storage-server',
384 description
=> "The exported path.",
388 description
=> "NFS export options.",
397 my $server = $param->{server
};
398 my $res = PVE
::Storage
::scan_nfs
($server);
401 foreach my $k (keys %$res) {
402 push @$data, { path
=> $k, options
=> $res->{$k} };
407 __PACKAGE__-
>register_method ({
411 description
=> "Scan remote CIFS server.",
415 check
=> ['perm', '/storage', ['Datastore.Allocate']],
418 additionalProperties
=> 0,
420 node
=> get_standard_option
('pve-node'),
422 description
=> "The server address (name or IP).",
423 type
=> 'string', format
=> 'pve-storage-server',
426 description
=> "User name.",
431 description
=> "User password.",
436 description
=> "SMB domain (Workgroup).",
448 description
=> "The cifs share name.",
452 description
=> "Descriptive text from server.",
461 my $server = $param->{server
};
463 my $username = $param->{username
};
464 my $password = $param->{password
};
465 my $domain = $param->{domain
};
467 my $res = PVE
::Storage
::scan_cifs
($server, $username, $password, $domain);
470 foreach my $k (keys %$res) {
471 next if $k =~ m/NT_STATUS_/;
472 push @$data, { share
=> $k, description
=> $res->{$k} };
478 # Note: GlusterFS currently does not have an equivalent of showmount.
479 # As workaround, we simply use nfs showmount.
480 # see http://www.gluster.org/category/volumes/
482 __PACKAGE__-
>register_method ({
483 name
=> 'glusterfsscan',
486 description
=> "Scan remote GlusterFS server.",
490 check
=> ['perm', '/storage', ['Datastore.Allocate']],
493 additionalProperties
=> 0,
495 node
=> get_standard_option
('pve-node'),
497 description
=> "The server address (name or IP).",
498 type
=> 'string', format
=> 'pve-storage-server',
508 description
=> "The volume name.",
517 my $server = $param->{server
};
518 my $res = PVE
::Storage
::scan_nfs
($server);
521 foreach my $path (keys %$res) {
522 if ($path =~ m!^/([^\s/]+)$!) {
523 push @$data, { volname
=> $1 };
529 __PACKAGE__-
>register_method ({
533 description
=> "Scan remote iSCSI server.",
537 check
=> ['perm', '/storage', ['Datastore.Allocate']],
540 additionalProperties
=> 0,
542 node
=> get_standard_option
('pve-node'),
544 description
=> "The iSCSI portal (IP or DNS name with optional port).",
545 type
=> 'string', format
=> 'pve-storage-portal-dns',
555 description
=> "The iSCSI target name.",
559 description
=> "The iSCSI portal name.",
568 my $res = PVE
::Storage
::scan_iscsi
($param->{portal
});
571 foreach my $k (keys %$res) {
572 push @$data, { target
=> $k, portal
=> join(',', @{$res->{$k}}) };
578 __PACKAGE__-
>register_method ({
582 description
=> "List local LVM volume groups.",
586 check
=> ['perm', '/storage', ['Datastore.Allocate']],
589 additionalProperties
=> 0,
591 node
=> get_standard_option
('pve-node'),
600 description
=> "The LVM logical volume group name.",
609 my $res = PVE
::Storage
::LVMPlugin
::lvm_vgs
();
610 return PVE
::RESTHandler
::hash_to_array
($res, 'vg');
613 __PACKAGE__-
>register_method ({
614 name
=> 'lvmthinscan',
617 description
=> "List local LVM Thin Pools.",
621 check
=> ['perm', '/storage', ['Datastore.Allocate']],
624 additionalProperties
=> 0,
626 node
=> get_standard_option
('pve-node'),
629 pattern
=> '[a-zA-Z0-9\.\+\_][a-zA-Z0-9\.\+\_\-]+', # see lvm(8) manpage
640 description
=> "The LVM Thin Pool name (LVM logical volume).",
649 return PVE
::Storage
::LvmThinPlugin
::list_thinpools
($param->{vg
});
652 __PACKAGE__-
>register_method ({
656 description
=> "Scan zfs pool list on local node.",
660 check
=> ['perm', '/storage', ['Datastore.Allocate']],
663 additionalProperties
=> 0,
665 node
=> get_standard_option
('pve-node'),
674 description
=> "ZFS pool name.",
683 return PVE
::Storage
::scan_zfs
();
687 add
=> [ "PVE::API2::Storage::Config", 'create', ['type', 'storage'] ],
688 set
=> [ "PVE::API2::Storage::Config", 'update', ['storage'] ],
689 remove
=> [ "PVE::API2::Storage::Config", 'delete', ['storage'] ],
690 status
=> [ "PVE::API2::Storage::Status", 'index', [],
691 { node
=> $nodename }, $print_status ],
692 list
=> [ "PVE::API2::Storage::Content", 'index', ['storage'],
693 { node
=> $nodename }, $print_content ],
694 alloc
=> [ "PVE::API2::Storage::Content", 'create', ['storage', 'vmid', 'filename', 'size'],
695 { node
=> $nodename }, sub {
697 print "successfully created '$volid'\n";
699 free
=> [ "PVE::API2::Storage::Content", 'delete', ['volume'],
700 { node
=> $nodename } ],
702 nfs
=> [ __PACKAGE__
, 'nfsscan', ['server'], { node
=> $nodename }, sub {
706 foreach my $rec (@$res) {
707 my $len = length ($rec->{path
});
708 $maxlen = $len if $len > $maxlen;
710 foreach my $rec (@$res) {
711 printf "%-${maxlen}s %s\n", $rec->{path
}, $rec->{options
};
714 cifs
=> [ __PACKAGE__
, 'cifsscan', ['server'], { node
=> $nodename }, sub {
718 foreach my $rec (@$res) {
719 my $len = length ($rec->{share
});
720 $maxlen = $len if $len > $maxlen;
722 foreach my $rec (@$res) {
723 printf "%-${maxlen}s %s\n", $rec->{share
}, $rec->{description
};
726 glusterfs
=> [ __PACKAGE__
, 'glusterfsscan', ['server'], { node
=> $nodename }, sub {
729 foreach my $rec (@$res) {
730 printf "%s\n", $rec->{volname
};
733 iscsi
=> [ __PACKAGE__
, 'iscsiscan', ['portal'], { node
=> $nodename }, sub {
737 foreach my $rec (@$res) {
738 my $len = length ($rec->{target
});
739 $maxlen = $len if $len > $maxlen;
741 foreach my $rec (@$res) {
742 printf "%-${maxlen}s %s\n", $rec->{target
}, $rec->{portal
};
745 lvm
=> [ __PACKAGE__
, 'lvmscan', [], { node
=> $nodename }, sub {
747 foreach my $rec (@$res) {
748 printf "$rec->{vg}\n";
751 lvmthin
=> [ __PACKAGE__
, 'lvmthinscan', ['vg'], { node
=> $nodename }, sub {
753 foreach my $rec (@$res) {
754 printf "$rec->{lv}\n";
757 zfs
=> [ __PACKAGE__
, 'zfsscan', [], { node
=> $nodename }, sub {
760 foreach my $rec (@$res) {
761 printf "$rec->{pool}\n";
765 nfsscan
=> { alias
=> 'scan nfs' },
766 cifsscan
=> { alias
=> 'scan cifs' },
767 glusterfsscan
=> { alias
=> 'scan glusterfs' },
768 iscsiscan
=> { alias
=> 'scan iscsi' },
769 lvmscan
=> { alias
=> 'scan lvm' },
770 lvmthinscan
=> { alias
=> 'scan lvmthin' },
771 zfsscan
=> { alias
=> 'scan zfs' },
772 path
=> [ __PACKAGE__
, 'path', ['volume']],
773 extractconfig
=> [__PACKAGE__
, 'extractconfig', ['volume']],
774 export
=> [ __PACKAGE__
, 'export', ['volume', 'format', 'filename']],
775 import
=> [ __PACKAGE__
, 'import', ['volume', 'format', 'filename']],