]>
Commit | Line | Data |
---|---|---|
b6cf0a66 DM |
1 | package PVE::Storage; |
2 | ||
3 | use strict; | |
ffd6f2f3 | 4 | use warnings; |
dec97937 | 5 | use Data::Dumper; |
ffd6f2f3 | 6 | |
b6cf0a66 DM |
7 | use POSIX; |
8 | use IO::Select; | |
b6cf0a66 | 9 | use IO::File; |
b6cf0a66 DM |
10 | use File::Basename; |
11 | use File::Path; | |
b6cf0a66 | 12 | use Cwd 'abs_path'; |
7a2d5c1a | 13 | use Socket; |
b6cf0a66 | 14 | |
4dee23d3 | 15 | use PVE::Tools qw(run_command file_read_firstline dir_glob_foreach $IPV6RE); |
83d7192f | 16 | use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file); |
b6cf0a66 DM |
17 | use PVE::Exception qw(raise_param_exc); |
18 | use PVE::JSONSchema; | |
19 | use PVE::INotify; | |
88c3abaf | 20 | use PVE::RPCEnvironment; |
b6cf0a66 | 21 | |
1dc01b9f DM |
22 | use PVE::Storage::Plugin; |
23 | use PVE::Storage::DirPlugin; | |
24 | use PVE::Storage::LVMPlugin; | |
610798bc | 25 | use PVE::Storage::LvmThinPlugin; |
1dc01b9f DM |
26 | use PVE::Storage::NFSPlugin; |
27 | use PVE::Storage::ISCSIPlugin; | |
0509010d | 28 | use PVE::Storage::RBDPlugin; |
caf1960c | 29 | use PVE::Storage::SheepdogPlugin; |
86616554 | 30 | use PVE::Storage::ISCSIDirectPlugin; |
f4648aef | 31 | use PVE::Storage::GlusterfsPlugin; |
85fda4dd | 32 | use PVE::Storage::ZFSPoolPlugin; |
4f914e6e | 33 | use PVE::Storage::ZFSPlugin; |
14770890 | 34 | use PVE::Storage::DRBDPlugin; |
b6cf0a66 | 35 | |
4dee23d3 DP |
36 | # Storage API version. Icrement it on changes in storage API interface. |
37 | use constant APIVER => 1; | |
38 | ||
39 | # load standard plugins | |
1dc01b9f DM |
40 | PVE::Storage::DirPlugin->register(); |
41 | PVE::Storage::LVMPlugin->register(); | |
610798bc | 42 | PVE::Storage::LvmThinPlugin->register(); |
1dc01b9f DM |
43 | PVE::Storage::NFSPlugin->register(); |
44 | PVE::Storage::ISCSIPlugin->register(); | |
0509010d | 45 | PVE::Storage::RBDPlugin->register(); |
caf1960c | 46 | PVE::Storage::SheepdogPlugin->register(); |
86616554 | 47 | PVE::Storage::ISCSIDirectPlugin->register(); |
f4648aef | 48 | PVE::Storage::GlusterfsPlugin->register(); |
85fda4dd | 49 | PVE::Storage::ZFSPoolPlugin->register(); |
4f914e6e | 50 | PVE::Storage::ZFSPlugin->register(); |
14770890 | 51 | PVE::Storage::DRBDPlugin->register(); |
4dee23d3 DP |
52 | |
53 | # load third-party plugins | |
54 | if ( -d '/usr/share/perl5/PVE/Storage/Custom' ) { | |
55 | dir_glob_foreach('/usr/share/perl5/PVE/Storage/Custom', '.*\.pm$', sub { | |
56 | my ($file) = @_; | |
57 | my $modname = 'PVE::Storage::Custom::' . $file; | |
58 | $modname =~ s!\.pm$!!; | |
59 | $file = 'PVE/Storage/Custom/' . $file; | |
60 | ||
61 | eval { | |
62 | require $file; | |
63 | }; | |
64 | if ($@) { | |
65 | warn $@; | |
66 | # Check storage API version and that file is really storage plugin. | |
67 | } elsif ($modname->isa('PVE::Storage::Plugin') && $modname->can('api') && $modname->api() == APIVER) { | |
68 | eval { | |
69 | import $file; | |
70 | $modname->register(); | |
71 | }; | |
72 | warn $@ if $@; | |
73 | } else { | |
74 | warn "Error loading storage plugin \"$modname\" because of API version mismatch. Please, update it.\n" | |
75 | } | |
76 | }); | |
77 | } | |
78 | ||
79 | # initialize all plugins | |
1dc01b9f | 80 | PVE::Storage::Plugin->init(); |
b6cf0a66 | 81 | |
1dc01b9f | 82 | my $UDEVADM = '/sbin/udevadm'; |
b6cf0a66 | 83 | |
1dc01b9f | 84 | # PVE::Storage utility functions |
b6cf0a66 DM |
85 | |
86 | sub config { | |
87 | return cfs_read_file("storage.cfg"); | |
88 | } | |
89 | ||
83d7192f FG |
90 | sub write_config { |
91 | my ($cfg) = @_; | |
92 | ||
93 | cfs_write_file('storage.cfg', $cfg); | |
94 | } | |
95 | ||
b6cf0a66 DM |
96 | sub lock_storage_config { |
97 | my ($code, $errmsg) = @_; | |
98 | ||
99 | cfs_lock_file("storage.cfg", undef, $code); | |
100 | my $err = $@; | |
101 | if ($err) { | |
102 | $errmsg ? die "$errmsg: $err" : die $err; | |
103 | } | |
104 | } | |
105 | ||
b6cf0a66 DM |
106 | sub storage_config { |
107 | my ($cfg, $storeid, $noerr) = @_; | |
108 | ||
82fc923f | 109 | die "no storage ID specified\n" if !$storeid; |
1a3459ac | 110 | |
b6cf0a66 DM |
111 | my $scfg = $cfg->{ids}->{$storeid}; |
112 | ||
113 | die "storage '$storeid' does not exists\n" if (!$noerr && !$scfg); | |
114 | ||
115 | return $scfg; | |
116 | } | |
117 | ||
118 | sub storage_check_node { | |
119 | my ($cfg, $storeid, $node, $noerr) = @_; | |
120 | ||
1dc01b9f | 121 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
122 | |
123 | if ($scfg->{nodes}) { | |
124 | $node = PVE::INotify::nodename() if !$node || ($node eq 'localhost'); | |
125 | if (!$scfg->{nodes}->{$node}) { | |
da156fb3 | 126 | die "storage '$storeid' is not available on node '$node'\n" if !$noerr; |
b6cf0a66 DM |
127 | return undef; |
128 | } | |
129 | } | |
130 | ||
131 | return $scfg; | |
132 | } | |
133 | ||
134 | sub storage_check_enabled { | |
135 | my ($cfg, $storeid, $node, $noerr) = @_; | |
136 | ||
1dc01b9f | 137 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
138 | |
139 | if ($scfg->{disable}) { | |
140 | die "storage '$storeid' is disabled\n" if !$noerr; | |
141 | return undef; | |
142 | } | |
143 | ||
144 | return storage_check_node($cfg, $storeid, $node, $noerr); | |
145 | } | |
146 | ||
147 | sub storage_ids { | |
148 | my ($cfg) = @_; | |
149 | ||
1dc01b9f | 150 | return keys %{$cfg->{ids}}; |
b6cf0a66 DM |
151 | } |
152 | ||
1dc01b9f DM |
153 | sub file_size_info { |
154 | my ($filename, $timeout) = @_; | |
b6cf0a66 | 155 | |
1dc01b9f | 156 | return PVE::Storage::Plugin::file_size_info($filename, $timeout); |
b6cf0a66 DM |
157 | } |
158 | ||
20ccac1b AD |
159 | sub volume_size_info { |
160 | my ($cfg, $volid, $timeout) = @_; | |
161 | ||
f18199e5 DM |
162 | my ($storeid, $volname) = parse_volume_id($volid, 1); |
163 | if ($storeid) { | |
164 | my $scfg = storage_config($cfg, $storeid); | |
165 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
166 | return $plugin->volume_size_info($scfg, $storeid, $volname, $timeout); | |
167 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
168 | return file_size_info($volid, $timeout); | |
169 | } else { | |
170 | return 0; | |
171 | } | |
20ccac1b AD |
172 | } |
173 | ||
7e6c05dc AD |
174 | sub volume_resize { |
175 | my ($cfg, $volid, $size, $running) = @_; | |
176 | ||
177 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
178 | if ($storeid) { | |
179 | my $scfg = storage_config($cfg, $storeid); | |
180 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
181 | return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running); | |
182 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
f824c722 | 183 | die "resize file/device '$volid' is not possible\n"; |
7e6c05dc | 184 | } else { |
f824c722 | 185 | die "unable to parse volume ID '$volid'\n"; |
7e6c05dc AD |
186 | } |
187 | } | |
188 | ||
1597f1f9 WL |
189 | sub volume_rollback_is_possible { |
190 | my ($cfg, $volid, $snap) = @_; | |
e0852ba7 | 191 | |
1597f1f9 WL |
192 | my ($storeid, $volname) = parse_volume_id($volid, 1); |
193 | if ($storeid) { | |
194 | my $scfg = storage_config($cfg, $storeid); | |
195 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
196 | return $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap); | |
197 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
f824c722 | 198 | die "snapshot rollback file/device '$volid' is not possible\n"; |
1597f1f9 | 199 | } else { |
f824c722 | 200 | die "unable to parse volume ID '$volid'\n"; |
1597f1f9 WL |
201 | } |
202 | } | |
203 | ||
db60719c | 204 | sub volume_snapshot { |
f5640e7d | 205 | my ($cfg, $volid, $snap) = @_; |
db60719c AD |
206 | |
207 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
208 | if ($storeid) { | |
209 | my $scfg = storage_config($cfg, $storeid); | |
210 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
f5640e7d | 211 | return $plugin->volume_snapshot($scfg, $storeid, $volname, $snap); |
db60719c | 212 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { |
f824c722 | 213 | die "snapshot file/device '$volid' is not possible\n"; |
db60719c | 214 | } else { |
f824c722 | 215 | die "unable to parse volume ID '$volid'\n"; |
db60719c AD |
216 | } |
217 | } | |
218 | ||
b76774e5 WL |
219 | sub volume_send { |
220 | my ($cfg, $volid, $snap, $ip, $incremental_snap, $verbose, $limit, | |
221 | $target_path) = @_; | |
222 | ||
223 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
224 | if ($storeid) { | |
225 | my $scfg = storage_config($cfg, $storeid); | |
226 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
227 | return $plugin->volume_send($scfg, $storeid, $volname, $ip, $snap, | |
228 | $incremental_snap, $verbose, $limit, $target_path); | |
229 | ||
230 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
231 | die "send file/device '$volid' is not possible\n"; | |
232 | } else { | |
233 | die "unable to parse volume ID '$volid'\n"; | |
234 | } | |
235 | } | |
236 | ||
22a2a633 AD |
237 | sub volume_snapshot_rollback { |
238 | my ($cfg, $volid, $snap) = @_; | |
239 | ||
240 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
241 | if ($storeid) { | |
242 | my $scfg = storage_config($cfg, $storeid); | |
243 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b3f302c6 | 244 | $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap); |
22a2a633 AD |
245 | return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap); |
246 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
f824c722 | 247 | die "snapshot rollback file/device '$volid' is not possible\n"; |
22a2a633 | 248 | } else { |
f824c722 | 249 | die "unable to parse volume ID '$volid'\n"; |
22a2a633 AD |
250 | } |
251 | } | |
252 | ||
4bd0b38f WL |
253 | sub volume_snapshot_delete_remote { |
254 | my ($cfg, $volid, $snap, $ip) = @_; | |
255 | ||
256 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
257 | if ($storeid) { | |
258 | my $scfg = storage_config($cfg, $storeid); | |
259 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
260 | return $plugin->volume_snapshot_delete_remote($scfg, $storeid, $volname, $snap, $ip); | |
261 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
262 | die "snapshot delete file/device '$volid' is not possible\n"; | |
263 | } else { | |
264 | die "unable to parse volume ID '$volid'\n"; | |
265 | } | |
266 | } | |
267 | ||
5753c9d1 AD |
268 | sub volume_snapshot_delete { |
269 | my ($cfg, $volid, $snap, $running) = @_; | |
270 | ||
271 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
272 | if ($storeid) { | |
273 | my $scfg = storage_config($cfg, $storeid); | |
274 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
27cc55d4 | 275 | return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running); |
5753c9d1 | 276 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { |
f824c722 | 277 | die "snapshot delete file/device '$volid' is not possible\n"; |
5753c9d1 | 278 | } else { |
f824c722 | 279 | die "unable to parse volume ID '$volid'\n"; |
5753c9d1 AD |
280 | } |
281 | } | |
282 | ||
99473759 AD |
283 | sub volume_has_feature { |
284 | my ($cfg, $feature, $volid, $snap, $running) = @_; | |
285 | ||
286 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
287 | if ($storeid) { | |
288 | my $scfg = storage_config($cfg, $storeid); | |
289 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
290 | return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running); | |
291 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
292 | return undef; | |
293 | } else { | |
294 | return undef; | |
295 | } | |
296 | } | |
297 | ||
aefe82ea WL |
298 | sub volume_snapshot_list { |
299 | my ($cfg, $volid, $prefix, $ip) = @_; | |
300 | ||
301 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
302 | if ($storeid) { | |
303 | my $scfg = storage_config($cfg, $storeid); | |
304 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
305 | return $plugin->volume_snapshot_list($scfg, $storeid, $volname, $prefix, $ip); | |
306 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
307 | die "send file/device '$volid' is not possible\n"; | |
308 | } else { | |
309 | die "unable to parse volume ID '$volid'\n"; | |
310 | } | |
311 | # return an empty array if dataset does not exist. | |
312 | # youngest snap first | |
313 | } | |
314 | ||
1dc01b9f DM |
315 | sub get_image_dir { |
316 | my ($cfg, $storeid, $vmid) = @_; | |
b6cf0a66 | 317 | |
1dc01b9f DM |
318 | my $scfg = storage_config($cfg, $storeid); |
319 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b6cf0a66 | 320 | |
1dc01b9f | 321 | my $path = $plugin->get_subdir($scfg, 'images'); |
b6cf0a66 | 322 | |
1dc01b9f | 323 | return $vmid ? "$path/$vmid" : $path; |
b6cf0a66 DM |
324 | } |
325 | ||
1dc01b9f | 326 | sub get_private_dir { |
b6cf0a66 DM |
327 | my ($cfg, $storeid, $vmid) = @_; |
328 | ||
1dc01b9f DM |
329 | my $scfg = storage_config($cfg, $storeid); |
330 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b6cf0a66 | 331 | |
1dc01b9f | 332 | my $path = $plugin->get_subdir($scfg, 'rootdir'); |
d22a6133 | 333 | |
1dc01b9f | 334 | return $vmid ? "$path/$vmid" : $path; |
d22a6133 DM |
335 | } |
336 | ||
b6cf0a66 DM |
337 | sub get_iso_dir { |
338 | my ($cfg, $storeid) = @_; | |
339 | ||
1dc01b9f DM |
340 | my $scfg = storage_config($cfg, $storeid); |
341 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b6cf0a66 | 342 | |
1dc01b9f | 343 | return $plugin->get_subdir($scfg, 'iso'); |
b6cf0a66 DM |
344 | } |
345 | ||
346 | sub get_vztmpl_dir { | |
347 | my ($cfg, $storeid) = @_; | |
348 | ||
1dc01b9f DM |
349 | my $scfg = storage_config($cfg, $storeid); |
350 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b6cf0a66 | 351 | |
1dc01b9f | 352 | return $plugin->get_subdir($scfg, 'vztmpl'); |
b6cf0a66 DM |
353 | } |
354 | ||
568de3d1 DM |
355 | sub get_backup_dir { |
356 | my ($cfg, $storeid) = @_; | |
357 | ||
1dc01b9f DM |
358 | my $scfg = storage_config($cfg, $storeid); |
359 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b6cf0a66 | 360 | |
1dc01b9f | 361 | return $plugin->get_subdir($scfg, 'backup'); |
b6cf0a66 DM |
362 | } |
363 | ||
364 | # library implementation | |
365 | ||
b6cf0a66 DM |
366 | sub parse_vmid { |
367 | my $vmid = shift; | |
368 | ||
369 | die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/; | |
370 | ||
371 | return int($vmid); | |
372 | } | |
373 | ||
787624df FG |
374 | # NOTE: basename and basevmid are always undef for LVM-thin, where the |
375 | # clone -> base reference is not encoded in the volume ID. | |
376 | # see note in PVE::Storage::LvmThinPlugin for details. | |
ec4b0dc7 AD |
377 | sub parse_volname { |
378 | my ($cfg, $volid) = @_; | |
379 | ||
380 | my ($storeid, $volname) = parse_volume_id($volid); | |
381 | ||
382 | my $scfg = storage_config($cfg, $storeid); | |
383 | ||
384 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
a6f12626 DM |
385 | |
386 | # returns ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) | |
387 | ||
ec4b0dc7 AD |
388 | return $plugin->parse_volname($volname); |
389 | } | |
390 | ||
b6cf0a66 DM |
391 | sub parse_volume_id { |
392 | my ($volid, $noerr) = @_; | |
393 | ||
a7f3d909 | 394 | return PVE::Storage::Plugin::parse_volume_id($volid, $noerr); |
b6cf0a66 DM |
395 | } |
396 | ||
04a13668 DM |
397 | # test if we have read access to volid |
398 | sub check_volume_access { | |
399 | my ($rpcenv, $user, $cfg, $vmid, $volid) = @_; | |
400 | ||
401 | my ($sid, $volname) = parse_volume_id($volid, 1); | |
402 | if ($sid) { | |
403 | my ($vtype, undef, $ownervm) = parse_volname($cfg, $volid); | |
404 | if ($vtype eq 'iso' || $vtype eq 'vztmpl') { | |
405 | # we simply allow access | |
406 | } elsif (defined($ownervm) && defined($vmid) && ($ownervm == $vmid)) { | |
407 | # we are owner - allow access | |
408 | } elsif ($vtype eq 'backup' && $ownervm) { | |
409 | $rpcenv->check($user, "/storage/$sid", ['Datastore.AllocateSpace']); | |
410 | $rpcenv->check($user, "/vms/$ownervm", ['VM.Backup']); | |
411 | } else { | |
412 | # allow if we are Datastore administrator | |
413 | $rpcenv->check($user, "/storage/$sid", ['Datastore.Allocate']); | |
414 | } | |
415 | } else { | |
416 | die "Only root can pass arbitrary filesystem paths." | |
417 | if $user ne 'root@pam'; | |
418 | } | |
419 | ||
420 | return undef; | |
421 | } | |
422 | ||
17fb7e42 FG |
423 | my $volume_is_base_and_used__no_lock = sub { |
424 | my ($scfg, $storeid, $plugin, $volname) = @_; | |
425 | ||
426 | my ($vtype, $name, $vmid, undef, undef, $isBase, undef) = | |
427 | $plugin->parse_volname($volname); | |
428 | ||
429 | if ($isBase) { | |
430 | my $vollist = $plugin->list_images($storeid, $scfg); | |
431 | foreach my $info (@$vollist) { | |
432 | my (undef, $tmpvolname) = parse_volume_id($info->{volid}); | |
433 | my $basename = undef; | |
434 | my $basevmid = undef; | |
435 | ||
436 | eval{ | |
437 | (undef, undef, undef, $basename, $basevmid) = | |
438 | $plugin->parse_volname($tmpvolname); | |
439 | }; | |
440 | ||
441 | if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) { | |
442 | return 1; | |
443 | } | |
444 | } | |
445 | } | |
446 | return 0; | |
447 | }; | |
448 | ||
787624df FG |
449 | # NOTE: this check does not work for LVM-thin, where the clone -> base |
450 | # reference is not encoded in the volume ID. | |
451 | # see note in PVE::Storage::LvmThinPlugin for details. | |
17fb7e42 FG |
452 | sub volume_is_base_and_used { |
453 | my ($cfg, $volid) = @_; | |
454 | ||
455 | my ($storeid, $volname) = parse_volume_id($volid); | |
456 | my $scfg = storage_config($cfg, $storeid); | |
457 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
458 | ||
459 | $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
460 | return &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname); | |
461 | }); | |
462 | } | |
463 | ||
b6cf0a66 DM |
464 | # try to map a filesystem path to a volume identifier |
465 | sub path_to_volume_id { | |
466 | my ($cfg, $path) = @_; | |
467 | ||
468 | my $ids = $cfg->{ids}; | |
469 | ||
1dc01b9f | 470 | my ($sid, $volname) = parse_volume_id($path, 1); |
b6cf0a66 | 471 | if ($sid) { |
1dc01b9f | 472 | if (my $scfg = $ids->{$sid}) { |
188aca38 | 473 | if ($scfg->{path}) { |
1dc01b9f DM |
474 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
475 | my ($vtype, $name, $vmid) = $plugin->parse_volname($volname); | |
b6cf0a66 DM |
476 | return ($vtype, $path); |
477 | } | |
478 | } | |
479 | return (''); | |
480 | } | |
481 | ||
1a3459ac | 482 | # Note: abs_path() return undef if $path doesn not exist |
75d75990 DM |
483 | # for example when nfs storage is not mounted |
484 | $path = abs_path($path) || $path; | |
b6cf0a66 DM |
485 | |
486 | foreach my $sid (keys %$ids) { | |
1dc01b9f DM |
487 | my $scfg = $ids->{$sid}; |
488 | next if !$scfg->{path}; | |
489 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
490 | my $imagedir = $plugin->get_subdir($scfg, 'images'); | |
491 | my $isodir = $plugin->get_subdir($scfg, 'iso'); | |
492 | my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl'); | |
493 | my $backupdir = $plugin->get_subdir($scfg, 'backup'); | |
494 | my $privatedir = $plugin->get_subdir($scfg, 'rootdir'); | |
b6cf0a66 DM |
495 | |
496 | if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) { | |
497 | my $vmid = $1; | |
498 | my $name = $2; | |
fcbec654 DM |
499 | |
500 | my $vollist = $plugin->list_images($sid, $scfg, $vmid); | |
501 | foreach my $info (@$vollist) { | |
502 | my ($storeid, $volname) = parse_volume_id($info->{volid}); | |
503 | my $volpath = $plugin->path($scfg, $volname, $storeid); | |
504 | if ($volpath eq $path) { | |
505 | return ('images', $info->{volid}); | |
506 | } | |
507 | } | |
b6cf0a66 DM |
508 | } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) { |
509 | my $name = $1; | |
1a3459ac | 510 | return ('iso', "$sid:iso/$name"); |
b6cf0a66 DM |
511 | } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) { |
512 | my $name = $1; | |
513 | return ('vztmpl', "$sid:vztmpl/$name"); | |
1ac17c74 DM |
514 | } elsif ($path =~ m!^$privatedir/(\d+)$!) { |
515 | my $vmid = $1; | |
516 | return ('rootdir', "$sid:rootdir/$vmid"); | |
a22854e5 | 517 | } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!) { |
568de3d1 | 518 | my $name = $1; |
1a3459ac | 519 | return ('iso', "$sid:backup/$name"); |
b6cf0a66 DM |
520 | } |
521 | } | |
522 | ||
523 | # can't map path to volume id | |
524 | return (''); | |
525 | } | |
526 | ||
527 | sub path { | |
207ea852 | 528 | my ($cfg, $volid, $snapname) = @_; |
b6cf0a66 | 529 | |
1dc01b9f | 530 | my ($storeid, $volname) = parse_volume_id($volid); |
b6cf0a66 | 531 | |
1dc01b9f | 532 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 | 533 | |
1dc01b9f | 534 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
207ea852 | 535 | my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname); |
2494896a | 536 | return wantarray ? ($path, $owner, $vtype) : $path; |
b6cf0a66 DM |
537 | } |
538 | ||
35fbb2e6 DM |
539 | sub abs_filesystem_path { |
540 | my ($cfg, $volid) = @_; | |
541 | ||
542 | my $path; | |
543 | if (PVE::Storage::parse_volume_id ($volid, 1)) { | |
544 | PVE::Storage::activate_volumes($cfg, [ $volid ]); | |
545 | $path = PVE::Storage::path($cfg, $volid); | |
546 | } else { | |
547 | if (-f $volid) { | |
548 | my $abspath = abs_path($volid); | |
549 | if ($abspath && $abspath =~ m|^(/.+)$|) { | |
550 | $path = $1; # untaint any path | |
551 | } | |
552 | } | |
553 | } | |
554 | ||
555 | die "can't find file '$volid'\n" if !($path && -f $path); | |
556 | ||
557 | return $path; | |
558 | } | |
559 | ||
b6cf0a66 DM |
560 | sub storage_migrate { |
561 | my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_; | |
562 | ||
6bf56298 | 563 | my ($storeid, $volname) = parse_volume_id($volid); |
b6cf0a66 DM |
564 | $target_volname = $volname if !$target_volname; |
565 | ||
6bf56298 | 566 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
567 | |
568 | # no need to migrate shared content | |
569 | return if $storeid eq $target_storeid && $scfg->{shared}; | |
570 | ||
6bf56298 | 571 | my $tcfg = storage_config($cfg, $target_storeid); |
b6cf0a66 DM |
572 | |
573 | my $target_volid = "${target_storeid}:${target_volname}"; | |
574 | ||
575 | my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_host'"; | |
576 | ||
45c2ee35 | 577 | my $sshoptions = "-o 'BatchMode=yes'"; |
b6cf0a66 DM |
578 | my $ssh = "/usr/bin/ssh $sshoptions"; |
579 | ||
580 | local $ENV{RSYNC_RSH} = $ssh; | |
581 | ||
1dc01b9f DM |
582 | # only implemented for file system based storage |
583 | if ($scfg->{path}) { | |
584 | if ($tcfg->{path}) { | |
b6cf0a66 | 585 | |
1dc01b9f DM |
586 | my $src_plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
587 | my $dst_plugin = PVE::Storage::Plugin->lookup($tcfg->{type}); | |
6bf56298 DM |
588 | my $src = $src_plugin->path($scfg, $volname, $storeid); |
589 | my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid); | |
b6cf0a66 | 590 | |
1dc01b9f | 591 | my $dirname = dirname($dst); |
b6cf0a66 DM |
592 | |
593 | if ($tcfg->{shared}) { # we can do a local copy | |
1a3459ac | 594 | |
f81372ac | 595 | run_command(['/bin/mkdir', '-p', $dirname]); |
b6cf0a66 | 596 | |
1dc01b9f | 597 | run_command(['/bin/cp', $src, $dst]); |
b6cf0a66 | 598 | |
1dc01b9f | 599 | } else { |
1a3459ac | 600 | run_command(['/usr/bin/ssh', "root\@${target_host}", |
1dc01b9f | 601 | '/bin/mkdir', '-p', $dirname]); |
b6cf0a66 | 602 | |
1dc01b9f DM |
603 | # we use rsync with --sparse, so we can't use --inplace, |
604 | # so we remove file on the target if it already exists to | |
605 | # save space | |
606 | my ($size, $format) = PVE::Storage::Plugin::file_size_info($src); | |
607 | if ($format && ($format eq 'raw') && $size) { | |
1a3459ac | 608 | run_command(['/usr/bin/ssh', "root\@${target_host}", |
1dc01b9f DM |
609 | 'rm', '-f', $dst], |
610 | outfunc => sub {}); | |
611 | } | |
b6cf0a66 | 612 | |
986303ca WL |
613 | my $cmd; |
614 | if ($format eq 'subvol') { | |
615 | $cmd = ['/usr/bin/rsync', '--progress', '-X', '-A', '--numeric-ids', | |
616 | '-aH', '--delete', '--no-whole-file', '--inplace', | |
617 | '--one-file-system', "$src/", "[root\@${target_host}]:$dst"]; | |
618 | } else { | |
619 | $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file', | |
620 | $src, "[root\@${target_host}]:$dst"]; | |
621 | } | |
b6cf0a66 | 622 | |
1dc01b9f | 623 | my $percent = -1; |
b6cf0a66 | 624 | |
1dc01b9f DM |
625 | run_command($cmd, outfunc => sub { |
626 | my $line = shift; | |
b6cf0a66 | 627 | |
1dc01b9f DM |
628 | if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) { |
629 | if ($2 > $percent) { | |
630 | $percent = $2; | |
631 | print "rsync status: $1\n"; | |
632 | *STDOUT->flush(); | |
633 | } | |
634 | } else { | |
635 | print "$line\n"; | |
636 | *STDOUT->flush(); | |
637 | } | |
638 | }); | |
639 | } | |
640 | } else { | |
641 | die "$errstr - target type '$tcfg->{type}' not implemented\n"; | |
642 | } | |
e0852ba7 | 643 | |
3d621977 WL |
644 | } elsif ($scfg->{type} eq 'zfspool') { |
645 | ||
e0852ba7 | 646 | if ($tcfg->{type} eq 'zfspool') { |
3d621977 | 647 | |
82fc923f | 648 | die "$errstr - pool on target does not have the same name as on source!" |
e0852ba7 | 649 | if $tcfg->{pool} ne $scfg->{pool}; |
3d621977 | 650 | |
e0852ba7 | 651 | my (undef, $volname) = parse_volname($cfg, $volid); |
3d621977 WL |
652 | |
653 | my $zfspath = "$scfg->{pool}\/$volname"; | |
654 | ||
b650f029 | 655 | my $snap = ['zfs', 'snapshot', "$zfspath\@__migration__"]; |
3d621977 | 656 | |
b650f029 WL |
657 | my $send = [['zfs', 'send', '-Rpv', "$zfspath\@__migration__"], ['ssh', "root\@$target_host", |
658 | 'zfs', 'recv', $zfspath]]; | |
3d621977 | 659 | |
b650f029 | 660 | my $destroy_target = ['ssh', "root\@$target_host", 'zfs', 'destroy', "$zfspath\@__migration__"]; |
3d621977 | 661 | run_command($snap); |
e0852ba7 | 662 | eval{ |
3d621977 WL |
663 | run_command($send); |
664 | }; | |
afd0afe4 FG |
665 | my $err = $@; |
666 | warn "zfs send/receive failed, cleaning up snapshot(s)..\n" if $err; | |
667 | eval { run_command(['zfs', 'destroy', "$zfspath\@__migration__"]); }; | |
668 | warn "could not remove source snapshot: $@\n" if $@; | |
669 | eval { run_command($destroy_target); }; | |
670 | warn "could not remove target snapshot: $@\n" if $@; | |
671 | die $err if $err; | |
3d621977 WL |
672 | |
673 | } else { | |
674 | die "$errstr - target type $tcfg->{type} is not valid\n"; | |
675 | } | |
0a29ad61 WL |
676 | |
677 | } elsif ($scfg->{type} eq 'lvmthin' || $scfg->{type} eq 'lvm') { | |
678 | ||
76f13c9e DM |
679 | if (($scfg->{type} eq $tcfg->{type}) && |
680 | ($tcfg->{type} eq 'lvmthin' || $tcfg->{type} eq 'lvm')) { | |
0a29ad61 | 681 | |
84252f67 | 682 | my (undef, $volname, $vmid) = parse_volname($cfg, $volid); |
0a29ad61 | 683 | my $size = volume_size_info($cfg, $volid, 5); |
84252f67 DM |
684 | my $src = path($cfg, $volid); |
685 | my $dst = path($cfg, $target_volid); | |
0a29ad61 WL |
686 | |
687 | run_command(['/usr/bin/ssh', "root\@${target_host}", | |
688 | 'pvesm', 'alloc', $target_storeid, $vmid, | |
84252f67 | 689 | $target_volname, int($size/1024)]); |
0a29ad61 WL |
690 | |
691 | eval { | |
e83f7b40 | 692 | if ($tcfg->{type} eq 'lvmthin') { |
46f114a5 DM |
693 | run_command([["dd", "if=$src", "bs=4k"],["/usr/bin/ssh", "root\@${target_host}", |
694 | "dd", 'conv=sparse', "of=$dst", "bs=4k"]]); | |
e83f7b40 | 695 | } else { |
46f114a5 DM |
696 | run_command([["dd", "if=$src", "bs=4k"],["/usr/bin/ssh", "root\@${target_host}", |
697 | "dd", "of=$dst", "bs=4k"]]); | |
e83f7b40 | 698 | } |
0a29ad61 WL |
699 | }; |
700 | if (my $err = $@) { | |
701 | run_command(['/usr/bin/ssh', "root\@${target_host}", | |
84252f67 | 702 | 'pvesm', 'free', $target_volid]); |
966ecef2 | 703 | die $err; |
0a29ad61 WL |
704 | } |
705 | } else { | |
76f13c9e | 706 | die "$errstr - migrate from source type '$scfg->{type}' to '$tcfg->{type}' not implemented\n"; |
0a29ad61 | 707 | } |
1dc01b9f DM |
708 | } else { |
709 | die "$errstr - source type '$scfg->{type}' not implemented\n"; | |
b6cf0a66 DM |
710 | } |
711 | } | |
712 | ||
2502b33b | 713 | sub vdisk_clone { |
7bbc4004 | 714 | my ($cfg, $volid, $vmid, $snap) = @_; |
1a3459ac | 715 | |
2502b33b DM |
716 | my ($storeid, $volname) = parse_volume_id($volid); |
717 | ||
718 | my $scfg = storage_config($cfg, $storeid); | |
719 | ||
720 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1a3459ac | 721 | |
2502b33b DM |
722 | activate_storage($cfg, $storeid); |
723 | ||
724 | # lock shared storage | |
725 | return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
7bbc4004 | 726 | my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap); |
2502b33b DM |
727 | return "$storeid:$volname"; |
728 | }); | |
729 | } | |
730 | ||
731 | sub vdisk_create_base { | |
732 | my ($cfg, $volid) = @_; | |
733 | ||
734 | my ($storeid, $volname) = parse_volume_id($volid); | |
735 | ||
736 | my $scfg = storage_config($cfg, $storeid); | |
737 | ||
738 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1a3459ac | 739 | |
2502b33b DM |
740 | activate_storage($cfg, $storeid); |
741 | ||
742 | # lock shared storage | |
743 | return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
744 | my $volname = $plugin->create_base($storeid, $scfg, $volname); | |
745 | return "$storeid:$volname"; | |
746 | }); | |
747 | } | |
748 | ||
1dc01b9f DM |
749 | sub vdisk_alloc { |
750 | my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_; | |
b6cf0a66 | 751 | |
82fc923f | 752 | die "no storage ID specified\n" if !$storeid; |
b6cf0a66 | 753 | |
1dc01b9f | 754 | PVE::JSONSchema::parse_storage_id($storeid); |
b6cf0a66 | 755 | |
1dc01b9f | 756 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 | 757 | |
1dc01b9f | 758 | die "no VMID specified\n" if !$vmid; |
b6cf0a66 | 759 | |
1dc01b9f | 760 | $vmid = parse_vmid($vmid); |
b6cf0a66 | 761 | |
1dc01b9f | 762 | my $defformat = PVE::Storage::Plugin::default_format($scfg); |
b6cf0a66 | 763 | |
1dc01b9f | 764 | $fmt = $defformat if !$fmt; |
b6cf0a66 | 765 | |
1dc01b9f | 766 | activate_storage($cfg, $storeid); |
3af60e62 | 767 | |
1dc01b9f | 768 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
b6cf0a66 | 769 | |
1dc01b9f DM |
770 | # lock shared storage |
771 | return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
afdfbe55 WB |
772 | my $old_umask = umask(umask|0037); |
773 | my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) }; | |
774 | my $err = $@; | |
775 | umask $old_umask; | |
776 | die $err if $err; | |
1dc01b9f DM |
777 | return "$storeid:$volname"; |
778 | }); | |
b6cf0a66 DM |
779 | } |
780 | ||
1dc01b9f DM |
781 | sub vdisk_free { |
782 | my ($cfg, $volid) = @_; | |
b6cf0a66 | 783 | |
1dc01b9f | 784 | my ($storeid, $volname) = parse_volume_id($volid); |
1dc01b9f | 785 | my $scfg = storage_config($cfg, $storeid); |
1dc01b9f | 786 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
1a3459ac | 787 | |
1dc01b9f | 788 | activate_storage($cfg, $storeid); |
b6cf0a66 | 789 | |
1dc01b9f | 790 | my $cleanup_worker; |
b6cf0a66 | 791 | |
1dc01b9f DM |
792 | # lock shared storage |
793 | $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
787624df | 794 | # LVM-thin allows deletion of still referenced base volumes! |
17fb7e42 FG |
795 | die "base volume '$volname' is still in use by linked clones\n" |
796 | if &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname); | |
32437ed2 | 797 | |
17fb7e42 | 798 | my (undef, undef, undef, undef, undef, $isBase, $format) = |
32437ed2 | 799 | $plugin->parse_volname($volname); |
35533c68 | 800 | $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format); |
1dc01b9f | 801 | }); |
b6cf0a66 | 802 | |
1dc01b9f | 803 | return if !$cleanup_worker; |
b6cf0a66 | 804 | |
1dc01b9f DM |
805 | my $rpcenv = PVE::RPCEnvironment::get(); |
806 | my $authuser = $rpcenv->get_user(); | |
b6cf0a66 | 807 | |
1dc01b9f | 808 | $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker); |
b6cf0a66 DM |
809 | } |
810 | ||
b6cf0a66 DM |
811 | #list iso or openvz template ($tt = <iso|vztmpl|backup>) |
812 | sub template_list { | |
813 | my ($cfg, $storeid, $tt) = @_; | |
814 | ||
1a3459ac DM |
815 | die "unknown template type '$tt'\n" |
816 | if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup'); | |
b6cf0a66 DM |
817 | |
818 | my $ids = $cfg->{ids}; | |
819 | ||
820 | storage_check_enabled($cfg, $storeid) if ($storeid); | |
821 | ||
822 | my $res = {}; | |
823 | ||
824 | # query the storage | |
825 | ||
826 | foreach my $sid (keys %$ids) { | |
827 | next if $storeid && $storeid ne $sid; | |
828 | ||
829 | my $scfg = $ids->{$sid}; | |
830 | my $type = $scfg->{type}; | |
831 | ||
832 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
833 | ||
834 | next if $tt eq 'iso' && !$scfg->{content}->{iso}; | |
835 | next if $tt eq 'vztmpl' && !$scfg->{content}->{vztmpl}; | |
836 | next if $tt eq 'backup' && !$scfg->{content}->{backup}; | |
837 | ||
1dc01b9f | 838 | activate_storage($cfg, $sid); |
b6cf0a66 | 839 | |
1dc01b9f DM |
840 | if ($scfg->{path}) { |
841 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b6cf0a66 | 842 | |
1dc01b9f | 843 | my $path = $plugin->get_subdir($scfg, $tt); |
b6cf0a66 DM |
844 | |
845 | foreach my $fn (<$path/*>) { | |
846 | ||
847 | my $info; | |
848 | ||
849 | if ($tt eq 'iso') { | |
850 | next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!; | |
851 | ||
852 | $info = { volid => "$sid:iso/$1", format => 'iso' }; | |
853 | ||
854 | } elsif ($tt eq 'vztmpl') { | |
13d2cb79 | 855 | next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!; |
b6cf0a66 | 856 | |
13d2cb79 | 857 | $info = { volid => "$sid:vztmpl/$1", format => "t$2" }; |
b6cf0a66 DM |
858 | |
859 | } elsif ($tt eq 'backup') { | |
a22854e5 | 860 | next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!; |
1a3459ac | 861 | |
b6cf0a66 DM |
862 | $info = { volid => "$sid:backup/$1", format => $2 }; |
863 | } | |
864 | ||
865 | $info->{size} = -s $fn; | |
866 | ||
867 | push @{$res->{$sid}}, $info; | |
868 | } | |
869 | ||
870 | } | |
871 | ||
872 | @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid}; | |
873 | } | |
874 | ||
875 | return $res; | |
876 | } | |
877 | ||
20ccac1b | 878 | |
b6cf0a66 DM |
879 | sub vdisk_list { |
880 | my ($cfg, $storeid, $vmid, $vollist) = @_; | |
881 | ||
882 | my $ids = $cfg->{ids}; | |
883 | ||
884 | storage_check_enabled($cfg, $storeid) if ($storeid); | |
885 | ||
886 | my $res = {}; | |
887 | ||
888 | # prepare/activate/refresh all storages | |
889 | ||
b6cf0a66 DM |
890 | my $storage_list = []; |
891 | if ($vollist) { | |
892 | foreach my $volid (@$vollist) { | |
1dc01b9f DM |
893 | my ($sid, undef) = parse_volume_id($volid); |
894 | next if !defined($ids->{$sid}); | |
b6cf0a66 DM |
895 | next if !storage_check_enabled($cfg, $sid, undef, 1); |
896 | push @$storage_list, $sid; | |
b6cf0a66 DM |
897 | } |
898 | } else { | |
899 | foreach my $sid (keys %$ids) { | |
900 | next if $storeid && $storeid ne $sid; | |
901 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
902 | push @$storage_list, $sid; | |
b6cf0a66 DM |
903 | } |
904 | } | |
905 | ||
1dc01b9f | 906 | my $cache = {}; |
b6cf0a66 | 907 | |
1dc01b9f | 908 | activate_storage_list($cfg, $storage_list, $cache); |
b6cf0a66 DM |
909 | |
910 | foreach my $sid (keys %$ids) { | |
1dc01b9f DM |
911 | next if $storeid && $storeid ne $sid; |
912 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
b6cf0a66 | 913 | |
1dc01b9f DM |
914 | my $scfg = $ids->{$sid}; |
915 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
916 | $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache); | |
b6cf0a66 DM |
917 | @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid}; |
918 | } | |
919 | ||
920 | return $res; | |
921 | } | |
922 | ||
37ba0aea DM |
923 | sub volume_list { |
924 | my ($cfg, $storeid, $vmid, $content) = @_; | |
925 | ||
926 | my @ctypes = qw(images vztmpl iso backup); | |
927 | ||
928 | my $cts = $content ? [ $content ] : [ @ctypes ]; | |
929 | ||
930 | my $scfg = PVE::Storage::storage_config($cfg, $storeid); | |
931 | ||
932 | my $res = []; | |
933 | foreach my $ct (@$cts) { | |
934 | my $data; | |
935 | if ($ct eq 'images') { | |
936 | $data = vdisk_list($cfg, $storeid, $vmid); | |
937 | } elsif ($ct eq 'iso' && !defined($vmid)) { | |
938 | $data = template_list($cfg, $storeid, 'iso'); | |
939 | } elsif ($ct eq 'vztmpl'&& !defined($vmid)) { | |
940 | $data = template_list ($cfg, $storeid, 'vztmpl'); | |
941 | } elsif ($ct eq 'backup') { | |
942 | $data = template_list ($cfg, $storeid, 'backup'); | |
943 | foreach my $item (@{$data->{$storeid}}) { | |
944 | if (defined($vmid)) { | |
945 | @{$data->{$storeid}} = grep { $_->{volid} =~ m/\S+-$vmid-\S+/ } @{$data->{$storeid}}; | |
946 | } | |
947 | } | |
948 | } | |
949 | ||
950 | next if !$data || !$data->{$storeid}; | |
951 | ||
952 | foreach my $item (@{$data->{$storeid}}) { | |
953 | $item->{content} = $ct; | |
954 | push @$res, $item; | |
955 | } | |
956 | } | |
957 | ||
958 | return $res; | |
959 | } | |
960 | ||
b6cf0a66 DM |
961 | sub uevent_seqnum { |
962 | ||
963 | my $filename = "/sys/kernel/uevent_seqnum"; | |
964 | ||
965 | my $seqnum = 0; | |
1dc01b9f | 966 | if (my $fh = IO::File->new($filename, "r")) { |
b6cf0a66 DM |
967 | my $line = <$fh>; |
968 | if ($line =~ m/^(\d+)$/) { | |
1dc01b9f | 969 | $seqnum = int($1); |
b6cf0a66 DM |
970 | } |
971 | close ($fh); | |
972 | } | |
973 | return $seqnum; | |
974 | } | |
975 | ||
f3d4ef46 | 976 | sub activate_storage { |
1dc01b9f | 977 | my ($cfg, $storeid, $cache) = @_; |
b6cf0a66 | 978 | |
f3d4ef46 DM |
979 | $cache = {} if !$cache; |
980 | ||
b6cf0a66 DM |
981 | my $scfg = storage_check_enabled($cfg, $storeid); |
982 | ||
1dc01b9f | 983 | return if $cache->{activated}->{$storeid}; |
b6cf0a66 | 984 | |
1dc01b9f | 985 | $cache->{uevent_seqnum} = uevent_seqnum() if !$cache->{uevent_seqnum}; |
b6cf0a66 | 986 | |
1dc01b9f | 987 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
b6cf0a66 | 988 | |
1dc01b9f DM |
989 | if ($scfg->{base}) { |
990 | my ($baseid, undef) = parse_volume_id ($scfg->{base}); | |
f3d4ef46 DM |
991 | activate_storage($cfg, $baseid, $cache); |
992 | } | |
993 | ||
994 | if (!$plugin->check_connection($storeid, $scfg)) { | |
995 | die "storage '$storeid' is not online\n"; | |
b6cf0a66 DM |
996 | } |
997 | ||
1dc01b9f DM |
998 | $plugin->activate_storage($storeid, $scfg, $cache); |
999 | ||
b6cf0a66 DM |
1000 | my $newseq = uevent_seqnum (); |
1001 | ||
1002 | # only call udevsettle if there are events | |
1dc01b9f | 1003 | if ($newseq > $cache->{uevent_seqnum}) { |
b6cf0a66 DM |
1004 | my $timeout = 30; |
1005 | system ("$UDEVADM settle --timeout=$timeout"); # ignore errors | |
1dc01b9f | 1006 | $cache->{uevent_seqnum} = $newseq; |
b6cf0a66 DM |
1007 | } |
1008 | ||
1dc01b9f | 1009 | $cache->{activated}->{$storeid} = 1; |
b6cf0a66 DM |
1010 | } |
1011 | ||
1012 | sub activate_storage_list { | |
1dc01b9f | 1013 | my ($cfg, $storeid_list, $cache) = @_; |
b6cf0a66 | 1014 | |
1dc01b9f | 1015 | $cache = {} if !$cache; |
b6cf0a66 DM |
1016 | |
1017 | foreach my $storeid (@$storeid_list) { | |
f3d4ef46 | 1018 | activate_storage($cfg, $storeid, $cache); |
b6cf0a66 DM |
1019 | } |
1020 | } | |
1021 | ||
1dc01b9f DM |
1022 | sub deactivate_storage { |
1023 | my ($cfg, $storeid) = @_; | |
1024 | ||
1025 | my $scfg = storage_config ($cfg, $storeid); | |
1026 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b6cf0a66 | 1027 | |
1dc01b9f DM |
1028 | my $cache = {}; |
1029 | $plugin->deactivate_storage($storeid, $scfg, $cache); | |
b6cf0a66 DM |
1030 | } |
1031 | ||
1032 | sub activate_volumes { | |
02e797b8 | 1033 | my ($cfg, $vollist, $snapname) = @_; |
6703353b DM |
1034 | |
1035 | return if !($vollist && scalar(@$vollist)); | |
1036 | ||
b6cf0a66 DM |
1037 | my $storagehash = {}; |
1038 | foreach my $volid (@$vollist) { | |
1dc01b9f | 1039 | my ($storeid, undef) = parse_volume_id($volid); |
b6cf0a66 DM |
1040 | $storagehash->{$storeid} = 1; |
1041 | } | |
1042 | ||
1dc01b9f DM |
1043 | my $cache = {}; |
1044 | ||
1045 | activate_storage_list($cfg, [keys %$storagehash], $cache); | |
b6cf0a66 DM |
1046 | |
1047 | foreach my $volid (@$vollist) { | |
5521b580 | 1048 | my ($storeid, $volname) = parse_volume_id($volid); |
1dc01b9f DM |
1049 | my $scfg = storage_config($cfg, $storeid); |
1050 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
02e797b8 | 1051 | $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache); |
b6cf0a66 DM |
1052 | } |
1053 | } | |
1054 | ||
1055 | sub deactivate_volumes { | |
02e797b8 | 1056 | my ($cfg, $vollist, $snapname) = @_; |
b6cf0a66 | 1057 | |
6703353b DM |
1058 | return if !($vollist && scalar(@$vollist)); |
1059 | ||
1dc01b9f DM |
1060 | my $cache = {}; |
1061 | ||
6703353b | 1062 | my @errlist = (); |
b6cf0a66 | 1063 | foreach my $volid (@$vollist) { |
1dc01b9f | 1064 | my ($storeid, $volname) = parse_volume_id($volid); |
b6cf0a66 | 1065 | |
1dc01b9f DM |
1066 | my $scfg = storage_config($cfg, $storeid); |
1067 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1a3459ac | 1068 | |
1dc01b9f | 1069 | eval { |
02e797b8 | 1070 | $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache); |
1dc01b9f DM |
1071 | }; |
1072 | if (my $err = $@) { | |
1073 | warn $err; | |
1074 | push @errlist, $volid; | |
b6cf0a66 DM |
1075 | } |
1076 | } | |
6703353b | 1077 | |
82fc923f | 1078 | die "volume deactivation failed: " . join(' ', @errlist) |
6703353b | 1079 | if scalar(@errlist); |
b6cf0a66 DM |
1080 | } |
1081 | ||
1a3459ac | 1082 | sub storage_info { |
b6cf0a66 DM |
1083 | my ($cfg, $content) = @_; |
1084 | ||
1085 | my $ids = $cfg->{ids}; | |
1086 | ||
1087 | my $info = {}; | |
ff3badd8 | 1088 | |
583c2802 | 1089 | my @ctypes = PVE::Tools::split_list($content); |
ff3badd8 | 1090 | |
b6cf0a66 DM |
1091 | my $slist = []; |
1092 | foreach my $storeid (keys %$ids) { | |
1093 | ||
b6cf0a66 DM |
1094 | next if !storage_check_enabled($cfg, $storeid, undef, 1); |
1095 | ||
d73060be DM |
1096 | if (defined($content)) { |
1097 | my $want_ctype = 0; | |
1098 | foreach my $ctype (@ctypes) { | |
1099 | if ($ids->{$storeid}->{content}->{$ctype}) { | |
1100 | $want_ctype = 1; | |
1101 | last; | |
1102 | } | |
583c2802 | 1103 | } |
d73060be | 1104 | next if !$want_ctype; |
583c2802 | 1105 | } |
ff3badd8 | 1106 | |
b6cf0a66 DM |
1107 | my $type = $ids->{$storeid}->{type}; |
1108 | ||
1a3459ac | 1109 | $info->{$storeid} = { |
b6cf0a66 | 1110 | type => $type, |
1a3459ac DM |
1111 | total => 0, |
1112 | avail => 0, | |
1113 | used => 0, | |
04a2e4f3 | 1114 | shared => $ids->{$storeid}->{shared} ? 1 : 0, |
1dc01b9f | 1115 | content => PVE::Storage::Plugin::content_hash_to_string($ids->{$storeid}->{content}), |
b6cf0a66 DM |
1116 | active => 0, |
1117 | }; | |
1118 | ||
b6cf0a66 DM |
1119 | push @$slist, $storeid; |
1120 | } | |
1121 | ||
1dc01b9f | 1122 | my $cache = {}; |
b6cf0a66 | 1123 | |
b6cf0a66 DM |
1124 | foreach my $storeid (keys %$ids) { |
1125 | my $scfg = $ids->{$storeid}; | |
b6cf0a66 DM |
1126 | next if !$info->{$storeid}; |
1127 | ||
f3d4ef46 DM |
1128 | eval { activate_storage($cfg, $storeid, $cache); }; |
1129 | if (my $err = $@) { | |
1130 | warn $err; | |
1131 | next; | |
1132 | } | |
1133 | ||
1dc01b9f | 1134 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
7028645e DM |
1135 | my ($total, $avail, $used, $active); |
1136 | eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); }; | |
1137 | warn $@ if $@; | |
1dc01b9f | 1138 | next if !$active; |
ff3badd8 DM |
1139 | $info->{$storeid}->{total} = int($total); |
1140 | $info->{$storeid}->{avail} = int($avail); | |
1141 | $info->{$storeid}->{used} = int($used); | |
1dc01b9f | 1142 | $info->{$storeid}->{active} = $active; |
b6cf0a66 DM |
1143 | } |
1144 | ||
1145 | return $info; | |
1146 | } | |
1147 | ||
1148 | sub resolv_server { | |
1149 | my ($server) = @_; | |
1a3459ac | 1150 | |
c67daeac WB |
1151 | my ($packed_ip, $family); |
1152 | eval { | |
1153 | my @res = PVE::Tools::getaddrinfo_all($server); | |
1154 | $family = $res[0]->{family}; | |
1155 | $packed_ip = (PVE::Tools::unpack_sockaddr_in46($res[0]->{addr}))[2]; | |
1156 | }; | |
b6cf0a66 | 1157 | if (defined $packed_ip) { |
ee302b1c | 1158 | return Socket::inet_ntop($family, $packed_ip); |
b6cf0a66 DM |
1159 | } |
1160 | return undef; | |
1161 | } | |
1162 | ||
1163 | sub scan_nfs { | |
1164 | my ($server_in) = @_; | |
1165 | ||
1166 | my $server; | |
1167 | if (!($server = resolv_server ($server_in))) { | |
1168 | die "unable to resolve address for server '${server_in}'\n"; | |
1169 | } | |
1170 | ||
1171 | my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server]; | |
1172 | ||
1173 | my $res = {}; | |
f81372ac | 1174 | run_command($cmd, outfunc => sub { |
b6cf0a66 DM |
1175 | my $line = shift; |
1176 | ||
1177 | # note: howto handle white spaces in export path?? | |
1178 | if ($line =~ m!^(/\S+)\s+(.+)$!) { | |
1179 | $res->{$1} = $2; | |
1180 | } | |
1181 | }); | |
1182 | ||
1183 | return $res; | |
1184 | } | |
1185 | ||
584d97f6 DM |
1186 | sub scan_zfs { |
1187 | ||
3932390b | 1188 | my $cmd = ['zfs', 'list', '-t', 'filesystem', '-H', '-o', 'name,avail,used']; |
584d97f6 DM |
1189 | |
1190 | my $res = []; | |
1191 | run_command($cmd, outfunc => sub { | |
1192 | my $line = shift; | |
1193 | ||
1194 | if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) { | |
3932390b | 1195 | my ($pool, $size_str, $used_str) = ($1, $2, $3); |
584d97f6 | 1196 | my $size = PVE::Storage::ZFSPoolPlugin::zfs_parse_size($size_str); |
3932390b | 1197 | my $used = PVE::Storage::ZFSPoolPlugin::zfs_parse_size($used_str); |
48e27f79 | 1198 | # ignore subvolumes generated by our ZFSPoolPlugin |
851658c3 WL |
1199 | return if $pool =~ m!/subvol-\d+-[^/]+$!; |
1200 | return if $pool =~ m!/basevol-\d+-[^/]+$!; | |
3932390b | 1201 | push @$res, { pool => $pool, size => $size, free => $size-$used }; |
584d97f6 DM |
1202 | } |
1203 | }); | |
1204 | ||
1205 | return $res; | |
1206 | } | |
1207 | ||
b6cf0a66 DM |
1208 | sub resolv_portal { |
1209 | my ($portal, $noerr) = @_; | |
1210 | ||
1689e627 WB |
1211 | my ($server, $port) = PVE::Tools::parse_host_and_port($portal); |
1212 | if ($server) { | |
b6cf0a66 DM |
1213 | if (my $ip = resolv_server($server)) { |
1214 | $server = $ip; | |
1689e627 | 1215 | $server = "[$server]" if $server =~ /^$IPV6RE$/; |
b6cf0a66 DM |
1216 | return $port ? "$server:$port" : $server; |
1217 | } | |
1218 | } | |
1219 | return undef if $noerr; | |
1220 | ||
1221 | raise_param_exc({ portal => "unable to resolve portal address '$portal'" }); | |
1222 | } | |
1223 | ||
1224 | # idea is from usbutils package (/usr/bin/usb-devices) script | |
1225 | sub __scan_usb_device { | |
1226 | my ($res, $devpath, $parent, $level) = @_; | |
1227 | ||
1228 | return if ! -d $devpath; | |
1229 | return if $level && $devpath !~ m/^.*[-.](\d+)$/; | |
1230 | my $port = $level ? int($1 - 1) : 0; | |
1231 | ||
1232 | my $busnum = int(file_read_firstline("$devpath/busnum")); | |
1233 | my $devnum = int(file_read_firstline("$devpath/devnum")); | |
1234 | ||
1235 | my $d = { | |
1236 | port => $port, | |
1237 | level => $level, | |
1238 | busnum => $busnum, | |
1239 | devnum => $devnum, | |
1240 | speed => file_read_firstline("$devpath/speed"), | |
1241 | class => hex(file_read_firstline("$devpath/bDeviceClass")), | |
1242 | vendid => file_read_firstline("$devpath/idVendor"), | |
1243 | prodid => file_read_firstline("$devpath/idProduct"), | |
1244 | }; | |
1245 | ||
1246 | if ($level) { | |
1247 | my $usbpath = $devpath; | |
1248 | $usbpath =~ s|^.*/\d+\-||; | |
1249 | $d->{usbpath} = $usbpath; | |
1250 | } | |
1251 | ||
1252 | my $product = file_read_firstline("$devpath/product"); | |
1253 | $d->{product} = $product if $product; | |
1a3459ac | 1254 | |
b6cf0a66 DM |
1255 | my $manu = file_read_firstline("$devpath/manufacturer"); |
1256 | $d->{manufacturer} = $manu if $manu; | |
1257 | ||
1258 | my $serial => file_read_firstline("$devpath/serial"); | |
1259 | $d->{serial} = $serial if $serial; | |
1260 | ||
1261 | push @$res, $d; | |
1262 | ||
1263 | foreach my $subdev (<$devpath/$busnum-*>) { | |
1264 | next if $subdev !~ m|/$busnum-[0-9]+(\.[0-9]+)*$|; | |
1265 | __scan_usb_device($res, $subdev, $devnum, $level + 1); | |
1266 | } | |
1267 | ||
1268 | }; | |
1269 | ||
1270 | sub scan_usb { | |
1271 | ||
1272 | my $devlist = []; | |
1273 | ||
1274 | foreach my $device (</sys/bus/usb/devices/usb*>) { | |
1275 | __scan_usb_device($devlist, $device, 0, 0); | |
1276 | } | |
1277 | ||
1278 | return $devlist; | |
1279 | } | |
1280 | ||
1281 | sub scan_iscsi { | |
1282 | my ($portal_in) = @_; | |
1283 | ||
1284 | my $portal; | |
1dc01b9f | 1285 | if (!($portal = resolv_portal($portal_in))) { |
b6cf0a66 DM |
1286 | die "unable to parse/resolve portal address '${portal_in}'\n"; |
1287 | } | |
1288 | ||
1dc01b9f | 1289 | return PVE::Storage::ISCSIPlugin::iscsi_discovery($portal); |
b6cf0a66 DM |
1290 | } |
1291 | ||
1292 | sub storage_default_format { | |
1293 | my ($cfg, $storeid) = @_; | |
1294 | ||
1295 | my $scfg = storage_config ($cfg, $storeid); | |
1296 | ||
1dc01b9f | 1297 | return PVE::Storage::Plugin::default_format($scfg); |
b6cf0a66 DM |
1298 | } |
1299 | ||
1300 | sub vgroup_is_used { | |
1301 | my ($cfg, $vgname) = @_; | |
1302 | ||
1303 | foreach my $storeid (keys %{$cfg->{ids}}) { | |
1dc01b9f | 1304 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
1305 | if ($scfg->{type} eq 'lvm' && $scfg->{vgname} eq $vgname) { |
1306 | return 1; | |
1307 | } | |
1308 | } | |
1309 | ||
1310 | return undef; | |
1311 | } | |
1312 | ||
1313 | sub target_is_used { | |
1314 | my ($cfg, $target) = @_; | |
1315 | ||
1316 | foreach my $storeid (keys %{$cfg->{ids}}) { | |
1dc01b9f | 1317 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
1318 | if ($scfg->{type} eq 'iscsi' && $scfg->{target} eq $target) { |
1319 | return 1; | |
1320 | } | |
1321 | } | |
1322 | ||
1323 | return undef; | |
1324 | } | |
1325 | ||
1326 | sub volume_is_used { | |
1327 | my ($cfg, $volid) = @_; | |
1328 | ||
1329 | foreach my $storeid (keys %{$cfg->{ids}}) { | |
1dc01b9f | 1330 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
1331 | if ($scfg->{base} && $scfg->{base} eq $volid) { |
1332 | return 1; | |
1333 | } | |
1334 | } | |
1335 | ||
1336 | return undef; | |
1337 | } | |
1338 | ||
1339 | sub storage_is_used { | |
1340 | my ($cfg, $storeid) = @_; | |
1341 | ||
1342 | foreach my $sid (keys %{$cfg->{ids}}) { | |
1dc01b9f | 1343 | my $scfg = storage_config($cfg, $sid); |
b6cf0a66 | 1344 | next if !$scfg->{base}; |
1dc01b9f | 1345 | my ($st) = parse_volume_id($scfg->{base}); |
b6cf0a66 DM |
1346 | return 1 if $st && $st eq $storeid; |
1347 | } | |
1348 | ||
1349 | return undef; | |
1350 | } | |
1351 | ||
1352 | sub foreach_volid { | |
1353 | my ($list, $func) = @_; | |
1354 | ||
1355 | return if !$list; | |
1356 | ||
1357 | foreach my $sid (keys %$list) { | |
1358 | foreach my $info (@{$list->{$sid}}) { | |
1359 | my $volid = $info->{volid}; | |
1dc01b9f | 1360 | my ($sid1, $volname) = parse_volume_id($volid, 1); |
b6cf0a66 DM |
1361 | if ($sid1 && $sid1 eq $sid) { |
1362 | &$func ($volid, $sid, $info); | |
1363 | } else { | |
1364 | warn "detected strange volid '$volid' in volume list for '$sid'\n"; | |
1365 | } | |
1366 | } | |
1367 | } | |
1368 | } | |
1369 | ||
8898dd7b FG |
1370 | sub extract_vzdump_config_tar { |
1371 | my ($archive, $conf_re) = @_; | |
1372 | ||
1373 | die "ERROR: file '$archive' does not exist\n" if ! -f $archive; | |
1374 | ||
1375 | my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) || | |
1376 | die "unable to open file '$archive'\n"; | |
1377 | ||
1378 | my $file; | |
1379 | while (defined($file = <$fh>)) { | |
086c4bf1 | 1380 | if ($file =~ $conf_re) { |
8898dd7b FG |
1381 | $file = $1; # untaint |
1382 | last; | |
1383 | } | |
1384 | } | |
1385 | ||
1386 | kill 15, $pid; | |
1387 | waitpid $pid, 0; | |
1388 | close $fh; | |
1389 | ||
1390 | die "ERROR: archive contains no configuration file\n" if !$file; | |
1391 | chomp $file; | |
1392 | ||
1393 | my $raw = ''; | |
1394 | my $out = sub { | |
1395 | my $output = shift; | |
1396 | $raw .= "$output\n"; | |
1397 | }; | |
1398 | ||
1399 | PVE::Tools::run_command(['tar', '-xpOf', $archive, $file, '--occurrence'], outfunc => $out); | |
1400 | ||
1401 | return wantarray ? ($raw, $file) : $raw; | |
1402 | } | |
1403 | ||
1404 | sub extract_vzdump_config_vma { | |
1405 | my ($archive, $comp) = @_; | |
1406 | ||
1407 | my $cmd; | |
1408 | my $raw = ''; | |
1409 | my $out = sub { | |
1410 | my $output = shift; | |
1411 | $raw .= "$output\n"; | |
1412 | }; | |
1413 | ||
1414 | ||
1415 | if ($comp) { | |
1416 | my $uncomp; | |
1417 | if ($comp eq 'gz') { | |
1418 | $uncomp = ["zcat", $archive]; | |
1419 | } elsif ($comp eq 'lzo') { | |
1420 | $uncomp = ["lzop", "-d", "-c", $archive]; | |
1421 | } else { | |
1422 | die "unknown compression method '$comp'\n"; | |
1423 | } | |
1424 | $cmd = [$uncomp, ["vma", "config", "-"]]; | |
1425 | ||
1426 | # in some cases, lzop/zcat exits with 1 when its stdout pipe is | |
1427 | # closed early by vma, detect this and ignore the exit code later | |
1428 | my $broken_pipe; | |
1429 | my $errstring; | |
1430 | my $err = sub { | |
1431 | my $output = shift; | |
1432 | if ($output =~ m/lzop: Broken pipe: <stdout>/ || $output =~ m/gzip: stdout: Broken pipe/) { | |
1433 | $broken_pipe = 1; | |
1434 | } elsif (!defined ($errstring) && $output !~ m/^\s*$/) { | |
1435 | $errstring = "Failed to extract config from VMA archive: $output\n"; | |
1436 | } | |
1437 | }; | |
1438 | ||
1439 | # in other cases, the pipeline will exit with exit code 141 | |
1440 | # because of the broken pipe, handle / ignore this as well | |
1441 | my $rc; | |
1442 | eval { | |
1443 | $rc = PVE::Tools::run_command($cmd, outfunc => $out, errfunc => $err, noerr => 1); | |
1444 | }; | |
1445 | my $rerr = $@; | |
1446 | ||
1447 | # use exit code if no stderr output and not just broken pipe | |
fc1089fc | 1448 | if (!$errstring && !$broken_pipe && $rc != 0 && $rc != 141) { |
8898dd7b FG |
1449 | die "$rerr\n" if $rerr; |
1450 | die "config extraction failed with exit code $rc\n"; | |
1451 | } | |
1452 | die "$errstring\n" if $errstring; | |
1453 | } else { | |
1454 | # simple case without compression and weird piping behaviour | |
1455 | PVE::Tools::run_command(["vma", "config", $archive], outfunc => $out); | |
1456 | } | |
1457 | ||
1458 | return wantarray ? ($raw, undef) : $raw; | |
1459 | } | |
1460 | ||
1461 | sub extract_vzdump_config { | |
1462 | my ($cfg, $volid) = @_; | |
1463 | ||
1464 | my $archive = abs_filesystem_path($cfg, $volid); | |
1465 | ||
89443394 | 1466 | if ($volid =~ /vzdump-(lxc|openvz)-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|(tar(\.(gz|lzo))?))$/) { |
086c4bf1 | 1467 | return extract_vzdump_config_tar($archive, qr!^(\./etc/vzdump/(pct|vps)\.conf)$!); |
89443394 | 1468 | } elsif ($volid =~ /vzdump-qemu-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|((tar|vma)(\.(gz|lzo))?))$/) { |
8898dd7b FG |
1469 | my $format; |
1470 | my $comp; | |
1471 | if ($7 eq 'tgz') { | |
1472 | $format = 'tar'; | |
1473 | $comp = 'gz'; | |
1474 | } else { | |
1475 | $format = $9; | |
1476 | $comp = $11 if defined($11); | |
1477 | } | |
1478 | ||
1479 | if ($format eq 'tar') { | |
1480 | return extract_vzdump_config_tar($archive, qr!\(\./qemu-server\.conf\)!); | |
1481 | } else { | |
1482 | return extract_vzdump_config_vma($archive, $comp); | |
1483 | } | |
1484 | } else { | |
1485 | die "cannot determine backup guest type for backup archive '$volid'\n"; | |
1486 | } | |
1487 | } | |
1488 | ||
f7621c01 DM |
1489 | # bash completion helper |
1490 | ||
1491 | sub complete_storage { | |
746e530f | 1492 | my ($cmdname, $pname, $cvalue) = @_; |
f7621c01 | 1493 | |
746e530f | 1494 | my $cfg = PVE::Storage::config(); |
180c8b02 | 1495 | |
746e530f | 1496 | return $cmdname eq 'add' ? [] : [ PVE::Storage::storage_ids($cfg) ]; |
f7621c01 DM |
1497 | } |
1498 | ||
1499 | sub complete_storage_enabled { | |
746e530f | 1500 | my ($cmdname, $pname, $cvalue) = @_; |
f7621c01 | 1501 | |
746e530f | 1502 | my $res = []; |
f7621c01 | 1503 | |
746e530f DM |
1504 | my $cfg = PVE::Storage::config(); |
1505 | foreach my $sid (keys %{$cfg->{ids}}) { | |
1506 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
1507 | push @$res, $sid; | |
1508 | } | |
1509 | return $res; | |
f7621c01 DM |
1510 | } |
1511 | ||
98437f4c DM |
1512 | sub complete_content_type { |
1513 | my ($cmdname, $pname, $cvalue) = @_; | |
1514 | ||
1515 | return [qw(rootdir images vztmpl iso backup)]; | |
1516 | } | |
1517 | ||
bf7aed26 DM |
1518 | sub complete_volume { |
1519 | my ($cmdname, $pname, $cvalue) = @_; | |
1520 | ||
1521 | my $cfg = config(); | |
1522 | ||
1523 | my $storage_list = complete_storage_enabled(); | |
1524 | ||
b70b0c58 DM |
1525 | if ($cvalue =~ m/^([^:]+):/) { |
1526 | $storage_list = [ $1 ]; | |
1527 | } else { | |
1528 | if (scalar(@$storage_list) > 1) { | |
1529 | # only list storage IDs to avoid large listings | |
1530 | my $res = []; | |
1531 | foreach my $storeid (@$storage_list) { | |
1532 | # Hack: simply return 2 artificial values, so that | |
1533 | # completions does not finish | |
1534 | push @$res, "$storeid:volname", "$storeid:..."; | |
1535 | } | |
1536 | return $res; | |
1537 | } | |
1538 | } | |
1539 | ||
bf7aed26 DM |
1540 | my $res = []; |
1541 | foreach my $storeid (@$storage_list) { | |
1542 | my $vollist = PVE::Storage::volume_list($cfg, $storeid); | |
1543 | ||
1544 | foreach my $item (@$vollist) { | |
1545 | push @$res, $item->{volid}; | |
1546 | } | |
1547 | } | |
1548 | ||
1549 | return $res; | |
1550 | } | |
1551 | ||
b6cf0a66 | 1552 | 1; |