]>
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);
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
();
29 sub setup_environment
{
30 PVE
::RPCEnvironment-
>setup_default_cli_env();
33 __PACKAGE__-
>register_method ({
37 description
=> "Get filesystem path for specified volume",
39 additionalProperties
=> 0,
42 description
=> "Volume identifier",
43 type
=> 'string', format
=> 'pve-volume-id',
44 completion
=> \
&PVE
::Storage
::complete_volume
,
48 returns
=> { type
=> 'null' },
53 my $cfg = PVE
::Storage
::config
();
55 my $path = PVE
::Storage
::path
($cfg, $param->{volume
});
63 __PACKAGE__-
>register_method ({
64 name
=> 'extractconfig',
65 path
=> 'extractconfig',
67 description
=> "Extract configuration from vzdump backup archive.",
69 description
=> "The user needs 'VM.Backup' permissions on the backed up guest ID, and 'Datastore.AllocateSpace' on the backup storage.",
74 additionalProperties
=> 0,
77 description
=> "Volume identifier",
79 completion
=> \
&PVE
::Storage
::complete_volume
,
83 returns
=> { type
=> 'null' },
86 my $volume = $param->{volume
};
88 my $rpcenv = PVE
::RPCEnvironment
::get
();
89 my $authuser = $rpcenv->get_user();
91 my $storage_cfg = PVE
::Storage
::config
();
92 PVE
::Storage
::check_volume_access
($rpcenv, $authuser, $storage_cfg, undef, $volume);
94 my $config_raw = PVE
::Storage
::extract_vzdump_config
($storage_cfg, $volume);
96 print "$config_raw\n";
100 my $print_content = sub {
104 foreach my $info (@$list) {
106 my $volid = $info->{volid
};
107 my $sidlen = length ($volid);
108 $maxlenname = $sidlen if $sidlen > $maxlenname;
111 foreach my $info (@$list) {
112 next if !$info->{vmid
};
113 my $volid = $info->{volid
};
115 printf "%-${maxlenname}s %5s %10d %d\n", $volid,
116 $info->{format
}, $info->{size
}, $info->{vmid
};
119 foreach my $info (sort { $a->{format
} cmp $b->{format
} } @$list) {
120 next if $info->{vmid
};
121 my $volid = $info->{volid
};
123 printf "%-${maxlenname}s %5s %10d\n", $volid,
124 $info->{format
}, $info->{size
};
128 my $print_status = sub {
132 foreach my $res (@$res) {
133 my $storeid = $res->{storage
};
134 $maxlen = length ($storeid) if length ($storeid) > $maxlen;
138 foreach my $res (sort { $a->{storage
} cmp $b->{storage
} } @$res) {
139 my $storeid = $res->{storage
};
141 my $sum = $res->{used
} + $res->{avail
};
142 my $per = $sum ?
(0.5 + ($res->{used
}*100)/$sum) : 100;
144 printf "%-${maxlen}s %5s %1d %15d %15d %15d %.2f%%\n", $storeid,
145 $res->{type
}, $res->{active
},
146 $res->{total
}/1024, $res->{used}/1024, $res->{avail
}/1024, $per;
150 __PACKAGE__-
>register_method ({
154 description
=> "Export a volume.",
157 additionalProperties
=> 0,
160 description
=> "Volume identifier",
162 completion
=> \
&PVE
::Storage
::complete_volume
,
165 description
=> "Export stream format",
167 enum
=> $KNOWN_EXPORT_FORMATS,
170 description
=> "Destination file name",
174 description
=> "Snapshot to start an incremental stream from",
176 pattern
=> qr/[a-z0-9_\-]{1,40}/,
181 description
=> "Snapshot to export",
183 pattern
=> qr/[a-z0-9_\-]{1,40}/,
187 'with-snapshots' => {
189 "Whether to include intermediate snapshots in the stream",
196 returns
=> { type
=> 'null' },
200 my $filename = $param->{filename
};
203 if ($filename eq '-') {
206 sysopen($outfh, $filename, O_CREAT
|O_WRONLY
|O_TRUNC
)
207 or die "open($filename): $!\n";
211 my $cfg = PVE
::Storage
::config
();
212 PVE
::Storage
::volume_export
($cfg, $outfh, $param->{volume
}, $param->{format
},
213 $param->{snapshot
}, $param->{base
}, $param->{'with-snapshots'});
216 if ($filename ne '-') {
218 unlink($filename) if $err;
225 __PACKAGE__-
>register_method ({
229 description
=> "Import a volume.",
232 additionalProperties
=> 0,
235 description
=> "Volume identifier",
237 completion
=> \
&PVE
::Storage
::complete_volume
,
240 description
=> "Import stream format",
242 enum
=> $KNOWN_EXPORT_FORMATS,
245 description
=> "Source file name",
249 description
=> "Base snapshot of an incremental stream",
251 pattern
=> qr/[a-z0-9_\-]{1,40}/,
255 'with-snapshots' => {
257 "Whether the stream includes intermediate snapshots",
262 'delete-snapshot' => {
263 description
=> "A snapshot to delete on success",
265 pattern
=> qr/[a-z0-9_\-]{1,80}/,
271 returns
=> { type
=> 'null' },
275 my $filename = $param->{filename
};
278 if ($filename eq '-') {
281 sysopen($infh, $filename, O_RDONLY
)
282 or die "open($filename): $!\n";
285 my $cfg = PVE
::Storage
::config
();
286 my $volume = $param->{volume
};
287 my $delete = $param->{'delete-snapshot'};
288 PVE
::Storage
::volume_import
($cfg, $infh, $volume, $param->{format
},
289 $param->{base
}, $param->{'with-snapshots'});
290 PVE
::Storage
::volume_snapshot_delete
($cfg, $volume, $delete)
297 add
=> [ "PVE::API2::Storage::Config", 'create', ['type', 'storage'] ],
298 set
=> [ "PVE::API2::Storage::Config", 'update', ['storage'] ],
299 remove
=> [ "PVE::API2::Storage::Config", 'delete', ['storage'] ],
300 status
=> [ "PVE::API2::Storage::Status", 'index', [],
301 { node
=> $nodename }, $print_status ],
302 list
=> [ "PVE::API2::Storage::Content", 'index', ['storage'],
303 { node
=> $nodename }, $print_content ],
304 alloc
=> [ "PVE::API2::Storage::Content", 'create', ['storage', 'vmid', 'filename', 'size'],
305 { node
=> $nodename }, sub {
307 print "successfully created '$volid'\n";
309 free
=> [ "PVE::API2::Storage::Content", 'delete', ['volume'],
310 { node
=> $nodename } ],
311 nfsscan
=> [ "PVE::API2::Storage::Scan", 'nfsscan', ['server'],
312 { node
=> $nodename }, sub {
316 foreach my $rec (@$res) {
317 my $len = length ($rec->{path
});
318 $maxlen = $len if $len > $maxlen;
320 foreach my $rec (@$res) {
321 printf "%-${maxlen}s %s\n", $rec->{path
}, $rec->{options
};
324 glusterfsscan
=> [ "PVE::API2::Storage::Scan", 'glusterfsscan', ['server'],
325 { node
=> $nodename }, sub {
328 foreach my $rec (@$res) {
329 printf "%s\n", $rec->{volname
};
332 iscsiscan
=> [ "PVE::API2::Storage::Scan", 'iscsiscan', ['server'],
333 { node
=> $nodename }, sub {
337 foreach my $rec (@$res) {
338 my $len = length ($rec->{target
});
339 $maxlen = $len if $len > $maxlen;
341 foreach my $rec (@$res) {
342 printf "%-${maxlen}s %s\n", $rec->{target
}, $rec->{portal
};
345 lvmscan
=> [ "PVE::API2::Storage::Scan", 'lvmscan', [],
346 { node
=> $nodename }, sub {
348 foreach my $rec (@$res) {
349 printf "$rec->{vg}\n";
352 lvmthinscan
=> [ "PVE::API2::Storage::Scan", 'lvmthinscan', ['vg'],
353 { node
=> $nodename }, sub {
355 foreach my $rec (@$res) {
356 printf "$rec->{lv}\n";
359 zfsscan
=> [ "PVE::API2::Storage::Scan", 'zfsscan', [],
360 { node
=> $nodename }, sub {
363 foreach my $rec (@$res) {
364 printf "$rec->{pool}\n";
367 path
=> [ __PACKAGE__
, 'path', ['volume']],
368 extractconfig
=> [__PACKAGE__
, 'extractconfig', ['volume']],
369 export
=> [ __PACKAGE__
, 'export', ['volume', 'format', 'filename']],
370 import
=> [ __PACKAGE__
, 'import', ['volume', 'format', 'filename']],