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