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