]> git.proxmox.com Git - pve-storage.git/blob - PVE/CLI/pvesm.pm
import: add -delete-snapshot parameter
[pve-storage.git] / PVE / CLI / pvesm.pm
1 package PVE::CLI::pvesm;
2
3 use strict;
4 use warnings;
5
6 use Fcntl ':flock';
7 use File::Path;
8
9 use PVE::SafeSyslog;
10 use PVE::Cluster;
11 use PVE::INotify;
12 use PVE::RPCEnvironment;
13 use PVE::Storage;
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);
19
20 use PVE::CLIHandler;
21
22 use base qw(PVE::CLIHandler);
23
24 my $KNOWN_EXPORT_FORMATS = ['zfs'];
25
26 my $nodename = PVE::INotify::nodename();
27
28 sub setup_environment {
29 PVE::RPCEnvironment->setup_default_cli_env();
30 }
31
32 __PACKAGE__->register_method ({
33 name => 'path',
34 path => 'path',
35 method => 'GET',
36 description => "Get filesystem path for specified volume",
37 parameters => {
38 additionalProperties => 0,
39 properties => {
40 volume => {
41 description => "Volume identifier",
42 type => 'string', format => 'pve-volume-id',
43 completion => \&PVE::Storage::complete_volume,
44 },
45 },
46 },
47 returns => { type => 'null' },
48
49 code => sub {
50 my ($param) = @_;
51
52 my $cfg = PVE::Storage::config();
53
54 my $path = PVE::Storage::path ($cfg, $param->{volume});
55
56 print "$path\n";
57
58 return undef;
59
60 }});
61
62 __PACKAGE__->register_method ({
63 name => 'extractconfig',
64 path => 'extractconfig',
65 method => 'GET',
66 description => "Extract configuration from vzdump backup archive.",
67 permissions => {
68 description => "The user needs 'VM.Backup' permissions on the backed up guest ID, and 'Datastore.AllocateSpace' on the backup storage.",
69 user => 'all',
70 },
71 protected => 1,
72 parameters => {
73 additionalProperties => 0,
74 properties => {
75 volume => {
76 description => "Volume identifier",
77 type => 'string',
78 completion => \&PVE::Storage::complete_volume,
79 },
80 },
81 },
82 returns => { type => 'null' },
83 code => sub {
84 my ($param) = @_;
85 my $volume = $param->{volume};
86
87 my $rpcenv = PVE::RPCEnvironment::get();
88 my $authuser = $rpcenv->get_user();
89
90 my $storage_cfg = PVE::Storage::config();
91 PVE::Storage::check_volume_access($rpcenv, $authuser, $storage_cfg, undef, $volume);
92
93 my $config_raw = PVE::Storage::extract_vzdump_config($storage_cfg, $volume);
94
95 print "$config_raw\n";
96 return;
97 }});
98
99 my $print_content = sub {
100 my ($list) = @_;
101
102 my $maxlenname = 0;
103 foreach my $info (@$list) {
104
105 my $volid = $info->{volid};
106 my $sidlen = length ($volid);
107 $maxlenname = $sidlen if $sidlen > $maxlenname;
108 }
109
110 foreach my $info (@$list) {
111 next if !$info->{vmid};
112 my $volid = $info->{volid};
113
114 printf "%-${maxlenname}s %5s %10d %d\n", $volid,
115 $info->{format}, $info->{size}, $info->{vmid};
116 }
117
118 foreach my $info (sort { $a->{format} cmp $b->{format} } @$list) {
119 next if $info->{vmid};
120 my $volid = $info->{volid};
121
122 printf "%-${maxlenname}s %5s %10d\n", $volid,
123 $info->{format}, $info->{size};
124 }
125 };
126
127 my $print_status = sub {
128 my $res = shift;
129
130 my $maxlen = 0;
131 foreach my $res (@$res) {
132 my $storeid = $res->{storage};
133 $maxlen = length ($storeid) if length ($storeid) > $maxlen;
134 }
135 $maxlen+=1;
136
137 foreach my $res (sort { $a->{storage} cmp $b->{storage} } @$res) {
138 my $storeid = $res->{storage};
139
140 my $sum = $res->{used} + $res->{avail};
141 my $per = $sum ? (0.5 + ($res->{used}*100)/$sum) : 100;
142
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;
146 }
147 };
148
149 __PACKAGE__->register_method ({
150 name => 'export',
151 path => 'export',
152 method => 'GET',
153 description => "Export a volume.",
154 protected => 1,
155 parameters => {
156 additionalProperties => 0,
157 properties => {
158 volume => {
159 description => "Volume identifier",
160 type => 'string',
161 completion => \&PVE::Storage::complete_volume,
162 },
163 format => {
164 description => "Export stream format",
165 type => 'string',
166 enum => $KNOWN_EXPORT_FORMATS,
167 },
168 filename => {
169 description => "Destination file name",
170 type => 'string',
171 },
172 base => {
173 description => "Snapshot to start an incremental stream from",
174 type => 'string',
175 pattern => qr/[a-z0-9_\-]{1,40}/,
176 maxLength => 40,
177 optional => 1,
178 },
179 snapshot => {
180 description => "Snapshot to export",
181 type => 'string',
182 pattern => qr/[a-z0-9_\-]{1,40}/,
183 maxLength => 40,
184 optional => 1,
185 },
186 'with-snapshots' => {
187 description =>
188 "Whether to include intermediate snapshots in the stream",
189 type => 'boolean',
190 optional => 1,
191 default => 0,
192 },
193 },
194 },
195 returns => { type => 'null' },
196 code => sub {
197 my ($param) = @_;
198
199 my $filename = $param->{filename};
200
201 my $outfh;
202 if ($filename eq '-') {
203 $outfh = \*STDOUT;
204 } else {
205 open($outfh, '>', $filename)
206 or die "open($filename): $!\n";
207 }
208
209 eval {
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'});
213 };
214 my $err = $@;
215 if ($filename ne '-') {
216 close($outfh);
217 unlink($filename) if $err;
218 }
219 die $err if $err;
220 return;
221 }
222 });
223
224 __PACKAGE__->register_method ({
225 name => 'import',
226 path => 'import',
227 method => 'PUT',
228 description => "Import a volume.",
229 protected => 1,
230 parameters => {
231 additionalProperties => 0,
232 properties => {
233 volume => {
234 description => "Volume identifier",
235 type => 'string',
236 completion => \&PVE::Storage::complete_volume,
237 },
238 format => {
239 description => "Import stream format",
240 type => 'string',
241 enum => $KNOWN_EXPORT_FORMATS,
242 },
243 filename => {
244 description => "Source file name",
245 type => 'string',
246 },
247 base => {
248 description => "Base snapshot of an incremental stream",
249 type => 'string',
250 pattern => qr/[a-z0-9_\-]{1,40}/,
251 maxLength => 40,
252 optional => 1,
253 },
254 'with-snapshots' => {
255 description =>
256 "Whether the stream includes intermediate snapshots",
257 type => 'boolean',
258 optional => 1,
259 default => 0,
260 },
261 'delete-snapshot' => {
262 description => "A snapshot to delete on success",
263 type => 'string',
264 pattern => qr/[a-z0-9_\-]{1,80}/,
265 maxLength => 80,
266 optional => 1,
267 },
268 },
269 },
270 returns => { type => 'null' },
271 code => sub {
272 my ($param) = @_;
273
274 my $filename = $param->{filename};
275
276 my $infh;
277 if ($filename eq '-') {
278 $infh = \*STDIN;
279 } else {
280 open($infh, '<', $filename)
281 or die "open($filename): $!\n";
282 }
283
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)
290 if defined($delete);
291 return;
292 }
293 });
294
295 our $cmddef = {
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 {
305 my $volid = shift;
306 print "successfully created '$volid'\n";
307 }],
308 free => [ "PVE::API2::Storage::Content", 'delete', ['volume'],
309 { node => $nodename } ],
310 nfsscan => [ "PVE::API2::Storage::Scan", 'nfsscan', ['server'],
311 { node => $nodename }, sub {
312 my $res = shift;
313
314 my $maxlen = 0;
315 foreach my $rec (@$res) {
316 my $len = length ($rec->{path});
317 $maxlen = $len if $len > $maxlen;
318 }
319 foreach my $rec (@$res) {
320 printf "%-${maxlen}s %s\n", $rec->{path}, $rec->{options};
321 }
322 }],
323 glusterfsscan => [ "PVE::API2::Storage::Scan", 'glusterfsscan', ['server'],
324 { node => $nodename }, sub {
325 my $res = shift;
326
327 foreach my $rec (@$res) {
328 printf "%s\n", $rec->{volname};
329 }
330 }],
331 iscsiscan => [ "PVE::API2::Storage::Scan", 'iscsiscan', ['server'],
332 { node => $nodename }, sub {
333 my $res = shift;
334
335 my $maxlen = 0;
336 foreach my $rec (@$res) {
337 my $len = length ($rec->{target});
338 $maxlen = $len if $len > $maxlen;
339 }
340 foreach my $rec (@$res) {
341 printf "%-${maxlen}s %s\n", $rec->{target}, $rec->{portal};
342 }
343 }],
344 lvmscan => [ "PVE::API2::Storage::Scan", 'lvmscan', [],
345 { node => $nodename }, sub {
346 my $res = shift;
347 foreach my $rec (@$res) {
348 printf "$rec->{vg}\n";
349 }
350 }],
351 lvmthinscan => [ "PVE::API2::Storage::Scan", 'lvmthinscan', ['vg'],
352 { node => $nodename }, sub {
353 my $res = shift;
354 foreach my $rec (@$res) {
355 printf "$rec->{lv}\n";
356 }
357 }],
358 zfsscan => [ "PVE::API2::Storage::Scan", 'zfsscan', [],
359 { node => $nodename }, sub {
360 my $res = shift;
361
362 foreach my $rec (@$res) {
363 printf "$rec->{pool}\n";
364 }
365 }],
366 path => [ __PACKAGE__, 'path', ['volume']],
367 extractconfig => [__PACKAGE__, 'extractconfig', ['volume']],
368 export => [ __PACKAGE__, 'export', ['volume', 'format', 'filename']],
369 import => [ __PACKAGE__, 'import', ['volume', 'format', 'filename']],
370 };
371
372 1;