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