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; |
57acd6a1 | 11 | use IPC::Open3; |
b6cf0a66 DM |
12 | use File::Basename; |
13 | use File::Path; | |
b6cf0a66 | 14 | use Cwd 'abs_path'; |
7a2d5c1a | 15 | use Socket; |
fb821c18 | 16 | use Time::Local qw(timelocal); |
b6cf0a66 | 17 | |
4dee23d3 | 18 | use PVE::Tools qw(run_command file_read_firstline dir_glob_foreach $IPV6RE); |
83d7192f | 19 | use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file); |
2f08fb4b | 20 | use PVE::DataCenterConfig; |
be18d6da | 21 | use PVE::Exception qw(raise_param_exc raise); |
b6cf0a66 DM |
22 | use PVE::JSONSchema; |
23 | use PVE::INotify; | |
88c3abaf | 24 | use PVE::RPCEnvironment; |
65bb9859 | 25 | use PVE::SSHInfo; |
b6cf0a66 | 26 | |
1dc01b9f DM |
27 | use PVE::Storage::Plugin; |
28 | use PVE::Storage::DirPlugin; | |
29 | use PVE::Storage::LVMPlugin; | |
610798bc | 30 | use PVE::Storage::LvmThinPlugin; |
1dc01b9f | 31 | use PVE::Storage::NFSPlugin; |
d7875239 | 32 | use PVE::Storage::CIFSPlugin; |
1dc01b9f | 33 | use PVE::Storage::ISCSIPlugin; |
0509010d | 34 | use PVE::Storage::RBDPlugin; |
e34ce144 | 35 | use PVE::Storage::CephFSPlugin; |
86616554 | 36 | use PVE::Storage::ISCSIDirectPlugin; |
f4648aef | 37 | use PVE::Storage::GlusterfsPlugin; |
85fda4dd | 38 | use PVE::Storage::ZFSPoolPlugin; |
4f914e6e | 39 | use PVE::Storage::ZFSPlugin; |
271fe394 | 40 | use PVE::Storage::PBSPlugin; |
af50c2e6 | 41 | use PVE::Storage::BTRFSPlugin; |
b6cf0a66 | 42 | |
af2dd59e | 43 | # Storage API version. Increment it on changes in storage API interface. |
3cc29a04 | 44 | use constant APIVER => 9; |
042dd4be WB |
45 | # Age is the number of versions we're backward compatible with. |
46 | # This is like having 'current=APIVER' and age='APIAGE' in libtool, | |
47 | # see https://www.gnu.org/software/libtool/manual/html_node/Libtool-versioning.html | |
3cc29a04 | 48 | use constant APIAGE => 0; |
4dee23d3 DP |
49 | |
50 | # load standard plugins | |
1dc01b9f DM |
51 | PVE::Storage::DirPlugin->register(); |
52 | PVE::Storage::LVMPlugin->register(); | |
610798bc | 53 | PVE::Storage::LvmThinPlugin->register(); |
1dc01b9f | 54 | PVE::Storage::NFSPlugin->register(); |
d7875239 | 55 | PVE::Storage::CIFSPlugin->register(); |
1dc01b9f | 56 | PVE::Storage::ISCSIPlugin->register(); |
0509010d | 57 | PVE::Storage::RBDPlugin->register(); |
e34ce144 | 58 | PVE::Storage::CephFSPlugin->register(); |
86616554 | 59 | PVE::Storage::ISCSIDirectPlugin->register(); |
f4648aef | 60 | PVE::Storage::GlusterfsPlugin->register(); |
85fda4dd | 61 | PVE::Storage::ZFSPoolPlugin->register(); |
4f914e6e | 62 | PVE::Storage::ZFSPlugin->register(); |
271fe394 | 63 | PVE::Storage::PBSPlugin->register(); |
af50c2e6 | 64 | PVE::Storage::BTRFSPlugin->register(); |
4dee23d3 DP |
65 | |
66 | # load third-party plugins | |
67 | if ( -d '/usr/share/perl5/PVE/Storage/Custom' ) { | |
68 | dir_glob_foreach('/usr/share/perl5/PVE/Storage/Custom', '.*\.pm$', sub { | |
69 | my ($file) = @_; | |
70 | my $modname = 'PVE::Storage::Custom::' . $file; | |
71 | $modname =~ s!\.pm$!!; | |
72 | $file = 'PVE/Storage/Custom/' . $file; | |
73 | ||
74 | eval { | |
75 | require $file; | |
042dd4be WB |
76 | |
77 | # Check perl interface: | |
823e8afe TL |
78 | die "not derived from PVE::Storage::Plugin\n" if !$modname->isa('PVE::Storage::Plugin'); |
79 | die "does not provide an api() method\n" if !$modname->can('api'); | |
042dd4be WB |
80 | # Check storage API version and that file is really storage plugin. |
81 | my $version = $modname->api(); | |
a0908caa | 82 | die "implements an API version newer than current ($version > " . APIVER . ")\n" |
042dd4be | 83 | if $version > APIVER; |
a0908caa TL |
84 | my $min_version = (APIVER - APIAGE); |
85 | die "API version too old, please update the plugin ($version < $min_version)\n" | |
86 | if $version < $min_version; | |
823e8afe | 87 | # all OK, do import and register (i.e., "use") |
042dd4be WB |
88 | import $file; |
89 | $modname->register(); | |
90 | ||
823e8afe | 91 | # If we got this far and the API version is not the same, make some noise: |
042dd4be WB |
92 | warn "Plugin \"$modname\" is implementing an older storage API, an upgrade is recommended\n" |
93 | if $version != APIVER; | |
4dee23d3 DP |
94 | }; |
95 | if ($@) { | |
042dd4be | 96 | warn "Error loading storage plugin \"$modname\": $@"; |
4dee23d3 DP |
97 | } |
98 | }); | |
99 | } | |
100 | ||
101 | # initialize all plugins | |
1dc01b9f | 102 | PVE::Storage::Plugin->init(); |
b6cf0a66 | 103 | |
b295e05e | 104 | our $iso_extension_re = qr/\.(?:iso|img)/i; |
4c693491 | 105 | |
2caa1194 | 106 | our $vztmpl_extension_re = qr/\.tar\.(gz|xz|zst)/i; |
bba10cf4 | 107 | |
1dc01b9f | 108 | # PVE::Storage utility functions |
b6cf0a66 DM |
109 | |
110 | sub config { | |
111 | return cfs_read_file("storage.cfg"); | |
112 | } | |
113 | ||
83d7192f FG |
114 | sub write_config { |
115 | my ($cfg) = @_; | |
116 | ||
117 | cfs_write_file('storage.cfg', $cfg); | |
118 | } | |
119 | ||
b6cf0a66 DM |
120 | sub lock_storage_config { |
121 | my ($code, $errmsg) = @_; | |
122 | ||
123 | cfs_lock_file("storage.cfg", undef, $code); | |
124 | my $err = $@; | |
125 | if ($err) { | |
126 | $errmsg ? die "$errmsg: $err" : die $err; | |
127 | } | |
128 | } | |
129 | ||
bbadd165 | 130 | # FIXME remove maxfiles for PVE 8.0 or PVE 9.0 |
d4e8a1f2 FE |
131 | my $convert_maxfiles_to_prune_backups = sub { |
132 | my ($scfg) = @_; | |
133 | ||
134 | return if !$scfg; | |
135 | ||
136 | my $maxfiles = delete $scfg->{maxfiles}; | |
137 | ||
138 | if (!defined($scfg->{'prune-backups'}) && defined($maxfiles)) { | |
139 | my $prune_backups; | |
140 | if ($maxfiles) { | |
141 | $prune_backups = { 'keep-last' => $maxfiles }; | |
142 | } else { # maxfiles 0 means no limit | |
143 | $prune_backups = { 'keep-all' => 1 }; | |
144 | } | |
145 | $scfg->{'prune-backups'} = PVE::JSONSchema::print_property_string( | |
146 | $prune_backups, | |
147 | 'prune-backups' | |
148 | ); | |
149 | } | |
150 | }; | |
151 | ||
b6cf0a66 DM |
152 | sub storage_config { |
153 | my ($cfg, $storeid, $noerr) = @_; | |
154 | ||
82fc923f | 155 | die "no storage ID specified\n" if !$storeid; |
1a3459ac | 156 | |
b6cf0a66 DM |
157 | my $scfg = $cfg->{ids}->{$storeid}; |
158 | ||
481f6177 | 159 | die "storage '$storeid' does not exist\n" if (!$noerr && !$scfg); |
b6cf0a66 | 160 | |
d4e8a1f2 FE |
161 | $convert_maxfiles_to_prune_backups->($scfg); |
162 | ||
b6cf0a66 DM |
163 | return $scfg; |
164 | } | |
165 | ||
166 | sub storage_check_node { | |
167 | my ($cfg, $storeid, $node, $noerr) = @_; | |
168 | ||
1dc01b9f | 169 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
170 | |
171 | if ($scfg->{nodes}) { | |
172 | $node = PVE::INotify::nodename() if !$node || ($node eq 'localhost'); | |
173 | if (!$scfg->{nodes}->{$node}) { | |
da156fb3 | 174 | die "storage '$storeid' is not available on node '$node'\n" if !$noerr; |
b6cf0a66 DM |
175 | return undef; |
176 | } | |
177 | } | |
178 | ||
179 | return $scfg; | |
180 | } | |
181 | ||
182 | sub storage_check_enabled { | |
183 | my ($cfg, $storeid, $node, $noerr) = @_; | |
184 | ||
1dc01b9f | 185 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
186 | |
187 | if ($scfg->{disable}) { | |
188 | die "storage '$storeid' is disabled\n" if !$noerr; | |
189 | return undef; | |
190 | } | |
191 | ||
192 | return storage_check_node($cfg, $storeid, $node, $noerr); | |
193 | } | |
194 | ||
7118dd91 DM |
195 | # storage_can_replicate: |
196 | # return true if storage supports replication | |
ffc31266 | 197 | # (volumes allocated with vdisk_alloc() has replication feature) |
7118dd91 DM |
198 | sub storage_can_replicate { |
199 | my ($cfg, $storeid, $format) = @_; | |
200 | ||
201 | my $scfg = storage_config($cfg, $storeid); | |
202 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
203 | return $plugin->storage_can_replicate($scfg, $storeid, $format); | |
204 | } | |
205 | ||
b6cf0a66 DM |
206 | sub storage_ids { |
207 | my ($cfg) = @_; | |
208 | ||
1dc01b9f | 209 | return keys %{$cfg->{ids}}; |
b6cf0a66 DM |
210 | } |
211 | ||
1dc01b9f DM |
212 | sub file_size_info { |
213 | my ($filename, $timeout) = @_; | |
b6cf0a66 | 214 | |
1dc01b9f | 215 | return PVE::Storage::Plugin::file_size_info($filename, $timeout); |
b6cf0a66 DM |
216 | } |
217 | ||
e9991d26 DC |
218 | sub get_volume_notes { |
219 | my ($cfg, $volid, $timeout) = @_; | |
220 | ||
221 | my ($storeid, $volname) = parse_volume_id($volid); | |
222 | my $scfg = storage_config($cfg, $storeid); | |
223 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
224 | ||
225 | return $plugin->get_volume_notes($scfg, $storeid, $volname, $timeout); | |
226 | } | |
227 | ||
228 | sub update_volume_notes { | |
229 | my ($cfg, $volid, $notes, $timeout) = @_; | |
230 | ||
231 | my ($storeid, $volname) = parse_volume_id($volid); | |
232 | my $scfg = storage_config($cfg, $storeid); | |
233 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
234 | ||
235 | $plugin->update_volume_notes($scfg, $storeid, $volname, $notes, $timeout); | |
236 | } | |
237 | ||
20ccac1b AD |
238 | sub volume_size_info { |
239 | my ($cfg, $volid, $timeout) = @_; | |
240 | ||
f18199e5 DM |
241 | my ($storeid, $volname) = parse_volume_id($volid, 1); |
242 | if ($storeid) { | |
243 | my $scfg = storage_config($cfg, $storeid); | |
244 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
245 | return $plugin->volume_size_info($scfg, $storeid, $volname, $timeout); | |
246 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
247 | return file_size_info($volid, $timeout); | |
248 | } else { | |
249 | return 0; | |
250 | } | |
20ccac1b AD |
251 | } |
252 | ||
7e6c05dc AD |
253 | sub volume_resize { |
254 | my ($cfg, $volid, $size, $running) = @_; | |
255 | ||
c7215573 FE |
256 | my $padding = (1024 - $size % 1024) % 1024; |
257 | $size = $size + $padding; | |
258 | ||
7e6c05dc AD |
259 | my ($storeid, $volname) = parse_volume_id($volid, 1); |
260 | if ($storeid) { | |
261 | my $scfg = storage_config($cfg, $storeid); | |
262 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
263 | return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running); | |
264 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
f824c722 | 265 | die "resize file/device '$volid' is not possible\n"; |
7e6c05dc | 266 | } else { |
f824c722 | 267 | die "unable to parse volume ID '$volid'\n"; |
7e6c05dc AD |
268 | } |
269 | } | |
270 | ||
1597f1f9 | 271 | sub volume_rollback_is_possible { |
9a5d5095 | 272 | my ($cfg, $volid, $snap, $blockers) = @_; |
e0852ba7 | 273 | |
1597f1f9 WL |
274 | my ($storeid, $volname) = parse_volume_id($volid, 1); |
275 | if ($storeid) { | |
276 | my $scfg = storage_config($cfg, $storeid); | |
277 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
9a5d5095 | 278 | return $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap, $blockers); |
1597f1f9 | 279 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { |
f824c722 | 280 | die "snapshot rollback file/device '$volid' is not possible\n"; |
1597f1f9 | 281 | } else { |
f824c722 | 282 | die "unable to parse volume ID '$volid'\n"; |
1597f1f9 WL |
283 | } |
284 | } | |
285 | ||
db60719c | 286 | sub volume_snapshot { |
f5640e7d | 287 | my ($cfg, $volid, $snap) = @_; |
db60719c AD |
288 | |
289 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
290 | if ($storeid) { | |
291 | my $scfg = storage_config($cfg, $storeid); | |
292 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
f5640e7d | 293 | return $plugin->volume_snapshot($scfg, $storeid, $volname, $snap); |
db60719c | 294 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { |
f824c722 | 295 | die "snapshot file/device '$volid' is not possible\n"; |
db60719c | 296 | } else { |
f824c722 | 297 | die "unable to parse volume ID '$volid'\n"; |
db60719c AD |
298 | } |
299 | } | |
300 | ||
22a2a633 AD |
301 | sub volume_snapshot_rollback { |
302 | my ($cfg, $volid, $snap) = @_; | |
303 | ||
304 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
305 | if ($storeid) { | |
306 | my $scfg = storage_config($cfg, $storeid); | |
307 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b3f302c6 | 308 | $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap); |
22a2a633 AD |
309 | return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap); |
310 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
f824c722 | 311 | die "snapshot rollback file/device '$volid' is not possible\n"; |
22a2a633 | 312 | } else { |
f824c722 | 313 | die "unable to parse volume ID '$volid'\n"; |
22a2a633 AD |
314 | } |
315 | } | |
316 | ||
5753c9d1 AD |
317 | sub volume_snapshot_delete { |
318 | my ($cfg, $volid, $snap, $running) = @_; | |
319 | ||
320 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
321 | if ($storeid) { | |
322 | my $scfg = storage_config($cfg, $storeid); | |
323 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
27cc55d4 | 324 | return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running); |
5753c9d1 | 325 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { |
f824c722 | 326 | die "snapshot delete file/device '$volid' is not possible\n"; |
5753c9d1 | 327 | } else { |
f824c722 | 328 | die "unable to parse volume ID '$volid'\n"; |
5753c9d1 AD |
329 | } |
330 | } | |
331 | ||
2c036838 SI |
332 | # check if a filesystem on top of a volume needs to flush its journal for |
333 | # consistency (see fsfreeze(8)) before a snapshot is taken - needed for | |
334 | # container mountpoints | |
335 | sub volume_snapshot_needs_fsfreeze { | |
336 | my ($cfg, $volid) = @_; | |
337 | ||
338 | my ($storeid, $volname) = parse_volume_id($volid); | |
339 | my $scfg = storage_config($cfg, $storeid); | |
340 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
341 | return $plugin->volume_snapshot_needs_fsfreeze(); | |
342 | } | |
343 | ||
2668adce FE |
344 | # check if a volume or snapshot supports a given feature |
345 | # $feature - one of: | |
346 | # clone - linked clone is possible | |
347 | # copy - full clone is possible | |
348 | # replicate - replication is possible | |
349 | # snapshot - taking a snapshot is possible | |
350 | # sparseinit - volume is sparsely initialized | |
351 | # template - conversion to base image is possible | |
352 | # $snap - check if the feature is supported for a given snapshot | |
353 | # $running - if the guest owning the volume is running | |
354 | # $opts - hash with further options: | |
355 | # valid_target_formats - list of formats for the target of a copy/clone | |
356 | # operation that the caller could work with. The | |
357 | # format of $volid is always considered valid and if | |
358 | # no list is specified, all formats are considered valid. | |
99473759 | 359 | sub volume_has_feature { |
e6f4eed4 | 360 | my ($cfg, $feature, $volid, $snap, $running, $opts) = @_; |
99473759 AD |
361 | |
362 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
363 | if ($storeid) { | |
364 | my $scfg = storage_config($cfg, $storeid); | |
365 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
e6f4eed4 | 366 | return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running, $opts); |
99473759 AD |
367 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { |
368 | return undef; | |
369 | } else { | |
370 | return undef; | |
371 | } | |
372 | } | |
373 | ||
8c20d8af FE |
374 | sub volume_snapshot_info { |
375 | my ($cfg, $volid) = @_; | |
376 | ||
377 | my ($storeid, $volname) = parse_volume_id($volid); | |
378 | my $scfg = storage_config($cfg, $storeid); | |
379 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
380 | return $plugin->volume_snapshot_info($scfg, $storeid, $volname); | |
381 | } | |
382 | ||
1dc01b9f DM |
383 | sub get_image_dir { |
384 | my ($cfg, $storeid, $vmid) = @_; | |
b6cf0a66 | 385 | |
1dc01b9f DM |
386 | my $scfg = storage_config($cfg, $storeid); |
387 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b6cf0a66 | 388 | |
1dc01b9f | 389 | my $path = $plugin->get_subdir($scfg, 'images'); |
b6cf0a66 | 390 | |
1dc01b9f | 391 | return $vmid ? "$path/$vmid" : $path; |
b6cf0a66 DM |
392 | } |
393 | ||
1dc01b9f | 394 | sub get_private_dir { |
b6cf0a66 DM |
395 | my ($cfg, $storeid, $vmid) = @_; |
396 | ||
1dc01b9f DM |
397 | my $scfg = storage_config($cfg, $storeid); |
398 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b6cf0a66 | 399 | |
1dc01b9f | 400 | my $path = $plugin->get_subdir($scfg, 'rootdir'); |
d22a6133 | 401 | |
1dc01b9f | 402 | return $vmid ? "$path/$vmid" : $path; |
d22a6133 DM |
403 | } |
404 | ||
b6cf0a66 DM |
405 | sub get_iso_dir { |
406 | my ($cfg, $storeid) = @_; | |
407 | ||
1dc01b9f DM |
408 | my $scfg = storage_config($cfg, $storeid); |
409 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b6cf0a66 | 410 | |
1dc01b9f | 411 | return $plugin->get_subdir($scfg, 'iso'); |
b6cf0a66 DM |
412 | } |
413 | ||
414 | sub get_vztmpl_dir { | |
415 | my ($cfg, $storeid) = @_; | |
416 | ||
1dc01b9f DM |
417 | my $scfg = storage_config($cfg, $storeid); |
418 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b6cf0a66 | 419 | |
1dc01b9f | 420 | return $plugin->get_subdir($scfg, 'vztmpl'); |
b6cf0a66 DM |
421 | } |
422 | ||
568de3d1 DM |
423 | sub get_backup_dir { |
424 | my ($cfg, $storeid) = @_; | |
425 | ||
1dc01b9f DM |
426 | my $scfg = storage_config($cfg, $storeid); |
427 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b6cf0a66 | 428 | |
1dc01b9f | 429 | return $plugin->get_subdir($scfg, 'backup'); |
b6cf0a66 DM |
430 | } |
431 | ||
432 | # library implementation | |
433 | ||
b6cf0a66 DM |
434 | sub parse_vmid { |
435 | my $vmid = shift; | |
436 | ||
437 | die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/; | |
438 | ||
439 | return int($vmid); | |
440 | } | |
441 | ||
787624df FG |
442 | # NOTE: basename and basevmid are always undef for LVM-thin, where the |
443 | # clone -> base reference is not encoded in the volume ID. | |
444 | # see note in PVE::Storage::LvmThinPlugin for details. | |
ec4b0dc7 AD |
445 | sub parse_volname { |
446 | my ($cfg, $volid) = @_; | |
447 | ||
448 | my ($storeid, $volname) = parse_volume_id($volid); | |
449 | ||
450 | my $scfg = storage_config($cfg, $storeid); | |
451 | ||
452 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
a6f12626 DM |
453 | |
454 | # returns ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) | |
455 | ||
ec4b0dc7 AD |
456 | return $plugin->parse_volname($volname); |
457 | } | |
458 | ||
b6cf0a66 DM |
459 | sub parse_volume_id { |
460 | my ($volid, $noerr) = @_; | |
461 | ||
a7f3d909 | 462 | return PVE::Storage::Plugin::parse_volume_id($volid, $noerr); |
b6cf0a66 DM |
463 | } |
464 | ||
04a13668 DM |
465 | # test if we have read access to volid |
466 | sub check_volume_access { | |
467 | my ($rpcenv, $user, $cfg, $vmid, $volid) = @_; | |
468 | ||
469 | my ($sid, $volname) = parse_volume_id($volid, 1); | |
470 | if ($sid) { | |
471 | my ($vtype, undef, $ownervm) = parse_volname($cfg, $volid); | |
472 | if ($vtype eq 'iso' || $vtype eq 'vztmpl') { | |
775fdc69 | 473 | # require at least read access to storage, (custom) templates/ISOs could be sensitive |
061b9ca6 | 474 | $rpcenv->check_any($user, "/storage/$sid", ['Datastore.AllocateSpace', 'Datastore.Audit']); |
04a13668 DM |
475 | } elsif (defined($ownervm) && defined($vmid) && ($ownervm == $vmid)) { |
476 | # we are owner - allow access | |
477 | } elsif ($vtype eq 'backup' && $ownervm) { | |
478 | $rpcenv->check($user, "/storage/$sid", ['Datastore.AllocateSpace']); | |
479 | $rpcenv->check($user, "/vms/$ownervm", ['VM.Backup']); | |
480 | } else { | |
481 | # allow if we are Datastore administrator | |
482 | $rpcenv->check($user, "/storage/$sid", ['Datastore.Allocate']); | |
483 | } | |
484 | } else { | |
485 | die "Only root can pass arbitrary filesystem paths." | |
486 | if $user ne 'root@pam'; | |
487 | } | |
488 | ||
489 | return undef; | |
490 | } | |
491 | ||
50853be2 FE |
492 | # NOTE: this check does not work for LVM-thin, where the clone -> base |
493 | # reference is not encoded in the volume ID. | |
494 | # see note in PVE::Storage::LvmThinPlugin for details. | |
495 | sub volume_is_base_and_used { | |
496 | my ($cfg, $volid) = @_; | |
497 | ||
498 | my ($storeid, $volname) = parse_volume_id($volid); | |
499 | my $scfg = storage_config($cfg, $storeid); | |
500 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
17fb7e42 FG |
501 | |
502 | my ($vtype, $name, $vmid, undef, undef, $isBase, undef) = | |
503 | $plugin->parse_volname($volname); | |
504 | ||
505 | if ($isBase) { | |
506 | my $vollist = $plugin->list_images($storeid, $scfg); | |
507 | foreach my $info (@$vollist) { | |
508 | my (undef, $tmpvolname) = parse_volume_id($info->{volid}); | |
509 | my $basename = undef; | |
510 | my $basevmid = undef; | |
511 | ||
512 | eval{ | |
513 | (undef, undef, undef, $basename, $basevmid) = | |
514 | $plugin->parse_volname($tmpvolname); | |
515 | }; | |
516 | ||
517 | if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) { | |
518 | return 1; | |
519 | } | |
520 | } | |
521 | } | |
522 | return 0; | |
17fb7e42 FG |
523 | } |
524 | ||
b6cf0a66 DM |
525 | # try to map a filesystem path to a volume identifier |
526 | sub path_to_volume_id { | |
527 | my ($cfg, $path) = @_; | |
528 | ||
529 | my $ids = $cfg->{ids}; | |
530 | ||
1dc01b9f | 531 | my ($sid, $volname) = parse_volume_id($path, 1); |
b6cf0a66 | 532 | if ($sid) { |
1dc01b9f | 533 | if (my $scfg = $ids->{$sid}) { |
188aca38 | 534 | if ($scfg->{path}) { |
1dc01b9f DM |
535 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
536 | my ($vtype, $name, $vmid) = $plugin->parse_volname($volname); | |
b6cf0a66 DM |
537 | return ($vtype, $path); |
538 | } | |
539 | } | |
540 | return (''); | |
541 | } | |
542 | ||
1a3459ac | 543 | # Note: abs_path() return undef if $path doesn not exist |
75d75990 DM |
544 | # for example when nfs storage is not mounted |
545 | $path = abs_path($path) || $path; | |
b6cf0a66 DM |
546 | |
547 | foreach my $sid (keys %$ids) { | |
1dc01b9f DM |
548 | my $scfg = $ids->{$sid}; |
549 | next if !$scfg->{path}; | |
550 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
551 | my $imagedir = $plugin->get_subdir($scfg, 'images'); | |
552 | my $isodir = $plugin->get_subdir($scfg, 'iso'); | |
553 | my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl'); | |
554 | my $backupdir = $plugin->get_subdir($scfg, 'backup'); | |
555 | my $privatedir = $plugin->get_subdir($scfg, 'rootdir'); | |
73fcb7bf | 556 | my $snippetsdir = $plugin->get_subdir($scfg, 'snippets'); |
b6cf0a66 DM |
557 | |
558 | if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) { | |
559 | my $vmid = $1; | |
560 | my $name = $2; | |
fcbec654 DM |
561 | |
562 | my $vollist = $plugin->list_images($sid, $scfg, $vmid); | |
563 | foreach my $info (@$vollist) { | |
564 | my ($storeid, $volname) = parse_volume_id($info->{volid}); | |
565 | my $volpath = $plugin->path($scfg, $volname, $storeid); | |
566 | if ($volpath eq $path) { | |
567 | return ('images', $info->{volid}); | |
568 | } | |
569 | } | |
4c693491 | 570 | } elsif ($path =~ m!^$isodir/([^/]+$iso_extension_re)$!) { |
b6cf0a66 | 571 | my $name = $1; |
1a3459ac | 572 | return ('iso', "$sid:iso/$name"); |
bba10cf4 | 573 | } elsif ($path =~ m!^$tmpldir/([^/]+$vztmpl_extension_re)$!) { |
b6cf0a66 DM |
574 | my $name = $1; |
575 | return ('vztmpl', "$sid:vztmpl/$name"); | |
1ac17c74 DM |
576 | } elsif ($path =~ m!^$privatedir/(\d+)$!) { |
577 | my $vmid = $1; | |
578 | return ('rootdir', "$sid:rootdir/$vmid"); | |
277cafc0 | 579 | } elsif ($path =~ m!^$backupdir/([^/]+\.(?:tgz|(?:(?:tar|vma)(?:\.(?:${\PVE::Storage::Plugin::COMPRESSOR_RE}))?)))$!) { |
568de3d1 | 580 | my $name = $1; |
892dc992 | 581 | return ('backup', "$sid:backup/$name"); |
73fcb7bf AA |
582 | } elsif ($path =~ m!^$snippetsdir/([^/]+)$!) { |
583 | my $name = $1; | |
584 | return ('snippets', "$sid:snippets/$name"); | |
b6cf0a66 DM |
585 | } |
586 | } | |
587 | ||
588 | # can't map path to volume id | |
589 | return (''); | |
590 | } | |
591 | ||
592 | sub path { | |
207ea852 | 593 | my ($cfg, $volid, $snapname) = @_; |
b6cf0a66 | 594 | |
1dc01b9f | 595 | my ($storeid, $volname) = parse_volume_id($volid); |
b6cf0a66 | 596 | |
1dc01b9f | 597 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 | 598 | |
1dc01b9f | 599 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
207ea852 | 600 | my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname); |
2494896a | 601 | return wantarray ? ($path, $owner, $vtype) : $path; |
b6cf0a66 DM |
602 | } |
603 | ||
35fbb2e6 | 604 | sub abs_filesystem_path { |
4b84ad5e | 605 | my ($cfg, $volid, $allow_blockdev) = @_; |
35fbb2e6 DM |
606 | |
607 | my $path; | |
6ed43d81 TL |
608 | if (parse_volume_id ($volid, 1)) { |
609 | activate_volumes($cfg, [ $volid ]); | |
35fbb2e6 DM |
610 | $path = PVE::Storage::path($cfg, $volid); |
611 | } else { | |
4b84ad5e | 612 | if (-f $volid || ($allow_blockdev && -b $volid)) { |
35fbb2e6 DM |
613 | my $abspath = abs_path($volid); |
614 | if ($abspath && $abspath =~ m|^(/.+)$|) { | |
615 | $path = $1; # untaint any path | |
616 | } | |
617 | } | |
618 | } | |
4b84ad5e DJ |
619 | die "can't find file '$volid'\n" |
620 | if !($path && (-f $path || ($allow_blockdev && -b $path))); | |
35fbb2e6 DM |
621 | |
622 | return $path; | |
623 | } | |
624 | ||
683a3f46 FE |
625 | my $volname_for_storage = sub { |
626 | my ($cfg, $volid, $target_storeid) = @_; | |
627 | ||
628 | my (undef, $name, $vmid, undef, undef, undef, $format) = parse_volname($cfg, $volid); | |
629 | my $target_scfg = storage_config($cfg, $target_storeid); | |
630 | ||
631 | my (undef, $valid_formats) = PVE::Storage::Plugin::default_format($target_scfg); | |
632 | my $format_is_valid = grep { $_ eq $format } @$valid_formats; | |
633 | die "unsupported format '$format' for storage type $target_scfg->{type}\n" if !$format_is_valid; | |
634 | ||
635 | (my $name_without_extension = $name) =~ s/\.$format$//; | |
636 | ||
637 | if ($target_scfg->{path}) { | |
638 | return "$vmid/$name_without_extension.$format"; | |
639 | } else { | |
640 | return "$name_without_extension"; | |
641 | } | |
642 | }; | |
643 | ||
b6cf0a66 | 644 | sub storage_migrate { |
dc3655a1 FE |
645 | my ($cfg, $volid, $target_sshinfo, $target_storeid, $opts, $logfunc) = @_; |
646 | ||
647 | my $base_snapshot = $opts->{base_snapshot}; | |
648 | my $snapshot = $opts->{snapshot}; | |
649 | my $ratelimit_bps = $opts->{ratelimit_bps}; | |
650 | my $insecure = $opts->{insecure}; | |
651 | my $with_snapshots = $opts->{with_snapshots} ? 1 : 0; | |
652 | my $allow_rename = $opts->{allow_rename} ? 1 : 0; | |
b6cf0a66 | 653 | |
6bf56298 | 654 | my ($storeid, $volname) = parse_volume_id($volid); |
b6cf0a66 | 655 | |
6bf56298 | 656 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
657 | |
658 | # no need to migrate shared content | |
a97d3ee4 | 659 | return $volid if $storeid eq $target_storeid && $scfg->{shared}; |
b6cf0a66 | 660 | |
6bf56298 | 661 | my $tcfg = storage_config($cfg, $target_storeid); |
b6cf0a66 | 662 | |
683a3f46 FE |
663 | my $target_volname; |
664 | if ($opts->{target_volname}) { | |
665 | $target_volname = $opts->{target_volname}; | |
666 | } elsif ($scfg->{type} eq $tcfg->{type}) { | |
667 | $target_volname = $volname; | |
668 | } else { | |
669 | $target_volname = $volname_for_storage->($cfg, $volid, $target_storeid); | |
670 | } | |
671 | ||
b6cf0a66 DM |
672 | my $target_volid = "${target_storeid}:${target_volname}"; |
673 | ||
4b4c580d | 674 | my $target_ip = $target_sshinfo->{ip}; |
b6cf0a66 | 675 | |
65bb9859 FG |
676 | my $ssh = PVE::SSHInfo::ssh_info_to_command($target_sshinfo); |
677 | my $ssh_base = PVE::SSHInfo::ssh_info_to_command_base($target_sshinfo); | |
47cea194 | 678 | local $ENV{RSYNC_RSH} = PVE::Tools::cmd2string($ssh_base); |
b6cf0a66 | 679 | |
93d1812e FE |
680 | my @cstream; |
681 | if (defined($ratelimit_bps)) { | |
682 | @cstream = ([ '/usr/bin/cstream', '-t', $ratelimit_bps ]); | |
683 | $logfunc->("using a bandwidth limit of $ratelimit_bps bps for transferring '$volid'") if $logfunc; | |
684 | } | |
01f7e902 | 685 | |
da72898c WB |
686 | my $migration_snapshot; |
687 | if (!defined($snapshot)) { | |
a0e3e224 | 688 | if ($scfg->{type} eq 'zfspool' || $scfg->{type} eq 'btrfs') { |
da72898c WB |
689 | $migration_snapshot = 1; |
690 | $snapshot = '__migration__'; | |
1dc01b9f | 691 | } |
da72898c | 692 | } |
e0852ba7 | 693 | |
cfdffd8a | 694 | my @formats = volume_transfer_formats($cfg, $volid, $target_volid, $snapshot, $base_snapshot, $with_snapshots); |
da72898c WB |
695 | die "cannot migrate from storage type '$scfg->{type}' to '$tcfg->{type}'\n" if !@formats; |
696 | my $format = $formats[0]; | |
7459cb3d | 697 | |
228e5be9 | 698 | my $import_fn = '-'; # let pvesm import read from stdin per default |
da72898c | 699 | if ($insecure) { |
228e5be9 TL |
700 | my $net = $target_sshinfo->{network} // $target_sshinfo->{ip}; |
701 | $import_fn = "tcp://$net"; | |
da72898c | 702 | } |
7ba34faa | 703 | |
a97d3ee4 FE |
704 | my $target_apiver = 1; # if there is no apiinfo call, assume 1 |
705 | my $get_api_version = [@$ssh, 'pvesm', 'apiinfo']; | |
706 | my $match_api_version = sub { $target_apiver = $1 if $_[0] =~ m!^APIVER (\d+)$!; }; | |
707 | eval { run_command($get_api_version, logfunc => $match_api_version); }; | |
708 | ||
e8a7e764 | 709 | my $send = ['pvesm', 'export', $volid, $format, '-', '-with-snapshots', $with_snapshots]; |
cfdffd8a | 710 | my $recv = [@$ssh, '--', 'pvesm', 'import', $target_volid, $format, $import_fn, '-with-snapshots', $with_snapshots]; |
da72898c | 711 | if (defined($snapshot)) { |
3cc29a04 WB |
712 | push @$send, '-snapshot', $snapshot; |
713 | push @$recv, '-snapshot', $snapshot; | |
da72898c WB |
714 | } |
715 | if ($migration_snapshot) { | |
716 | push @$recv, '-delete-snapshot', $snapshot; | |
717 | } | |
a97d3ee4 | 718 | push @$recv, '-allow-rename', $allow_rename if $target_apiver >= 5; |
3d621977 | 719 | |
da72898c WB |
720 | if (defined($base_snapshot)) { |
721 | # Check if the snapshot exists on the remote side: | |
722 | push @$send, '-base', $base_snapshot; | |
3cc29a04 | 723 | push @$recv, '-base', $base_snapshot if $target_apiver >= 9; |
da72898c | 724 | } |
ac191ec7 | 725 | |
a97d3ee4 FE |
726 | my $new_volid; |
727 | my $pattern = volume_imported_message(undef, 1); | |
728 | my $match_volid_and_log = sub { | |
729 | my $line = shift; | |
730 | ||
731 | $new_volid = $1 if ($line =~ $pattern); | |
732 | ||
733 | if ($logfunc) { | |
734 | chomp($line); | |
735 | $logfunc->($line); | |
736 | } | |
737 | }; | |
738 | ||
da72898c | 739 | volume_snapshot($cfg, $volid, $snapshot) if $migration_snapshot; |
5324ceff FE |
740 | |
741 | if (defined($snapshot)) { | |
742 | activate_volumes($cfg, [$volid], $snapshot); | |
743 | } else { | |
744 | activate_volumes($cfg, [$volid]); | |
745 | } | |
746 | ||
da72898c WB |
747 | eval { |
748 | if ($insecure) { | |
57acd6a1 FE |
749 | my $input = IO::File->new(); |
750 | my $info = IO::File->new(); | |
751 | open3($input, $info, $info, @{$recv}) | |
da72898c | 752 | or die "receive command failed: $!\n"; |
57acd6a1 FE |
753 | close($input); |
754 | ||
7cdc75a2 FE |
755 | my $try_ip = <$info> // ''; |
756 | my ($ip) = $try_ip =~ /^($PVE::Tools::IPRE)$/ # untaint | |
757 | or die "no tunnel IP received, got '$try_ip'\n"; | |
758 | ||
759 | my $try_port = <$info> // ''; | |
760 | my ($port) = $try_port =~ /^(\d+)$/ # untaint | |
761 | or die "no tunnel port received, got '$try_port'\n"; | |
762 | ||
da72898c WB |
763 | my $socket = IO::Socket::IP->new(PeerHost => $ip, PeerPort => $port, Type => SOCK_STREAM) |
764 | or die "failed to connect to tunnel at $ip:$port\n"; | |
765 | # we won't be reading from the socket | |
766 | shutdown($socket, 0); | |
57acd6a1 FE |
767 | |
768 | eval { run_command([$send, @cstream], output => '>&'.fileno($socket), errfunc => $logfunc); }; | |
769 | my $send_error = $@; | |
770 | ||
da72898c WB |
771 | # don't close the connection entirely otherwise the receiving end |
772 | # might not get all buffered data (and fails with 'connection reset by peer') | |
773 | shutdown($socket, 1); | |
aca83310 FE |
774 | |
775 | # wait for the remote process to finish | |
a97d3ee4 FE |
776 | while (my $line = <$info>) { |
777 | $match_volid_and_log->("[$target_sshinfo->{name}] $line"); | |
aca83310 FE |
778 | } |
779 | ||
da72898c WB |
780 | # now close the socket |
781 | close($socket); | |
782 | if (!close($info)) { # does waitpid() | |
783 | die "import failed: $!\n" if $!; | |
784 | die "import failed: exit code ".($?>>8)."\n"; | |
0a29ad61 | 785 | } |
57acd6a1 FE |
786 | |
787 | die $send_error if $send_error; | |
0a29ad61 | 788 | } else { |
a97d3ee4 | 789 | run_command([$send, @cstream, $recv], logfunc => $match_volid_and_log); |
0a29ad61 | 790 | } |
a97d3ee4 FE |
791 | |
792 | die "unable to get ID of the migrated volume\n" | |
793 | if !defined($new_volid) && $target_apiver >= 5; | |
da72898c WB |
794 | }; |
795 | my $err = $@; | |
796 | warn "send/receive failed, cleaning up snapshot(s)..\n" if $err; | |
797 | if ($migration_snapshot) { | |
798 | eval { volume_snapshot_delete($cfg, $volid, $snapshot, 0) }; | |
799 | warn "could not remove source snapshot: $@\n" if $@; | |
b6cf0a66 | 800 | } |
da72898c | 801 | die $err if $err; |
a97d3ee4 FE |
802 | |
803 | return $new_volid // $target_volid; | |
b6cf0a66 DM |
804 | } |
805 | ||
2502b33b | 806 | sub vdisk_clone { |
7bbc4004 | 807 | my ($cfg, $volid, $vmid, $snap) = @_; |
1a3459ac | 808 | |
2502b33b DM |
809 | my ($storeid, $volname) = parse_volume_id($volid); |
810 | ||
811 | my $scfg = storage_config($cfg, $storeid); | |
812 | ||
813 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1a3459ac | 814 | |
2502b33b DM |
815 | activate_storage($cfg, $storeid); |
816 | ||
817 | # lock shared storage | |
818 | return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
7bbc4004 | 819 | my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap); |
2502b33b DM |
820 | return "$storeid:$volname"; |
821 | }); | |
822 | } | |
823 | ||
824 | sub vdisk_create_base { | |
825 | my ($cfg, $volid) = @_; | |
826 | ||
827 | my ($storeid, $volname) = parse_volume_id($volid); | |
828 | ||
829 | my $scfg = storage_config($cfg, $storeid); | |
830 | ||
831 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1a3459ac | 832 | |
2502b33b DM |
833 | activate_storage($cfg, $storeid); |
834 | ||
835 | # lock shared storage | |
836 | return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
837 | my $volname = $plugin->create_base($storeid, $scfg, $volname); | |
838 | return "$storeid:$volname"; | |
839 | }); | |
840 | } | |
841 | ||
40d69893 DM |
842 | sub map_volume { |
843 | my ($cfg, $volid, $snapname) = @_; | |
844 | ||
845 | my ($storeid, $volname) = parse_volume_id($volid); | |
846 | ||
847 | my $scfg = storage_config($cfg, $storeid); | |
848 | ||
849 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
850 | ||
851 | return $plugin->map_volume($storeid, $scfg, $volname, $snapname); | |
852 | } | |
853 | ||
854 | sub unmap_volume { | |
855 | my ($cfg, $volid, $snapname) = @_; | |
856 | ||
857 | my ($storeid, $volname) = parse_volume_id($volid); | |
858 | ||
859 | my $scfg = storage_config($cfg, $storeid); | |
860 | ||
861 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
862 | ||
863 | return $plugin->unmap_volume($storeid, $scfg, $volname, $snapname); | |
864 | } | |
865 | ||
1dc01b9f DM |
866 | sub vdisk_alloc { |
867 | my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_; | |
b6cf0a66 | 868 | |
82fc923f | 869 | die "no storage ID specified\n" if !$storeid; |
b6cf0a66 | 870 | |
1dc01b9f | 871 | PVE::JSONSchema::parse_storage_id($storeid); |
b6cf0a66 | 872 | |
1dc01b9f | 873 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 | 874 | |
1dc01b9f | 875 | die "no VMID specified\n" if !$vmid; |
b6cf0a66 | 876 | |
1dc01b9f | 877 | $vmid = parse_vmid($vmid); |
b6cf0a66 | 878 | |
1dc01b9f | 879 | my $defformat = PVE::Storage::Plugin::default_format($scfg); |
b6cf0a66 | 880 | |
1dc01b9f | 881 | $fmt = $defformat if !$fmt; |
b6cf0a66 | 882 | |
1dc01b9f | 883 | activate_storage($cfg, $storeid); |
3af60e62 | 884 | |
1dc01b9f | 885 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
b6cf0a66 | 886 | |
1dc01b9f DM |
887 | # lock shared storage |
888 | return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
afdfbe55 WB |
889 | my $old_umask = umask(umask|0037); |
890 | my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) }; | |
891 | my $err = $@; | |
892 | umask $old_umask; | |
893 | die $err if $err; | |
1dc01b9f DM |
894 | return "$storeid:$volname"; |
895 | }); | |
b6cf0a66 DM |
896 | } |
897 | ||
1dc01b9f DM |
898 | sub vdisk_free { |
899 | my ($cfg, $volid) = @_; | |
b6cf0a66 | 900 | |
1dc01b9f | 901 | my ($storeid, $volname) = parse_volume_id($volid); |
1dc01b9f | 902 | my $scfg = storage_config($cfg, $storeid); |
1dc01b9f | 903 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
1a3459ac | 904 | |
1dc01b9f | 905 | activate_storage($cfg, $storeid); |
b6cf0a66 | 906 | |
1dc01b9f | 907 | my $cleanup_worker; |
b6cf0a66 | 908 | |
1dc01b9f DM |
909 | # lock shared storage |
910 | $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
787624df | 911 | # LVM-thin allows deletion of still referenced base volumes! |
17fb7e42 | 912 | die "base volume '$volname' is still in use by linked clones\n" |
50853be2 | 913 | if volume_is_base_and_used($cfg, $volid); |
32437ed2 | 914 | |
17fb7e42 | 915 | my (undef, undef, undef, undef, undef, $isBase, $format) = |
32437ed2 | 916 | $plugin->parse_volname($volname); |
35533c68 | 917 | $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format); |
1dc01b9f | 918 | }); |
b6cf0a66 | 919 | |
1dc01b9f | 920 | return if !$cleanup_worker; |
b6cf0a66 | 921 | |
1dc01b9f DM |
922 | my $rpcenv = PVE::RPCEnvironment::get(); |
923 | my $authuser = $rpcenv->get_user(); | |
b6cf0a66 | 924 | |
1dc01b9f | 925 | $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker); |
b6cf0a66 DM |
926 | } |
927 | ||
b6cf0a66 | 928 | sub vdisk_list { |
2c5246e1 | 929 | my ($cfg, $storeid, $vmid, $vollist, $ctype) = @_; |
b6cf0a66 DM |
930 | |
931 | my $ids = $cfg->{ids}; | |
932 | ||
933 | storage_check_enabled($cfg, $storeid) if ($storeid); | |
934 | ||
d96b789a | 935 | my $res = $storeid ? { $storeid => [] } : {}; |
b6cf0a66 DM |
936 | |
937 | # prepare/activate/refresh all storages | |
938 | ||
b6cf0a66 DM |
939 | my $storage_list = []; |
940 | if ($vollist) { | |
941 | foreach my $volid (@$vollist) { | |
1dc01b9f DM |
942 | my ($sid, undef) = parse_volume_id($volid); |
943 | next if !defined($ids->{$sid}); | |
b6cf0a66 DM |
944 | next if !storage_check_enabled($cfg, $sid, undef, 1); |
945 | push @$storage_list, $sid; | |
b6cf0a66 DM |
946 | } |
947 | } else { | |
948 | foreach my $sid (keys %$ids) { | |
949 | next if $storeid && $storeid ne $sid; | |
950 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
c43655d2 | 951 | my $content = $ids->{$sid}->{content}; |
2c5246e1 | 952 | next if defined($ctype) && !$content->{$ctype}; |
c43655d2 | 953 | next if !($content->{rootdir} || $content->{images}); |
b6cf0a66 | 954 | push @$storage_list, $sid; |
b6cf0a66 DM |
955 | } |
956 | } | |
957 | ||
1dc01b9f | 958 | my $cache = {}; |
b6cf0a66 | 959 | |
1dc01b9f | 960 | activate_storage_list($cfg, $storage_list, $cache); |
b6cf0a66 | 961 | |
d96b789a | 962 | for my $sid ($storage_list->@*) { |
1dc01b9f | 963 | next if $storeid && $storeid ne $sid; |
b6cf0a66 | 964 | |
1dc01b9f DM |
965 | my $scfg = $ids->{$sid}; |
966 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
967 | $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache); | |
b6cf0a66 DM |
968 | @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid}; |
969 | } | |
970 | ||
971 | return $res; | |
972 | } | |
973 | ||
c2fc9fbe DM |
974 | sub template_list { |
975 | my ($cfg, $storeid, $tt) = @_; | |
976 | ||
977 | die "unknown template type '$tt'\n" | |
978 | if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup' || $tt eq 'snippets'); | |
979 | ||
980 | my $ids = $cfg->{ids}; | |
981 | ||
982 | storage_check_enabled($cfg, $storeid) if ($storeid); | |
983 | ||
984 | my $res = {}; | |
985 | ||
986 | # query the storage | |
987 | foreach my $sid (keys %$ids) { | |
988 | next if $storeid && $storeid ne $sid; | |
989 | ||
990 | my $scfg = $ids->{$sid}; | |
991 | my $type = $scfg->{type}; | |
992 | ||
993 | next if !$scfg->{content}->{$tt}; | |
994 | ||
995 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
996 | ||
997 | $res->{$sid} = volume_list($cfg, $sid, undef, $tt); | |
998 | } | |
999 | ||
1000 | return $res; | |
1001 | } | |
1002 | ||
37ba0aea DM |
1003 | sub volume_list { |
1004 | my ($cfg, $storeid, $vmid, $content) = @_; | |
1005 | ||
be785439 | 1006 | my @ctypes = qw(rootdir images vztmpl iso backup snippets); |
37ba0aea DM |
1007 | |
1008 | my $cts = $content ? [ $content ] : [ @ctypes ]; | |
1009 | ||
1010 | my $scfg = PVE::Storage::storage_config($cfg, $storeid); | |
1011 | ||
c2fc9fbe | 1012 | $cts = [ grep { defined($scfg->{content}->{$_}) } @$cts ]; |
37ba0aea | 1013 | |
c2fc9fbe | 1014 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
37ba0aea | 1015 | |
c2fc9fbe DM |
1016 | activate_storage($cfg, $storeid); |
1017 | ||
1018 | my $res = $plugin->list_volumes($storeid, $scfg, $vmid, $cts); | |
1019 | ||
1020 | @$res = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @$res; | |
37ba0aea DM |
1021 | |
1022 | return $res; | |
1023 | } | |
1024 | ||
b6cf0a66 DM |
1025 | sub uevent_seqnum { |
1026 | ||
1027 | my $filename = "/sys/kernel/uevent_seqnum"; | |
1028 | ||
1029 | my $seqnum = 0; | |
1dc01b9f | 1030 | if (my $fh = IO::File->new($filename, "r")) { |
b6cf0a66 DM |
1031 | my $line = <$fh>; |
1032 | if ($line =~ m/^(\d+)$/) { | |
1dc01b9f | 1033 | $seqnum = int($1); |
b6cf0a66 DM |
1034 | } |
1035 | close ($fh); | |
1036 | } | |
1037 | return $seqnum; | |
1038 | } | |
1039 | ||
f3d4ef46 | 1040 | sub activate_storage { |
1dc01b9f | 1041 | my ($cfg, $storeid, $cache) = @_; |
b6cf0a66 | 1042 | |
f3d4ef46 DM |
1043 | $cache = {} if !$cache; |
1044 | ||
b6cf0a66 DM |
1045 | my $scfg = storage_check_enabled($cfg, $storeid); |
1046 | ||
1dc01b9f | 1047 | return if $cache->{activated}->{$storeid}; |
b6cf0a66 | 1048 | |
1dc01b9f | 1049 | $cache->{uevent_seqnum} = uevent_seqnum() if !$cache->{uevent_seqnum}; |
b6cf0a66 | 1050 | |
1dc01b9f | 1051 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
b6cf0a66 | 1052 | |
1dc01b9f DM |
1053 | if ($scfg->{base}) { |
1054 | my ($baseid, undef) = parse_volume_id ($scfg->{base}); | |
f3d4ef46 DM |
1055 | activate_storage($cfg, $baseid, $cache); |
1056 | } | |
1057 | ||
1058 | if (!$plugin->check_connection($storeid, $scfg)) { | |
1059 | die "storage '$storeid' is not online\n"; | |
b6cf0a66 DM |
1060 | } |
1061 | ||
1dc01b9f DM |
1062 | $plugin->activate_storage($storeid, $scfg, $cache); |
1063 | ||
b6cf0a66 DM |
1064 | my $newseq = uevent_seqnum (); |
1065 | ||
1066 | # only call udevsettle if there are events | |
1dc01b9f | 1067 | if ($newseq > $cache->{uevent_seqnum}) { |
d3a5e309 | 1068 | system ("udevadm settle --timeout=30"); # ignore errors |
1dc01b9f | 1069 | $cache->{uevent_seqnum} = $newseq; |
b6cf0a66 DM |
1070 | } |
1071 | ||
1dc01b9f | 1072 | $cache->{activated}->{$storeid} = 1; |
b6cf0a66 DM |
1073 | } |
1074 | ||
1075 | sub activate_storage_list { | |
1dc01b9f | 1076 | my ($cfg, $storeid_list, $cache) = @_; |
b6cf0a66 | 1077 | |
1dc01b9f | 1078 | $cache = {} if !$cache; |
b6cf0a66 DM |
1079 | |
1080 | foreach my $storeid (@$storeid_list) { | |
f3d4ef46 | 1081 | activate_storage($cfg, $storeid, $cache); |
b6cf0a66 DM |
1082 | } |
1083 | } | |
1084 | ||
1dc01b9f DM |
1085 | sub deactivate_storage { |
1086 | my ($cfg, $storeid) = @_; | |
1087 | ||
1088 | my $scfg = storage_config ($cfg, $storeid); | |
1089 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b6cf0a66 | 1090 | |
1dc01b9f DM |
1091 | my $cache = {}; |
1092 | $plugin->deactivate_storage($storeid, $scfg, $cache); | |
b6cf0a66 DM |
1093 | } |
1094 | ||
1095 | sub activate_volumes { | |
02e797b8 | 1096 | my ($cfg, $vollist, $snapname) = @_; |
6703353b DM |
1097 | |
1098 | return if !($vollist && scalar(@$vollist)); | |
1099 | ||
b6cf0a66 DM |
1100 | my $storagehash = {}; |
1101 | foreach my $volid (@$vollist) { | |
1dc01b9f | 1102 | my ($storeid, undef) = parse_volume_id($volid); |
b6cf0a66 DM |
1103 | $storagehash->{$storeid} = 1; |
1104 | } | |
1105 | ||
1dc01b9f DM |
1106 | my $cache = {}; |
1107 | ||
1108 | activate_storage_list($cfg, [keys %$storagehash], $cache); | |
b6cf0a66 DM |
1109 | |
1110 | foreach my $volid (@$vollist) { | |
5521b580 | 1111 | my ($storeid, $volname) = parse_volume_id($volid); |
1dc01b9f DM |
1112 | my $scfg = storage_config($cfg, $storeid); |
1113 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
02e797b8 | 1114 | $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache); |
b6cf0a66 DM |
1115 | } |
1116 | } | |
1117 | ||
1118 | sub deactivate_volumes { | |
02e797b8 | 1119 | my ($cfg, $vollist, $snapname) = @_; |
b6cf0a66 | 1120 | |
6703353b DM |
1121 | return if !($vollist && scalar(@$vollist)); |
1122 | ||
1dc01b9f DM |
1123 | my $cache = {}; |
1124 | ||
6703353b | 1125 | my @errlist = (); |
b6cf0a66 | 1126 | foreach my $volid (@$vollist) { |
1dc01b9f | 1127 | my ($storeid, $volname) = parse_volume_id($volid); |
b6cf0a66 | 1128 | |
1dc01b9f DM |
1129 | my $scfg = storage_config($cfg, $storeid); |
1130 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1a3459ac | 1131 | |
1dc01b9f | 1132 | eval { |
02e797b8 | 1133 | $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache); |
1dc01b9f DM |
1134 | }; |
1135 | if (my $err = $@) { | |
1136 | warn $err; | |
1137 | push @errlist, $volid; | |
b6cf0a66 DM |
1138 | } |
1139 | } | |
6703353b | 1140 | |
82fc923f | 1141 | die "volume deactivation failed: " . join(' ', @errlist) |
6703353b | 1142 | if scalar(@errlist); |
b6cf0a66 DM |
1143 | } |
1144 | ||
1a3459ac | 1145 | sub storage_info { |
856c54bd | 1146 | my ($cfg, $content, $includeformat) = @_; |
b6cf0a66 DM |
1147 | |
1148 | my $ids = $cfg->{ids}; | |
1149 | ||
1150 | my $info = {}; | |
ff3badd8 | 1151 | |
583c2802 | 1152 | my @ctypes = PVE::Tools::split_list($content); |
ff3badd8 | 1153 | |
b6cf0a66 DM |
1154 | my $slist = []; |
1155 | foreach my $storeid (keys %$ids) { | |
6ce4f724 | 1156 | my $storage_enabled = defined(storage_check_enabled($cfg, $storeid, undef, 1)); |
b6cf0a66 | 1157 | |
d73060be DM |
1158 | if (defined($content)) { |
1159 | my $want_ctype = 0; | |
1160 | foreach my $ctype (@ctypes) { | |
1161 | if ($ids->{$storeid}->{content}->{$ctype}) { | |
1162 | $want_ctype = 1; | |
1163 | last; | |
1164 | } | |
583c2802 | 1165 | } |
6ce4f724 | 1166 | next if !$want_ctype || !$storage_enabled; |
583c2802 | 1167 | } |
ff3badd8 | 1168 | |
b6cf0a66 DM |
1169 | my $type = $ids->{$storeid}->{type}; |
1170 | ||
1a3459ac | 1171 | $info->{$storeid} = { |
b6cf0a66 | 1172 | type => $type, |
1a3459ac DM |
1173 | total => 0, |
1174 | avail => 0, | |
1175 | used => 0, | |
04a2e4f3 | 1176 | shared => $ids->{$storeid}->{shared} ? 1 : 0, |
1dc01b9f | 1177 | content => PVE::Storage::Plugin::content_hash_to_string($ids->{$storeid}->{content}), |
b6cf0a66 | 1178 | active => 0, |
6ce4f724 | 1179 | enabled => $storage_enabled ? 1 : 0, |
b6cf0a66 DM |
1180 | }; |
1181 | ||
b6cf0a66 DM |
1182 | push @$slist, $storeid; |
1183 | } | |
1184 | ||
1dc01b9f | 1185 | my $cache = {}; |
b6cf0a66 | 1186 | |
b6cf0a66 DM |
1187 | foreach my $storeid (keys %$ids) { |
1188 | my $scfg = $ids->{$storeid}; | |
b43b073b | 1189 | |
b6cf0a66 | 1190 | next if !$info->{$storeid}; |
b43b073b | 1191 | next if !$info->{$storeid}->{enabled}; |
b6cf0a66 | 1192 | |
856c54bd DC |
1193 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
1194 | if ($includeformat) { | |
1195 | my $pd = $plugin->plugindata(); | |
1196 | $info->{$storeid}->{format} = $pd->{format} | |
1197 | if $pd->{format}; | |
1198 | $info->{$storeid}->{select_existing} = $pd->{select_existing} | |
1199 | if $pd->{select_existing}; | |
1200 | } | |
1201 | ||
f3d4ef46 DM |
1202 | eval { activate_storage($cfg, $storeid, $cache); }; |
1203 | if (my $err = $@) { | |
1204 | warn $err; | |
1205 | next; | |
1206 | } | |
1207 | ||
41aacc6c | 1208 | my ($total, $avail, $used, $active) = eval { $plugin->status($storeid, $scfg, $cache); }; |
7028645e | 1209 | warn $@ if $@; |
1dc01b9f | 1210 | next if !$active; |
ff3badd8 DM |
1211 | $info->{$storeid}->{total} = int($total); |
1212 | $info->{$storeid}->{avail} = int($avail); | |
1213 | $info->{$storeid}->{used} = int($used); | |
1dc01b9f | 1214 | $info->{$storeid}->{active} = $active; |
b6cf0a66 DM |
1215 | } |
1216 | ||
1217 | return $info; | |
1218 | } | |
1219 | ||
1220 | sub resolv_server { | |
1221 | my ($server) = @_; | |
1a3459ac | 1222 | |
c67daeac WB |
1223 | my ($packed_ip, $family); |
1224 | eval { | |
1225 | my @res = PVE::Tools::getaddrinfo_all($server); | |
1226 | $family = $res[0]->{family}; | |
1227 | $packed_ip = (PVE::Tools::unpack_sockaddr_in46($res[0]->{addr}))[2]; | |
1228 | }; | |
b6cf0a66 | 1229 | if (defined $packed_ip) { |
ee302b1c | 1230 | return Socket::inet_ntop($family, $packed_ip); |
b6cf0a66 DM |
1231 | } |
1232 | return undef; | |
1233 | } | |
1234 | ||
1235 | sub scan_nfs { | |
1236 | my ($server_in) = @_; | |
1237 | ||
1238 | my $server; | |
1239 | if (!($server = resolv_server ($server_in))) { | |
1240 | die "unable to resolve address for server '${server_in}'\n"; | |
1241 | } | |
1242 | ||
1243 | my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server]; | |
1244 | ||
1245 | my $res = {}; | |
f81372ac | 1246 | run_command($cmd, outfunc => sub { |
b6cf0a66 DM |
1247 | my $line = shift; |
1248 | ||
1249 | # note: howto handle white spaces in export path?? | |
1250 | if ($line =~ m!^(/\S+)\s+(.+)$!) { | |
1251 | $res->{$1} = $2; | |
1252 | } | |
1253 | }); | |
1254 | ||
1255 | return $res; | |
1256 | } | |
1257 | ||
4cab0acd WL |
1258 | sub scan_cifs { |
1259 | my ($server_in, $user, $password, $domain) = @_; | |
1260 | ||
436773fd TL |
1261 | my $server = resolv_server($server_in); |
1262 | die "unable to resolve address for server '${server_in}'\n" if !$server; | |
4cab0acd | 1263 | |
436773fd | 1264 | # we only support Windows 2012 and newer, so just use smb3 |
4cab0acd | 1265 | my $cmd = ['/usr/bin/smbclient', '-m', 'smb3', '-d', '0', '-L', $server]; |
fd55f51e | 1266 | push @$cmd, '-W', $domain if defined($domain); |
afaa98f1 | 1267 | |
8594155a | 1268 | push @$cmd, '-N' if !defined($password); |
afaa98f1 TL |
1269 | local $ENV{USER} = $user if defined($user); |
1270 | local $ENV{PASSWD} = $password if defined($password); | |
4cab0acd WL |
1271 | |
1272 | my $res = {}; | |
be18d6da | 1273 | my $err = ''; |
4cab0acd | 1274 | run_command($cmd, |
436773fd | 1275 | noerr => 1, |
be18d6da TL |
1276 | errfunc => sub { |
1277 | $err .= "$_[0]\n" | |
1278 | }, | |
436773fd TL |
1279 | outfunc => sub { |
1280 | my $line = shift; | |
1281 | if ($line =~ m/(\S+)\s*Disk\s*(\S*)/) { | |
1282 | $res->{$1} = $2; | |
c42d8655 TL |
1283 | } elsif ($line =~ m/(NT_STATUS_(\S+))/) { |
1284 | my $status = $1; | |
1285 | $err .= "unexpected status: $1\n" if uc($1) ne 'SUCCESS'; | |
436773fd TL |
1286 | } |
1287 | }, | |
4cab0acd | 1288 | ); |
d37729b9 TL |
1289 | # only die if we got no share, else it's just some followup check error |
1290 | # (like workgroup querying) | |
1291 | raise($err) if $err && !%$res; | |
4cab0acd WL |
1292 | |
1293 | return $res; | |
1294 | } | |
1295 | ||
584d97f6 DM |
1296 | sub scan_zfs { |
1297 | ||
3881e680 | 1298 | my $cmd = ['zfs', 'list', '-t', 'filesystem', '-Hp', '-o', 'name,avail,used']; |
584d97f6 DM |
1299 | |
1300 | my $res = []; | |
1301 | run_command($cmd, outfunc => sub { | |
1302 | my $line = shift; | |
1303 | ||
1304 | if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) { | |
3932390b | 1305 | my ($pool, $size_str, $used_str) = ($1, $2, $3); |
3881e680 AL |
1306 | my $size = $size_str + 0; |
1307 | my $used = $used_str + 0; | |
48e27f79 | 1308 | # ignore subvolumes generated by our ZFSPoolPlugin |
851658c3 WL |
1309 | return if $pool =~ m!/subvol-\d+-[^/]+$!; |
1310 | return if $pool =~ m!/basevol-\d+-[^/]+$!; | |
3932390b | 1311 | push @$res, { pool => $pool, size => $size, free => $size-$used }; |
584d97f6 DM |
1312 | } |
1313 | }); | |
1314 | ||
1315 | return $res; | |
1316 | } | |
1317 | ||
b6cf0a66 DM |
1318 | sub resolv_portal { |
1319 | my ($portal, $noerr) = @_; | |
1320 | ||
1689e627 WB |
1321 | my ($server, $port) = PVE::Tools::parse_host_and_port($portal); |
1322 | if ($server) { | |
b6cf0a66 DM |
1323 | if (my $ip = resolv_server($server)) { |
1324 | $server = $ip; | |
1689e627 | 1325 | $server = "[$server]" if $server =~ /^$IPV6RE$/; |
b6cf0a66 DM |
1326 | return $port ? "$server:$port" : $server; |
1327 | } | |
1328 | } | |
1329 | return undef if $noerr; | |
1330 | ||
1331 | raise_param_exc({ portal => "unable to resolve portal address '$portal'" }); | |
1332 | } | |
1333 | ||
b6cf0a66 DM |
1334 | |
1335 | sub scan_iscsi { | |
1336 | my ($portal_in) = @_; | |
1337 | ||
1338 | my $portal; | |
1dc01b9f | 1339 | if (!($portal = resolv_portal($portal_in))) { |
b6cf0a66 DM |
1340 | die "unable to parse/resolve portal address '${portal_in}'\n"; |
1341 | } | |
1342 | ||
1dc01b9f | 1343 | return PVE::Storage::ISCSIPlugin::iscsi_discovery($portal); |
b6cf0a66 DM |
1344 | } |
1345 | ||
1346 | sub storage_default_format { | |
1347 | my ($cfg, $storeid) = @_; | |
1348 | ||
1349 | my $scfg = storage_config ($cfg, $storeid); | |
1350 | ||
1dc01b9f | 1351 | return PVE::Storage::Plugin::default_format($scfg); |
b6cf0a66 DM |
1352 | } |
1353 | ||
1354 | sub vgroup_is_used { | |
1355 | my ($cfg, $vgname) = @_; | |
1356 | ||
1357 | foreach my $storeid (keys %{$cfg->{ids}}) { | |
1dc01b9f | 1358 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
1359 | if ($scfg->{type} eq 'lvm' && $scfg->{vgname} eq $vgname) { |
1360 | return 1; | |
1361 | } | |
1362 | } | |
1363 | ||
1364 | return undef; | |
1365 | } | |
1366 | ||
1367 | sub target_is_used { | |
1368 | my ($cfg, $target) = @_; | |
1369 | ||
1370 | foreach my $storeid (keys %{$cfg->{ids}}) { | |
1dc01b9f | 1371 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
1372 | if ($scfg->{type} eq 'iscsi' && $scfg->{target} eq $target) { |
1373 | return 1; | |
1374 | } | |
1375 | } | |
1376 | ||
1377 | return undef; | |
1378 | } | |
1379 | ||
1380 | sub volume_is_used { | |
1381 | my ($cfg, $volid) = @_; | |
1382 | ||
1383 | foreach my $storeid (keys %{$cfg->{ids}}) { | |
1dc01b9f | 1384 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
1385 | if ($scfg->{base} && $scfg->{base} eq $volid) { |
1386 | return 1; | |
1387 | } | |
1388 | } | |
1389 | ||
1390 | return undef; | |
1391 | } | |
1392 | ||
1393 | sub storage_is_used { | |
1394 | my ($cfg, $storeid) = @_; | |
1395 | ||
1396 | foreach my $sid (keys %{$cfg->{ids}}) { | |
1dc01b9f | 1397 | my $scfg = storage_config($cfg, $sid); |
b6cf0a66 | 1398 | next if !$scfg->{base}; |
1dc01b9f | 1399 | my ($st) = parse_volume_id($scfg->{base}); |
b6cf0a66 DM |
1400 | return 1 if $st && $st eq $storeid; |
1401 | } | |
1402 | ||
1403 | return undef; | |
1404 | } | |
1405 | ||
1406 | sub foreach_volid { | |
1407 | my ($list, $func) = @_; | |
1408 | ||
1409 | return if !$list; | |
1410 | ||
1411 | foreach my $sid (keys %$list) { | |
1412 | foreach my $info (@{$list->{$sid}}) { | |
1413 | my $volid = $info->{volid}; | |
1dc01b9f | 1414 | my ($sid1, $volname) = parse_volume_id($volid, 1); |
b6cf0a66 DM |
1415 | if ($sid1 && $sid1 eq $sid) { |
1416 | &$func ($volid, $sid, $info); | |
1417 | } else { | |
1418 | warn "detected strange volid '$volid' in volume list for '$sid'\n"; | |
1419 | } | |
1420 | } | |
1421 | } | |
1422 | } | |
1423 | ||
cd554b79 AA |
1424 | sub decompressor_info { |
1425 | my ($format, $comp) = @_; | |
1426 | ||
1427 | if ($format eq 'tgz' && !defined($comp)) { | |
1428 | ($format, $comp) = ('tar', 'gz'); | |
1429 | } | |
1430 | ||
1431 | my $decompressor = { | |
1432 | tar => { | |
1433 | gz => ['tar', '-z'], | |
1434 | lzo => ['tar', '--lzop'], | |
014d36db | 1435 | zst => ['tar', '--zstd'], |
cd554b79 AA |
1436 | }, |
1437 | vma => { | |
1438 | gz => ['zcat'], | |
1439 | lzo => ['lzop', '-d', '-c'], | |
014d36db | 1440 | zst => ['zstd', '-q', '-d', '-c'], |
cd554b79 AA |
1441 | }, |
1442 | }; | |
1443 | ||
1444 | die "ERROR: archive format not defined\n" | |
1445 | if !defined($decompressor->{$format}); | |
1446 | ||
1207620c TL |
1447 | my $decomp; |
1448 | $decomp = $decompressor->{$format}->{$comp} if $comp; | |
cd554b79 AA |
1449 | |
1450 | my $info = { | |
1451 | format => $format, | |
1452 | compression => $comp, | |
1453 | decompressor => $decomp, | |
1454 | }; | |
1455 | ||
1456 | return $info; | |
1457 | } | |
1458 | ||
1459 | sub archive_info { | |
1460 | my ($archive) = shift; | |
1461 | my $info; | |
1462 | ||
1463 | my $volid = basename($archive); | |
28be2a43 | 1464 | if ($volid =~ /^(vzdump-(lxc|openvz|qemu)-.+\.(tgz$|tar|vma)(?:\.(${\PVE::Storage::Plugin::COMPRESSOR_RE}))?)$/) { |
e34afeb1 FE |
1465 | my $filename = "$1"; # untaint |
1466 | my ($type, $format, $comp) = ($2, $3, $4); | |
fb821c18 FE |
1467 | my $format_re = defined($comp) ? "$format.$comp" : "$format"; |
1468 | $info = decompressor_info($format, $comp); | |
e34afeb1 | 1469 | $info->{filename} = $filename; |
fb821c18 FE |
1470 | $info->{type} = $type; |
1471 | ||
e34afeb1 FE |
1472 | if ($volid =~ /^(vzdump-${type}-([1-9][0-9]{2,8})-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2}))\.${format_re}$/) { |
1473 | $info->{logfilename} = "$1.log"; | |
1474 | $info->{vmid} = int($2); | |
b1ddc54a | 1475 | $info->{ctime} = timelocal($8, $7, $6, $5, $4 - 1, $3); |
fb821c18 FE |
1476 | $info->{is_std_name} = 1; |
1477 | } else { | |
1478 | $info->{is_std_name} = 0; | |
1479 | } | |
cd554b79 | 1480 | } else { |
bf5af0fb | 1481 | die "ERROR: couldn't determine archive info from '$archive'\n"; |
cd554b79 AA |
1482 | } |
1483 | ||
1484 | return $info; | |
1485 | } | |
1486 | ||
35a39532 FE |
1487 | sub archive_remove { |
1488 | my ($archive_path) = @_; | |
1489 | ||
1490 | my $dirname = dirname($archive_path); | |
1491 | my $archive_info = eval { archive_info($archive_path) } // {}; | |
1492 | my $logfn = $archive_info->{logfilename}; | |
1493 | ||
1494 | unlink $archive_path or die "removing archive $archive_path failed: $!\n"; | |
1495 | ||
1496 | if (defined($logfn)) { | |
1497 | my $logpath = "$dirname/$logfn"; | |
1498 | if (-e $logpath) { | |
1499 | unlink $logpath or warn "removing log file $logpath failed: $!\n"; | |
1500 | } | |
1501 | } | |
1502 | } | |
1503 | ||
8898dd7b FG |
1504 | sub extract_vzdump_config_tar { |
1505 | my ($archive, $conf_re) = @_; | |
1506 | ||
1507 | die "ERROR: file '$archive' does not exist\n" if ! -f $archive; | |
1508 | ||
1509 | my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) || | |
1510 | die "unable to open file '$archive'\n"; | |
1511 | ||
1512 | my $file; | |
1513 | while (defined($file = <$fh>)) { | |
086c4bf1 | 1514 | if ($file =~ $conf_re) { |
8898dd7b FG |
1515 | $file = $1; # untaint |
1516 | last; | |
1517 | } | |
1518 | } | |
1519 | ||
1520 | kill 15, $pid; | |
1521 | waitpid $pid, 0; | |
1522 | close $fh; | |
1523 | ||
1524 | die "ERROR: archive contains no configuration file\n" if !$file; | |
1525 | chomp $file; | |
1526 | ||
1527 | my $raw = ''; | |
1528 | my $out = sub { | |
1529 | my $output = shift; | |
1530 | $raw .= "$output\n"; | |
1531 | }; | |
1532 | ||
63e89295 | 1533 | run_command(['tar', '-xpOf', $archive, $file, '--occurrence'], outfunc => $out); |
8898dd7b FG |
1534 | |
1535 | return wantarray ? ($raw, $file) : $raw; | |
1536 | } | |
1537 | ||
1538 | sub extract_vzdump_config_vma { | |
1539 | my ($archive, $comp) = @_; | |
1540 | ||
8898dd7b | 1541 | my $raw = ''; |
63e89295 | 1542 | my $out = sub { $raw .= "$_[0]\n"; }; |
8898dd7b | 1543 | |
cd554b79 AA |
1544 | my $info = archive_info($archive); |
1545 | $comp //= $info->{compression}; | |
1546 | my $decompressor = $info->{decompressor}; | |
1547 | ||
8898dd7b | 1548 | if ($comp) { |
63e89295 | 1549 | my $cmd = [ [@$decompressor, $archive], ["vma", "config", "-"] ]; |
8898dd7b | 1550 | |
63e89295 | 1551 | # lzop/zcat exits with 1 when the pipe is closed early by vma, detect this and ignore the exit code later |
8898dd7b FG |
1552 | my $broken_pipe; |
1553 | my $errstring; | |
1554 | my $err = sub { | |
1555 | my $output = shift; | |
576e560d | 1556 | if ($output =~ m/lzop: Broken pipe: <stdout>/ || $output =~ m/gzip: stdout: Broken pipe/ || $output =~ m/zstd: error 70 : Write error.*Broken pipe/) { |
8898dd7b FG |
1557 | $broken_pipe = 1; |
1558 | } elsif (!defined ($errstring) && $output !~ m/^\s*$/) { | |
1559 | $errstring = "Failed to extract config from VMA archive: $output\n"; | |
1560 | } | |
1561 | }; | |
1562 | ||
63e89295 | 1563 | my $rc = eval { run_command($cmd, outfunc => $out, errfunc => $err, noerr => 1) }; |
8898dd7b FG |
1564 | my $rerr = $@; |
1565 | ||
63e89295 TL |
1566 | $broken_pipe ||= $rc == 141; # broken pipe from vma POV |
1567 | ||
1568 | if (!$errstring && !$broken_pipe && $rc != 0) { | |
8898dd7b FG |
1569 | die "$rerr\n" if $rerr; |
1570 | die "config extraction failed with exit code $rc\n"; | |
1571 | } | |
1572 | die "$errstring\n" if $errstring; | |
1573 | } else { | |
63e89295 | 1574 | run_command(["vma", "config", $archive], outfunc => $out); |
8898dd7b FG |
1575 | } |
1576 | ||
1577 | return wantarray ? ($raw, undef) : $raw; | |
1578 | } | |
1579 | ||
1580 | sub extract_vzdump_config { | |
1581 | my ($cfg, $volid) = @_; | |
1582 | ||
c855ac15 DM |
1583 | my ($storeid, $volname) = parse_volume_id($volid); |
1584 | if (defined($storeid)) { | |
1585 | my $scfg = storage_config($cfg, $storeid); | |
1586 | if ($scfg->{type} eq 'pbs') { | |
1587 | storage_check_enabled($cfg, $storeid); | |
1588 | return PVE::Storage::PBSPlugin->extract_vzdump_config($scfg, $volname, $storeid); | |
1589 | } | |
1590 | } | |
1591 | ||
8898dd7b | 1592 | my $archive = abs_filesystem_path($cfg, $volid); |
cd554b79 AA |
1593 | my $info = archive_info($archive); |
1594 | my $format = $info->{format}; | |
1595 | my $comp = $info->{compression}; | |
1596 | my $type = $info->{type}; | |
8898dd7b | 1597 | |
cd554b79 | 1598 | if ($type eq 'lxc' || $type eq 'openvz') { |
086c4bf1 | 1599 | return extract_vzdump_config_tar($archive, qr!^(\./etc/vzdump/(pct|vps)\.conf)$!); |
cd554b79 | 1600 | } elsif ($type eq 'qemu') { |
8898dd7b FG |
1601 | if ($format eq 'tar') { |
1602 | return extract_vzdump_config_tar($archive, qr!\(\./qemu-server\.conf\)!); | |
1603 | } else { | |
1604 | return extract_vzdump_config_vma($archive, $comp); | |
1605 | } | |
1606 | } else { | |
1607 | die "cannot determine backup guest type for backup archive '$volid'\n"; | |
1608 | } | |
1609 | } | |
1610 | ||
8f26b391 FE |
1611 | sub prune_backups { |
1612 | my ($cfg, $storeid, $keep, $vmid, $type, $dryrun, $logfunc) = @_; | |
1613 | ||
1614 | my $scfg = storage_config($cfg, $storeid); | |
1615 | die "storage '$storeid' does not support backups\n" if !$scfg->{content}->{backup}; | |
1616 | ||
1617 | if (!defined($keep)) { | |
1618 | die "no prune-backups options configured for storage '$storeid'\n" | |
1619 | if !defined($scfg->{'prune-backups'}); | |
1620 | $keep = PVE::JSONSchema::parse_property_string('prune-backups', $scfg->{'prune-backups'}); | |
1621 | } | |
1622 | ||
883c811f FE |
1623 | activate_storage($cfg, $storeid); |
1624 | ||
8f26b391 FE |
1625 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
1626 | return $plugin->prune_backups($scfg, $storeid, $keep, $vmid, $type, $dryrun, $logfunc); | |
1627 | } | |
1628 | ||
1629 | my $prune_mark = sub { | |
1630 | my ($prune_entries, $keep_count, $id_func) = @_; | |
1631 | ||
1632 | return if !$keep_count; | |
1633 | ||
1634 | my $already_included = {}; | |
1635 | my $newly_included = {}; | |
1636 | ||
1637 | foreach my $prune_entry (@{$prune_entries}) { | |
1638 | my $mark = $prune_entry->{mark}; | |
1639 | my $id = $id_func->($prune_entry->{ctime}); | |
10dfeb9e FE |
1640 | $already_included->{$id} = 1 if defined($mark) && $mark eq 'keep'; |
1641 | } | |
8f26b391 | 1642 | |
10dfeb9e FE |
1643 | foreach my $prune_entry (@{$prune_entries}) { |
1644 | my $mark = $prune_entry->{mark}; | |
1645 | my $id = $id_func->($prune_entry->{ctime}); | |
8f26b391 | 1646 | |
10dfeb9e | 1647 | next if defined($mark) || $already_included->{$id}; |
8f26b391 FE |
1648 | |
1649 | if (!$newly_included->{$id}) { | |
1650 | last if scalar(keys %{$newly_included}) >= $keep_count; | |
1651 | $newly_included->{$id} = 1; | |
1652 | $prune_entry->{mark} = 'keep'; | |
1653 | } else { | |
1654 | $prune_entry->{mark} = 'remove'; | |
1655 | } | |
1656 | } | |
1657 | }; | |
1658 | ||
1659 | sub prune_mark_backup_group { | |
1660 | my ($backup_group, $keep) = @_; | |
1661 | ||
a000e26c | 1662 | my @positive_opts = grep { $_ ne 'keep-all' && $keep->{$_} > 0 } keys $keep->%*; |
1b87f013 | 1663 | |
a000e26c | 1664 | if ($keep->{'keep-all'} || scalar(@positive_opts) == 0) { |
f514181d FE |
1665 | foreach my $prune_entry (@{$backup_group}) { |
1666 | $prune_entry->{mark} = 'keep'; | |
1667 | } | |
1668 | return; | |
1669 | } | |
1670 | ||
8f26b391 FE |
1671 | my $prune_list = [ sort { $b->{ctime} <=> $a->{ctime} } @{$backup_group} ]; |
1672 | ||
1673 | $prune_mark->($prune_list, $keep->{'keep-last'}, sub { | |
1674 | my ($ctime) = @_; | |
1675 | return $ctime; | |
1676 | }); | |
1677 | $prune_mark->($prune_list, $keep->{'keep-hourly'}, sub { | |
1678 | my ($ctime) = @_; | |
1679 | my (undef, undef, $hour, $day, $month, $year) = localtime($ctime); | |
1680 | return "$hour/$day/$month/$year"; | |
1681 | }); | |
1682 | $prune_mark->($prune_list, $keep->{'keep-daily'}, sub { | |
1683 | my ($ctime) = @_; | |
1684 | my (undef, undef, undef, $day, $month, $year) = localtime($ctime); | |
1685 | return "$day/$month/$year"; | |
1686 | }); | |
1687 | $prune_mark->($prune_list, $keep->{'keep-weekly'}, sub { | |
1688 | my ($ctime) = @_; | |
1689 | my ($sec, $min, $hour, $day, $month, $year) = localtime($ctime); | |
189e67ff FE |
1690 | my $iso_week = int(strftime("%V", $sec, $min, $hour, $day, $month, $year)); |
1691 | my $iso_week_year = int(strftime("%G", $sec, $min, $hour, $day, $month, $year)); | |
8f26b391 FE |
1692 | return "$iso_week/$iso_week_year"; |
1693 | }); | |
1694 | $prune_mark->($prune_list, $keep->{'keep-monthly'}, sub { | |
1695 | my ($ctime) = @_; | |
1696 | my (undef, undef, undef, undef, $month, $year) = localtime($ctime); | |
1697 | return "$month/$year"; | |
1698 | }); | |
1699 | $prune_mark->($prune_list, $keep->{'keep-yearly'}, sub { | |
1700 | my ($ctime) = @_; | |
1701 | my $year = (localtime($ctime))[5]; | |
1702 | return "$year"; | |
1703 | }); | |
1704 | ||
1705 | foreach my $prune_entry (@{$prune_list}) { | |
1706 | $prune_entry->{mark} //= 'remove'; | |
1707 | } | |
1708 | } | |
1709 | ||
3cc29a04 | 1710 | sub volume_export : prototype($$$$$$$) { |
47f37b53 WB |
1711 | my ($cfg, $fh, $volid, $format, $snapshot, $base_snapshot, $with_snapshots) = @_; |
1712 | ||
1713 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
1714 | die "cannot export volume '$volid'\n" if !$storeid; | |
1715 | my $scfg = storage_config($cfg, $storeid); | |
1716 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1717 | return $plugin->volume_export($scfg, $storeid, $fh, $volname, $format, | |
1718 | $snapshot, $base_snapshot, $with_snapshots); | |
1719 | } | |
1720 | ||
3cc29a04 WB |
1721 | sub volume_import : prototype($$$$$$$$) { |
1722 | my ($cfg, $fh, $volid, $format, $snapshot, $base_snapshot, $with_snapshots, $allow_rename) = @_; | |
47f37b53 WB |
1723 | |
1724 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
1725 | die "cannot import into volume '$volid'\n" if !$storeid; | |
1726 | my $scfg = storage_config($cfg, $storeid); | |
1727 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
3cc29a04 WB |
1728 | return $plugin->volume_import( |
1729 | $scfg, | |
1730 | $storeid, | |
1731 | $fh, | |
1732 | $volname, | |
1733 | $format, | |
1734 | $snapshot, | |
1735 | $base_snapshot, | |
1736 | $with_snapshots, | |
1737 | $allow_rename, | |
1738 | ) // $volid; | |
1739 | } | |
1740 | ||
1741 | sub volume_export_formats : prototype($$$$$) { | |
d390328b WB |
1742 | my ($cfg, $volid, $snapshot, $base_snapshot, $with_snapshots) = @_; |
1743 | ||
1744 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
1745 | return if !$storeid; | |
1746 | my $scfg = storage_config($cfg, $storeid); | |
1747 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1748 | return $plugin->volume_export_formats($scfg, $storeid, $volname, | |
ae36189d WB |
1749 | $snapshot, $base_snapshot, |
1750 | $with_snapshots); | |
d390328b WB |
1751 | } |
1752 | ||
3cc29a04 WB |
1753 | sub volume_import_formats : prototype($$$$$) { |
1754 | my ($cfg, $volid, $snapshot, $base_snapshot, $with_snapshots) = @_; | |
d390328b WB |
1755 | |
1756 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
1757 | return if !$storeid; | |
1758 | my $scfg = storage_config($cfg, $storeid); | |
1759 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
3cc29a04 WB |
1760 | return $plugin->volume_import_formats( |
1761 | $scfg, | |
1762 | $storeid, | |
1763 | $volname, | |
1764 | $snapshot, | |
1765 | $base_snapshot, | |
1766 | $with_snapshots, | |
1767 | ); | |
d390328b WB |
1768 | } |
1769 | ||
1770 | sub volume_transfer_formats { | |
1771 | my ($cfg, $src_volid, $dst_volid, $snapshot, $base_snapshot, $with_snapshots) = @_; | |
1772 | my @export_formats = volume_export_formats($cfg, $src_volid, $snapshot, $base_snapshot, $with_snapshots); | |
3cc29a04 | 1773 | my @import_formats = volume_import_formats($cfg, $dst_volid, $snapshot, $base_snapshot, $with_snapshots); |
d390328b WB |
1774 | my %import_hash = map { $_ => 1 } @import_formats; |
1775 | my @common = grep { $import_hash{$_} } @export_formats; | |
1776 | return @common; | |
1777 | } | |
1778 | ||
a97d3ee4 FE |
1779 | sub volume_imported_message { |
1780 | my ($volid, $want_pattern) = @_; | |
1781 | ||
1782 | if ($want_pattern) { | |
1783 | return qr/successfully imported '([^']*)'$/; | |
1784 | } else { | |
1785 | return "successfully imported '$volid'\n"; | |
1786 | } | |
1787 | } | |
1788 | ||
f7621c01 DM |
1789 | # bash completion helper |
1790 | ||
1791 | sub complete_storage { | |
746e530f | 1792 | my ($cmdname, $pname, $cvalue) = @_; |
f7621c01 | 1793 | |
746e530f | 1794 | my $cfg = PVE::Storage::config(); |
180c8b02 | 1795 | |
746e530f | 1796 | return $cmdname eq 'add' ? [] : [ PVE::Storage::storage_ids($cfg) ]; |
f7621c01 DM |
1797 | } |
1798 | ||
1799 | sub complete_storage_enabled { | |
746e530f | 1800 | my ($cmdname, $pname, $cvalue) = @_; |
f7621c01 | 1801 | |
746e530f | 1802 | my $res = []; |
f7621c01 | 1803 | |
746e530f DM |
1804 | my $cfg = PVE::Storage::config(); |
1805 | foreach my $sid (keys %{$cfg->{ids}}) { | |
1806 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
1807 | push @$res, $sid; | |
1808 | } | |
1809 | return $res; | |
f7621c01 DM |
1810 | } |
1811 | ||
98437f4c DM |
1812 | sub complete_content_type { |
1813 | my ($cmdname, $pname, $cvalue) = @_; | |
1814 | ||
7c7ae12f | 1815 | return [qw(rootdir images vztmpl iso backup snippets)]; |
98437f4c DM |
1816 | } |
1817 | ||
bf7aed26 DM |
1818 | sub complete_volume { |
1819 | my ($cmdname, $pname, $cvalue) = @_; | |
1820 | ||
1821 | my $cfg = config(); | |
1822 | ||
1823 | my $storage_list = complete_storage_enabled(); | |
1824 | ||
b70b0c58 DM |
1825 | if ($cvalue =~ m/^([^:]+):/) { |
1826 | $storage_list = [ $1 ]; | |
1827 | } else { | |
1828 | if (scalar(@$storage_list) > 1) { | |
1829 | # only list storage IDs to avoid large listings | |
1830 | my $res = []; | |
1831 | foreach my $storeid (@$storage_list) { | |
1832 | # Hack: simply return 2 artificial values, so that | |
1833 | # completions does not finish | |
1834 | push @$res, "$storeid:volname", "$storeid:..."; | |
1835 | } | |
1836 | return $res; | |
1837 | } | |
1838 | } | |
1839 | ||
bf7aed26 DM |
1840 | my $res = []; |
1841 | foreach my $storeid (@$storage_list) { | |
1842 | my $vollist = PVE::Storage::volume_list($cfg, $storeid); | |
1843 | ||
1844 | foreach my $item (@$vollist) { | |
1845 | push @$res, $item->{volid}; | |
1846 | } | |
1847 | } | |
1848 | ||
1849 | return $res; | |
1850 | } | |
1851 | ||
9edb99a5 WB |
1852 | # Various io-heavy operations require io/bandwidth limits which can be |
1853 | # configured on multiple levels: The global defaults in datacenter.cfg, and | |
1854 | # per-storage overrides. When we want to do a restore from storage A to storage | |
1855 | # B, we should take the smaller limit defined for storages A and B, and if no | |
1856 | # such limit was specified, use the one from datacenter.cfg. | |
1857 | sub get_bandwidth_limit { | |
1858 | my ($operation, $storage_list, $override) = @_; | |
1859 | ||
1860 | # called for each limit (global, per-storage) with the 'default' and the | |
ffc31266 | 1861 | # $operation limit and should update $override for every limit affecting |
9edb99a5 WB |
1862 | # us. |
1863 | my $use_global_limits = 0; | |
1864 | my $apply_limit = sub { | |
1865 | my ($bwlimit) = @_; | |
1866 | if (defined($bwlimit)) { | |
1867 | my $limits = PVE::JSONSchema::parse_property_string('bwlimit', $bwlimit); | |
1868 | my $limit = $limits->{$operation} // $limits->{default}; | |
1869 | if (defined($limit)) { | |
1870 | if (!$override || $limit < $override) { | |
1871 | $override = $limit; | |
1872 | } | |
1873 | return; | |
1874 | } | |
1875 | } | |
1876 | # If there was no applicable limit, try to apply the global ones. | |
1877 | $use_global_limits = 1; | |
1878 | }; | |
1879 | ||
77445e9b WB |
1880 | my ($rpcenv, $authuser); |
1881 | if (defined($override)) { | |
1882 | $rpcenv = PVE::RPCEnvironment->get(); | |
1883 | $authuser = $rpcenv->get_user(); | |
1884 | } | |
9edb99a5 WB |
1885 | |
1886 | # Apply per-storage limits - if there are storages involved. | |
074bdd35 | 1887 | if (defined($storage_list) && @$storage_list) { |
9edb99a5 WB |
1888 | my $config = config(); |
1889 | ||
1890 | # The Datastore.Allocate permission allows us to modify the per-storage | |
1891 | # limits, therefore it also allows us to override them. | |
1892 | # Since we have most likely multiple storages to check, do a quick check on | |
1893 | # the general '/storage' path to see if we can skip the checks entirely: | |
77445e9b | 1894 | return $override if $rpcenv && $rpcenv->check($authuser, '/storage', ['Datastore.Allocate'], 1); |
9edb99a5 WB |
1895 | |
1896 | my %done; | |
1897 | foreach my $storage (@$storage_list) { | |
396aedff | 1898 | next if !defined($storage); |
9edb99a5 WB |
1899 | # Avoid duplicate checks: |
1900 | next if $done{$storage}; | |
1901 | $done{$storage} = 1; | |
1902 | ||
1903 | # Otherwise we may still have individual /storage/$ID permissions: | |
77445e9b | 1904 | if (!$rpcenv || !$rpcenv->check($authuser, "/storage/$storage", ['Datastore.Allocate'], 1)) { |
9edb99a5 WB |
1905 | # And if not: apply the limits. |
1906 | my $storecfg = storage_config($config, $storage); | |
1907 | $apply_limit->($storecfg->{bwlimit}); | |
1908 | } | |
1909 | } | |
1910 | ||
1911 | # Storage limits take precedence over the datacenter defaults, so if | |
1912 | # a limit was applied: | |
1913 | return $override if !$use_global_limits; | |
1914 | } | |
1915 | ||
1916 | # Sys.Modify on '/' means we can change datacenter.cfg which contains the | |
1917 | # global default limits. | |
77445e9b | 1918 | if (!$rpcenv || !$rpcenv->check($authuser, '/', ['Sys.Modify'], 1)) { |
9edb99a5 WB |
1919 | # So if we cannot modify global limits, apply them to our currently |
1920 | # requested override. | |
1921 | my $dc = cfs_read_file('datacenter.cfg'); | |
1922 | $apply_limit->($dc->{bwlimit}); | |
1923 | } | |
1924 | ||
1925 | return $override; | |
1926 | } | |
1927 | ||
76c1e57b | 1928 | # checks if the storage id is available and dies if not |
9280153e TL |
1929 | sub assert_sid_unused { |
1930 | my ($sid) = @_; | |
76c1e57b DC |
1931 | |
1932 | my $cfg = config(); | |
9280153e TL |
1933 | if (my $scfg = storage_config($cfg, $sid, 1)) { |
1934 | die "storage ID '$sid' already defined\n"; | |
76c1e57b DC |
1935 | } |
1936 | ||
1937 | return undef; | |
1938 | } | |
1939 | ||
edda43ed LS |
1940 | # removes leading/trailing spaces and (back)slashes completely |
1941 | # substitutes every non-ASCII-alphanumerical char with '_', except '_.-' | |
1942 | sub normalize_content_filename { | |
1943 | my ($filename) = @_; | |
1944 | ||
1945 | chomp $filename; | |
1946 | $filename =~ s/^.*[\/\\]//; | |
1947 | $filename =~ s/[^a-zA-Z0-9_.-]/_/g; | |
1948 | ||
1949 | return $filename; | |
1950 | } | |
1951 | ||
b6cf0a66 | 1952 | 1; |