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