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