]>
git.proxmox.com Git - pve-storage.git/blob - PVE/CLI/pvesm.pm
1 package PVE
::CLI
::pvesm
;
12 use PVE
::RPCEnvironment
;
14 use PVE
::API2
::Storage
::Config
;
15 use PVE
::API2
::Storage
::Content
;
16 use PVE
::API2
::Storage
::Status
;
17 use PVE
::API2
::Storage
::Scan
;
18 use PVE
::JSONSchema
qw(get_standard_option);
22 use base
qw(PVE::CLIHandler);
24 my $KNOWN_EXPORT_FORMATS = ['zfs'];
26 my $nodename = PVE
::INotify
::nodename
();
28 sub setup_environment
{
29 PVE
::RPCEnvironment-
>setup_default_cli_env();
32 __PACKAGE__-
>register_method ({
36 description
=> "Get filesystem path for specified volume",
38 additionalProperties
=> 0,
41 description
=> "Volume identifier",
42 type
=> 'string', format
=> 'pve-volume-id',
43 completion
=> \
&PVE
::Storage
::complete_volume
,
47 returns
=> { type
=> 'null' },
52 my $cfg = PVE
::Storage
::config
();
54 my $path = PVE
::Storage
::path
($cfg, $param->{volume
});
62 __PACKAGE__-
>register_method ({
63 name
=> 'extractconfig',
64 path
=> 'extractconfig',
66 description
=> "Extract configuration from vzdump backup archive.",
68 description
=> "The user needs 'VM.Backup' permissions on the backed up guest ID, and 'Datastore.AllocateSpace' on the backup storage.",
73 additionalProperties
=> 0,
76 description
=> "Volume identifier",
78 completion
=> \
&PVE
::Storage
::complete_volume
,
82 returns
=> { type
=> 'null' },
85 my $volume = $param->{volume
};
87 my $rpcenv = PVE
::RPCEnvironment
::get
();
88 my $authuser = $rpcenv->get_user();
90 my $storage_cfg = PVE
::Storage
::config
();
91 PVE
::Storage
::check_volume_access
($rpcenv, $authuser, $storage_cfg, undef, $volume);
93 my $config_raw = PVE
::Storage
::extract_vzdump_config
($storage_cfg, $volume);
95 print "$config_raw\n";
99 my $print_content = sub {
103 foreach my $info (@$list) {
105 my $volid = $info->{volid
};
106 my $sidlen = length ($volid);
107 $maxlenname = $sidlen if $sidlen > $maxlenname;
110 foreach my $info (@$list) {
111 next if !$info->{vmid
};
112 my $volid = $info->{volid
};
114 printf "%-${maxlenname}s %5s %10d %d\n", $volid,
115 $info->{format
}, $info->{size
}, $info->{vmid
};
118 foreach my $info (sort { $a->{format
} cmp $b->{format
} } @$list) {
119 next if $info->{vmid
};
120 my $volid = $info->{volid
};
122 printf "%-${maxlenname}s %5s %10d\n", $volid,
123 $info->{format
}, $info->{size
};
127 my $print_status = sub {
131 foreach my $res (@$res) {
132 my $storeid = $res->{storage
};
133 $maxlen = length ($storeid) if length ($storeid) > $maxlen;
137 foreach my $res (sort { $a->{storage
} cmp $b->{storage
} } @$res) {
138 my $storeid = $res->{storage
};
140 my $sum = $res->{used
} + $res->{avail
};
141 my $per = $sum ?
(0.5 + ($res->{used
}*100)/$sum) : 100;
143 printf "%-${maxlen}s %5s %1d %15d %15d %15d %.2f%%\n", $storeid,
144 $res->{type
}, $res->{active
},
145 $res->{total
}/1024, $res->{used}/1024, $res->{avail
}/1024, $per;
149 __PACKAGE__-
>register_method ({
153 description
=> "Export a volume.",
156 additionalProperties
=> 0,
159 description
=> "Volume identifier",
161 completion
=> \
&PVE
::Storage
::complete_volume
,
164 description
=> "Export stream format",
166 enum
=> $KNOWN_EXPORT_FORMATS,
169 description
=> "Destination file name",
173 description
=> "Snapshot to start an incremental stream from",
175 pattern
=> qr/[a-z0-9_\-]{1,40}/,
180 description
=> "Snapshot to export",
182 pattern
=> qr/[a-z0-9_\-]{1,40}/,
186 'with-snapshots' => {
188 "Whether to include intermediate snapshots in the stream",
195 returns
=> { type
=> 'null' },
199 my $filename = $param->{filename
};
202 if ($filename eq '-') {
205 open($outfh, '>', $filename)
206 or die "open($filename): $!\n";
210 my $cfg = PVE
::Storage
::config
();
211 PVE
::Storage
::volume_export
($cfg, $outfh, $param->{volume
}, $param->{format
},
212 $param->{snapshot
}, $param->{base
}, $param->{'with-snapshots'});
215 if ($filename ne '-') {
217 unlink($filename) if $err;
224 __PACKAGE__-
>register_method ({
228 description
=> "Import a volume.",
231 additionalProperties
=> 0,
234 description
=> "Volume identifier",
236 completion
=> \
&PVE
::Storage
::complete_volume
,
239 description
=> "Import stream format",
241 enum
=> $KNOWN_EXPORT_FORMATS,
244 description
=> "Source file name",
248 description
=> "Base snapshot of an incremental stream",
250 pattern
=> qr/[a-z0-9_\-]{1,40}/,
254 'with-snapshots' => {
256 "Whether the stream includes intermediate snapshots",
261 'delete-snapshot' => {
262 description
=> "A snapshot to delete on success",
264 pattern
=> qr/[a-z0-9_\-]{1,80}/,
270 returns
=> { type
=> 'null' },
274 my $filename = $param->{filename
};
277 if ($filename eq '-') {
280 open($infh, '<', $filename)
281 or die "open($filename): $!\n";
284 my $cfg = PVE
::Storage
::config
();
285 my $volume = $param->{volume
};
286 my $delete = $param->{'delete-snapshot'};
287 PVE
::Storage
::volume_import
($cfg, $infh, $volume, $param->{format
},
288 $param->{base
}, $param->{'with-snapshots'});
289 PVE
::Storage
::volume_snapshot_delete
($cfg, $volume, $delete)
296 add
=> [ "PVE::API2::Storage::Config", 'create', ['type', 'storage'] ],
297 set
=> [ "PVE::API2::Storage::Config", 'update', ['storage'] ],
298 remove
=> [ "PVE::API2::Storage::Config", 'delete', ['storage'] ],
299 status
=> [ "PVE::API2::Storage::Status", 'index', [],
300 { node
=> $nodename }, $print_status ],
301 list
=> [ "PVE::API2::Storage::Content", 'index', ['storage'],
302 { node
=> $nodename }, $print_content ],
303 alloc
=> [ "PVE::API2::Storage::Content", 'create', ['storage', 'vmid', 'filename', 'size'],
304 { node
=> $nodename }, sub {
306 print "successfully created '$volid'\n";
308 free
=> [ "PVE::API2::Storage::Content", 'delete', ['volume'],
309 { node
=> $nodename } ],
310 nfsscan
=> [ "PVE::API2::Storage::Scan", 'nfsscan', ['server'],
311 { node
=> $nodename }, sub {
315 foreach my $rec (@$res) {
316 my $len = length ($rec->{path
});
317 $maxlen = $len if $len > $maxlen;
319 foreach my $rec (@$res) {
320 printf "%-${maxlen}s %s\n", $rec->{path
}, $rec->{options
};
323 glusterfsscan
=> [ "PVE::API2::Storage::Scan", 'glusterfsscan', ['server'],
324 { node
=> $nodename }, sub {
327 foreach my $rec (@$res) {
328 printf "%s\n", $rec->{volname
};
331 iscsiscan
=> [ "PVE::API2::Storage::Scan", 'iscsiscan', ['server'],
332 { node
=> $nodename }, sub {
336 foreach my $rec (@$res) {
337 my $len = length ($rec->{target
});
338 $maxlen = $len if $len > $maxlen;
340 foreach my $rec (@$res) {
341 printf "%-${maxlen}s %s\n", $rec->{target
}, $rec->{portal
};
344 lvmscan
=> [ "PVE::API2::Storage::Scan", 'lvmscan', [],
345 { node
=> $nodename }, sub {
347 foreach my $rec (@$res) {
348 printf "$rec->{vg}\n";
351 lvmthinscan
=> [ "PVE::API2::Storage::Scan", 'lvmthinscan', ['vg'],
352 { node
=> $nodename }, sub {
354 foreach my $rec (@$res) {
355 printf "$rec->{lv}\n";
358 zfsscan
=> [ "PVE::API2::Storage::Scan", 'zfsscan', [],
359 { node
=> $nodename }, sub {
362 foreach my $rec (@$res) {
363 printf "$rec->{pool}\n";
366 path
=> [ __PACKAGE__
, 'path', ['volume']],
367 extractconfig
=> [__PACKAGE__
, 'extractconfig', ['volume']],
368 export
=> [ __PACKAGE__
, 'export', ['volume', 'format', 'filename']],
369 import
=> [ __PACKAGE__
, 'import', ['volume', 'format', 'filename']],