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