]>
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: ");
41 'cifsscan' => [ $password_map ],
42 'create' => [ $password_map, 'encryption_key' ],
43 'update' => [ $password_map, 'encryption_key' ],
45 return $mapping->{$name};
48 sub setup_environment
{
49 PVE
::RPCEnvironment-
>setup_default_cli_env();
52 __PACKAGE__-
>register_method ({
56 description
=> "Returns APIVER and APIAGE.",
58 additionalProperties
=> 0,
64 apiver
=> { type
=> 'integer' },
65 apiage
=> { type
=> 'integer' },
70 apiver
=> PVE
::Storage
::APIVER
,
71 apiage
=> PVE
::Storage
::APIAGE
,
76 __PACKAGE__-
>register_method ({
80 description
=> "Get filesystem path for specified volume",
82 additionalProperties
=> 0,
85 description
=> "Volume identifier",
86 type
=> 'string', format
=> 'pve-volume-id',
87 completion
=> \
&PVE
::Storage
::complete_volume
,
91 returns
=> { type
=> 'null' },
96 my $cfg = PVE
::Storage
::config
();
98 my $path = PVE
::Storage
::path
($cfg, $param->{volume
});
106 __PACKAGE__-
>register_method ({
107 name
=> 'extractconfig',
108 path
=> 'extractconfig',
110 description
=> "Extract configuration from vzdump backup archive.",
112 description
=> "The user needs 'VM.Backup' permissions on the backed up guest ID, and 'Datastore.AllocateSpace' on the backup storage.",
117 additionalProperties
=> 0,
120 description
=> "Volume identifier",
122 completion
=> \
&PVE
::Storage
::complete_volume
,
126 returns
=> { type
=> 'null' },
129 my $volume = $param->{volume
};
131 my $rpcenv = PVE
::RPCEnvironment
::get
();
132 my $authuser = $rpcenv->get_user();
134 my $storage_cfg = PVE
::Storage
::config
();
135 PVE
::Storage
::check_volume_access
($rpcenv, $authuser, $storage_cfg, undef, $volume);
137 my $config_raw = PVE
::Storage
::extract_vzdump_config
($storage_cfg, $volume);
139 print "$config_raw\n";
143 my $print_content = sub {
146 my ($maxlenname, $maxsize) = (0, 0);
147 foreach my $info (@$list) {
148 my $volid = $info->{volid
};
149 my $sidlen = length ($volid);
150 $maxlenname = $sidlen if $sidlen > $maxlenname;
151 $maxsize = $info->{size
} if ($info->{size
} // 0) > $maxsize;
153 my $sizemaxdigits = length($maxsize);
155 my $basefmt = "%-${maxlenname}s %-7s %-9s %${sizemaxdigits}s";
156 printf "$basefmt %s\n", "Volid", "Format", "Type", "Size", "VMID";
158 foreach my $info (@$list) {
159 next if !$info->{vmid
};
160 my $volid = $info->{volid
};
162 printf "$basefmt %d\n", $volid, $info->{format
}, $info->{content
}, $info->{size
}, $info->{vmid
};
165 foreach my $info (sort { $a->{format
} cmp $b->{format
} } @$list) {
166 next if $info->{vmid
};
167 my $volid = $info->{volid
};
169 printf "$basefmt\n", $volid, $info->{format
}, $info->{content
}, $info->{size
};
173 my $print_status = sub {
177 foreach my $res (@$res) {
178 my $storeid = $res->{storage
};
179 $maxlen = length ($storeid) if length ($storeid) > $maxlen;
183 printf "%-${maxlen}s %10s %10s %15s %15s %15s %8s\n", 'Name', 'Type',
184 'Status', 'Total', 'Used', 'Available', '%';
186 foreach my $res (sort { $a->{storage
} cmp $b->{storage
} } @$res) {
187 my $storeid = $res->{storage
};
189 my $active = $res->{active
} ?
'active' : 'inactive';
190 my ($per, $per_fmt) = (0, '% 7.2f%%');
191 $per = ($res->{used
}*100)/$res->{total
} if $res->{total
} > 0;
193 if (!$res->{enabled
}) {
196 $active = 'disabled';
199 printf "%-${maxlen}s %10s %10s %15d %15d %15d $per_fmt\n", $storeid,
200 $res->{type
}, $active, $res->{total
}/1024, $res->{used}/1024,
201 $res->{avail
}/1024, $per;
205 __PACKAGE__-
>register_method ({
209 description
=> "Used internally to export a volume.",
212 additionalProperties
=> 0,
215 description
=> "Volume identifier",
217 completion
=> \
&PVE
::Storage
::complete_volume
,
220 description
=> "Export stream format",
222 enum
=> $KNOWN_EXPORT_FORMATS,
225 description
=> "Destination file name",
229 description
=> "Snapshot to start an incremental stream from",
231 pattern
=> qr/[a-z0-9_\-]{1,40}/,
236 description
=> "Snapshot to export",
238 pattern
=> qr/[a-z0-9_\-]{1,40}/,
242 'with-snapshots' => {
244 "Whether to include intermediate snapshots in the stream",
251 returns
=> { type
=> 'null' },
255 my $filename = $param->{filename
};
258 if ($filename eq '-') {
261 sysopen($outfh, $filename, O_CREAT
|O_WRONLY
|O_TRUNC
)
262 or die "open($filename): $!\n";
266 my $cfg = PVE
::Storage
::config
();
267 PVE
::Storage
::volume_export
($cfg, $outfh, $param->{volume
}, $param->{format
},
268 $param->{snapshot
}, $param->{base
}, $param->{'with-snapshots'});
271 if ($filename ne '-') {
273 unlink($filename) if $err;
280 __PACKAGE__-
>register_method ({
284 description
=> "Used internally to import a volume.",
287 additionalProperties
=> 0,
290 description
=> "Volume identifier",
292 completion
=> \
&PVE
::Storage
::complete_volume
,
295 description
=> "Import stream format",
297 enum
=> $KNOWN_EXPORT_FORMATS,
300 description
=> "Source file name. For '-' stdin is used, the " .
301 "tcp://<IP-or-CIDR> format allows to use a TCP connection as input. " .
302 "Else, the file is treated as common file.",
306 description
=> "Base snapshot of an incremental stream",
308 pattern
=> qr/[a-z0-9_\-]{1,40}/,
312 'with-snapshots' => {
314 "Whether the stream includes intermediate snapshots",
319 'delete-snapshot' => {
320 description
=> "A snapshot to delete on success",
322 pattern
=> qr/[a-z0-9_\-]{1,80}/,
327 description
=> "Choose a new volume ID if the requested " .
328 "volume ID already exists, instead of throwing an error.",
335 returns
=> { type
=> 'string' },
339 my $filename = $param->{filename
};
342 if ($filename eq '-') {
344 } elsif ($filename =~ m!^tcp://(([^/]+)(/\d+)?)$!) {
345 my ($cidr, $ip, $subnet) = ($1, $2, $3);
346 if ($subnet) { # got real CIDR notation, not just IP
347 my $ips = PVE
::Network
::get_local_ip_from_cidr
($cidr);
348 die "Unable to get any local IP address in network '$cidr'\n"
349 if scalar(@$ips) < 1;
350 die "Got multiple local IP address in network '$cidr'\n"
351 if scalar(@$ips) > 1;
355 my $family = PVE
::Tools
::get_host_address_family
($ip);
356 my $port = PVE
::Tools
::next_migrate_port
($family, $ip);
361 Proto
=> &Socket
::IPPROTO_TCP
,
362 GetAddrInfoFlags
=> 0,
366 my $socket = IO
::Socket
::IP-
>new(%$sock_params)
367 or die "failed to open socket: $!\n";
369 print "$ip\n$port\n"; # tell remote where to connect
372 my $prev_alarm = alarm 0;
373 local $SIG{ALRM
} = sub { die "timed out waiting for client\n" };
375 my $client = $socket->accept; # Wait for a client
381 sysopen($infh, $filename, O_RDONLY
)
382 or die "open($filename): $!\n";
385 my $cfg = PVE
::Storage
::config
();
386 my $volume = $param->{volume
};
387 my $delete = $param->{'delete-snapshot'};
388 my $imported_volid = PVE
::Storage
::volume_import
($cfg, $infh, $volume, $param->{format
},
389 $param->{base
}, $param->{'with-snapshots'}, $param->{'allow-rename'});
390 PVE
::Storage
::volume_snapshot_delete
($cfg, $imported_volid, $delete)
392 return $imported_volid;
396 __PACKAGE__-
>register_method ({
400 description
=> "Scan remote NFS server.",
404 check
=> ['perm', '/storage', ['Datastore.Allocate']],
407 additionalProperties
=> 0,
409 node
=> get_standard_option
('pve-node'),
411 description
=> "The server address (name or IP).",
412 type
=> 'string', format
=> 'pve-storage-server',
422 description
=> "The exported path.",
426 description
=> "NFS export options.",
435 my $server = $param->{server
};
436 my $res = PVE
::Storage
::scan_nfs
($server);
439 foreach my $k (sort keys %$res) {
440 push @$data, { path
=> $k, options
=> $res->{$k} };
445 __PACKAGE__-
>register_method ({
449 description
=> "Scan remote CIFS server.",
453 check
=> ['perm', '/storage', ['Datastore.Allocate']],
456 additionalProperties
=> 0,
458 node
=> get_standard_option
('pve-node'),
460 description
=> "The server address (name or IP).",
461 type
=> 'string', format
=> 'pve-storage-server',
464 description
=> "User name.",
469 description
=> "User password.",
474 description
=> "SMB domain (Workgroup).",
486 description
=> "The cifs share name.",
490 description
=> "Descriptive text from server.",
499 my $server = $param->{server
};
501 my $username = $param->{username
};
502 my $password = $param->{password
};
503 my $domain = $param->{domain
};
505 my $res = PVE
::Storage
::scan_cifs
($server, $username, $password, $domain);
508 foreach my $k (sort keys %$res) {
509 push @$data, { share
=> $k, description
=> $res->{$k} };
515 # Note: GlusterFS currently does not have an equivalent of showmount.
516 # As workaround, we simply use nfs showmount.
517 # see http://www.gluster.org/category/volumes/
519 __PACKAGE__-
>register_method ({
520 name
=> 'glusterfsscan',
523 description
=> "Scan remote GlusterFS server.",
527 check
=> ['perm', '/storage', ['Datastore.Allocate']],
530 additionalProperties
=> 0,
532 node
=> get_standard_option
('pve-node'),
534 description
=> "The server address (name or IP).",
535 type
=> 'string', format
=> 'pve-storage-server',
545 description
=> "The volume name.",
554 my $server = $param->{server
};
555 my $res = PVE
::Storage
::scan_nfs
($server);
558 foreach my $path (sort keys %$res) {
559 if ($path =~ m!^/([^\s/]+)$!) {
560 push @$data, { volname
=> $1 };
566 __PACKAGE__-
>register_method ({
570 description
=> "Scan remote iSCSI server.",
574 check
=> ['perm', '/storage', ['Datastore.Allocate']],
577 additionalProperties
=> 0,
579 node
=> get_standard_option
('pve-node'),
581 description
=> "The iSCSI portal (IP or DNS name with optional port).",
582 type
=> 'string', format
=> 'pve-storage-portal-dns',
592 description
=> "The iSCSI target name.",
596 description
=> "The iSCSI portal name.",
605 my $res = PVE
::Storage
::scan_iscsi
($param->{portal
});
608 foreach my $k (sort keys %$res) {
609 push @$data, { target
=> $k, portal
=> join(',', @{$res->{$k}}) };
615 __PACKAGE__-
>register_method ({
619 description
=> "List local LVM volume groups.",
623 check
=> ['perm', '/storage', ['Datastore.Allocate']],
626 additionalProperties
=> 0,
628 node
=> get_standard_option
('pve-node'),
637 description
=> "The LVM logical volume group name.",
646 my $res = PVE
::Storage
::LVMPlugin
::lvm_vgs
();
647 return PVE
::RESTHandler
::hash_to_array
($res, 'vg');
650 __PACKAGE__-
>register_method ({
651 name
=> 'lvmthinscan',
654 description
=> "List local LVM Thin Pools.",
658 check
=> ['perm', '/storage', ['Datastore.Allocate']],
661 additionalProperties
=> 0,
663 node
=> get_standard_option
('pve-node'),
666 pattern
=> '[a-zA-Z0-9\.\+\_][a-zA-Z0-9\.\+\_\-]+', # see lvm(8) manpage
677 description
=> "The LVM Thin Pool name (LVM logical volume).",
686 return PVE
::Storage
::LvmThinPlugin
::list_thinpools
($param->{vg
});
689 __PACKAGE__-
>register_method ({
693 description
=> "Scan zfs pool list on local node.",
697 check
=> ['perm', '/storage', ['Datastore.Allocate']],
700 additionalProperties
=> 0,
702 node
=> get_standard_option
('pve-node'),
711 description
=> "ZFS pool name.",
720 return PVE
::Storage
::scan_zfs
();
724 add
=> [ "PVE::API2::Storage::Config", 'create', ['type', 'storage'] ],
725 set
=> [ "PVE::API2::Storage::Config", 'update', ['storage'] ],
726 remove
=> [ "PVE::API2::Storage::Config", 'delete', ['storage'] ],
727 status
=> [ "PVE::API2::Storage::Status", 'index', [],
728 { node
=> $nodename }, $print_status ],
729 list
=> [ "PVE::API2::Storage::Content", 'index', ['storage'],
730 { node
=> $nodename }, $print_content ],
731 alloc
=> [ "PVE::API2::Storage::Content", 'create', ['storage', 'vmid', 'filename', 'size'],
732 { node
=> $nodename }, sub {
734 print "successfully created '$volid'\n";
736 free
=> [ "PVE::API2::Storage::Content", 'delete', ['volume'],
737 { node
=> $nodename } ],
739 nfs
=> [ __PACKAGE__
, 'nfsscan', ['server'], { node
=> $nodename }, sub {
743 foreach my $rec (@$res) {
744 my $len = length ($rec->{path
});
745 $maxlen = $len if $len > $maxlen;
747 foreach my $rec (@$res) {
748 printf "%-${maxlen}s %s\n", $rec->{path
}, $rec->{options
};
751 cifs
=> [ __PACKAGE__
, 'cifsscan', ['server'], { node
=> $nodename }, sub {
755 foreach my $rec (@$res) {
756 my $len = length ($rec->{share
});
757 $maxlen = $len if $len > $maxlen;
759 foreach my $rec (@$res) {
760 printf "%-${maxlen}s %s\n", $rec->{share
}, $rec->{description
};
763 glusterfs
=> [ __PACKAGE__
, 'glusterfsscan', ['server'], { node
=> $nodename }, sub {
766 foreach my $rec (@$res) {
767 printf "%s\n", $rec->{volname
};
770 iscsi
=> [ __PACKAGE__
, 'iscsiscan', ['portal'], { node
=> $nodename }, sub {
774 foreach my $rec (@$res) {
775 my $len = length ($rec->{target
});
776 $maxlen = $len if $len > $maxlen;
778 foreach my $rec (@$res) {
779 printf "%-${maxlen}s %s\n", $rec->{target
}, $rec->{portal
};
782 lvm
=> [ __PACKAGE__
, 'lvmscan', [], { node
=> $nodename }, sub {
784 foreach my $rec (@$res) {
785 printf "$rec->{vg}\n";
788 lvmthin
=> [ __PACKAGE__
, 'lvmthinscan', ['vg'], { node
=> $nodename }, sub {
790 foreach my $rec (@$res) {
791 printf "$rec->{lv}\n";
794 zfs
=> [ __PACKAGE__
, 'zfsscan', [], { node
=> $nodename }, sub {
797 foreach my $rec (@$res) {
798 printf "$rec->{pool}\n";
802 nfsscan
=> { alias
=> 'scan nfs' },
803 cifsscan
=> { alias
=> 'scan cifs' },
804 glusterfsscan
=> { alias
=> 'scan glusterfs' },
805 iscsiscan
=> { alias
=> 'scan iscsi' },
806 lvmscan
=> { alias
=> 'scan lvm' },
807 lvmthinscan
=> { alias
=> 'scan lvmthin' },
808 zfsscan
=> { alias
=> 'scan zfs' },
809 path
=> [ __PACKAGE__
, 'path', ['volume']],
810 extractconfig
=> [__PACKAGE__
, 'extractconfig', ['volume']],
811 export
=> [ __PACKAGE__
, 'export', ['volume', 'format', 'filename']],
812 import
=> [ __PACKAGE__
, 'import', ['volume', 'format', 'filename'], {}, sub {
814 print PVE
::Storage
::volume_imported_message
($volid);
816 apiinfo
=> [ __PACKAGE__
, 'apiinfo', [], {}, sub {
819 print "APIVER $res->{apiver}\n";
820 print "APIAGE $res->{apiage}\n";