]>
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; |
7ba34faa | 10 | use IO::Socket::IP; |
b6cf0a66 DM |
11 | use File::Basename; |
12 | use File::Path; | |
b6cf0a66 | 13 | use Cwd 'abs_path'; |
7a2d5c1a | 14 | use Socket; |
b6cf0a66 | 15 | |
4dee23d3 | 16 | use PVE::Tools qw(run_command file_read_firstline dir_glob_foreach $IPV6RE); |
83d7192f | 17 | use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file); |
b6cf0a66 DM |
18 | use PVE::Exception qw(raise_param_exc); |
19 | use PVE::JSONSchema; | |
20 | use PVE::INotify; | |
88c3abaf | 21 | use PVE::RPCEnvironment; |
b6cf0a66 | 22 | |
1dc01b9f DM |
23 | use PVE::Storage::Plugin; |
24 | use PVE::Storage::DirPlugin; | |
25 | use PVE::Storage::LVMPlugin; | |
610798bc | 26 | use PVE::Storage::LvmThinPlugin; |
1dc01b9f DM |
27 | use PVE::Storage::NFSPlugin; |
28 | use PVE::Storage::ISCSIPlugin; | |
0509010d | 29 | use PVE::Storage::RBDPlugin; |
caf1960c | 30 | use PVE::Storage::SheepdogPlugin; |
86616554 | 31 | use PVE::Storage::ISCSIDirectPlugin; |
f4648aef | 32 | use PVE::Storage::GlusterfsPlugin; |
85fda4dd | 33 | use PVE::Storage::ZFSPoolPlugin; |
4f914e6e | 34 | use PVE::Storage::ZFSPlugin; |
14770890 | 35 | use PVE::Storage::DRBDPlugin; |
b6cf0a66 | 36 | |
4dee23d3 DP |
37 | # Storage API version. Icrement it on changes in storage API interface. |
38 | use constant APIVER => 1; | |
39 | ||
40 | # load standard plugins | |
1dc01b9f DM |
41 | PVE::Storage::DirPlugin->register(); |
42 | PVE::Storage::LVMPlugin->register(); | |
610798bc | 43 | PVE::Storage::LvmThinPlugin->register(); |
1dc01b9f DM |
44 | PVE::Storage::NFSPlugin->register(); |
45 | PVE::Storage::ISCSIPlugin->register(); | |
0509010d | 46 | PVE::Storage::RBDPlugin->register(); |
caf1960c | 47 | PVE::Storage::SheepdogPlugin->register(); |
86616554 | 48 | PVE::Storage::ISCSIDirectPlugin->register(); |
f4648aef | 49 | PVE::Storage::GlusterfsPlugin->register(); |
85fda4dd | 50 | PVE::Storage::ZFSPoolPlugin->register(); |
4f914e6e | 51 | PVE::Storage::ZFSPlugin->register(); |
14770890 | 52 | PVE::Storage::DRBDPlugin->register(); |
4dee23d3 DP |
53 | |
54 | # load third-party plugins | |
55 | if ( -d '/usr/share/perl5/PVE/Storage/Custom' ) { | |
56 | dir_glob_foreach('/usr/share/perl5/PVE/Storage/Custom', '.*\.pm$', sub { | |
57 | my ($file) = @_; | |
58 | my $modname = 'PVE::Storage::Custom::' . $file; | |
59 | $modname =~ s!\.pm$!!; | |
60 | $file = 'PVE/Storage/Custom/' . $file; | |
61 | ||
62 | eval { | |
63 | require $file; | |
64 | }; | |
65 | if ($@) { | |
66 | warn $@; | |
67 | # Check storage API version and that file is really storage plugin. | |
68 | } elsif ($modname->isa('PVE::Storage::Plugin') && $modname->can('api') && $modname->api() == APIVER) { | |
69 | eval { | |
70 | import $file; | |
71 | $modname->register(); | |
72 | }; | |
73 | warn $@ if $@; | |
74 | } else { | |
75 | warn "Error loading storage plugin \"$modname\" because of API version mismatch. Please, update it.\n" | |
76 | } | |
77 | }); | |
78 | } | |
79 | ||
80 | # initialize all plugins | |
1dc01b9f | 81 | PVE::Storage::Plugin->init(); |
b6cf0a66 | 82 | |
1dc01b9f | 83 | my $UDEVADM = '/sbin/udevadm'; |
b6cf0a66 | 84 | |
1dc01b9f | 85 | # PVE::Storage utility functions |
b6cf0a66 DM |
86 | |
87 | sub config { | |
88 | return cfs_read_file("storage.cfg"); | |
89 | } | |
90 | ||
83d7192f FG |
91 | sub write_config { |
92 | my ($cfg) = @_; | |
93 | ||
94 | cfs_write_file('storage.cfg', $cfg); | |
95 | } | |
96 | ||
b6cf0a66 DM |
97 | sub lock_storage_config { |
98 | my ($code, $errmsg) = @_; | |
99 | ||
100 | cfs_lock_file("storage.cfg", undef, $code); | |
101 | my $err = $@; | |
102 | if ($err) { | |
103 | $errmsg ? die "$errmsg: $err" : die $err; | |
104 | } | |
105 | } | |
106 | ||
b6cf0a66 DM |
107 | sub storage_config { |
108 | my ($cfg, $storeid, $noerr) = @_; | |
109 | ||
82fc923f | 110 | die "no storage ID specified\n" if !$storeid; |
1a3459ac | 111 | |
b6cf0a66 DM |
112 | my $scfg = $cfg->{ids}->{$storeid}; |
113 | ||
114 | die "storage '$storeid' does not exists\n" if (!$noerr && !$scfg); | |
115 | ||
116 | return $scfg; | |
117 | } | |
118 | ||
119 | sub storage_check_node { | |
120 | my ($cfg, $storeid, $node, $noerr) = @_; | |
121 | ||
1dc01b9f | 122 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
123 | |
124 | if ($scfg->{nodes}) { | |
125 | $node = PVE::INotify::nodename() if !$node || ($node eq 'localhost'); | |
126 | if (!$scfg->{nodes}->{$node}) { | |
da156fb3 | 127 | die "storage '$storeid' is not available on node '$node'\n" if !$noerr; |
b6cf0a66 DM |
128 | return undef; |
129 | } | |
130 | } | |
131 | ||
132 | return $scfg; | |
133 | } | |
134 | ||
135 | sub storage_check_enabled { | |
136 | my ($cfg, $storeid, $node, $noerr) = @_; | |
137 | ||
1dc01b9f | 138 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
139 | |
140 | if ($scfg->{disable}) { | |
141 | die "storage '$storeid' is disabled\n" if !$noerr; | |
142 | return undef; | |
143 | } | |
144 | ||
145 | return storage_check_node($cfg, $storeid, $node, $noerr); | |
146 | } | |
147 | ||
148 | sub storage_ids { | |
149 | my ($cfg) = @_; | |
150 | ||
1dc01b9f | 151 | return keys %{$cfg->{ids}}; |
b6cf0a66 DM |
152 | } |
153 | ||
1dc01b9f DM |
154 | sub file_size_info { |
155 | my ($filename, $timeout) = @_; | |
b6cf0a66 | 156 | |
1dc01b9f | 157 | return PVE::Storage::Plugin::file_size_info($filename, $timeout); |
b6cf0a66 DM |
158 | } |
159 | ||
20ccac1b AD |
160 | sub volume_size_info { |
161 | my ($cfg, $volid, $timeout) = @_; | |
162 | ||
f18199e5 DM |
163 | my ($storeid, $volname) = parse_volume_id($volid, 1); |
164 | if ($storeid) { | |
165 | my $scfg = storage_config($cfg, $storeid); | |
166 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
167 | return $plugin->volume_size_info($scfg, $storeid, $volname, $timeout); | |
168 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
169 | return file_size_info($volid, $timeout); | |
170 | } else { | |
171 | return 0; | |
172 | } | |
20ccac1b AD |
173 | } |
174 | ||
7e6c05dc AD |
175 | sub volume_resize { |
176 | my ($cfg, $volid, $size, $running) = @_; | |
177 | ||
178 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
179 | if ($storeid) { | |
180 | my $scfg = storage_config($cfg, $storeid); | |
181 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
182 | return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running); | |
183 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
f824c722 | 184 | die "resize file/device '$volid' is not possible\n"; |
7e6c05dc | 185 | } else { |
f824c722 | 186 | die "unable to parse volume ID '$volid'\n"; |
7e6c05dc AD |
187 | } |
188 | } | |
189 | ||
1597f1f9 WL |
190 | sub volume_rollback_is_possible { |
191 | my ($cfg, $volid, $snap) = @_; | |
e0852ba7 | 192 | |
1597f1f9 WL |
193 | my ($storeid, $volname) = parse_volume_id($volid, 1); |
194 | if ($storeid) { | |
195 | my $scfg = storage_config($cfg, $storeid); | |
196 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
197 | return $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap); | |
198 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
f824c722 | 199 | die "snapshot rollback file/device '$volid' is not possible\n"; |
1597f1f9 | 200 | } else { |
f824c722 | 201 | die "unable to parse volume ID '$volid'\n"; |
1597f1f9 WL |
202 | } |
203 | } | |
204 | ||
db60719c | 205 | sub volume_snapshot { |
f5640e7d | 206 | my ($cfg, $volid, $snap) = @_; |
db60719c AD |
207 | |
208 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
209 | if ($storeid) { | |
210 | my $scfg = storage_config($cfg, $storeid); | |
211 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
f5640e7d | 212 | return $plugin->volume_snapshot($scfg, $storeid, $volname, $snap); |
db60719c | 213 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { |
f824c722 | 214 | die "snapshot file/device '$volid' is not possible\n"; |
db60719c | 215 | } else { |
f824c722 | 216 | die "unable to parse volume ID '$volid'\n"; |
db60719c AD |
217 | } |
218 | } | |
219 | ||
22a2a633 AD |
220 | sub volume_snapshot_rollback { |
221 | my ($cfg, $volid, $snap) = @_; | |
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}); | |
b3f302c6 | 227 | $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap); |
22a2a633 AD |
228 | return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap); |
229 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
f824c722 | 230 | die "snapshot rollback file/device '$volid' is not possible\n"; |
22a2a633 | 231 | } else { |
f824c722 | 232 | die "unable to parse volume ID '$volid'\n"; |
22a2a633 AD |
233 | } |
234 | } | |
235 | ||
5753c9d1 AD |
236 | sub volume_snapshot_delete { |
237 | my ($cfg, $volid, $snap, $running) = @_; | |
238 | ||
239 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
240 | if ($storeid) { | |
241 | my $scfg = storage_config($cfg, $storeid); | |
242 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
27cc55d4 | 243 | return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running); |
5753c9d1 | 244 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { |
f824c722 | 245 | die "snapshot delete file/device '$volid' is not possible\n"; |
5753c9d1 | 246 | } else { |
f824c722 | 247 | die "unable to parse volume ID '$volid'\n"; |
5753c9d1 AD |
248 | } |
249 | } | |
250 | ||
99473759 AD |
251 | sub volume_has_feature { |
252 | my ($cfg, $feature, $volid, $snap, $running) = @_; | |
253 | ||
254 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
255 | if ($storeid) { | |
256 | my $scfg = storage_config($cfg, $storeid); | |
257 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
258 | return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running); | |
259 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
260 | return undef; | |
261 | } else { | |
262 | return undef; | |
263 | } | |
264 | } | |
265 | ||
aefe82ea | 266 | sub volume_snapshot_list { |
8b622c2d | 267 | my ($cfg, $volid) = @_; |
aefe82ea WL |
268 | |
269 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
270 | if ($storeid) { | |
271 | my $scfg = storage_config($cfg, $storeid); | |
272 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
8b622c2d | 273 | return $plugin->volume_snapshot_list($scfg, $storeid, $volname); |
aefe82ea WL |
274 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { |
275 | die "send file/device '$volid' is not possible\n"; | |
276 | } else { | |
277 | die "unable to parse volume ID '$volid'\n"; | |
278 | } | |
279 | # return an empty array if dataset does not exist. | |
aefe82ea WL |
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 { |
7ba34faa | 528 | my ($cfg, $volid, $target_sshinfo, $target_storeid, $target_volname, $base_snapshot, $snapshot, $ratelimit_bps, $insecure) = @_; |
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 | ||
4b4c580d WB |
542 | my $target_ip = $target_sshinfo->{ip}; |
543 | my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_sshinfo->{name}'"; | |
b6cf0a66 | 544 | |
acd27197 | 545 | my $ssh = PVE::Cluster::ssh_info_to_command($target_sshinfo); |
47cea194 WB |
546 | my $ssh_base = PVE::Cluster::ssh_info_to_command_base($target_sshinfo); |
547 | local $ENV{RSYNC_RSH} = PVE::Tools::cmd2string($ssh_base); | |
b6cf0a66 | 548 | |
ac191ec7 WB |
549 | my $no_incremental = sub { |
550 | my ($type) = @_; | |
551 | die "incremental migration not supported on storage type $type\n" | |
552 | if defined($base_snapshot); | |
553 | }; | |
dc769537 WB |
554 | my $no_snapshot = sub { |
555 | my ($type) = @_; | |
556 | # $snapshot is currently only used by replication | |
557 | die "replicating storage migration not supported on storage type $type\n" | |
558 | if defined($snapshot); | |
559 | }; | |
ac191ec7 | 560 | |
01f7e902 WB |
561 | my @cstream = ([ '/usr/bin/cstream', '-t', $ratelimit_bps ]) |
562 | if defined($ratelimit_bps); | |
563 | ||
1dc01b9f DM |
564 | # only implemented for file system based storage |
565 | if ($scfg->{path}) { | |
ac191ec7 | 566 | $no_incremental->($scfg->{type}); |
dc769537 | 567 | $no_snapshot->($scfg->{type}); |
b6cf0a66 | 568 | |
ac191ec7 | 569 | if ($tcfg->{path}) { |
1dc01b9f DM |
570 | my $src_plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
571 | my $dst_plugin = PVE::Storage::Plugin->lookup($tcfg->{type}); | |
6bf56298 DM |
572 | my $src = $src_plugin->path($scfg, $volname, $storeid); |
573 | my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid); | |
b6cf0a66 | 574 | |
1dc01b9f | 575 | my $dirname = dirname($dst); |
b6cf0a66 DM |
576 | |
577 | if ($tcfg->{shared}) { # we can do a local copy | |
1a3459ac | 578 | |
f81372ac | 579 | run_command(['/bin/mkdir', '-p', $dirname]); |
b6cf0a66 | 580 | |
1dc01b9f | 581 | run_command(['/bin/cp', $src, $dst]); |
b6cf0a66 | 582 | |
1dc01b9f | 583 | } else { |
acd27197 | 584 | run_command([@$ssh, '/bin/mkdir', '-p', $dirname]); |
b6cf0a66 | 585 | |
1dc01b9f DM |
586 | # we use rsync with --sparse, so we can't use --inplace, |
587 | # so we remove file on the target if it already exists to | |
588 | # save space | |
589 | my ($size, $format) = PVE::Storage::Plugin::file_size_info($src); | |
590 | if ($format && ($format eq 'raw') && $size) { | |
acd27197 | 591 | run_command([@$ssh, 'rm', '-f', $dst], |
1dc01b9f DM |
592 | outfunc => sub {}); |
593 | } | |
b6cf0a66 | 594 | |
986303ca | 595 | my $cmd; |
01f7e902 | 596 | my @bwlimit = ("--bwlimit=${ratelimit_bps}b") if defined($ratelimit_bps); |
986303ca WL |
597 | if ($format eq 'subvol') { |
598 | $cmd = ['/usr/bin/rsync', '--progress', '-X', '-A', '--numeric-ids', | |
599 | '-aH', '--delete', '--no-whole-file', '--inplace', | |
01f7e902 | 600 | '--one-file-system', @bwlimit, |
4b4c580d | 601 | "$src/", "[root\@${target_ip}]:$dst"]; |
986303ca WL |
602 | } else { |
603 | $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file', | |
01f7e902 | 604 | @bwlimit, |
4b4c580d | 605 | $src, "[root\@${target_ip}]:$dst"]; |
986303ca | 606 | } |
b6cf0a66 | 607 | |
1dc01b9f | 608 | my $percent = -1; |
b6cf0a66 | 609 | |
1dc01b9f DM |
610 | run_command($cmd, outfunc => sub { |
611 | my $line = shift; | |
b6cf0a66 | 612 | |
1dc01b9f DM |
613 | if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) { |
614 | if ($2 > $percent) { | |
615 | $percent = $2; | |
616 | print "rsync status: $1\n"; | |
617 | *STDOUT->flush(); | |
618 | } | |
619 | } else { | |
620 | print "$line\n"; | |
621 | *STDOUT->flush(); | |
622 | } | |
623 | }); | |
624 | } | |
625 | } else { | |
626 | die "$errstr - target type '$tcfg->{type}' not implemented\n"; | |
627 | } | |
e0852ba7 | 628 | |
3d621977 WL |
629 | } elsif ($scfg->{type} eq 'zfspool') { |
630 | ||
e0852ba7 | 631 | if ($tcfg->{type} eq 'zfspool') { |
3d621977 | 632 | |
82fc923f | 633 | die "$errstr - pool on target does not have the same name as on source!" |
e0852ba7 | 634 | if $tcfg->{pool} ne $scfg->{pool}; |
3d621977 | 635 | |
b57a863a WB |
636 | my $migration_snapshot; |
637 | if (!defined($snapshot)) { | |
638 | $migration_snapshot = 1; | |
639 | $snapshot = '__migration__'; | |
640 | } | |
dc769537 | 641 | |
e0852ba7 | 642 | my (undef, $volname) = parse_volname($cfg, $volid); |
3d621977 WL |
643 | my $zfspath = "$scfg->{pool}\/$volname"; |
644 | ||
b57a863a | 645 | my @formats = volume_transfer_formats($cfg, $volid, $volid, $snapshot, $base_snapshot, 1); |
7459cb3d WB |
646 | die "cannot migrate from storage type '$scfg->{type}' to '$tcfg->{type}'\n" if !@formats; |
647 | my $format = $formats[0]; | |
648 | ||
7ba34faa WB |
649 | my @insecurecmd; |
650 | if ($insecure) { | |
651 | @insecurecmd = ('pvecm', 'mtunnel', '-run-command', 1); | |
652 | if (my $network = $target_sshinfo->{network}) { | |
653 | push @insecurecmd, '-migration_network', $network; | |
654 | } | |
655 | } | |
656 | ||
b57a863a | 657 | my $send = ['pvesm', 'export', $volid, $format, '-', '-snapshot', $snapshot, '-with-snapshots', '1']; |
7ba34faa | 658 | my $recv = [@$ssh, @insecurecmd, '--', 'pvesm', 'import', $volid, $format, '-', '-with-snapshots', '1']; |
b57a863a WB |
659 | if ($migration_snapshot) { |
660 | push @$recv, '-delete-snapshot', $snapshot; | |
661 | } | |
3d621977 | 662 | |
ac191ec7 WB |
663 | if (defined($base_snapshot)) { |
664 | # Check if the snapshot exists on the remote side: | |
7459cb3d | 665 | push @$send, '-base', $base_snapshot; |
ac191ec7 WB |
666 | push @$recv, '-base', $base_snapshot; |
667 | } | |
668 | ||
b57a863a | 669 | volume_snapshot($cfg, $volid, $snapshot) if $migration_snapshot; |
7459cb3d | 670 | eval { |
7ba34faa WB |
671 | if ($insecure) { |
672 | my $pid = open(my $info, '-|', @$recv) | |
673 | or die "receive command failed: $!\n"; | |
488cf14d WB |
674 | my ($ip) = <$info> =~ /^($PVE::Tools::IPRE)$/ or die "no tunnel IP received\n"; |
675 | my ($port) = <$info> =~ /^(\d+)$/ or die "no tunnel port received\n"; | |
7ba34faa WB |
676 | my $socket = IO::Socket::IP->new(PeerHost => $ip, PeerPort => $port, Type => SOCK_STREAM) |
677 | or die "failed to connect to tunnel at $ip:$port\n"; | |
678 | run_command([$send, @cstream], output => '>&'.fileno($socket)); | |
679 | } else { | |
680 | run_command([$send, @cstream, $recv]); | |
681 | } | |
3d621977 | 682 | }; |
afd0afe4 | 683 | my $err = $@; |
865710f4 | 684 | warn "send/receive failed, cleaning up snapshot(s)..\n" if $err; |
b57a863a WB |
685 | if ($migration_snapshot) { |
686 | eval { volume_snapshot_delete($cfg, $volid, $snapshot, 0) }; | |
dc769537 | 687 | warn "could not remove source snapshot: $@\n" if $@; |
dc769537 | 688 | } |
afd0afe4 | 689 | die $err if $err; |
3d621977 WL |
690 | } else { |
691 | die "$errstr - target type $tcfg->{type} is not valid\n"; | |
692 | } | |
0a29ad61 WL |
693 | |
694 | } elsif ($scfg->{type} eq 'lvmthin' || $scfg->{type} eq 'lvm') { | |
ac191ec7 | 695 | $no_incremental->($scfg->{type}); |
dc769537 | 696 | $no_snapshot->($scfg->{type}); |
0a29ad61 | 697 | |
76f13c9e DM |
698 | if (($scfg->{type} eq $tcfg->{type}) && |
699 | ($tcfg->{type} eq 'lvmthin' || $tcfg->{type} eq 'lvm')) { | |
0a29ad61 | 700 | |
84252f67 | 701 | my (undef, $volname, $vmid) = parse_volname($cfg, $volid); |
0a29ad61 | 702 | my $size = volume_size_info($cfg, $volid, 5); |
84252f67 DM |
703 | my $src = path($cfg, $volid); |
704 | my $dst = path($cfg, $target_volid); | |
0a29ad61 | 705 | |
acd27197 | 706 | run_command([@$ssh, '--', |
0a29ad61 | 707 | 'pvesm', 'alloc', $target_storeid, $vmid, |
84252f67 | 708 | $target_volname, int($size/1024)]); |
0a29ad61 WL |
709 | |
710 | eval { | |
e83f7b40 | 711 | if ($tcfg->{type} eq 'lvmthin') { |
01f7e902 WB |
712 | run_command([["dd", "if=$src", "bs=4k"], @cstream, |
713 | [@$ssh, "dd", 'conv=sparse', "of=$dst", "bs=4k"]]); | |
e83f7b40 | 714 | } else { |
01f7e902 WB |
715 | run_command([["dd", "if=$src", "bs=4k"], @cstream, |
716 | [@$ssh, "dd", "of=$dst", "bs=4k"]]); | |
e83f7b40 | 717 | } |
0a29ad61 WL |
718 | }; |
719 | if (my $err = $@) { | |
acd27197 | 720 | run_command([@$ssh, 'pvesm', 'free', $target_volid]); |
966ecef2 | 721 | die $err; |
0a29ad61 WL |
722 | } |
723 | } else { | |
76f13c9e | 724 | die "$errstr - migrate from source type '$scfg->{type}' to '$tcfg->{type}' not implemented\n"; |
0a29ad61 | 725 | } |
1dc01b9f DM |
726 | } else { |
727 | die "$errstr - source type '$scfg->{type}' not implemented\n"; | |
b6cf0a66 DM |
728 | } |
729 | } | |
730 | ||
2502b33b | 731 | sub vdisk_clone { |
7bbc4004 | 732 | my ($cfg, $volid, $vmid, $snap) = @_; |
1a3459ac | 733 | |
2502b33b DM |
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 { | |
7bbc4004 | 744 | my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap); |
2502b33b DM |
745 | return "$storeid:$volname"; |
746 | }); | |
747 | } | |
748 | ||
749 | sub vdisk_create_base { | |
750 | my ($cfg, $volid) = @_; | |
751 | ||
752 | my ($storeid, $volname) = parse_volume_id($volid); | |
753 | ||
754 | my $scfg = storage_config($cfg, $storeid); | |
755 | ||
756 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1a3459ac | 757 | |
2502b33b DM |
758 | activate_storage($cfg, $storeid); |
759 | ||
760 | # lock shared storage | |
761 | return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
762 | my $volname = $plugin->create_base($storeid, $scfg, $volname); | |
763 | return "$storeid:$volname"; | |
764 | }); | |
765 | } | |
766 | ||
1dc01b9f DM |
767 | sub vdisk_alloc { |
768 | my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_; | |
b6cf0a66 | 769 | |
82fc923f | 770 | die "no storage ID specified\n" if !$storeid; |
b6cf0a66 | 771 | |
1dc01b9f | 772 | PVE::JSONSchema::parse_storage_id($storeid); |
b6cf0a66 | 773 | |
1dc01b9f | 774 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 | 775 | |
1dc01b9f | 776 | die "no VMID specified\n" if !$vmid; |
b6cf0a66 | 777 | |
1dc01b9f | 778 | $vmid = parse_vmid($vmid); |
b6cf0a66 | 779 | |
1dc01b9f | 780 | my $defformat = PVE::Storage::Plugin::default_format($scfg); |
b6cf0a66 | 781 | |
1dc01b9f | 782 | $fmt = $defformat if !$fmt; |
b6cf0a66 | 783 | |
1dc01b9f | 784 | activate_storage($cfg, $storeid); |
3af60e62 | 785 | |
1dc01b9f | 786 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
b6cf0a66 | 787 | |
1dc01b9f DM |
788 | # lock shared storage |
789 | return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
afdfbe55 WB |
790 | my $old_umask = umask(umask|0037); |
791 | my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) }; | |
792 | my $err = $@; | |
793 | umask $old_umask; | |
794 | die $err if $err; | |
1dc01b9f DM |
795 | return "$storeid:$volname"; |
796 | }); | |
b6cf0a66 DM |
797 | } |
798 | ||
1dc01b9f DM |
799 | sub vdisk_free { |
800 | my ($cfg, $volid) = @_; | |
b6cf0a66 | 801 | |
1dc01b9f | 802 | my ($storeid, $volname) = parse_volume_id($volid); |
1dc01b9f | 803 | my $scfg = storage_config($cfg, $storeid); |
1dc01b9f | 804 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
1a3459ac | 805 | |
1dc01b9f | 806 | activate_storage($cfg, $storeid); |
b6cf0a66 | 807 | |
1dc01b9f | 808 | my $cleanup_worker; |
b6cf0a66 | 809 | |
1dc01b9f DM |
810 | # lock shared storage |
811 | $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
787624df | 812 | # LVM-thin allows deletion of still referenced base volumes! |
17fb7e42 FG |
813 | die "base volume '$volname' is still in use by linked clones\n" |
814 | if &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname); | |
32437ed2 | 815 | |
17fb7e42 | 816 | my (undef, undef, undef, undef, undef, $isBase, $format) = |
32437ed2 | 817 | $plugin->parse_volname($volname); |
35533c68 | 818 | $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format); |
1dc01b9f | 819 | }); |
b6cf0a66 | 820 | |
1dc01b9f | 821 | return if !$cleanup_worker; |
b6cf0a66 | 822 | |
1dc01b9f DM |
823 | my $rpcenv = PVE::RPCEnvironment::get(); |
824 | my $authuser = $rpcenv->get_user(); | |
b6cf0a66 | 825 | |
1dc01b9f | 826 | $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker); |
b6cf0a66 DM |
827 | } |
828 | ||
b6cf0a66 DM |
829 | #list iso or openvz template ($tt = <iso|vztmpl|backup>) |
830 | sub template_list { | |
831 | my ($cfg, $storeid, $tt) = @_; | |
832 | ||
1a3459ac DM |
833 | die "unknown template type '$tt'\n" |
834 | if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup'); | |
b6cf0a66 DM |
835 | |
836 | my $ids = $cfg->{ids}; | |
837 | ||
838 | storage_check_enabled($cfg, $storeid) if ($storeid); | |
839 | ||
840 | my $res = {}; | |
841 | ||
842 | # query the storage | |
843 | ||
844 | foreach my $sid (keys %$ids) { | |
845 | next if $storeid && $storeid ne $sid; | |
846 | ||
847 | my $scfg = $ids->{$sid}; | |
848 | my $type = $scfg->{type}; | |
849 | ||
850 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
851 | ||
852 | next if $tt eq 'iso' && !$scfg->{content}->{iso}; | |
853 | next if $tt eq 'vztmpl' && !$scfg->{content}->{vztmpl}; | |
854 | next if $tt eq 'backup' && !$scfg->{content}->{backup}; | |
855 | ||
1dc01b9f | 856 | activate_storage($cfg, $sid); |
b6cf0a66 | 857 | |
1dc01b9f DM |
858 | if ($scfg->{path}) { |
859 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b6cf0a66 | 860 | |
1dc01b9f | 861 | my $path = $plugin->get_subdir($scfg, $tt); |
b6cf0a66 DM |
862 | |
863 | foreach my $fn (<$path/*>) { | |
864 | ||
865 | my $info; | |
866 | ||
867 | if ($tt eq 'iso') { | |
868 | next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!; | |
869 | ||
870 | $info = { volid => "$sid:iso/$1", format => 'iso' }; | |
871 | ||
872 | } elsif ($tt eq 'vztmpl') { | |
13d2cb79 | 873 | next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!; |
b6cf0a66 | 874 | |
13d2cb79 | 875 | $info = { volid => "$sid:vztmpl/$1", format => "t$2" }; |
b6cf0a66 DM |
876 | |
877 | } elsif ($tt eq 'backup') { | |
a22854e5 | 878 | next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!; |
1a3459ac | 879 | |
b6cf0a66 DM |
880 | $info = { volid => "$sid:backup/$1", format => $2 }; |
881 | } | |
882 | ||
883 | $info->{size} = -s $fn; | |
884 | ||
885 | push @{$res->{$sid}}, $info; | |
886 | } | |
887 | ||
888 | } | |
889 | ||
890 | @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid}; | |
891 | } | |
892 | ||
893 | return $res; | |
894 | } | |
895 | ||
20ccac1b | 896 | |
b6cf0a66 DM |
897 | sub vdisk_list { |
898 | my ($cfg, $storeid, $vmid, $vollist) = @_; | |
899 | ||
900 | my $ids = $cfg->{ids}; | |
901 | ||
902 | storage_check_enabled($cfg, $storeid) if ($storeid); | |
903 | ||
904 | my $res = {}; | |
905 | ||
906 | # prepare/activate/refresh all storages | |
907 | ||
b6cf0a66 DM |
908 | my $storage_list = []; |
909 | if ($vollist) { | |
910 | foreach my $volid (@$vollist) { | |
1dc01b9f DM |
911 | my ($sid, undef) = parse_volume_id($volid); |
912 | next if !defined($ids->{$sid}); | |
b6cf0a66 DM |
913 | next if !storage_check_enabled($cfg, $sid, undef, 1); |
914 | push @$storage_list, $sid; | |
b6cf0a66 DM |
915 | } |
916 | } else { | |
917 | foreach my $sid (keys %$ids) { | |
918 | next if $storeid && $storeid ne $sid; | |
919 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
920 | push @$storage_list, $sid; | |
b6cf0a66 DM |
921 | } |
922 | } | |
923 | ||
1dc01b9f | 924 | my $cache = {}; |
b6cf0a66 | 925 | |
1dc01b9f | 926 | activate_storage_list($cfg, $storage_list, $cache); |
b6cf0a66 DM |
927 | |
928 | foreach my $sid (keys %$ids) { | |
1dc01b9f DM |
929 | next if $storeid && $storeid ne $sid; |
930 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
b6cf0a66 | 931 | |
1dc01b9f DM |
932 | my $scfg = $ids->{$sid}; |
933 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
934 | $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache); | |
b6cf0a66 DM |
935 | @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid}; |
936 | } | |
937 | ||
938 | return $res; | |
939 | } | |
940 | ||
37ba0aea DM |
941 | sub volume_list { |
942 | my ($cfg, $storeid, $vmid, $content) = @_; | |
943 | ||
944 | my @ctypes = qw(images vztmpl iso backup); | |
945 | ||
946 | my $cts = $content ? [ $content ] : [ @ctypes ]; | |
947 | ||
948 | my $scfg = PVE::Storage::storage_config($cfg, $storeid); | |
949 | ||
950 | my $res = []; | |
951 | foreach my $ct (@$cts) { | |
952 | my $data; | |
953 | if ($ct eq 'images') { | |
954 | $data = vdisk_list($cfg, $storeid, $vmid); | |
955 | } elsif ($ct eq 'iso' && !defined($vmid)) { | |
956 | $data = template_list($cfg, $storeid, 'iso'); | |
957 | } elsif ($ct eq 'vztmpl'&& !defined($vmid)) { | |
958 | $data = template_list ($cfg, $storeid, 'vztmpl'); | |
959 | } elsif ($ct eq 'backup') { | |
960 | $data = template_list ($cfg, $storeid, 'backup'); | |
961 | foreach my $item (@{$data->{$storeid}}) { | |
962 | if (defined($vmid)) { | |
963 | @{$data->{$storeid}} = grep { $_->{volid} =~ m/\S+-$vmid-\S+/ } @{$data->{$storeid}}; | |
964 | } | |
965 | } | |
966 | } | |
967 | ||
968 | next if !$data || !$data->{$storeid}; | |
969 | ||
970 | foreach my $item (@{$data->{$storeid}}) { | |
971 | $item->{content} = $ct; | |
972 | push @$res, $item; | |
973 | } | |
974 | } | |
975 | ||
976 | return $res; | |
977 | } | |
978 | ||
b6cf0a66 DM |
979 | sub uevent_seqnum { |
980 | ||
981 | my $filename = "/sys/kernel/uevent_seqnum"; | |
982 | ||
983 | my $seqnum = 0; | |
1dc01b9f | 984 | if (my $fh = IO::File->new($filename, "r")) { |
b6cf0a66 DM |
985 | my $line = <$fh>; |
986 | if ($line =~ m/^(\d+)$/) { | |
1dc01b9f | 987 | $seqnum = int($1); |
b6cf0a66 DM |
988 | } |
989 | close ($fh); | |
990 | } | |
991 | return $seqnum; | |
992 | } | |
993 | ||
f3d4ef46 | 994 | sub activate_storage { |
1dc01b9f | 995 | my ($cfg, $storeid, $cache) = @_; |
b6cf0a66 | 996 | |
f3d4ef46 DM |
997 | $cache = {} if !$cache; |
998 | ||
b6cf0a66 DM |
999 | my $scfg = storage_check_enabled($cfg, $storeid); |
1000 | ||
1dc01b9f | 1001 | return if $cache->{activated}->{$storeid}; |
b6cf0a66 | 1002 | |
1dc01b9f | 1003 | $cache->{uevent_seqnum} = uevent_seqnum() if !$cache->{uevent_seqnum}; |
b6cf0a66 | 1004 | |
1dc01b9f | 1005 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
b6cf0a66 | 1006 | |
1dc01b9f DM |
1007 | if ($scfg->{base}) { |
1008 | my ($baseid, undef) = parse_volume_id ($scfg->{base}); | |
f3d4ef46 DM |
1009 | activate_storage($cfg, $baseid, $cache); |
1010 | } | |
1011 | ||
1012 | if (!$plugin->check_connection($storeid, $scfg)) { | |
1013 | die "storage '$storeid' is not online\n"; | |
b6cf0a66 DM |
1014 | } |
1015 | ||
1dc01b9f DM |
1016 | $plugin->activate_storage($storeid, $scfg, $cache); |
1017 | ||
b6cf0a66 DM |
1018 | my $newseq = uevent_seqnum (); |
1019 | ||
1020 | # only call udevsettle if there are events | |
1dc01b9f | 1021 | if ($newseq > $cache->{uevent_seqnum}) { |
b6cf0a66 DM |
1022 | my $timeout = 30; |
1023 | system ("$UDEVADM settle --timeout=$timeout"); # ignore errors | |
1dc01b9f | 1024 | $cache->{uevent_seqnum} = $newseq; |
b6cf0a66 DM |
1025 | } |
1026 | ||
1dc01b9f | 1027 | $cache->{activated}->{$storeid} = 1; |
b6cf0a66 DM |
1028 | } |
1029 | ||
1030 | sub activate_storage_list { | |
1dc01b9f | 1031 | my ($cfg, $storeid_list, $cache) = @_; |
b6cf0a66 | 1032 | |
1dc01b9f | 1033 | $cache = {} if !$cache; |
b6cf0a66 DM |
1034 | |
1035 | foreach my $storeid (@$storeid_list) { | |
f3d4ef46 | 1036 | activate_storage($cfg, $storeid, $cache); |
b6cf0a66 DM |
1037 | } |
1038 | } | |
1039 | ||
1dc01b9f DM |
1040 | sub deactivate_storage { |
1041 | my ($cfg, $storeid) = @_; | |
1042 | ||
1043 | my $scfg = storage_config ($cfg, $storeid); | |
1044 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b6cf0a66 | 1045 | |
1dc01b9f DM |
1046 | my $cache = {}; |
1047 | $plugin->deactivate_storage($storeid, $scfg, $cache); | |
b6cf0a66 DM |
1048 | } |
1049 | ||
1050 | sub activate_volumes { | |
02e797b8 | 1051 | my ($cfg, $vollist, $snapname) = @_; |
6703353b DM |
1052 | |
1053 | return if !($vollist && scalar(@$vollist)); | |
1054 | ||
b6cf0a66 DM |
1055 | my $storagehash = {}; |
1056 | foreach my $volid (@$vollist) { | |
1dc01b9f | 1057 | my ($storeid, undef) = parse_volume_id($volid); |
b6cf0a66 DM |
1058 | $storagehash->{$storeid} = 1; |
1059 | } | |
1060 | ||
1dc01b9f DM |
1061 | my $cache = {}; |
1062 | ||
1063 | activate_storage_list($cfg, [keys %$storagehash], $cache); | |
b6cf0a66 DM |
1064 | |
1065 | foreach my $volid (@$vollist) { | |
5521b580 | 1066 | my ($storeid, $volname) = parse_volume_id($volid); |
1dc01b9f DM |
1067 | my $scfg = storage_config($cfg, $storeid); |
1068 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
02e797b8 | 1069 | $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache); |
b6cf0a66 DM |
1070 | } |
1071 | } | |
1072 | ||
1073 | sub deactivate_volumes { | |
02e797b8 | 1074 | my ($cfg, $vollist, $snapname) = @_; |
b6cf0a66 | 1075 | |
6703353b DM |
1076 | return if !($vollist && scalar(@$vollist)); |
1077 | ||
1dc01b9f DM |
1078 | my $cache = {}; |
1079 | ||
6703353b | 1080 | my @errlist = (); |
b6cf0a66 | 1081 | foreach my $volid (@$vollist) { |
1dc01b9f | 1082 | my ($storeid, $volname) = parse_volume_id($volid); |
b6cf0a66 | 1083 | |
1dc01b9f DM |
1084 | my $scfg = storage_config($cfg, $storeid); |
1085 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1a3459ac | 1086 | |
1dc01b9f | 1087 | eval { |
02e797b8 | 1088 | $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache); |
1dc01b9f DM |
1089 | }; |
1090 | if (my $err = $@) { | |
1091 | warn $err; | |
1092 | push @errlist, $volid; | |
b6cf0a66 DM |
1093 | } |
1094 | } | |
6703353b | 1095 | |
82fc923f | 1096 | die "volume deactivation failed: " . join(' ', @errlist) |
6703353b | 1097 | if scalar(@errlist); |
b6cf0a66 DM |
1098 | } |
1099 | ||
1a3459ac | 1100 | sub storage_info { |
b6cf0a66 DM |
1101 | my ($cfg, $content) = @_; |
1102 | ||
1103 | my $ids = $cfg->{ids}; | |
1104 | ||
1105 | my $info = {}; | |
ff3badd8 | 1106 | |
583c2802 | 1107 | my @ctypes = PVE::Tools::split_list($content); |
ff3badd8 | 1108 | |
b6cf0a66 DM |
1109 | my $slist = []; |
1110 | foreach my $storeid (keys %$ids) { | |
1111 | ||
b6cf0a66 DM |
1112 | next if !storage_check_enabled($cfg, $storeid, undef, 1); |
1113 | ||
d73060be DM |
1114 | if (defined($content)) { |
1115 | my $want_ctype = 0; | |
1116 | foreach my $ctype (@ctypes) { | |
1117 | if ($ids->{$storeid}->{content}->{$ctype}) { | |
1118 | $want_ctype = 1; | |
1119 | last; | |
1120 | } | |
583c2802 | 1121 | } |
d73060be | 1122 | next if !$want_ctype; |
583c2802 | 1123 | } |
ff3badd8 | 1124 | |
b6cf0a66 DM |
1125 | my $type = $ids->{$storeid}->{type}; |
1126 | ||
1a3459ac | 1127 | $info->{$storeid} = { |
b6cf0a66 | 1128 | type => $type, |
1a3459ac DM |
1129 | total => 0, |
1130 | avail => 0, | |
1131 | used => 0, | |
04a2e4f3 | 1132 | shared => $ids->{$storeid}->{shared} ? 1 : 0, |
1dc01b9f | 1133 | content => PVE::Storage::Plugin::content_hash_to_string($ids->{$storeid}->{content}), |
b6cf0a66 DM |
1134 | active => 0, |
1135 | }; | |
1136 | ||
b6cf0a66 DM |
1137 | push @$slist, $storeid; |
1138 | } | |
1139 | ||
1dc01b9f | 1140 | my $cache = {}; |
b6cf0a66 | 1141 | |
b6cf0a66 DM |
1142 | foreach my $storeid (keys %$ids) { |
1143 | my $scfg = $ids->{$storeid}; | |
b6cf0a66 DM |
1144 | next if !$info->{$storeid}; |
1145 | ||
f3d4ef46 DM |
1146 | eval { activate_storage($cfg, $storeid, $cache); }; |
1147 | if (my $err = $@) { | |
1148 | warn $err; | |
1149 | next; | |
1150 | } | |
1151 | ||
1dc01b9f | 1152 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
7028645e DM |
1153 | my ($total, $avail, $used, $active); |
1154 | eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); }; | |
1155 | warn $@ if $@; | |
1dc01b9f | 1156 | next if !$active; |
ff3badd8 DM |
1157 | $info->{$storeid}->{total} = int($total); |
1158 | $info->{$storeid}->{avail} = int($avail); | |
1159 | $info->{$storeid}->{used} = int($used); | |
1dc01b9f | 1160 | $info->{$storeid}->{active} = $active; |
b6cf0a66 DM |
1161 | } |
1162 | ||
1163 | return $info; | |
1164 | } | |
1165 | ||
1166 | sub resolv_server { | |
1167 | my ($server) = @_; | |
1a3459ac | 1168 | |
c67daeac WB |
1169 | my ($packed_ip, $family); |
1170 | eval { | |
1171 | my @res = PVE::Tools::getaddrinfo_all($server); | |
1172 | $family = $res[0]->{family}; | |
1173 | $packed_ip = (PVE::Tools::unpack_sockaddr_in46($res[0]->{addr}))[2]; | |
1174 | }; | |
b6cf0a66 | 1175 | if (defined $packed_ip) { |
ee302b1c | 1176 | return Socket::inet_ntop($family, $packed_ip); |
b6cf0a66 DM |
1177 | } |
1178 | return undef; | |
1179 | } | |
1180 | ||
1181 | sub scan_nfs { | |
1182 | my ($server_in) = @_; | |
1183 | ||
1184 | my $server; | |
1185 | if (!($server = resolv_server ($server_in))) { | |
1186 | die "unable to resolve address for server '${server_in}'\n"; | |
1187 | } | |
1188 | ||
1189 | my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server]; | |
1190 | ||
1191 | my $res = {}; | |
f81372ac | 1192 | run_command($cmd, outfunc => sub { |
b6cf0a66 DM |
1193 | my $line = shift; |
1194 | ||
1195 | # note: howto handle white spaces in export path?? | |
1196 | if ($line =~ m!^(/\S+)\s+(.+)$!) { | |
1197 | $res->{$1} = $2; | |
1198 | } | |
1199 | }); | |
1200 | ||
1201 | return $res; | |
1202 | } | |
1203 | ||
584d97f6 DM |
1204 | sub scan_zfs { |
1205 | ||
3932390b | 1206 | my $cmd = ['zfs', 'list', '-t', 'filesystem', '-H', '-o', 'name,avail,used']; |
584d97f6 DM |
1207 | |
1208 | my $res = []; | |
1209 | run_command($cmd, outfunc => sub { | |
1210 | my $line = shift; | |
1211 | ||
1212 | if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) { | |
3932390b | 1213 | my ($pool, $size_str, $used_str) = ($1, $2, $3); |
584d97f6 | 1214 | my $size = PVE::Storage::ZFSPoolPlugin::zfs_parse_size($size_str); |
3932390b | 1215 | my $used = PVE::Storage::ZFSPoolPlugin::zfs_parse_size($used_str); |
48e27f79 | 1216 | # ignore subvolumes generated by our ZFSPoolPlugin |
851658c3 WL |
1217 | return if $pool =~ m!/subvol-\d+-[^/]+$!; |
1218 | return if $pool =~ m!/basevol-\d+-[^/]+$!; | |
3932390b | 1219 | push @$res, { pool => $pool, size => $size, free => $size-$used }; |
584d97f6 DM |
1220 | } |
1221 | }); | |
1222 | ||
1223 | return $res; | |
1224 | } | |
1225 | ||
b6cf0a66 DM |
1226 | sub resolv_portal { |
1227 | my ($portal, $noerr) = @_; | |
1228 | ||
1689e627 WB |
1229 | my ($server, $port) = PVE::Tools::parse_host_and_port($portal); |
1230 | if ($server) { | |
b6cf0a66 DM |
1231 | if (my $ip = resolv_server($server)) { |
1232 | $server = $ip; | |
1689e627 | 1233 | $server = "[$server]" if $server =~ /^$IPV6RE$/; |
b6cf0a66 DM |
1234 | return $port ? "$server:$port" : $server; |
1235 | } | |
1236 | } | |
1237 | return undef if $noerr; | |
1238 | ||
1239 | raise_param_exc({ portal => "unable to resolve portal address '$portal'" }); | |
1240 | } | |
1241 | ||
1242 | # idea is from usbutils package (/usr/bin/usb-devices) script | |
1243 | sub __scan_usb_device { | |
1244 | my ($res, $devpath, $parent, $level) = @_; | |
1245 | ||
1246 | return if ! -d $devpath; | |
1247 | return if $level && $devpath !~ m/^.*[-.](\d+)$/; | |
1248 | my $port = $level ? int($1 - 1) : 0; | |
1249 | ||
1250 | my $busnum = int(file_read_firstline("$devpath/busnum")); | |
1251 | my $devnum = int(file_read_firstline("$devpath/devnum")); | |
1252 | ||
1253 | my $d = { | |
1254 | port => $port, | |
1255 | level => $level, | |
1256 | busnum => $busnum, | |
1257 | devnum => $devnum, | |
1258 | speed => file_read_firstline("$devpath/speed"), | |
1259 | class => hex(file_read_firstline("$devpath/bDeviceClass")), | |
1260 | vendid => file_read_firstline("$devpath/idVendor"), | |
1261 | prodid => file_read_firstline("$devpath/idProduct"), | |
1262 | }; | |
1263 | ||
1264 | if ($level) { | |
1265 | my $usbpath = $devpath; | |
1266 | $usbpath =~ s|^.*/\d+\-||; | |
1267 | $d->{usbpath} = $usbpath; | |
1268 | } | |
1269 | ||
1270 | my $product = file_read_firstline("$devpath/product"); | |
1271 | $d->{product} = $product if $product; | |
1a3459ac | 1272 | |
b6cf0a66 DM |
1273 | my $manu = file_read_firstline("$devpath/manufacturer"); |
1274 | $d->{manufacturer} = $manu if $manu; | |
1275 | ||
1276 | my $serial => file_read_firstline("$devpath/serial"); | |
1277 | $d->{serial} = $serial if $serial; | |
1278 | ||
1279 | push @$res, $d; | |
1280 | ||
1281 | foreach my $subdev (<$devpath/$busnum-*>) { | |
1282 | next if $subdev !~ m|/$busnum-[0-9]+(\.[0-9]+)*$|; | |
1283 | __scan_usb_device($res, $subdev, $devnum, $level + 1); | |
1284 | } | |
1285 | ||
1286 | }; | |
1287 | ||
1288 | sub scan_usb { | |
1289 | ||
1290 | my $devlist = []; | |
1291 | ||
1292 | foreach my $device (</sys/bus/usb/devices/usb*>) { | |
1293 | __scan_usb_device($devlist, $device, 0, 0); | |
1294 | } | |
1295 | ||
1296 | return $devlist; | |
1297 | } | |
1298 | ||
1299 | sub scan_iscsi { | |
1300 | my ($portal_in) = @_; | |
1301 | ||
1302 | my $portal; | |
1dc01b9f | 1303 | if (!($portal = resolv_portal($portal_in))) { |
b6cf0a66 DM |
1304 | die "unable to parse/resolve portal address '${portal_in}'\n"; |
1305 | } | |
1306 | ||
1dc01b9f | 1307 | return PVE::Storage::ISCSIPlugin::iscsi_discovery($portal); |
b6cf0a66 DM |
1308 | } |
1309 | ||
1310 | sub storage_default_format { | |
1311 | my ($cfg, $storeid) = @_; | |
1312 | ||
1313 | my $scfg = storage_config ($cfg, $storeid); | |
1314 | ||
1dc01b9f | 1315 | return PVE::Storage::Plugin::default_format($scfg); |
b6cf0a66 DM |
1316 | } |
1317 | ||
1318 | sub vgroup_is_used { | |
1319 | my ($cfg, $vgname) = @_; | |
1320 | ||
1321 | foreach my $storeid (keys %{$cfg->{ids}}) { | |
1dc01b9f | 1322 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
1323 | if ($scfg->{type} eq 'lvm' && $scfg->{vgname} eq $vgname) { |
1324 | return 1; | |
1325 | } | |
1326 | } | |
1327 | ||
1328 | return undef; | |
1329 | } | |
1330 | ||
1331 | sub target_is_used { | |
1332 | my ($cfg, $target) = @_; | |
1333 | ||
1334 | foreach my $storeid (keys %{$cfg->{ids}}) { | |
1dc01b9f | 1335 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
1336 | if ($scfg->{type} eq 'iscsi' && $scfg->{target} eq $target) { |
1337 | return 1; | |
1338 | } | |
1339 | } | |
1340 | ||
1341 | return undef; | |
1342 | } | |
1343 | ||
1344 | sub volume_is_used { | |
1345 | my ($cfg, $volid) = @_; | |
1346 | ||
1347 | foreach my $storeid (keys %{$cfg->{ids}}) { | |
1dc01b9f | 1348 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
1349 | if ($scfg->{base} && $scfg->{base} eq $volid) { |
1350 | return 1; | |
1351 | } | |
1352 | } | |
1353 | ||
1354 | return undef; | |
1355 | } | |
1356 | ||
1357 | sub storage_is_used { | |
1358 | my ($cfg, $storeid) = @_; | |
1359 | ||
1360 | foreach my $sid (keys %{$cfg->{ids}}) { | |
1dc01b9f | 1361 | my $scfg = storage_config($cfg, $sid); |
b6cf0a66 | 1362 | next if !$scfg->{base}; |
1dc01b9f | 1363 | my ($st) = parse_volume_id($scfg->{base}); |
b6cf0a66 DM |
1364 | return 1 if $st && $st eq $storeid; |
1365 | } | |
1366 | ||
1367 | return undef; | |
1368 | } | |
1369 | ||
1370 | sub foreach_volid { | |
1371 | my ($list, $func) = @_; | |
1372 | ||
1373 | return if !$list; | |
1374 | ||
1375 | foreach my $sid (keys %$list) { | |
1376 | foreach my $info (@{$list->{$sid}}) { | |
1377 | my $volid = $info->{volid}; | |
1dc01b9f | 1378 | my ($sid1, $volname) = parse_volume_id($volid, 1); |
b6cf0a66 DM |
1379 | if ($sid1 && $sid1 eq $sid) { |
1380 | &$func ($volid, $sid, $info); | |
1381 | } else { | |
1382 | warn "detected strange volid '$volid' in volume list for '$sid'\n"; | |
1383 | } | |
1384 | } | |
1385 | } | |
1386 | } | |
1387 | ||
8898dd7b FG |
1388 | sub extract_vzdump_config_tar { |
1389 | my ($archive, $conf_re) = @_; | |
1390 | ||
1391 | die "ERROR: file '$archive' does not exist\n" if ! -f $archive; | |
1392 | ||
1393 | my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) || | |
1394 | die "unable to open file '$archive'\n"; | |
1395 | ||
1396 | my $file; | |
1397 | while (defined($file = <$fh>)) { | |
086c4bf1 | 1398 | if ($file =~ $conf_re) { |
8898dd7b FG |
1399 | $file = $1; # untaint |
1400 | last; | |
1401 | } | |
1402 | } | |
1403 | ||
1404 | kill 15, $pid; | |
1405 | waitpid $pid, 0; | |
1406 | close $fh; | |
1407 | ||
1408 | die "ERROR: archive contains no configuration file\n" if !$file; | |
1409 | chomp $file; | |
1410 | ||
1411 | my $raw = ''; | |
1412 | my $out = sub { | |
1413 | my $output = shift; | |
1414 | $raw .= "$output\n"; | |
1415 | }; | |
1416 | ||
1417 | PVE::Tools::run_command(['tar', '-xpOf', $archive, $file, '--occurrence'], outfunc => $out); | |
1418 | ||
1419 | return wantarray ? ($raw, $file) : $raw; | |
1420 | } | |
1421 | ||
1422 | sub extract_vzdump_config_vma { | |
1423 | my ($archive, $comp) = @_; | |
1424 | ||
1425 | my $cmd; | |
1426 | my $raw = ''; | |
1427 | my $out = sub { | |
1428 | my $output = shift; | |
1429 | $raw .= "$output\n"; | |
1430 | }; | |
1431 | ||
1432 | ||
1433 | if ($comp) { | |
1434 | my $uncomp; | |
1435 | if ($comp eq 'gz') { | |
1436 | $uncomp = ["zcat", $archive]; | |
1437 | } elsif ($comp eq 'lzo') { | |
1438 | $uncomp = ["lzop", "-d", "-c", $archive]; | |
1439 | } else { | |
1440 | die "unknown compression method '$comp'\n"; | |
1441 | } | |
1442 | $cmd = [$uncomp, ["vma", "config", "-"]]; | |
1443 | ||
1444 | # in some cases, lzop/zcat exits with 1 when its stdout pipe is | |
1445 | # closed early by vma, detect this and ignore the exit code later | |
1446 | my $broken_pipe; | |
1447 | my $errstring; | |
1448 | my $err = sub { | |
1449 | my $output = shift; | |
1450 | if ($output =~ m/lzop: Broken pipe: <stdout>/ || $output =~ m/gzip: stdout: Broken pipe/) { | |
1451 | $broken_pipe = 1; | |
1452 | } elsif (!defined ($errstring) && $output !~ m/^\s*$/) { | |
1453 | $errstring = "Failed to extract config from VMA archive: $output\n"; | |
1454 | } | |
1455 | }; | |
1456 | ||
1457 | # in other cases, the pipeline will exit with exit code 141 | |
1458 | # because of the broken pipe, handle / ignore this as well | |
1459 | my $rc; | |
1460 | eval { | |
1461 | $rc = PVE::Tools::run_command($cmd, outfunc => $out, errfunc => $err, noerr => 1); | |
1462 | }; | |
1463 | my $rerr = $@; | |
1464 | ||
1465 | # use exit code if no stderr output and not just broken pipe | |
fc1089fc | 1466 | if (!$errstring && !$broken_pipe && $rc != 0 && $rc != 141) { |
8898dd7b FG |
1467 | die "$rerr\n" if $rerr; |
1468 | die "config extraction failed with exit code $rc\n"; | |
1469 | } | |
1470 | die "$errstring\n" if $errstring; | |
1471 | } else { | |
1472 | # simple case without compression and weird piping behaviour | |
1473 | PVE::Tools::run_command(["vma", "config", $archive], outfunc => $out); | |
1474 | } | |
1475 | ||
1476 | return wantarray ? ($raw, undef) : $raw; | |
1477 | } | |
1478 | ||
1479 | sub extract_vzdump_config { | |
1480 | my ($cfg, $volid) = @_; | |
1481 | ||
1482 | my $archive = abs_filesystem_path($cfg, $volid); | |
1483 | ||
89443394 | 1484 | if ($volid =~ /vzdump-(lxc|openvz)-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|(tar(\.(gz|lzo))?))$/) { |
086c4bf1 | 1485 | return extract_vzdump_config_tar($archive, qr!^(\./etc/vzdump/(pct|vps)\.conf)$!); |
89443394 | 1486 | } elsif ($volid =~ /vzdump-qemu-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|((tar|vma)(\.(gz|lzo))?))$/) { |
8898dd7b FG |
1487 | my $format; |
1488 | my $comp; | |
1489 | if ($7 eq 'tgz') { | |
1490 | $format = 'tar'; | |
1491 | $comp = 'gz'; | |
1492 | } else { | |
1493 | $format = $9; | |
1494 | $comp = $11 if defined($11); | |
1495 | } | |
1496 | ||
1497 | if ($format eq 'tar') { | |
1498 | return extract_vzdump_config_tar($archive, qr!\(\./qemu-server\.conf\)!); | |
1499 | } else { | |
1500 | return extract_vzdump_config_vma($archive, $comp); | |
1501 | } | |
1502 | } else { | |
1503 | die "cannot determine backup guest type for backup archive '$volid'\n"; | |
1504 | } | |
1505 | } | |
1506 | ||
47f37b53 WB |
1507 | sub volume_export { |
1508 | my ($cfg, $fh, $volid, $format, $snapshot, $base_snapshot, $with_snapshots) = @_; | |
1509 | ||
1510 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
1511 | die "cannot export volume '$volid'\n" if !$storeid; | |
1512 | my $scfg = storage_config($cfg, $storeid); | |
1513 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1514 | return $plugin->volume_export($scfg, $storeid, $fh, $volname, $format, | |
1515 | $snapshot, $base_snapshot, $with_snapshots); | |
1516 | } | |
1517 | ||
1518 | sub volume_import { | |
1519 | my ($cfg, $fh, $volid, $format, $base_snapshot, $with_snapshots) = @_; | |
1520 | ||
1521 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
1522 | die "cannot import into volume '$volid'\n" if !$storeid; | |
1523 | my $scfg = storage_config($cfg, $storeid); | |
1524 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1525 | return $plugin->volume_import($scfg, $storeid, $fh, $volname, $format, | |
1526 | $base_snapshot, $with_snapshots); | |
1527 | } | |
1528 | ||
d390328b WB |
1529 | sub volume_export_formats { |
1530 | my ($cfg, $volid, $snapshot, $base_snapshot, $with_snapshots) = @_; | |
1531 | ||
1532 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
1533 | return if !$storeid; | |
1534 | my $scfg = storage_config($cfg, $storeid); | |
1535 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1536 | return $plugin->volume_export_formats($scfg, $storeid, $volname, | |
1537 | $base_snapshot, $with_snapshots); | |
1538 | } | |
1539 | ||
1540 | sub volume_import_formats { | |
1541 | my ($cfg, $volid, $base_snapshot, $with_snapshots) = @_; | |
1542 | ||
1543 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
1544 | return if !$storeid; | |
1545 | my $scfg = storage_config($cfg, $storeid); | |
1546 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1547 | return $plugin->volume_import_formats($scfg, $storeid, $volname, | |
1548 | $base_snapshot, $with_snapshots); | |
1549 | } | |
1550 | ||
1551 | sub volume_transfer_formats { | |
1552 | my ($cfg, $src_volid, $dst_volid, $snapshot, $base_snapshot, $with_snapshots) = @_; | |
1553 | my @export_formats = volume_export_formats($cfg, $src_volid, $snapshot, $base_snapshot, $with_snapshots); | |
1554 | my @import_formats = volume_import_formats($cfg, $dst_volid, $base_snapshot, $with_snapshots); | |
1555 | my %import_hash = map { $_ => 1 } @import_formats; | |
1556 | my @common = grep { $import_hash{$_} } @export_formats; | |
1557 | return @common; | |
1558 | } | |
1559 | ||
f7621c01 DM |
1560 | # bash completion helper |
1561 | ||
1562 | sub complete_storage { | |
746e530f | 1563 | my ($cmdname, $pname, $cvalue) = @_; |
f7621c01 | 1564 | |
746e530f | 1565 | my $cfg = PVE::Storage::config(); |
180c8b02 | 1566 | |
746e530f | 1567 | return $cmdname eq 'add' ? [] : [ PVE::Storage::storage_ids($cfg) ]; |
f7621c01 DM |
1568 | } |
1569 | ||
1570 | sub complete_storage_enabled { | |
746e530f | 1571 | my ($cmdname, $pname, $cvalue) = @_; |
f7621c01 | 1572 | |
746e530f | 1573 | my $res = []; |
f7621c01 | 1574 | |
746e530f DM |
1575 | my $cfg = PVE::Storage::config(); |
1576 | foreach my $sid (keys %{$cfg->{ids}}) { | |
1577 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
1578 | push @$res, $sid; | |
1579 | } | |
1580 | return $res; | |
f7621c01 DM |
1581 | } |
1582 | ||
98437f4c DM |
1583 | sub complete_content_type { |
1584 | my ($cmdname, $pname, $cvalue) = @_; | |
1585 | ||
1586 | return [qw(rootdir images vztmpl iso backup)]; | |
1587 | } | |
1588 | ||
bf7aed26 DM |
1589 | sub complete_volume { |
1590 | my ($cmdname, $pname, $cvalue) = @_; | |
1591 | ||
1592 | my $cfg = config(); | |
1593 | ||
1594 | my $storage_list = complete_storage_enabled(); | |
1595 | ||
b70b0c58 DM |
1596 | if ($cvalue =~ m/^([^:]+):/) { |
1597 | $storage_list = [ $1 ]; | |
1598 | } else { | |
1599 | if (scalar(@$storage_list) > 1) { | |
1600 | # only list storage IDs to avoid large listings | |
1601 | my $res = []; | |
1602 | foreach my $storeid (@$storage_list) { | |
1603 | # Hack: simply return 2 artificial values, so that | |
1604 | # completions does not finish | |
1605 | push @$res, "$storeid:volname", "$storeid:..."; | |
1606 | } | |
1607 | return $res; | |
1608 | } | |
1609 | } | |
1610 | ||
bf7aed26 DM |
1611 | my $res = []; |
1612 | foreach my $storeid (@$storage_list) { | |
1613 | my $vollist = PVE::Storage::volume_list($cfg, $storeid); | |
1614 | ||
1615 | foreach my $item (@$vollist) { | |
1616 | push @$res, $item->{volid}; | |
1617 | } | |
1618 | } | |
1619 | ||
1620 | return $res; | |
1621 | } | |
1622 | ||
b6cf0a66 | 1623 | 1; |