]>
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;
127 printf "%-${maxlenname}s %-6s %-9s %-10s %s\n", "Volid",
128 "Format", "Type", "Size", "VMID";
130 foreach my $info (@$list) {
131 next if !$info->{vmid
};
132 my $volid = $info->{volid
};
134 printf "%-${maxlenname}s %-6s %-9s %-10d %d\n", $volid,
135 $info->{format
}, $info->{content
}, $info->{size
}, $info->{vmid
};
138 foreach my $info (sort { $a->{format
} cmp $b->{format
} } @$list) {
139 next if $info->{vmid
};
140 my $volid = $info->{volid
};
142 printf "%-${maxlenname}s %-6s %-9s %-10d\n", $volid,
143 $info->{format
}, $info->{content
}, $info->{size
};
147 my $print_status = sub {
151 foreach my $res (@$res) {
152 my $storeid = $res->{storage
};
153 $maxlen = length ($storeid) if length ($storeid) > $maxlen;
157 printf "%-${maxlen}s %10s %10s %15s %15s %15s %8s\n", 'Name', 'Type',
158 'Status', 'Total', 'Used', 'Available', '%';
160 foreach my $res (sort { $a->{storage
} cmp $b->{storage
} } @$res) {
161 my $storeid = $res->{storage
};
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;
167 if (!$res->{enabled
}) {
170 $active = 'disabled';
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;
179 __PACKAGE__-
>register_method ({
183 description
=> "Export a volume.",
186 additionalProperties
=> 0,
189 description
=> "Volume identifier",
191 completion
=> \
&PVE
::Storage
::complete_volume
,
194 description
=> "Export stream format",
196 enum
=> $KNOWN_EXPORT_FORMATS,
199 description
=> "Destination file name",
203 description
=> "Snapshot to start an incremental stream from",
205 pattern
=> qr/[a-z0-9_\-]{1,40}/,
210 description
=> "Snapshot to export",
212 pattern
=> qr/[a-z0-9_\-]{1,40}/,
216 'with-snapshots' => {
218 "Whether to include intermediate snapshots in the stream",
225 returns
=> { type
=> 'null' },
229 my $filename = $param->{filename
};
232 if ($filename eq '-') {
235 sysopen($outfh, $filename, O_CREAT
|O_WRONLY
|O_TRUNC
)
236 or die "open($filename): $!\n";
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'});
245 if ($filename ne '-') {
247 unlink($filename) if $err;
254 __PACKAGE__-
>register_method ({
258 description
=> "Import a volume.",
261 additionalProperties
=> 0,
264 description
=> "Volume identifier",
266 completion
=> \
&PVE
::Storage
::complete_volume
,
269 description
=> "Import stream format",
271 enum
=> $KNOWN_EXPORT_FORMATS,
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.",
280 description
=> "Base snapshot of an incremental stream",
282 pattern
=> qr/[a-z0-9_\-]{1,40}/,
286 'with-snapshots' => {
288 "Whether the stream includes intermediate snapshots",
293 'delete-snapshot' => {
294 description
=> "A snapshot to delete on success",
296 pattern
=> qr/[a-z0-9_\-]{1,80}/,
302 returns
=> { type
=> 'null' },
306 my $filename = $param->{filename
};
309 if ($filename eq '-') {
311 } elsif ($filename =~ m!^tcp://(([^/]+)(/\d+)?)$!) {
312 my ($cidr, $ip, $subnet) = ($1, $2, $3);
313 if ($subnet) { # got real CIDR notation, not just IP
314 my $ips = PVE
::Network
::get_local_ip_from_cidr
($cidr);
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;
322 my $family = PVE
::Tools
::get_host_address_family
($ip);
323 my $port = PVE
::Tools
::next_migrate_port
($family, $ip);
328 Proto
=> &Socket
::IPPROTO_TCP
,
329 GetAddrInfoFlags
=> 0,
333 my $socket = IO
::Socket
::IP-
>new(%$sock_params)
334 or die "failed to open socket: $!\n";
336 print "$ip\n$port\n"; # tell remote where to connect
339 my $prev_alarm = alarm 0;
340 local $SIG{ALRM
} = sub { die "timed out waiting for client\n" };
342 my $client = $socket->accept; # Wait for a client
348 sysopen($infh, $filename, O_RDONLY
)
349 or die "open($filename): $!\n";
352 my $cfg = PVE
::Storage
::config
();
353 my $volume = $param->{volume
};
354 my $delete = $param->{'delete-snapshot'};
355 PVE
::Storage
::volume_import
($cfg, $infh, $volume, $param->{format
},
356 $param->{base
}, $param->{'with-snapshots'});
357 PVE
::Storage
::volume_snapshot_delete
($cfg, $volume, $delete)
363 __PACKAGE__-
>register_method ({
367 description
=> "Scan remote NFS server.",
371 check
=> ['perm', '/storage', ['Datastore.Allocate']],
374 additionalProperties
=> 0,
376 node
=> get_standard_option
('pve-node'),
378 description
=> "The server address (name or IP).",
379 type
=> 'string', format
=> 'pve-storage-server',
389 description
=> "The exported path.",
393 description
=> "NFS export options.",
402 my $server = $param->{server
};
403 my $res = PVE
::Storage
::scan_nfs
($server);
406 foreach my $k (keys %$res) {
407 push @$data, { path
=> $k, options
=> $res->{$k} };
412 __PACKAGE__-
>register_method ({
416 description
=> "Scan remote CIFS server.",
420 check
=> ['perm', '/storage', ['Datastore.Allocate']],
423 additionalProperties
=> 0,
425 node
=> get_standard_option
('pve-node'),
427 description
=> "The server address (name or IP).",
428 type
=> 'string', format
=> 'pve-storage-server',
431 description
=> "User name.",
436 description
=> "User password.",
441 description
=> "SMB domain (Workgroup).",
453 description
=> "The cifs share name.",
457 description
=> "Descriptive text from server.",
466 my $server = $param->{server
};
468 my $username = $param->{username
};
469 my $password = $param->{password
};
470 my $domain = $param->{domain
};
472 my $res = PVE
::Storage
::scan_cifs
($server, $username, $password, $domain);
475 foreach my $k (keys %$res) {
476 next if $k =~ m/NT_STATUS_/;
477 push @$data, { share
=> $k, description
=> $res->{$k} };
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/
487 __PACKAGE__-
>register_method ({
488 name
=> 'glusterfsscan',
491 description
=> "Scan remote GlusterFS server.",
495 check
=> ['perm', '/storage', ['Datastore.Allocate']],
498 additionalProperties
=> 0,
500 node
=> get_standard_option
('pve-node'),
502 description
=> "The server address (name or IP).",
503 type
=> 'string', format
=> 'pve-storage-server',
513 description
=> "The volume name.",
522 my $server = $param->{server
};
523 my $res = PVE
::Storage
::scan_nfs
($server);
526 foreach my $path (keys %$res) {
527 if ($path =~ m!^/([^\s/]+)$!) {
528 push @$data, { volname
=> $1 };
534 __PACKAGE__-
>register_method ({
538 description
=> "Scan remote iSCSI server.",
542 check
=> ['perm', '/storage', ['Datastore.Allocate']],
545 additionalProperties
=> 0,
547 node
=> get_standard_option
('pve-node'),
549 description
=> "The iSCSI portal (IP or DNS name with optional port).",
550 type
=> 'string', format
=> 'pve-storage-portal-dns',
560 description
=> "The iSCSI target name.",
564 description
=> "The iSCSI portal name.",
573 my $res = PVE
::Storage
::scan_iscsi
($param->{portal
});
576 foreach my $k (keys %$res) {
577 push @$data, { target
=> $k, portal
=> join(',', @{$res->{$k}}) };
583 __PACKAGE__-
>register_method ({
587 description
=> "List local LVM volume groups.",
591 check
=> ['perm', '/storage', ['Datastore.Allocate']],
594 additionalProperties
=> 0,
596 node
=> get_standard_option
('pve-node'),
605 description
=> "The LVM logical volume group name.",
614 my $res = PVE
::Storage
::LVMPlugin
::lvm_vgs
();
615 return PVE
::RESTHandler
::hash_to_array
($res, 'vg');
618 __PACKAGE__-
>register_method ({
619 name
=> 'lvmthinscan',
622 description
=> "List local LVM Thin Pools.",
626 check
=> ['perm', '/storage', ['Datastore.Allocate']],
629 additionalProperties
=> 0,
631 node
=> get_standard_option
('pve-node'),
634 pattern
=> '[a-zA-Z0-9\.\+\_][a-zA-Z0-9\.\+\_\-]+', # see lvm(8) manpage
645 description
=> "The LVM Thin Pool name (LVM logical volume).",
654 return PVE
::Storage
::LvmThinPlugin
::list_thinpools
($param->{vg
});
657 __PACKAGE__-
>register_method ({
661 description
=> "Scan zfs pool list on local node.",
665 check
=> ['perm', '/storage', ['Datastore.Allocate']],
668 additionalProperties
=> 0,
670 node
=> get_standard_option
('pve-node'),
679 description
=> "ZFS pool name.",
688 return PVE
::Storage
::scan_zfs
();
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 {
702 print "successfully created '$volid'\n";
704 free
=> [ "PVE::API2::Storage::Content", 'delete', ['volume'],
705 { node
=> $nodename } ],
707 nfs
=> [ __PACKAGE__
, 'nfsscan', ['server'], { node
=> $nodename }, sub {
711 foreach my $rec (@$res) {
712 my $len = length ($rec->{path
});
713 $maxlen = $len if $len > $maxlen;
715 foreach my $rec (@$res) {
716 printf "%-${maxlen}s %s\n", $rec->{path
}, $rec->{options
};
719 cifs
=> [ __PACKAGE__
, 'cifsscan', ['server'], { node
=> $nodename }, sub {
723 foreach my $rec (@$res) {
724 my $len = length ($rec->{share
});
725 $maxlen = $len if $len > $maxlen;
727 foreach my $rec (@$res) {
728 printf "%-${maxlen}s %s\n", $rec->{share
}, $rec->{description
};
731 glusterfs
=> [ __PACKAGE__
, 'glusterfsscan', ['server'], { node
=> $nodename }, sub {
734 foreach my $rec (@$res) {
735 printf "%s\n", $rec->{volname
};
738 iscsi
=> [ __PACKAGE__
, 'iscsiscan', ['portal'], { node
=> $nodename }, sub {
742 foreach my $rec (@$res) {
743 my $len = length ($rec->{target
});
744 $maxlen = $len if $len > $maxlen;
746 foreach my $rec (@$res) {
747 printf "%-${maxlen}s %s\n", $rec->{target
}, $rec->{portal
};
750 lvm
=> [ __PACKAGE__
, 'lvmscan', [], { node
=> $nodename }, sub {
752 foreach my $rec (@$res) {
753 printf "$rec->{vg}\n";
756 lvmthin
=> [ __PACKAGE__
, 'lvmthinscan', ['vg'], { node
=> $nodename }, sub {
758 foreach my $rec (@$res) {
759 printf "$rec->{lv}\n";
762 zfs
=> [ __PACKAGE__
, 'zfsscan', [], { node
=> $nodename }, sub {
765 foreach my $rec (@$res) {
766 printf "$rec->{pool}\n";
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' },
777 path
=> [ __PACKAGE__
, 'path', ['volume']],
778 extractconfig
=> [__PACKAGE__
, 'extractconfig', ['volume']],
779 export
=> [ __PACKAGE__
, 'export', ['volume', 'format', 'filename']],
780 import
=> [ __PACKAGE__
, 'import', ['volume', 'format', 'filename']],