]>
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",
263 returns
=> { type
=> 'null' },
267 my $filename = $param->{filename
};
270 if ($filename eq '-') {
273 open($infh, '<', $filename)
274 or die "open($filename): $!\n";
277 my $cfg = PVE
::Storage
::config
();
278 PVE
::Storage
::volume_import
($cfg, $infh, $param->{volume
}, $param->{format
},
279 $param->{base
}, $param->{'with-snapshots'});
285 add
=> [ "PVE::API2::Storage::Config", 'create', ['type', 'storage'] ],
286 set
=> [ "PVE::API2::Storage::Config", 'update', ['storage'] ],
287 remove
=> [ "PVE::API2::Storage::Config", 'delete', ['storage'] ],
288 status
=> [ "PVE::API2::Storage::Status", 'index', [],
289 { node
=> $nodename }, $print_status ],
290 list
=> [ "PVE::API2::Storage::Content", 'index', ['storage'],
291 { node
=> $nodename }, $print_content ],
292 alloc
=> [ "PVE::API2::Storage::Content", 'create', ['storage', 'vmid', 'filename', 'size'],
293 { node
=> $nodename }, sub {
295 print "successfully created '$volid'\n";
297 free
=> [ "PVE::API2::Storage::Content", 'delete', ['volume'],
298 { node
=> $nodename } ],
299 nfsscan
=> [ "PVE::API2::Storage::Scan", 'nfsscan', ['server'],
300 { node
=> $nodename }, sub {
304 foreach my $rec (@$res) {
305 my $len = length ($rec->{path
});
306 $maxlen = $len if $len > $maxlen;
308 foreach my $rec (@$res) {
309 printf "%-${maxlen}s %s\n", $rec->{path
}, $rec->{options
};
312 glusterfsscan
=> [ "PVE::API2::Storage::Scan", 'glusterfsscan', ['server'],
313 { node
=> $nodename }, sub {
316 foreach my $rec (@$res) {
317 printf "%s\n", $rec->{volname
};
320 iscsiscan
=> [ "PVE::API2::Storage::Scan", 'iscsiscan', ['server'],
321 { node
=> $nodename }, sub {
325 foreach my $rec (@$res) {
326 my $len = length ($rec->{target
});
327 $maxlen = $len if $len > $maxlen;
329 foreach my $rec (@$res) {
330 printf "%-${maxlen}s %s\n", $rec->{target
}, $rec->{portal
};
333 lvmscan
=> [ "PVE::API2::Storage::Scan", 'lvmscan', [],
334 { node
=> $nodename }, sub {
336 foreach my $rec (@$res) {
337 printf "$rec->{vg}\n";
340 lvmthinscan
=> [ "PVE::API2::Storage::Scan", 'lvmthinscan', ['vg'],
341 { node
=> $nodename }, sub {
343 foreach my $rec (@$res) {
344 printf "$rec->{lv}\n";
347 zfsscan
=> [ "PVE::API2::Storage::Scan", 'zfsscan', [],
348 { node
=> $nodename }, sub {
351 foreach my $rec (@$res) {
352 printf "$rec->{pool}\n";
355 path
=> [ __PACKAGE__
, 'path', ['volume']],
356 extractconfig
=> [__PACKAGE__
, 'extractconfig', ['volume']],
357 export
=> [ __PACKAGE__
, 'export', ['volume', 'format', 'filename']],
358 import
=> [ __PACKAGE__
, 'import', ['volume', 'format', 'filename']],