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