]>
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
=> "Returns APIVER and APIAGE.",
56 additionalProperties
=> 0,
62 apiver
=> { type
=> 'integer' },
63 apiage
=> { type
=> 'integer' },
68 apiver
=> PVE
::Storage
::APIVER
,
69 apiage
=> PVE
::Storage
::APIAGE
,
74 __PACKAGE__-
>register_method ({
78 description
=> "Get filesystem path for specified volume",
80 additionalProperties
=> 0,
83 description
=> "Volume identifier",
84 type
=> 'string', format
=> 'pve-volume-id',
85 completion
=> \
&PVE
::Storage
::complete_volume
,
89 returns
=> { type
=> 'null' },
94 my $cfg = PVE
::Storage
::config
();
96 my $path = PVE
::Storage
::path
($cfg, $param->{volume
});
104 __PACKAGE__-
>register_method ({
105 name
=> 'extractconfig',
106 path
=> 'extractconfig',
108 description
=> "Extract configuration from vzdump backup archive.",
110 description
=> "The user needs 'VM.Backup' permissions on the backed up guest ID, and 'Datastore.AllocateSpace' on the backup storage.",
115 additionalProperties
=> 0,
118 description
=> "Volume identifier",
120 completion
=> \
&PVE
::Storage
::complete_volume
,
124 returns
=> { type
=> 'null' },
127 my $volume = $param->{volume
};
129 my $rpcenv = PVE
::RPCEnvironment
::get
();
130 my $authuser = $rpcenv->get_user();
132 my $storage_cfg = PVE
::Storage
::config
();
133 PVE
::Storage
::check_volume_access
($rpcenv, $authuser, $storage_cfg, undef, $volume);
135 my $config_raw = PVE
::Storage
::extract_vzdump_config
($storage_cfg, $volume);
137 print "$config_raw\n";
141 my $print_content = sub {
144 my ($maxlenname, $maxsize) = (0, 0);
145 foreach my $info (@$list) {
146 my $volid = $info->{volid
};
147 my $sidlen = length ($volid);
148 $maxlenname = $sidlen if $sidlen > $maxlenname;
149 $maxsize = $info->{size
} if ($info->{size
} // 0) > $maxsize;
151 my $sizemaxdigits = length($maxsize);
153 my $basefmt = "%-${maxlenname}s %-7s %-9s %${sizemaxdigits}s";
154 printf "$basefmt %s\n", "Volid", "Format", "Type", "Size", "VMID";
156 foreach my $info (@$list) {
157 next if !$info->{vmid
};
158 my $volid = $info->{volid
};
160 printf "$basefmt %d\n", $volid, $info->{format
}, $info->{content
}, $info->{size
}, $info->{vmid
};
163 foreach my $info (sort { $a->{format
} cmp $b->{format
} } @$list) {
164 next if $info->{vmid
};
165 my $volid = $info->{volid
};
167 printf "$basefmt\n", $volid, $info->{format
}, $info->{content
}, $info->{size
};
171 my $print_status = sub {
175 foreach my $res (@$res) {
176 my $storeid = $res->{storage
};
177 $maxlen = length ($storeid) if length ($storeid) > $maxlen;
181 printf "%-${maxlen}s %10s %10s %15s %15s %15s %8s\n", 'Name', 'Type',
182 'Status', 'Total', 'Used', 'Available', '%';
184 foreach my $res (sort { $a->{storage
} cmp $b->{storage
} } @$res) {
185 my $storeid = $res->{storage
};
187 my $active = $res->{active
} ?
'active' : 'inactive';
188 my ($per, $per_fmt) = (0, '% 7.2f%%');
189 $per = ($res->{used
}*100)/$res->{total
} if $res->{total
} > 0;
191 if (!$res->{enabled
}) {
194 $active = 'disabled';
197 printf "%-${maxlen}s %10s %10s %15d %15d %15d $per_fmt\n", $storeid,
198 $res->{type
}, $active, $res->{total
}/1024, $res->{used}/1024,
199 $res->{avail
}/1024, $per;
203 __PACKAGE__-
>register_method ({
207 description
=> "Export a volume.",
210 additionalProperties
=> 0,
213 description
=> "Volume identifier",
215 completion
=> \
&PVE
::Storage
::complete_volume
,
218 description
=> "Export stream format",
220 enum
=> $KNOWN_EXPORT_FORMATS,
223 description
=> "Destination file name",
227 description
=> "Snapshot to start an incremental stream from",
229 pattern
=> qr/[a-z0-9_\-]{1,40}/,
234 description
=> "Snapshot to export",
236 pattern
=> qr/[a-z0-9_\-]{1,40}/,
240 'with-snapshots' => {
242 "Whether to include intermediate snapshots in the stream",
249 returns
=> { type
=> 'null' },
253 my $filename = $param->{filename
};
256 if ($filename eq '-') {
259 sysopen($outfh, $filename, O_CREAT
|O_WRONLY
|O_TRUNC
)
260 or die "open($filename): $!\n";
264 my $cfg = PVE
::Storage
::config
();
265 PVE
::Storage
::volume_export
($cfg, $outfh, $param->{volume
}, $param->{format
},
266 $param->{snapshot
}, $param->{base
}, $param->{'with-snapshots'});
269 if ($filename ne '-') {
271 unlink($filename) if $err;
278 __PACKAGE__-
>register_method ({
282 description
=> "Import a volume.",
285 additionalProperties
=> 0,
288 description
=> "Volume identifier",
290 completion
=> \
&PVE
::Storage
::complete_volume
,
293 description
=> "Import stream format",
295 enum
=> $KNOWN_EXPORT_FORMATS,
298 description
=> "Source file name. For '-' stdin is used, the " .
299 "tcp://<IP-or-CIDR> format allows to use a TCP connection as input. " .
300 "Else, the file is treated as common file.",
304 description
=> "Base snapshot of an incremental stream",
306 pattern
=> qr/[a-z0-9_\-]{1,40}/,
310 'with-snapshots' => {
312 "Whether the stream includes intermediate snapshots",
317 'delete-snapshot' => {
318 description
=> "A snapshot to delete on success",
320 pattern
=> qr/[a-z0-9_\-]{1,80}/,
325 description
=> "Choose a new volume ID if the requested " .
326 "volume ID already exists, instead of throwing an error.",
333 returns
=> { type
=> 'string' },
337 my $filename = $param->{filename
};
340 if ($filename eq '-') {
342 } elsif ($filename =~ m!^tcp://(([^/]+)(/\d+)?)$!) {
343 my ($cidr, $ip, $subnet) = ($1, $2, $3);
344 if ($subnet) { # got real CIDR notation, not just IP
345 my $ips = PVE
::Network
::get_local_ip_from_cidr
($cidr);
346 die "Unable to get any local IP address in network '$cidr'\n"
347 if scalar(@$ips) < 1;
348 die "Got multiple local IP address in network '$cidr'\n"
349 if scalar(@$ips) > 1;
353 my $family = PVE
::Tools
::get_host_address_family
($ip);
354 my $port = PVE
::Tools
::next_migrate_port
($family, $ip);
359 Proto
=> &Socket
::IPPROTO_TCP
,
360 GetAddrInfoFlags
=> 0,
364 my $socket = IO
::Socket
::IP-
>new(%$sock_params)
365 or die "failed to open socket: $!\n";
367 print "$ip\n$port\n"; # tell remote where to connect
370 my $prev_alarm = alarm 0;
371 local $SIG{ALRM
} = sub { die "timed out waiting for client\n" };
373 my $client = $socket->accept; # Wait for a client
379 sysopen($infh, $filename, O_RDONLY
)
380 or die "open($filename): $!\n";
383 my $cfg = PVE
::Storage
::config
();
384 my $volume = $param->{volume
};
385 my $delete = $param->{'delete-snapshot'};
386 my $imported_volid = PVE
::Storage
::volume_import
($cfg, $infh, $volume, $param->{format
},
387 $param->{base
}, $param->{'with-snapshots'}, $param->{'allow-rename'});
388 PVE
::Storage
::volume_snapshot_delete
($cfg, $imported_volid, $delete)
390 return $imported_volid;
394 __PACKAGE__-
>register_method ({
398 description
=> "Scan remote NFS server.",
402 check
=> ['perm', '/storage', ['Datastore.Allocate']],
405 additionalProperties
=> 0,
407 node
=> get_standard_option
('pve-node'),
409 description
=> "The server address (name or IP).",
410 type
=> 'string', format
=> 'pve-storage-server',
420 description
=> "The exported path.",
424 description
=> "NFS export options.",
433 my $server = $param->{server
};
434 my $res = PVE
::Storage
::scan_nfs
($server);
437 foreach my $k (keys %$res) {
438 push @$data, { path
=> $k, options
=> $res->{$k} };
443 __PACKAGE__-
>register_method ({
447 description
=> "Scan remote CIFS server.",
451 check
=> ['perm', '/storage', ['Datastore.Allocate']],
454 additionalProperties
=> 0,
456 node
=> get_standard_option
('pve-node'),
458 description
=> "The server address (name or IP).",
459 type
=> 'string', format
=> 'pve-storage-server',
462 description
=> "User name.",
467 description
=> "User password.",
472 description
=> "SMB domain (Workgroup).",
484 description
=> "The cifs share name.",
488 description
=> "Descriptive text from server.",
497 my $server = $param->{server
};
499 my $username = $param->{username
};
500 my $password = $param->{password
};
501 my $domain = $param->{domain
};
503 my $res = PVE
::Storage
::scan_cifs
($server, $username, $password, $domain);
506 foreach my $k (keys %$res) {
507 next if $k =~ m/NT_STATUS_/;
508 push @$data, { share
=> $k, description
=> $res->{$k} };
514 # Note: GlusterFS currently does not have an equivalent of showmount.
515 # As workaround, we simply use nfs showmount.
516 # see http://www.gluster.org/category/volumes/
518 __PACKAGE__-
>register_method ({
519 name
=> 'glusterfsscan',
522 description
=> "Scan remote GlusterFS server.",
526 check
=> ['perm', '/storage', ['Datastore.Allocate']],
529 additionalProperties
=> 0,
531 node
=> get_standard_option
('pve-node'),
533 description
=> "The server address (name or IP).",
534 type
=> 'string', format
=> 'pve-storage-server',
544 description
=> "The volume name.",
553 my $server = $param->{server
};
554 my $res = PVE
::Storage
::scan_nfs
($server);
557 foreach my $path (keys %$res) {
558 if ($path =~ m!^/([^\s/]+)$!) {
559 push @$data, { volname
=> $1 };
565 __PACKAGE__-
>register_method ({
569 description
=> "Scan remote iSCSI server.",
573 check
=> ['perm', '/storage', ['Datastore.Allocate']],
576 additionalProperties
=> 0,
578 node
=> get_standard_option
('pve-node'),
580 description
=> "The iSCSI portal (IP or DNS name with optional port).",
581 type
=> 'string', format
=> 'pve-storage-portal-dns',
591 description
=> "The iSCSI target name.",
595 description
=> "The iSCSI portal name.",
604 my $res = PVE
::Storage
::scan_iscsi
($param->{portal
});
607 foreach my $k (keys %$res) {
608 push @$data, { target
=> $k, portal
=> join(',', @{$res->{$k}}) };
614 __PACKAGE__-
>register_method ({
618 description
=> "List local LVM volume groups.",
622 check
=> ['perm', '/storage', ['Datastore.Allocate']],
625 additionalProperties
=> 0,
627 node
=> get_standard_option
('pve-node'),
636 description
=> "The LVM logical volume group name.",
645 my $res = PVE
::Storage
::LVMPlugin
::lvm_vgs
();
646 return PVE
::RESTHandler
::hash_to_array
($res, 'vg');
649 __PACKAGE__-
>register_method ({
650 name
=> 'lvmthinscan',
653 description
=> "List local LVM Thin Pools.",
657 check
=> ['perm', '/storage', ['Datastore.Allocate']],
660 additionalProperties
=> 0,
662 node
=> get_standard_option
('pve-node'),
665 pattern
=> '[a-zA-Z0-9\.\+\_][a-zA-Z0-9\.\+\_\-]+', # see lvm(8) manpage
676 description
=> "The LVM Thin Pool name (LVM logical volume).",
685 return PVE
::Storage
::LvmThinPlugin
::list_thinpools
($param->{vg
});
688 __PACKAGE__-
>register_method ({
692 description
=> "Scan zfs pool list on local node.",
696 check
=> ['perm', '/storage', ['Datastore.Allocate']],
699 additionalProperties
=> 0,
701 node
=> get_standard_option
('pve-node'),
710 description
=> "ZFS pool name.",
719 return PVE
::Storage
::scan_zfs
();
723 add
=> [ "PVE::API2::Storage::Config", 'create', ['type', 'storage'] ],
724 set
=> [ "PVE::API2::Storage::Config", 'update', ['storage'] ],
725 remove
=> [ "PVE::API2::Storage::Config", 'delete', ['storage'] ],
726 status
=> [ "PVE::API2::Storage::Status", 'index', [],
727 { node
=> $nodename }, $print_status ],
728 list
=> [ "PVE::API2::Storage::Content", 'index', ['storage'],
729 { node
=> $nodename }, $print_content ],
730 alloc
=> [ "PVE::API2::Storage::Content", 'create', ['storage', 'vmid', 'filename', 'size'],
731 { node
=> $nodename }, sub {
733 print "successfully created '$volid'\n";
735 free
=> [ "PVE::API2::Storage::Content", 'delete', ['volume'],
736 { node
=> $nodename } ],
738 nfs
=> [ __PACKAGE__
, 'nfsscan', ['server'], { node
=> $nodename }, sub {
742 foreach my $rec (@$res) {
743 my $len = length ($rec->{path
});
744 $maxlen = $len if $len > $maxlen;
746 foreach my $rec (@$res) {
747 printf "%-${maxlen}s %s\n", $rec->{path
}, $rec->{options
};
750 cifs
=> [ __PACKAGE__
, 'cifsscan', ['server'], { node
=> $nodename }, sub {
754 foreach my $rec (@$res) {
755 my $len = length ($rec->{share
});
756 $maxlen = $len if $len > $maxlen;
758 foreach my $rec (@$res) {
759 printf "%-${maxlen}s %s\n", $rec->{share
}, $rec->{description
};
762 glusterfs
=> [ __PACKAGE__
, 'glusterfsscan', ['server'], { node
=> $nodename }, sub {
765 foreach my $rec (@$res) {
766 printf "%s\n", $rec->{volname
};
769 iscsi
=> [ __PACKAGE__
, 'iscsiscan', ['portal'], { node
=> $nodename }, sub {
773 foreach my $rec (@$res) {
774 my $len = length ($rec->{target
});
775 $maxlen = $len if $len > $maxlen;
777 foreach my $rec (@$res) {
778 printf "%-${maxlen}s %s\n", $rec->{target
}, $rec->{portal
};
781 lvm
=> [ __PACKAGE__
, 'lvmscan', [], { node
=> $nodename }, sub {
783 foreach my $rec (@$res) {
784 printf "$rec->{vg}\n";
787 lvmthin
=> [ __PACKAGE__
, 'lvmthinscan', ['vg'], { node
=> $nodename }, sub {
789 foreach my $rec (@$res) {
790 printf "$rec->{lv}\n";
793 zfs
=> [ __PACKAGE__
, 'zfsscan', [], { node
=> $nodename }, sub {
796 foreach my $rec (@$res) {
797 printf "$rec->{pool}\n";
801 nfsscan
=> { alias
=> 'scan nfs' },
802 cifsscan
=> { alias
=> 'scan cifs' },
803 glusterfsscan
=> { alias
=> 'scan glusterfs' },
804 iscsiscan
=> { alias
=> 'scan iscsi' },
805 lvmscan
=> { alias
=> 'scan lvm' },
806 lvmthinscan
=> { alias
=> 'scan lvmthin' },
807 zfsscan
=> { alias
=> 'scan zfs' },
808 path
=> [ __PACKAGE__
, 'path', ['volume']],
809 extractconfig
=> [__PACKAGE__
, 'extractconfig', ['volume']],
810 export
=> [ __PACKAGE__
, 'export', ['volume', 'format', 'filename']],
811 import
=> [ __PACKAGE__
, 'import', ['volume', 'format', 'filename'], {}, sub {
813 print PVE
::Storage
::volume_imported_message
($volid);
815 apiinfo
=> [ __PACKAGE__
, 'apiinfo', [], {}, sub {
818 print "APIVER $res->{apiver}\n";
819 print "APIAGE $res->{apiage}\n";