15 use PVE
::Tools
qw(run_command file_read_firstline dir_glob_foreach $IPV6RE);
16 use PVE
::Cluster
qw(cfs_read_file cfs_write_file cfs_lock_file);
17 use PVE
::Exception
qw(raise_param_exc);
20 use PVE
::RPCEnvironment
;
22 use PVE
::Storage
::Plugin
;
23 use PVE
::Storage
::DirPlugin
;
24 use PVE
::Storage
::LVMPlugin
;
25 use PVE
::Storage
::LvmThinPlugin
;
26 use PVE
::Storage
::NFSPlugin
;
27 use PVE
::Storage
::ISCSIPlugin
;
28 use PVE
::Storage
::RBDPlugin
;
29 use PVE
::Storage
::SheepdogPlugin
;
30 use PVE
::Storage
::ISCSIDirectPlugin
;
31 use PVE
::Storage
::GlusterfsPlugin
;
32 use PVE
::Storage
::ZFSPoolPlugin
;
33 use PVE
::Storage
::ZFSPlugin
;
34 use PVE
::Storage
::DRBDPlugin
;
36 # Storage API version. Icrement it on changes in storage API interface.
37 use constant APIVER
=> 1;
39 # load standard plugins
40 PVE
::Storage
::DirPlugin-
>register();
41 PVE
::Storage
::LVMPlugin-
>register();
42 PVE
::Storage
::LvmThinPlugin-
>register();
43 PVE
::Storage
::NFSPlugin-
>register();
44 PVE
::Storage
::ISCSIPlugin-
>register();
45 PVE
::Storage
::RBDPlugin-
>register();
46 PVE
::Storage
::SheepdogPlugin-
>register();
47 PVE
::Storage
::ISCSIDirectPlugin-
>register();
48 PVE
::Storage
::GlusterfsPlugin-
>register();
49 PVE
::Storage
::ZFSPoolPlugin-
>register();
50 PVE
::Storage
::ZFSPlugin-
>register();
51 PVE
::Storage
::DRBDPlugin-
>register();
53 # load third-party plugins
54 if ( -d
'/usr/share/perl5/PVE/Storage/Custom' ) {
55 dir_glob_foreach
('/usr/share/perl5/PVE/Storage/Custom', '.*\.pm$', sub {
57 my $modname = 'PVE::Storage::Custom::' . $file;
58 $modname =~ s!\.pm$!!;
59 $file = 'PVE/Storage/Custom/' . $file;
66 # Check storage API version and that file is really storage plugin.
67 } elsif ($modname->isa('PVE::Storage::Plugin') && $modname->can('api') && $modname->api() == APIVER
) {
74 warn "Error loading storage plugin \"$modname\" because of API version mismatch
. Please
, update it
.\n"
79 # initialize all plugins
80 PVE::Storage::Plugin->init();
82 my $UDEVADM = '/sbin/udevadm';
84 # PVE::Storage utility functions
87 return cfs_read_file("storage
.cfg
");
93 cfs_write_file('storage.cfg', $cfg);
96 sub lock_storage_config {
97 my ($code, $errmsg) = @_;
99 cfs_lock_file("storage
.cfg
", undef, $code);
102 $errmsg ? die "$errmsg: $err" : die $err;
107 my ($cfg, $storeid, $noerr) = @_;
109 die "no storage ID specified
\n" if !$storeid;
111 my $scfg = $cfg->{ids}->{$storeid};
113 die "storage
'$storeid' does not exists\n" if (!$noerr && !$scfg);
118 sub storage_check_node {
119 my ($cfg, $storeid, $node, $noerr) = @_;
121 my $scfg = storage_config($cfg, $storeid);
123 if ($scfg->{nodes}) {
124 $node = PVE::INotify::nodename() if !$node || ($node eq 'localhost');
125 if (!$scfg->{nodes}->{$node}) {
126 die "storage
'$storeid' is not available on node
'$node'\n" if !$noerr;
134 sub storage_check_enabled {
135 my ($cfg, $storeid, $node, $noerr) = @_;
137 my $scfg = storage_config($cfg, $storeid);
139 if ($scfg->{disable}) {
140 die "storage
'$storeid' is disabled
\n" if !$noerr;
144 return storage_check_node($cfg, $storeid, $node, $noerr);
150 return keys %{$cfg->{ids}};
154 my ($filename, $timeout) = @_;
156 return PVE::Storage::Plugin::file_size_info($filename, $timeout);
159 sub volume_size_info {
160 my ($cfg, $volid, $timeout) = @_;
162 my ($storeid, $volname) = parse_volume_id($volid, 1);
164 my $scfg = storage_config($cfg, $storeid);
165 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
166 return $plugin->volume_size_info($scfg, $storeid, $volname, $timeout);
167 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
168 return file_size_info($volid, $timeout);
175 my ($cfg, $volid, $size, $running) = @_;
177 my ($storeid, $volname) = parse_volume_id($volid, 1);
179 my $scfg = storage_config($cfg, $storeid);
180 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
181 return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running);
182 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
183 die "resize file
/device
'$volid' is not possible
\n";
185 die "unable to parse volume ID
'$volid'\n";
189 sub volume_rollback_is_possible {
190 my ($cfg, $volid, $snap) = @_;
192 my ($storeid, $volname) = parse_volume_id($volid, 1);
194 my $scfg = storage_config($cfg, $storeid);
195 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
196 return $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
197 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
198 die "snapshot rollback file
/device
'$volid' is not possible
\n";
200 die "unable to parse volume ID
'$volid'\n";
204 sub volume_snapshot {
205 my ($cfg, $volid, $snap) = @_;
207 my ($storeid, $volname) = parse_volume_id($volid, 1);
209 my $scfg = storage_config($cfg, $storeid);
210 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
211 return $plugin->volume_snapshot($scfg, $storeid, $volname, $snap);
212 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
213 die "snapshot file
/device
'$volid' is not possible
\n";
215 die "unable to parse volume ID
'$volid'\n";
219 sub volume_snapshot_rollback {
220 my ($cfg, $volid, $snap) = @_;
222 my ($storeid, $volname) = parse_volume_id($volid, 1);
224 my $scfg = storage_config($cfg, $storeid);
225 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
226 $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
227 return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap);
228 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
229 die "snapshot rollback file
/device
'$volid' is not possible
\n";
231 die "unable to parse volume ID
'$volid'\n";
235 sub volume_snapshot_delete {
236 my ($cfg, $volid, $snap, $running) = @_;
238 my ($storeid, $volname) = parse_volume_id($volid, 1);
240 my $scfg = storage_config($cfg, $storeid);
241 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
242 return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running);
243 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
244 die "snapshot
delete file
/device
'$volid' is not possible
\n";
246 die "unable to parse volume ID
'$volid'\n";
250 sub volume_has_feature {
251 my ($cfg, $feature, $volid, $snap, $running) = @_;
253 my ($storeid, $volname) = parse_volume_id($volid, 1);
255 my $scfg = storage_config($cfg, $storeid);
256 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
257 return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running);
258 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
266 my ($cfg, $storeid, $vmid) = @_;
268 my $scfg = storage_config($cfg, $storeid);
269 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
271 my $path = $plugin->get_subdir($scfg, 'images');
273 return $vmid ? "$path/$vmid" : $path;
276 sub get_private_dir {
277 my ($cfg, $storeid, $vmid) = @_;
279 my $scfg = storage_config($cfg, $storeid);
280 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
282 my $path = $plugin->get_subdir($scfg, 'rootdir');
284 return $vmid ? "$path/$vmid" : $path;
288 my ($cfg, $storeid) = @_;
290 my $scfg = storage_config($cfg, $storeid);
291 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
293 return $plugin->get_subdir($scfg, 'iso');
297 my ($cfg, $storeid) = @_;
299 my $scfg = storage_config($cfg, $storeid);
300 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
302 return $plugin->get_subdir($scfg, 'vztmpl');
306 my ($cfg, $storeid) = @_;
308 my $scfg = storage_config($cfg, $storeid);
309 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
311 return $plugin->get_subdir($scfg, 'backup');
314 # library implementation
319 die "VMID
'$vmid' contains illegal characters
\n" if $vmid !~ m/^\d+$/;
325 my ($cfg, $volid) = @_;
327 my ($storeid, $volname) = parse_volume_id($volid);
329 my $scfg = storage_config($cfg, $storeid);
331 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
333 # returns ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format)
335 return $plugin->parse_volname($volname);
338 sub parse_volume_id {
339 my ($volid, $noerr) = @_;
341 return PVE::Storage::Plugin::parse_volume_id($volid, $noerr);
344 my $volume_is_base_and_used__no_lock = sub {
345 my ($scfg, $storeid, $plugin, $volname) = @_;
347 my ($vtype, $name, $vmid, undef, undef, $isBase, undef) =
348 $plugin->parse_volname($volname);
351 my $vollist = $plugin->list_images($storeid, $scfg);
352 foreach my $info (@$vollist) {
353 my (undef, $tmpvolname) = parse_volume_id($info->{volid});
354 my $basename = undef;
355 my $basevmid = undef;
358 (undef, undef, undef, $basename, $basevmid) =
359 $plugin->parse_volname($tmpvolname);
362 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
370 sub volume_is_base_and_used {
371 my ($cfg, $volid) = @_;
373 my ($storeid, $volname) = parse_volume_id($volid);
374 my $scfg = storage_config($cfg, $storeid);
375 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
377 $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
378 return &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
382 # try to map a filesystem path to a volume identifier
383 sub path_to_volume_id {
384 my ($cfg, $path) = @_;
386 my $ids = $cfg->{ids};
388 my ($sid, $volname) = parse_volume_id($path, 1);
390 if (my $scfg = $ids->{$sid}) {
392 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
393 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
394 return ($vtype, $path);
400 # Note: abs_path() return undef if $path doesn not exist
401 # for example when nfs storage is not mounted
402 $path = abs_path($path) || $path;
404 foreach my $sid (keys %$ids) {
405 my $scfg = $ids->{$sid};
406 next if !$scfg->{path};
407 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
408 my $imagedir = $plugin->get_subdir($scfg, 'images');
409 my $isodir = $plugin->get_subdir($scfg, 'iso');
410 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
411 my $backupdir = $plugin->get_subdir($scfg, 'backup');
412 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
414 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
418 my $vollist = $plugin->list_images($sid, $scfg, $vmid);
419 foreach my $info (@$vollist) {
420 my ($storeid, $volname) = parse_volume_id($info->{volid});
421 my $volpath = $plugin->path($scfg, $volname, $storeid);
422 if ($volpath eq $path) {
423 return ('images', $info->{volid});
426 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
428 return ('iso', "$sid:iso
/$name");
429 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
431 return ('vztmpl', "$sid:vztmpl
/$name");
432 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
434 return ('rootdir', "$sid:rootdir
/$vmid");
435 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!) {
437 return ('iso', "$sid:backup
/$name");
441 # can't map path to volume id
446 my ($cfg, $volid, $snapname) = @_;
448 my ($storeid, $volname) = parse_volume_id($volid);
450 my $scfg = storage_config($cfg, $storeid);
452 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
453 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname);
454 return wantarray ? ($path, $owner, $vtype) : $path;
457 sub abs_filesystem_path {
458 my ($cfg, $volid) = @_;
461 if (PVE::Storage::parse_volume_id ($volid, 1)) {
462 PVE::Storage::activate_volumes($cfg, [ $volid ]);
463 $path = PVE::Storage::path($cfg, $volid);
466 my $abspath = abs_path($volid);
467 if ($abspath && $abspath =~ m|^(/.+)$|) {
468 $path = $1; # untaint any path
473 die "can
't find file '$volid'\n" if !($path && -f $path);
478 sub storage_migrate {
479 my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
481 my ($storeid, $volname) = parse_volume_id($volid);
482 $target_volname = $volname if !$target_volname;
484 my $scfg = storage_config($cfg, $storeid);
486 # no need to migrate shared content
487 return if $storeid eq $target_storeid && $scfg->{shared};
489 my $tcfg = storage_config($cfg, $target_storeid);
491 my $target_volid = "${target_storeid}:${target_volname}";
493 my $errstr = "unable to migrate '$volid' to '${target_volid
}' on host '$target_host'";
495 my $sshoptions = "-o 'BatchMode
=yes
'";
496 my $ssh = "/usr/bin/ssh $sshoptions";
498 local $ENV{RSYNC_RSH} = $ssh;
500 # only implemented for file system based storage
504 my $src_plugin = PVE::Storage::Plugin->lookup($scfg->{type});
505 my $dst_plugin = PVE::Storage::Plugin->lookup($tcfg->{type});
506 my $src = $src_plugin->path($scfg, $volname, $storeid);
507 my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid);
509 my $dirname = dirname($dst);
511 if ($tcfg->{shared}) { # we can do a local copy
513 run_command(['/bin/mkdir
', '-p
', $dirname]);
515 run_command(['/bin/cp', $src, $dst]);
518 run_command(['/usr/bin
/ssh
', "root\@${target_host}",
519 '/bin/mkdir
', '-p
', $dirname]);
521 # we use rsync with --sparse, so we can't
use --inplace
,
522 # so we remove file on the target if it already exists to
524 my ($size, $format) = PVE
::Storage
::Plugin
::file_size_info
($src);
525 if ($format && ($format eq 'raw') && $size) {
526 run_command
(['/usr/bin/ssh', "root\@${target_host}",
532 if ($format eq 'subvol') {
533 $cmd = ['/usr/bin/rsync', '--progress', '-X', '-A', '--numeric-ids',
534 '-aH', '--delete', '--no-whole-file', '--inplace',
535 '--one-file-system', "$src/", "[root\@${target_host}]:$dst"];
537 $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
538 $src, "[root\@${target_host}]:$dst"];
543 run_command
($cmd, outfunc
=> sub {
546 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
549 print "rsync status: $1\n";
559 die "$errstr - target type '$tcfg->{type}' not implemented\n";
562 } elsif ($scfg->{type
} eq 'zfspool') {
564 if ($tcfg->{type
} eq 'zfspool') {
566 die "$errstr - pool on target does not have the same name as on source!"
567 if $tcfg->{pool
} ne $scfg->{pool
};
569 my (undef, $volname) = parse_volname
($cfg, $volid);
571 my $zfspath = "$scfg->{pool}\/$volname";
573 my $snap = ['zfs', 'snapshot', "$zfspath\@__migration__"];
575 my $send = [['zfs', 'send', '-Rpv', "$zfspath\@__migration__"], ['ssh', "root\@$target_host",
576 'zfs', 'recv', $zfspath]];
578 my $destroy_target = ['ssh', "root\@$target_host", 'zfs', 'destroy', "$zfspath\@__migration__"];
585 run_command
(['zfs', 'destroy', "$zfspath\@__migration__"]);
588 run_command
($destroy_target);
591 die "$errstr - target type $tcfg->{type} is not valid\n";
594 } elsif ($scfg->{type
} eq 'lvmthin' || $scfg->{type
} eq 'lvm') {
596 if (($scfg->{type
} eq $tcfg->{type
}) &&
597 ($tcfg->{type
} eq 'lvmthin' || $tcfg->{type
} eq 'lvm')) {
599 my (undef, $volname, $vmid) = parse_volname
($cfg, $volid);
600 my $size = volume_size_info
($cfg, $volid, 5);
601 my $src = path
($cfg, $volid);
602 my $dst = path
($cfg, $target_volid);
604 run_command
(['/usr/bin/ssh', "root\@${target_host}",
605 'pvesm', 'alloc', $target_storeid, $vmid,
606 $target_volname, int($size/1024)]);
609 if ($tcfg->{type
} eq 'lvmthin') {
610 run_command
([["dd", "if=$src"],["/usr/bin/ssh", "root\@${target_host}",
611 "dd", 'conv=sparse', "of=$dst"]]);
613 run_command
([["dd", "if=$src"],["/usr/bin/ssh", "root\@${target_host}",
618 run_command
(['/usr/bin/ssh', "root\@${target_host}",
619 'pvesm', 'free', $target_volid]);
623 die "$errstr - migrate from source type '$scfg->{type}' to '$tcfg->{type}' not implemented\n";
626 die "$errstr - source type '$scfg->{type}' not implemented\n";
631 my ($cfg, $volid, $vmid, $snap) = @_;
633 my ($storeid, $volname) = parse_volume_id
($volid);
635 my $scfg = storage_config
($cfg, $storeid);
637 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
639 activate_storage
($cfg, $storeid);
641 # lock shared storage
642 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
643 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
644 return "$storeid:$volname";
648 sub vdisk_create_base
{
649 my ($cfg, $volid) = @_;
651 my ($storeid, $volname) = parse_volume_id
($volid);
653 my $scfg = storage_config
($cfg, $storeid);
655 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
657 activate_storage
($cfg, $storeid);
659 # lock shared storage
660 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
661 my $volname = $plugin->create_base($storeid, $scfg, $volname);
662 return "$storeid:$volname";
667 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
669 die "no storage ID specified\n" if !$storeid;
671 PVE
::JSONSchema
::parse_storage_id
($storeid);
673 my $scfg = storage_config
($cfg, $storeid);
675 die "no VMID specified\n" if !$vmid;
677 $vmid = parse_vmid
($vmid);
679 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
681 $fmt = $defformat if !$fmt;
683 activate_storage
($cfg, $storeid);
685 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
687 # lock shared storage
688 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
689 my $old_umask = umask(umask|0037);
690 my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) };
694 return "$storeid:$volname";
699 my ($cfg, $volid) = @_;
701 my ($storeid, $volname) = parse_volume_id
($volid);
702 my $scfg = storage_config
($cfg, $storeid);
703 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
705 activate_storage
($cfg, $storeid);
709 # lock shared storage
710 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
711 die "base volume '$volname' is still in use by linked clones\n"
712 if &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
714 my (undef, undef, undef, undef, undef, $isBase, $format) =
715 $plugin->parse_volname($volname);
716 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format);
719 return if !$cleanup_worker;
721 my $rpcenv = PVE
::RPCEnvironment
::get
();
722 my $authuser = $rpcenv->get_user();
724 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
727 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
729 my ($cfg, $storeid, $tt) = @_;
731 die "unknown template type '$tt'\n"
732 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
734 my $ids = $cfg->{ids
};
736 storage_check_enabled
($cfg, $storeid) if ($storeid);
742 foreach my $sid (keys %$ids) {
743 next if $storeid && $storeid ne $sid;
745 my $scfg = $ids->{$sid};
746 my $type = $scfg->{type
};
748 next if !storage_check_enabled
($cfg, $sid, undef, 1);
750 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
751 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
752 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
754 activate_storage
($cfg, $sid);
757 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
759 my $path = $plugin->get_subdir($scfg, $tt);
761 foreach my $fn (<$path/*>) {
766 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
768 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
770 } elsif ($tt eq 'vztmpl') {
771 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
773 $info = { volid
=> "$sid:vztmpl/$1", format
=> "t$2" };
775 } elsif ($tt eq 'backup') {
776 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
778 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
781 $info->{size
} = -s
$fn;
783 push @{$res->{$sid}}, $info;
788 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
796 my ($cfg, $storeid, $vmid, $vollist) = @_;
798 my $ids = $cfg->{ids
};
800 storage_check_enabled
($cfg, $storeid) if ($storeid);
804 # prepare/activate/refresh all storages
806 my $storage_list = [];
808 foreach my $volid (@$vollist) {
809 my ($sid, undef) = parse_volume_id
($volid);
810 next if !defined($ids->{$sid});
811 next if !storage_check_enabled
($cfg, $sid, undef, 1);
812 push @$storage_list, $sid;
815 foreach my $sid (keys %$ids) {
816 next if $storeid && $storeid ne $sid;
817 next if !storage_check_enabled
($cfg, $sid, undef, 1);
818 push @$storage_list, $sid;
824 activate_storage_list
($cfg, $storage_list, $cache);
826 foreach my $sid (keys %$ids) {
827 next if $storeid && $storeid ne $sid;
828 next if !storage_check_enabled
($cfg, $sid, undef, 1);
830 my $scfg = $ids->{$sid};
831 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
832 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
833 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
840 my ($cfg, $storeid, $vmid, $content) = @_;
842 my @ctypes = qw(images vztmpl iso backup);
844 my $cts = $content ?
[ $content ] : [ @ctypes ];
846 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
849 foreach my $ct (@$cts) {
851 if ($ct eq 'images') {
852 $data = vdisk_list
($cfg, $storeid, $vmid);
853 } elsif ($ct eq 'iso' && !defined($vmid)) {
854 $data = template_list
($cfg, $storeid, 'iso');
855 } elsif ($ct eq 'vztmpl'&& !defined($vmid)) {
856 $data = template_list
($cfg, $storeid, 'vztmpl');
857 } elsif ($ct eq 'backup') {
858 $data = template_list
($cfg, $storeid, 'backup');
859 foreach my $item (@{$data->{$storeid}}) {
860 if (defined($vmid)) {
861 @{$data->{$storeid}} = grep { $_->{volid
} =~ m/\S+-$vmid-\S+/ } @{$data->{$storeid}};
866 next if !$data || !$data->{$storeid};
868 foreach my $item (@{$data->{$storeid}}) {
869 $item->{content
} = $ct;
879 my $filename = "/sys/kernel/uevent_seqnum";
882 if (my $fh = IO
::File-
>new($filename, "r")) {
884 if ($line =~ m/^(\d+)$/) {
892 sub activate_storage
{
893 my ($cfg, $storeid, $cache) = @_;
895 $cache = {} if !$cache;
897 my $scfg = storage_check_enabled
($cfg, $storeid);
899 return if $cache->{activated
}->{$storeid};
901 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
903 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
906 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
907 activate_storage
($cfg, $baseid, $cache);
910 if (!$plugin->check_connection($storeid, $scfg)) {
911 die "storage '$storeid' is not online\n";
914 $plugin->activate_storage($storeid, $scfg, $cache);
916 my $newseq = uevent_seqnum
();
918 # only call udevsettle if there are events
919 if ($newseq > $cache->{uevent_seqnum
}) {
921 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
922 $cache->{uevent_seqnum
} = $newseq;
925 $cache->{activated
}->{$storeid} = 1;
928 sub activate_storage_list
{
929 my ($cfg, $storeid_list, $cache) = @_;
931 $cache = {} if !$cache;
933 foreach my $storeid (@$storeid_list) {
934 activate_storage
($cfg, $storeid, $cache);
938 sub deactivate_storage
{
939 my ($cfg, $storeid) = @_;
941 my $scfg = storage_config
($cfg, $storeid);
942 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
945 $plugin->deactivate_storage($storeid, $scfg, $cache);
948 sub activate_volumes
{
949 my ($cfg, $vollist, $snapname) = @_;
951 return if !($vollist && scalar(@$vollist));
953 my $storagehash = {};
954 foreach my $volid (@$vollist) {
955 my ($storeid, undef) = parse_volume_id
($volid);
956 $storagehash->{$storeid} = 1;
961 activate_storage_list
($cfg, [keys %$storagehash], $cache);
963 foreach my $volid (@$vollist) {
964 my ($storeid, $volname) = parse_volume_id
($volid);
965 my $scfg = storage_config
($cfg, $storeid);
966 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
967 $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache);
971 sub deactivate_volumes
{
972 my ($cfg, $vollist, $snapname) = @_;
974 return if !($vollist && scalar(@$vollist));
979 foreach my $volid (@$vollist) {
980 my ($storeid, $volname) = parse_volume_id
($volid);
982 my $scfg = storage_config
($cfg, $storeid);
983 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
986 $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache);
990 push @errlist, $volid;
994 die "volume deactivation failed: " . join(' ', @errlist)
999 my ($cfg, $content) = @_;
1001 my $ids = $cfg->{ids
};
1005 my @ctypes = PVE
::Tools
::split_list
($content);
1008 foreach my $storeid (keys %$ids) {
1010 next if !storage_check_enabled
($cfg, $storeid, undef, 1);
1012 if (defined($content)) {
1014 foreach my $ctype (@ctypes) {
1015 if ($ids->{$storeid}->{content
}->{$ctype}) {
1020 next if !$want_ctype;
1023 my $type = $ids->{$storeid}->{type
};
1025 $info->{$storeid} = {
1030 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
1031 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
1035 push @$slist, $storeid;
1040 foreach my $storeid (keys %$ids) {
1041 my $scfg = $ids->{$storeid};
1042 next if !$info->{$storeid};
1044 eval { activate_storage
($cfg, $storeid, $cache); };
1050 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1051 my ($total, $avail, $used, $active);
1052 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
1055 $info->{$storeid}->{total
} = $total;
1056 $info->{$storeid}->{avail
} = $avail;
1057 $info->{$storeid}->{used
} = $used;
1058 $info->{$storeid}->{active
} = $active;
1067 my ($packed_ip, $family);
1069 my @res = PVE
::Tools
::getaddrinfo_all
($server);
1070 $family = $res[0]->{family
};
1071 $packed_ip = (PVE
::Tools
::unpack_sockaddr_in46
($res[0]->{addr
}))[2];
1073 if (defined $packed_ip) {
1074 return Socket
::inet_ntop
($family, $packed_ip);
1080 my ($server_in) = @_;
1083 if (!($server = resolv_server
($server_in))) {
1084 die "unable to resolve address for server '${server_in}'\n";
1087 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
1090 run_command
($cmd, outfunc
=> sub {
1093 # note: howto handle white spaces in export path??
1094 if ($line =~ m!^(/\S+)\s+(.+)$!) {
1104 my $cmd = ['zfs', 'list', '-t', 'filesystem', '-H', '-o', 'name,avail,used'];
1107 run_command
($cmd, outfunc
=> sub {
1110 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
1111 my ($pool, $size_str, $used_str) = ($1, $2, $3);
1112 my $size = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($size_str);
1113 my $used = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($used_str);
1114 # ignore subvolumes generated by our ZFSPoolPlugin
1115 return if $pool =~ m!/subvol-\d+-[^/]+$!;
1116 return if $pool =~ m!/basevol-\d+-[^/]+$!;
1117 push @$res, { pool
=> $pool, size
=> $size, free
=> $size-$used };
1125 my ($portal, $noerr) = @_;
1127 my ($server, $port) = PVE
::Tools
::parse_host_and_port
($portal);
1129 if (my $ip = resolv_server
($server)) {
1131 $server = "[$server]" if $server =~ /^$IPV6RE$/;
1132 return $port ?
"$server:$port" : $server;
1135 return undef if $noerr;
1137 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
1140 # idea is from usbutils package (/usr/bin/usb-devices) script
1141 sub __scan_usb_device
{
1142 my ($res, $devpath, $parent, $level) = @_;
1144 return if ! -d
$devpath;
1145 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
1146 my $port = $level ?
int($1 - 1) : 0;
1148 my $busnum = int(file_read_firstline
("$devpath/busnum"));
1149 my $devnum = int(file_read_firstline
("$devpath/devnum"));
1156 speed
=> file_read_firstline
("$devpath/speed"),
1157 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
1158 vendid
=> file_read_firstline
("$devpath/idVendor"),
1159 prodid
=> file_read_firstline
("$devpath/idProduct"),
1163 my $usbpath = $devpath;
1164 $usbpath =~ s
|^.*/\d
+\
-||;
1165 $d->{usbpath
} = $usbpath;
1168 my $product = file_read_firstline
("$devpath/product");
1169 $d->{product
} = $product if $product;
1171 my $manu = file_read_firstline
("$devpath/manufacturer");
1172 $d->{manufacturer
} = $manu if $manu;
1174 my $serial => file_read_firstline
("$devpath/serial");
1175 $d->{serial
} = $serial if $serial;
1179 foreach my $subdev (<$devpath/$busnum-*>) {
1180 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
1181 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
1190 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
1191 __scan_usb_device
($devlist, $device, 0, 0);
1198 my ($portal_in) = @_;
1201 if (!($portal = resolv_portal
($portal_in))) {
1202 die "unable to parse/resolve portal address '${portal_in}'\n";
1205 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
1208 sub storage_default_format
{
1209 my ($cfg, $storeid) = @_;
1211 my $scfg = storage_config
($cfg, $storeid);
1213 return PVE
::Storage
::Plugin
::default_format
($scfg);
1216 sub vgroup_is_used
{
1217 my ($cfg, $vgname) = @_;
1219 foreach my $storeid (keys %{$cfg->{ids
}}) {
1220 my $scfg = storage_config
($cfg, $storeid);
1221 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
1229 sub target_is_used
{
1230 my ($cfg, $target) = @_;
1232 foreach my $storeid (keys %{$cfg->{ids
}}) {
1233 my $scfg = storage_config
($cfg, $storeid);
1234 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
1242 sub volume_is_used
{
1243 my ($cfg, $volid) = @_;
1245 foreach my $storeid (keys %{$cfg->{ids
}}) {
1246 my $scfg = storage_config
($cfg, $storeid);
1247 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
1255 sub storage_is_used
{
1256 my ($cfg, $storeid) = @_;
1258 foreach my $sid (keys %{$cfg->{ids
}}) {
1259 my $scfg = storage_config
($cfg, $sid);
1260 next if !$scfg->{base
};
1261 my ($st) = parse_volume_id
($scfg->{base
});
1262 return 1 if $st && $st eq $storeid;
1269 my ($list, $func) = @_;
1273 foreach my $sid (keys %$list) {
1274 foreach my $info (@{$list->{$sid}}) {
1275 my $volid = $info->{volid
};
1276 my ($sid1, $volname) = parse_volume_id
($volid, 1);
1277 if ($sid1 && $sid1 eq $sid) {
1278 &$func ($volid, $sid, $info);
1280 warn "detected strange volid '$volid' in volume list for '$sid'\n";
1286 sub extract_vzdump_config_tar
{
1287 my ($archive, $conf_re) = @_;
1289 die "ERROR: file '$archive' does not exist\n" if ! -f
$archive;
1291 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
1292 die "unable to open file '$archive'\n";
1295 while (defined($file = <$fh>)) {
1296 if ($file =~ m!$conf_re!) {
1297 $file = $1; # untaint
1306 die "ERROR: archive contains no configuration file\n" if !$file;
1312 $raw .= "$output\n";
1315 PVE
::Tools
::run_command
(['tar', '-xpOf', $archive, $file, '--occurrence'], outfunc
=> $out);
1317 return wantarray ?
($raw, $file) : $raw;
1320 sub extract_vzdump_config_vma
{
1321 my ($archive, $comp) = @_;
1327 $raw .= "$output\n";
1333 if ($comp eq 'gz') {
1334 $uncomp = ["zcat", $archive];
1335 } elsif ($comp eq 'lzo') {
1336 $uncomp = ["lzop", "-d", "-c", $archive];
1338 die "unknown compression method '$comp'\n";
1340 $cmd = [$uncomp, ["vma", "config", "-"]];
1342 # in some cases, lzop/zcat exits with 1 when its stdout pipe is
1343 # closed early by vma, detect this and ignore the exit code later
1348 if ($output =~ m/lzop: Broken pipe: <stdout>/ || $output =~ m/gzip: stdout: Broken pipe/) {
1350 } elsif (!defined ($errstring) && $output !~ m/^\s*$/) {
1351 $errstring = "Failed to extract config from VMA archive: $output\n";
1355 # in other cases, the pipeline will exit with exit code 141
1356 # because of the broken pipe, handle / ignore this as well
1359 $rc = PVE
::Tools
::run_command
($cmd, outfunc
=> $out, errfunc
=> $err, noerr
=> 1);
1363 # use exit code if no stderr output and not just broken pipe
1364 if (!$errstring && !$broken_pipe && $rc > 0 && $rc != 141) {
1365 die "$rerr\n" if $rerr;
1366 die "config extraction failed with exit code $rc\n";
1368 die "$errstring\n" if $errstring;
1370 # simple case without compression and weird piping behaviour
1371 PVE
::Tools
::run_command
(["vma", "config", $archive], outfunc
=> $out);
1374 return wantarray ?
($raw, undef) : $raw;
1377 sub extract_vzdump_config
{
1378 my ($cfg, $volid) = @_;
1380 my $archive = abs_filesystem_path
($cfg, $volid);
1382 if ($volid =~ /\/vzdump-
(lxc
|openvz
)-\d
+-(\d
{4})_
(\d
{2})_
(\d
{2})-(\d
{2})_
(\d
{2})_
(\d
{2})\
.(tgz
|(tar
(\
.(gz
|lzo
))?
))$/) {
1383 return extract_vzdump_config_tar
($archive,'^(\./etc/vzdump/(pct|vps)\.conf)$');
1384 } elsif ($volid =~ /\/vzdump-qemu-\d
+-(\d
{4})_
(\d
{2})_
(\d
{2})-(\d
{2})_
(\d
{2})_
(\d
{2})\
.(tgz
|((tar
|vma
)(\
.(gz
|lzo
))?
))$/) {
1392 $comp = $11 if defined($11);
1395 if ($format eq 'tar') {
1396 return extract_vzdump_config_tar
($archive, qr!\(\./qemu-server\.conf\)!);
1398 return extract_vzdump_config_vma
($archive, $comp);
1401 die "cannot determine backup guest type for backup archive '$volid'\n";
1405 # bash completion helper
1407 sub complete_storage
{
1408 my ($cmdname, $pname, $cvalue) = @_;
1410 my $cfg = PVE
::Storage
::config
();
1412 return $cmdname eq 'add' ?
[] : [ PVE
::Storage
::storage_ids
($cfg) ];
1415 sub complete_storage_enabled
{
1416 my ($cmdname, $pname, $cvalue) = @_;
1420 my $cfg = PVE
::Storage
::config
();
1421 foreach my $sid (keys %{$cfg->{ids
}}) {
1422 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1428 sub complete_content_type
{
1429 my ($cmdname, $pname, $cvalue) = @_;
1431 return [qw(rootdir images vztmpl iso backup)];
1434 sub complete_volume
{
1435 my ($cmdname, $pname, $cvalue) = @_;
1439 my $storage_list = complete_storage_enabled
();
1441 if ($cvalue =~ m/^([^:]+):/) {
1442 $storage_list = [ $1 ];
1444 if (scalar(@$storage_list) > 1) {
1445 # only list storage IDs to avoid large listings
1447 foreach my $storeid (@$storage_list) {
1448 # Hack: simply return 2 artificial values, so that
1449 # completions does not finish
1450 push @$res, "$storeid:volname", "$storeid:...";
1457 foreach my $storeid (@$storage_list) {
1458 my $vollist = PVE
::Storage
::volume_list
($cfg, $storeid);
1460 foreach my $item (@$vollist) {
1461 push @$res, $item->{volid
};