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