]>
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
::API2
::Storage
::Scan
;
19 use PVE
::JSONSchema
qw(get_standard_option);
24 use base
qw(PVE::CLIHandler);
26 my $KNOWN_EXPORT_FORMATS = ['raw+size', 'tar+size', 'qcow2+size', 'vmdk+size', 'zfs'];
28 my $nodename = PVE
::INotify
::nodename
();
33 my $password_map = PVE
::CLIHandler
::get_standard_mapping
('pve-password', {
36 return $value if $value;
37 return PVE
::PTY
::read_password
("Enter Password: ");
41 'cifsscan' => [ $password_map ],
42 'create' => [ $password_map ],
44 return $mapping->{$name};
47 sub setup_environment
{
48 PVE
::RPCEnvironment-
>setup_default_cli_env();
51 __PACKAGE__-
>register_method ({
55 description
=> "Get filesystem path for specified volume",
57 additionalProperties
=> 0,
60 description
=> "Volume identifier",
61 type
=> 'string', format
=> 'pve-volume-id',
62 completion
=> \
&PVE
::Storage
::complete_volume
,
66 returns
=> { type
=> 'null' },
71 my $cfg = PVE
::Storage
::config
();
73 my $path = PVE
::Storage
::path
($cfg, $param->{volume
});
81 __PACKAGE__-
>register_method ({
82 name
=> 'extractconfig',
83 path
=> 'extractconfig',
85 description
=> "Extract configuration from vzdump backup archive.",
87 description
=> "The user needs 'VM.Backup' permissions on the backed up guest ID, and 'Datastore.AllocateSpace' on the backup storage.",
92 additionalProperties
=> 0,
95 description
=> "Volume identifier",
97 completion
=> \
&PVE
::Storage
::complete_volume
,
101 returns
=> { type
=> 'null' },
104 my $volume = $param->{volume
};
106 my $rpcenv = PVE
::RPCEnvironment
::get
();
107 my $authuser = $rpcenv->get_user();
109 my $storage_cfg = PVE
::Storage
::config
();
110 PVE
::Storage
::check_volume_access
($rpcenv, $authuser, $storage_cfg, undef, $volume);
112 my $config_raw = PVE
::Storage
::extract_vzdump_config
($storage_cfg, $volume);
114 print "$config_raw\n";
118 my $print_content = sub {
122 foreach my $info (@$list) {
124 my $volid = $info->{volid
};
125 my $sidlen = length ($volid);
126 $maxlenname = $sidlen if $sidlen > $maxlenname;
129 foreach my $info (@$list) {
130 next if !$info->{vmid
};
131 my $volid = $info->{volid
};
133 printf "%-${maxlenname}s %5s %10d %d\n", $volid,
134 $info->{format
}, $info->{size
}, $info->{vmid
};
137 foreach my $info (sort { $a->{format
} cmp $b->{format
} } @$list) {
138 next if $info->{vmid
};
139 my $volid = $info->{volid
};
141 printf "%-${maxlenname}s %5s %10d\n", $volid,
142 $info->{format
}, $info->{size
};
146 my $print_status = sub {
150 foreach my $res (@$res) {
151 my $storeid = $res->{storage
};
152 $maxlen = length ($storeid) if length ($storeid) > $maxlen;
156 printf "%-${maxlen}s %10s %10s %15s %15s %15s %8s\n", 'Name', 'Type',
157 'Status', 'Total', 'Used', 'Available', '%';
159 foreach my $res (sort { $a->{storage
} cmp $b->{storage
} } @$res) {
160 my $storeid = $res->{storage
};
162 my $active = $res->{active
} ?
'active' : 'inactive';
163 my ($per, $per_fmt) = (0, '% 7.2f%%');
164 $per = ($res->{used
}*100)/$res->{total
} if $res->{total
} > 0;
166 if (!$res->{enabled
}) {
169 $active = 'disabled';
172 printf "%-${maxlen}s %10s %10s %15d %15d %15d $per_fmt\n", $storeid,
173 $res->{type
}, $active, $res->{total
}/1024, $res->{used}/1024,
174 $res->{avail
}/1024, $per;
178 __PACKAGE__-
>register_method ({
182 description
=> "Export a volume.",
185 additionalProperties
=> 0,
188 description
=> "Volume identifier",
190 completion
=> \
&PVE
::Storage
::complete_volume
,
193 description
=> "Export stream format",
195 enum
=> $KNOWN_EXPORT_FORMATS,
198 description
=> "Destination file name",
202 description
=> "Snapshot to start an incremental stream from",
204 pattern
=> qr/[a-z0-9_\-]{1,40}/,
209 description
=> "Snapshot to export",
211 pattern
=> qr/[a-z0-9_\-]{1,40}/,
215 'with-snapshots' => {
217 "Whether to include intermediate snapshots in the stream",
224 returns
=> { type
=> 'null' },
228 my $filename = $param->{filename
};
231 if ($filename eq '-') {
234 sysopen($outfh, $filename, O_CREAT
|O_WRONLY
|O_TRUNC
)
235 or die "open($filename): $!\n";
239 my $cfg = PVE
::Storage
::config
();
240 PVE
::Storage
::volume_export
($cfg, $outfh, $param->{volume
}, $param->{format
},
241 $param->{snapshot
}, $param->{base
}, $param->{'with-snapshots'});
244 if ($filename ne '-') {
246 unlink($filename) if $err;
253 __PACKAGE__-
>register_method ({
257 description
=> "Import a volume.",
260 additionalProperties
=> 0,
263 description
=> "Volume identifier",
265 completion
=> \
&PVE
::Storage
::complete_volume
,
268 description
=> "Import stream format",
270 enum
=> $KNOWN_EXPORT_FORMATS,
273 description
=> "Source file name",
277 description
=> "Base snapshot of an incremental stream",
279 pattern
=> qr/[a-z0-9_\-]{1,40}/,
283 'with-snapshots' => {
285 "Whether the stream includes intermediate snapshots",
290 'delete-snapshot' => {
291 description
=> "A snapshot to delete on success",
293 pattern
=> qr/[a-z0-9_\-]{1,80}/,
299 returns
=> { type
=> 'null' },
303 my $filename = $param->{filename
};
306 if ($filename eq '-') {
309 sysopen($infh, $filename, O_RDONLY
)
310 or die "open($filename): $!\n";
313 my $cfg = PVE
::Storage
::config
();
314 my $volume = $param->{volume
};
315 my $delete = $param->{'delete-snapshot'};
316 PVE
::Storage
::volume_import
($cfg, $infh, $volume, $param->{format
},
317 $param->{base
}, $param->{'with-snapshots'});
318 PVE
::Storage
::volume_snapshot_delete
($cfg, $volume, $delete)
325 add
=> [ "PVE::API2::Storage::Config", 'create', ['type', 'storage'] ],
326 set
=> [ "PVE::API2::Storage::Config", 'update', ['storage'] ],
327 remove
=> [ "PVE::API2::Storage::Config", 'delete', ['storage'] ],
328 status
=> [ "PVE::API2::Storage::Status", 'index', [],
329 { node
=> $nodename }, $print_status ],
330 list
=> [ "PVE::API2::Storage::Content", 'index', ['storage'],
331 { node
=> $nodename }, $print_content ],
332 alloc
=> [ "PVE::API2::Storage::Content", 'create', ['storage', 'vmid', 'filename', 'size'],
333 { node
=> $nodename }, sub {
335 print "successfully created '$volid'\n";
337 free
=> [ "PVE::API2::Storage::Content", 'delete', ['volume'],
338 { node
=> $nodename } ],
339 nfsscan
=> [ "PVE::API2::Storage::Scan", 'nfsscan', ['server'],
340 { node
=> $nodename }, sub {
344 foreach my $rec (@$res) {
345 my $len = length ($rec->{path
});
346 $maxlen = $len if $len > $maxlen;
348 foreach my $rec (@$res) {
349 printf "%-${maxlen}s %s\n", $rec->{path
}, $rec->{options
};
352 cifsscan
=> [ "PVE::API2::Storage::Scan", 'cifsscan', ['server'],
353 { node
=> $nodename }, sub {
357 foreach my $rec (@$res) {
358 my $len = length ($rec->{share
});
359 $maxlen = $len if $len > $maxlen;
361 foreach my $rec (@$res) {
362 printf "%-${maxlen}s %s\n", $rec->{share
}, $rec->{description
};
365 glusterfsscan
=> [ "PVE::API2::Storage::Scan", 'glusterfsscan', ['server'],
366 { node
=> $nodename }, sub {
369 foreach my $rec (@$res) {
370 printf "%s\n", $rec->{volname
};
373 iscsiscan
=> [ "PVE::API2::Storage::Scan", 'iscsiscan', ['server'],
374 { node
=> $nodename }, sub {
378 foreach my $rec (@$res) {
379 my $len = length ($rec->{target
});
380 $maxlen = $len if $len > $maxlen;
382 foreach my $rec (@$res) {
383 printf "%-${maxlen}s %s\n", $rec->{target
}, $rec->{portal
};
386 lvmscan
=> [ "PVE::API2::Storage::Scan", 'lvmscan', [],
387 { node
=> $nodename }, sub {
389 foreach my $rec (@$res) {
390 printf "$rec->{vg}\n";
393 lvmthinscan
=> [ "PVE::API2::Storage::Scan", 'lvmthinscan', ['vg'],
394 { node
=> $nodename }, sub {
396 foreach my $rec (@$res) {
397 printf "$rec->{lv}\n";
400 zfsscan
=> [ "PVE::API2::Storage::Scan", 'zfsscan', [],
401 { node
=> $nodename }, sub {
404 foreach my $rec (@$res) {
405 printf "$rec->{pool}\n";
408 path
=> [ __PACKAGE__
, 'path', ['volume']],
409 extractconfig
=> [__PACKAGE__
, 'extractconfig', ['volume']],
410 export
=> [ __PACKAGE__
, 'export', ['volume', 'format', 'filename']],
411 import
=> [ __PACKAGE__
, 'import', ['volume', 'format', 'filename']],