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