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