]>
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",
276 description
=> "Base snapshot of an incremental stream",
278 pattern
=> qr/[a-z0-9_\-]{1,40}/,
282 'with-snapshots' => {
284 "Whether the stream includes intermediate snapshots",
289 'delete-snapshot' => {
290 description
=> "A snapshot to delete on success",
292 pattern
=> qr/[a-z0-9_\-]{1,80}/,
298 returns
=> { type
=> 'null' },
302 my $filename = $param->{filename
};
305 if ($filename eq '-') {
308 sysopen($infh, $filename, O_RDONLY
)
309 or die "open($filename): $!\n";
312 my $cfg = PVE
::Storage
::config
();
313 my $volume = $param->{volume
};
314 my $delete = $param->{'delete-snapshot'};
315 PVE
::Storage
::volume_import
($cfg, $infh, $volume, $param->{format
},
316 $param->{base
}, $param->{'with-snapshots'});
317 PVE
::Storage
::volume_snapshot_delete
($cfg, $volume, $delete)
323 __PACKAGE__-
>register_method ({
327 description
=> "Scan remote NFS server.",
331 check
=> ['perm', '/storage', ['Datastore.Allocate']],
334 additionalProperties
=> 0,
336 node
=> get_standard_option
('pve-node'),
338 description
=> "The server address (name or IP).",
339 type
=> 'string', format
=> 'pve-storage-server',
349 description
=> "The exported path.",
353 description
=> "NFS export options.",
362 my $server = $param->{server
};
363 my $res = PVE
::Storage
::scan_nfs
($server);
366 foreach my $k (keys %$res) {
367 push @$data, { path
=> $k, options
=> $res->{$k} };
372 __PACKAGE__-
>register_method ({
376 description
=> "Scan remote CIFS server.",
380 check
=> ['perm', '/storage', ['Datastore.Allocate']],
383 additionalProperties
=> 0,
385 node
=> get_standard_option
('pve-node'),
387 description
=> "The server address (name or IP).",
388 type
=> 'string', format
=> 'pve-storage-server',
391 description
=> "User name.",
396 description
=> "User password.",
401 description
=> "SMB domain (Workgroup).",
413 description
=> "The cifs share name.",
417 description
=> "Descriptive text from server.",
426 my $server = $param->{server
};
428 my $username = $param->{username
};
429 my $password = $param->{password
};
430 my $domain = $param->{domain
};
432 my $res = PVE
::Storage
::scan_cifs
($server, $username, $password, $domain);
435 foreach my $k (keys %$res) {
436 next if $k =~ m/NT_STATUS_/;
437 push @$data, { share
=> $k, description
=> $res->{$k} };
443 # Note: GlusterFS currently does not have an equivalent of showmount.
444 # As workaround, we simply use nfs showmount.
445 # see http://www.gluster.org/category/volumes/
447 __PACKAGE__-
>register_method ({
448 name
=> 'glusterfsscan',
451 description
=> "Scan remote GlusterFS server.",
455 check
=> ['perm', '/storage', ['Datastore.Allocate']],
458 additionalProperties
=> 0,
460 node
=> get_standard_option
('pve-node'),
462 description
=> "The server address (name or IP).",
463 type
=> 'string', format
=> 'pve-storage-server',
473 description
=> "The volume name.",
482 my $server = $param->{server
};
483 my $res = PVE
::Storage
::scan_nfs
($server);
486 foreach my $path (keys %$res) {
487 if ($path =~ m!^/([^\s/]+)$!) {
488 push @$data, { volname
=> $1 };
494 __PACKAGE__-
>register_method ({
498 description
=> "Scan remote iSCSI server.",
502 check
=> ['perm', '/storage', ['Datastore.Allocate']],
505 additionalProperties
=> 0,
507 node
=> get_standard_option
('pve-node'),
509 description
=> "The iSCSI portal (IP or DNS name with optional port).",
510 type
=> 'string', format
=> 'pve-storage-portal-dns',
520 description
=> "The iSCSI target name.",
524 description
=> "The iSCSI portal name.",
533 my $res = PVE
::Storage
::scan_iscsi
($param->{portal
});
536 foreach my $k (keys %$res) {
537 push @$data, { target
=> $k, portal
=> join(',', @{$res->{$k}}) };
543 __PACKAGE__-
>register_method ({
547 description
=> "List local LVM volume groups.",
551 check
=> ['perm', '/storage', ['Datastore.Allocate']],
554 additionalProperties
=> 0,
556 node
=> get_standard_option
('pve-node'),
565 description
=> "The LVM logical volume group name.",
574 my $res = PVE
::Storage
::LVMPlugin
::lvm_vgs
();
575 return PVE
::RESTHandler
::hash_to_array
($res, 'vg');
578 __PACKAGE__-
>register_method ({
579 name
=> 'lvmthinscan',
582 description
=> "List local LVM Thin Pools.",
586 check
=> ['perm', '/storage', ['Datastore.Allocate']],
589 additionalProperties
=> 0,
591 node
=> get_standard_option
('pve-node'),
594 pattern
=> '[a-zA-Z0-9\.\+\_][a-zA-Z0-9\.\+\_\-]+', # see lvm(8) manpage
605 description
=> "The LVM Thin Pool name (LVM logical volume).",
614 return PVE
::Storage
::LvmThinPlugin
::list_thinpools
($param->{vg
});
617 __PACKAGE__-
>register_method ({
621 description
=> "Scan zfs pool list on local node.",
625 check
=> ['perm', '/storage', ['Datastore.Allocate']],
628 additionalProperties
=> 0,
630 node
=> get_standard_option
('pve-node'),
639 description
=> "ZFS pool name.",
648 return PVE
::Storage
::scan_zfs
();
652 add
=> [ "PVE::API2::Storage::Config", 'create', ['type', 'storage'] ],
653 set
=> [ "PVE::API2::Storage::Config", 'update', ['storage'] ],
654 remove
=> [ "PVE::API2::Storage::Config", 'delete', ['storage'] ],
655 status
=> [ "PVE::API2::Storage::Status", 'index', [],
656 { node
=> $nodename }, $print_status ],
657 list
=> [ "PVE::API2::Storage::Content", 'index', ['storage'],
658 { node
=> $nodename }, $print_content ],
659 alloc
=> [ "PVE::API2::Storage::Content", 'create', ['storage', 'vmid', 'filename', 'size'],
660 { node
=> $nodename }, sub {
662 print "successfully created '$volid'\n";
664 free
=> [ "PVE::API2::Storage::Content", 'delete', ['volume'],
665 { node
=> $nodename } ],
667 nfs
=> [ __PACKAGE__
, 'nfsscan', ['server'], { node
=> $nodename }, sub {
671 foreach my $rec (@$res) {
672 my $len = length ($rec->{path
});
673 $maxlen = $len if $len > $maxlen;
675 foreach my $rec (@$res) {
676 printf "%-${maxlen}s %s\n", $rec->{path
}, $rec->{options
};
679 cifs
=> [ __PACKAGE__
, 'cifsscan', ['server'], { node
=> $nodename }, sub {
683 foreach my $rec (@$res) {
684 my $len = length ($rec->{share
});
685 $maxlen = $len if $len > $maxlen;
687 foreach my $rec (@$res) {
688 printf "%-${maxlen}s %s\n", $rec->{share
}, $rec->{description
};
691 glusterfs
=> [ __PACKAGE__
, 'glusterfsscan', ['server'], { node
=> $nodename }, sub {
694 foreach my $rec (@$res) {
695 printf "%s\n", $rec->{volname
};
698 iscsi
=> [ __PACKAGE__
, 'iscsiscan', ['portal'], { node
=> $nodename }, sub {
702 foreach my $rec (@$res) {
703 my $len = length ($rec->{target
});
704 $maxlen = $len if $len > $maxlen;
706 foreach my $rec (@$res) {
707 printf "%-${maxlen}s %s\n", $rec->{target
}, $rec->{portal
};
710 lvm
=> [ __PACKAGE__
, 'lvmscan', [], { node
=> $nodename }, sub {
712 foreach my $rec (@$res) {
713 printf "$rec->{vg}\n";
716 lvmthin
=> [ __PACKAGE__
, 'lvmthinscan', ['vg'], { node
=> $nodename }, sub {
718 foreach my $rec (@$res) {
719 printf "$rec->{lv}\n";
722 zfs
=> [ __PACKAGE__
, 'zfsscan', [], { node
=> $nodename }, sub {
725 foreach my $rec (@$res) {
726 printf "$rec->{pool}\n";
730 nfsscan
=> { alias
=> 'scan nfs' },
731 cifsscan
=> { alias
=> 'scan cifs' },
732 glusterfsscan
=> { alias
=> 'scan glusterfs' },
733 iscsiscan
=> { alias
=> 'scan iscsi' },
734 lvmscan
=> { alias
=> 'scan lvm' },
735 lvmthinscan
=> { alias
=> 'scan lvmthin' },
736 zfsscan
=> { alias
=> 'scan zfs' },
737 path
=> [ __PACKAGE__
, 'path', ['volume']],
738 extractconfig
=> [__PACKAGE__
, 'extractconfig', ['volume']],
739 export
=> [ __PACKAGE__
, 'export', ['volume', 'format', 'filename']],
740 import
=> [ __PACKAGE__
, 'import', ['volume', 'format', 'filename']],