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