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 # try to map a filesystem path to a volume identifier
345 sub path_to_volume_id {
346 my ($cfg, $path) = @_;
348 my $ids = $cfg->{ids};
350 my ($sid, $volname) = parse_volume_id($path, 1);
352 if (my $scfg = $ids->{$sid}) {
354 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
355 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
356 return ($vtype, $path);
362 # Note: abs_path() return undef if $path doesn not exist
363 # for example when nfs storage is not mounted
364 $path = abs_path($path) || $path;
366 foreach my $sid (keys %$ids) {
367 my $scfg = $ids->{$sid};
368 next if !$scfg->{path};
369 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
370 my $imagedir = $plugin->get_subdir($scfg, 'images');
371 my $isodir = $plugin->get_subdir($scfg, 'iso');
372 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
373 my $backupdir = $plugin->get_subdir($scfg, 'backup');
374 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
376 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
380 my $vollist = $plugin->list_images($sid, $scfg, $vmid);
381 foreach my $info (@$vollist) {
382 my ($storeid, $volname) = parse_volume_id($info->{volid});
383 my $volpath = $plugin->path($scfg, $volname, $storeid);
384 if ($volpath eq $path) {
385 return ('images', $info->{volid});
388 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
390 return ('iso', "$sid:iso
/$name");
391 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
393 return ('vztmpl', "$sid:vztmpl
/$name");
394 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
396 return ('rootdir', "$sid:rootdir
/$vmid");
397 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!) {
399 return ('iso', "$sid:backup
/$name");
403 # can't map path to volume id
408 my ($cfg, $volid, $snapname) = @_;
410 my ($storeid, $volname) = parse_volume_id($volid);
412 my $scfg = storage_config($cfg, $storeid);
414 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
415 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname);
416 return wantarray ? ($path, $owner, $vtype) : $path;
419 sub abs_filesystem_path {
420 my ($cfg, $volid) = @_;
423 if (PVE::Storage::parse_volume_id ($volid, 1)) {
424 PVE::Storage::activate_volumes($cfg, [ $volid ]);
425 $path = PVE::Storage::path($cfg, $volid);
428 my $abspath = abs_path($volid);
429 if ($abspath && $abspath =~ m|^(/.+)$|) {
430 $path = $1; # untaint any path
435 die "can
't find file '$volid'\n" if !($path && -f $path);
440 sub storage_migrate {
441 my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
443 my ($storeid, $volname) = parse_volume_id($volid);
444 $target_volname = $volname if !$target_volname;
446 my $scfg = storage_config($cfg, $storeid);
448 # no need to migrate shared content
449 return if $storeid eq $target_storeid && $scfg->{shared};
451 my $tcfg = storage_config($cfg, $target_storeid);
453 my $target_volid = "${target_storeid}:${target_volname}";
455 my $errstr = "unable to migrate '$volid' to '${target_volid
}' on host '$target_host'";
457 my $sshoptions = "-o 'BatchMode
=yes
'";
458 my $ssh = "/usr/bin/ssh $sshoptions";
460 local $ENV{RSYNC_RSH} = $ssh;
462 # only implemented for file system based storage
466 my $src_plugin = PVE::Storage::Plugin->lookup($scfg->{type});
467 my $dst_plugin = PVE::Storage::Plugin->lookup($tcfg->{type});
468 my $src = $src_plugin->path($scfg, $volname, $storeid);
469 my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid);
471 my $dirname = dirname($dst);
473 if ($tcfg->{shared}) { # we can do a local copy
475 run_command(['/bin/mkdir
', '-p
', $dirname]);
477 run_command(['/bin/cp', $src, $dst]);
480 run_command(['/usr/bin
/ssh
', "root\@${target_host}",
481 '/bin/mkdir
', '-p
', $dirname]);
483 # we use rsync with --sparse, so we can't
use --inplace
,
484 # so we remove file on the target if it already exists to
486 my ($size, $format) = PVE
::Storage
::Plugin
::file_size_info
($src);
487 if ($format && ($format eq 'raw') && $size) {
488 run_command
(['/usr/bin/ssh', "root\@${target_host}",
494 if ($format eq 'subvol') {
495 $cmd = ['/usr/bin/rsync', '--progress', '-X', '-A', '--numeric-ids',
496 '-aH', '--delete', '--no-whole-file', '--inplace',
497 '--one-file-system', "$src/", "[root\@${target_host}]:$dst"];
499 $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
500 $src, "[root\@${target_host}]:$dst"];
505 run_command
($cmd, outfunc
=> sub {
508 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
511 print "rsync status: $1\n";
521 die "$errstr - target type '$tcfg->{type}' not implemented\n";
524 } elsif ($scfg->{type
} eq 'zfspool') {
526 if ($tcfg->{type
} eq 'zfspool') {
528 die "$errstr - pool on target does not have the same name as on source!"
529 if $tcfg->{pool
} ne $scfg->{pool
};
531 my (undef, $volname) = parse_volname
($cfg, $volid);
533 my $zfspath = "$scfg->{pool}\/$volname";
535 my $snap = ['zfs', 'snapshot', "$zfspath\@__migration__"];
537 my $send = [['zfs', 'send', '-Rpv', "$zfspath\@__migration__"], ['ssh', "root\@$target_host",
538 'zfs', 'recv', $zfspath]];
540 my $destroy_target = ['ssh', "root\@$target_host", 'zfs', 'destroy', "$zfspath\@__migration__"];
547 run_command
(['zfs', 'destroy', "$zfspath\@__migration__"]);
550 run_command
($destroy_target);
553 die "$errstr - target type $tcfg->{type} is not valid\n";
556 } elsif ($scfg->{type
} eq 'lvmthin' || $scfg->{type
} eq 'lvm') {
558 if (($scfg->{type
} eq $tcfg->{type
}) &&
559 ($tcfg->{type
} eq 'lvmthin' || $tcfg->{type
} eq 'lvm')) {
561 my (undef, $volname, $vmid) = parse_volname
($cfg, $volid);
562 my $size = volume_size_info
($cfg, $volid, 5);
563 my $src = path
($cfg, $volid);
564 my $dst = path
($cfg, $target_volid);
566 run_command
(['/usr/bin/ssh', "root\@${target_host}",
567 'pvesm', 'alloc', $target_storeid, $vmid,
568 $target_volname, int($size/1024)]);
571 if ($tcfg->{type
} eq 'lvmthin') {
572 run_command
([["dd", "if=$src"],["/usr/bin/ssh", "root\@${target_host}",
573 "dd", 'conv=sparse', "of=$dst"]]);
575 run_command
([["dd", "if=$src"],["/usr/bin/ssh", "root\@${target_host}",
580 run_command
(['/usr/bin/ssh', "root\@${target_host}",
581 'pvesm', 'free', $target_volid]);
585 die "$errstr - migrate from source type '$scfg->{type}' to '$tcfg->{type}' not implemented\n";
588 die "$errstr - source type '$scfg->{type}' not implemented\n";
593 my ($cfg, $volid, $vmid, $snap) = @_;
595 my ($storeid, $volname) = parse_volume_id
($volid);
597 my $scfg = storage_config
($cfg, $storeid);
599 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
601 activate_storage
($cfg, $storeid);
603 # lock shared storage
604 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
605 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
606 return "$storeid:$volname";
610 sub vdisk_create_base
{
611 my ($cfg, $volid) = @_;
613 my ($storeid, $volname) = parse_volume_id
($volid);
615 my $scfg = storage_config
($cfg, $storeid);
617 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
619 activate_storage
($cfg, $storeid);
621 # lock shared storage
622 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
623 my $volname = $plugin->create_base($storeid, $scfg, $volname);
624 return "$storeid:$volname";
629 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
631 die "no storage ID specified\n" if !$storeid;
633 PVE
::JSONSchema
::parse_storage_id
($storeid);
635 my $scfg = storage_config
($cfg, $storeid);
637 die "no VMID specified\n" if !$vmid;
639 $vmid = parse_vmid
($vmid);
641 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
643 $fmt = $defformat if !$fmt;
645 activate_storage
($cfg, $storeid);
647 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
649 # lock shared storage
650 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
651 my $old_umask = umask(umask|0037);
652 my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) };
656 return "$storeid:$volname";
661 my ($cfg, $volid) = @_;
663 my ($storeid, $volname) = parse_volume_id
($volid);
665 my $scfg = storage_config
($cfg, $storeid);
667 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
669 activate_storage
($cfg, $storeid);
673 # lock shared storage
674 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
676 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
677 $plugin->parse_volname($volname);
679 my $vollist = $plugin->list_images($storeid, $scfg);
680 foreach my $info (@$vollist) {
681 my (undef, $tmpvolname) = parse_volume_id
($info->{volid
});
682 my $basename = undef;
683 my $basevmid = undef;
686 (undef, undef, undef, $basename, $basevmid) =
687 $plugin->parse_volname($tmpvolname);
690 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
691 die "base volume '$volname' is still in use " .
692 "(used by '$tmpvolname')\n";
696 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format);
699 return if !$cleanup_worker;
701 my $rpcenv = PVE
::RPCEnvironment
::get
();
702 my $authuser = $rpcenv->get_user();
704 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
707 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
709 my ($cfg, $storeid, $tt) = @_;
711 die "unknown template type '$tt'\n"
712 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
714 my $ids = $cfg->{ids
};
716 storage_check_enabled
($cfg, $storeid) if ($storeid);
722 foreach my $sid (keys %$ids) {
723 next if $storeid && $storeid ne $sid;
725 my $scfg = $ids->{$sid};
726 my $type = $scfg->{type
};
728 next if !storage_check_enabled
($cfg, $sid, undef, 1);
730 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
731 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
732 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
734 activate_storage
($cfg, $sid);
737 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
739 my $path = $plugin->get_subdir($scfg, $tt);
741 foreach my $fn (<$path/*>) {
746 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
748 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
750 } elsif ($tt eq 'vztmpl') {
751 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
753 $info = { volid
=> "$sid:vztmpl/$1", format
=> "t$2" };
755 } elsif ($tt eq 'backup') {
756 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
758 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
761 $info->{size
} = -s
$fn;
763 push @{$res->{$sid}}, $info;
768 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
776 my ($cfg, $storeid, $vmid, $vollist) = @_;
778 my $ids = $cfg->{ids
};
780 storage_check_enabled
($cfg, $storeid) if ($storeid);
784 # prepare/activate/refresh all storages
786 my $storage_list = [];
788 foreach my $volid (@$vollist) {
789 my ($sid, undef) = parse_volume_id
($volid);
790 next if !defined($ids->{$sid});
791 next if !storage_check_enabled
($cfg, $sid, undef, 1);
792 push @$storage_list, $sid;
795 foreach my $sid (keys %$ids) {
796 next if $storeid && $storeid ne $sid;
797 next if !storage_check_enabled
($cfg, $sid, undef, 1);
798 push @$storage_list, $sid;
804 activate_storage_list
($cfg, $storage_list, $cache);
806 foreach my $sid (keys %$ids) {
807 next if $storeid && $storeid ne $sid;
808 next if !storage_check_enabled
($cfg, $sid, undef, 1);
810 my $scfg = $ids->{$sid};
811 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
812 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
813 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
820 my ($cfg, $storeid, $vmid, $content) = @_;
822 my @ctypes = qw(images vztmpl iso backup);
824 my $cts = $content ?
[ $content ] : [ @ctypes ];
826 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
829 foreach my $ct (@$cts) {
831 if ($ct eq 'images') {
832 $data = vdisk_list
($cfg, $storeid, $vmid);
833 } elsif ($ct eq 'iso' && !defined($vmid)) {
834 $data = template_list
($cfg, $storeid, 'iso');
835 } elsif ($ct eq 'vztmpl'&& !defined($vmid)) {
836 $data = template_list
($cfg, $storeid, 'vztmpl');
837 } elsif ($ct eq 'backup') {
838 $data = template_list
($cfg, $storeid, 'backup');
839 foreach my $item (@{$data->{$storeid}}) {
840 if (defined($vmid)) {
841 @{$data->{$storeid}} = grep { $_->{volid
} =~ m/\S+-$vmid-\S+/ } @{$data->{$storeid}};
846 next if !$data || !$data->{$storeid};
848 foreach my $item (@{$data->{$storeid}}) {
849 $item->{content
} = $ct;
859 my $filename = "/sys/kernel/uevent_seqnum";
862 if (my $fh = IO
::File-
>new($filename, "r")) {
864 if ($line =~ m/^(\d+)$/) {
872 sub activate_storage
{
873 my ($cfg, $storeid, $cache) = @_;
875 $cache = {} if !$cache;
877 my $scfg = storage_check_enabled
($cfg, $storeid);
879 return if $cache->{activated
}->{$storeid};
881 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
883 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
886 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
887 activate_storage
($cfg, $baseid, $cache);
890 if (!$plugin->check_connection($storeid, $scfg)) {
891 die "storage '$storeid' is not online\n";
894 $plugin->activate_storage($storeid, $scfg, $cache);
896 my $newseq = uevent_seqnum
();
898 # only call udevsettle if there are events
899 if ($newseq > $cache->{uevent_seqnum
}) {
901 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
902 $cache->{uevent_seqnum
} = $newseq;
905 $cache->{activated
}->{$storeid} = 1;
908 sub activate_storage_list
{
909 my ($cfg, $storeid_list, $cache) = @_;
911 $cache = {} if !$cache;
913 foreach my $storeid (@$storeid_list) {
914 activate_storage
($cfg, $storeid, $cache);
918 sub deactivate_storage
{
919 my ($cfg, $storeid) = @_;
921 my $scfg = storage_config
($cfg, $storeid);
922 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
925 $plugin->deactivate_storage($storeid, $scfg, $cache);
928 sub activate_volumes
{
929 my ($cfg, $vollist, $snapname) = @_;
931 return if !($vollist && scalar(@$vollist));
933 my $storagehash = {};
934 foreach my $volid (@$vollist) {
935 my ($storeid, undef) = parse_volume_id
($volid);
936 $storagehash->{$storeid} = 1;
941 activate_storage_list
($cfg, [keys %$storagehash], $cache);
943 foreach my $volid (@$vollist) {
944 my ($storeid, $volname) = parse_volume_id
($volid);
945 my $scfg = storage_config
($cfg, $storeid);
946 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
947 $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache);
951 sub deactivate_volumes
{
952 my ($cfg, $vollist, $snapname) = @_;
954 return if !($vollist && scalar(@$vollist));
959 foreach my $volid (@$vollist) {
960 my ($storeid, $volname) = parse_volume_id
($volid);
962 my $scfg = storage_config
($cfg, $storeid);
963 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
966 $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache);
970 push @errlist, $volid;
974 die "volume deactivation failed: " . join(' ', @errlist)
979 my ($cfg, $content) = @_;
981 my $ids = $cfg->{ids
};
985 my @ctypes = PVE
::Tools
::split_list
($content);
988 foreach my $storeid (keys %$ids) {
990 next if !storage_check_enabled
($cfg, $storeid, undef, 1);
992 if (defined($content)) {
994 foreach my $ctype (@ctypes) {
995 if ($ids->{$storeid}->{content
}->{$ctype}) {
1000 next if !$want_ctype;
1003 my $type = $ids->{$storeid}->{type
};
1005 $info->{$storeid} = {
1010 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
1011 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
1015 push @$slist, $storeid;
1020 foreach my $storeid (keys %$ids) {
1021 my $scfg = $ids->{$storeid};
1022 next if !$info->{$storeid};
1024 eval { activate_storage
($cfg, $storeid, $cache); };
1030 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1031 my ($total, $avail, $used, $active);
1032 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
1035 $info->{$storeid}->{total
} = $total;
1036 $info->{$storeid}->{avail
} = $avail;
1037 $info->{$storeid}->{used
} = $used;
1038 $info->{$storeid}->{active
} = $active;
1047 my ($packed_ip, $family);
1049 my @res = PVE
::Tools
::getaddrinfo_all
($server);
1050 $family = $res[0]->{family
};
1051 $packed_ip = (PVE
::Tools
::unpack_sockaddr_in46
($res[0]->{addr
}))[2];
1053 if (defined $packed_ip) {
1054 return Socket
::inet_ntop
($family, $packed_ip);
1060 my ($server_in) = @_;
1063 if (!($server = resolv_server
($server_in))) {
1064 die "unable to resolve address for server '${server_in}'\n";
1067 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
1070 run_command
($cmd, outfunc
=> sub {
1073 # note: howto handle white spaces in export path??
1074 if ($line =~ m!^(/\S+)\s+(.+)$!) {
1084 my $cmd = ['zfs', 'list', '-t', 'filesystem', '-H', '-o', 'name,avail,used'];
1087 run_command
($cmd, outfunc
=> sub {
1090 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
1091 my ($pool, $size_str, $used_str) = ($1, $2, $3);
1092 my $size = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($size_str);
1093 my $used = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($used_str);
1094 # ignore subvolumes generated by our ZFSPoolPlugin
1095 return if $pool =~ m!/subvol-\d+-[^/]+$!;
1096 return if $pool =~ m!/basevol-\d+-[^/]+$!;
1097 push @$res, { pool
=> $pool, size
=> $size, free
=> $size-$used };
1105 my ($portal, $noerr) = @_;
1107 my ($server, $port) = PVE
::Tools
::parse_host_and_port
($portal);
1109 if (my $ip = resolv_server
($server)) {
1111 $server = "[$server]" if $server =~ /^$IPV6RE$/;
1112 return $port ?
"$server:$port" : $server;
1115 return undef if $noerr;
1117 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
1120 # idea is from usbutils package (/usr/bin/usb-devices) script
1121 sub __scan_usb_device
{
1122 my ($res, $devpath, $parent, $level) = @_;
1124 return if ! -d
$devpath;
1125 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
1126 my $port = $level ?
int($1 - 1) : 0;
1128 my $busnum = int(file_read_firstline
("$devpath/busnum"));
1129 my $devnum = int(file_read_firstline
("$devpath/devnum"));
1136 speed
=> file_read_firstline
("$devpath/speed"),
1137 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
1138 vendid
=> file_read_firstline
("$devpath/idVendor"),
1139 prodid
=> file_read_firstline
("$devpath/idProduct"),
1143 my $usbpath = $devpath;
1144 $usbpath =~ s
|^.*/\d
+\
-||;
1145 $d->{usbpath
} = $usbpath;
1148 my $product = file_read_firstline
("$devpath/product");
1149 $d->{product
} = $product if $product;
1151 my $manu = file_read_firstline
("$devpath/manufacturer");
1152 $d->{manufacturer
} = $manu if $manu;
1154 my $serial => file_read_firstline
("$devpath/serial");
1155 $d->{serial
} = $serial if $serial;
1159 foreach my $subdev (<$devpath/$busnum-*>) {
1160 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
1161 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
1170 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
1171 __scan_usb_device
($devlist, $device, 0, 0);
1178 my ($portal_in) = @_;
1181 if (!($portal = resolv_portal
($portal_in))) {
1182 die "unable to parse/resolve portal address '${portal_in}'\n";
1185 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
1188 sub storage_default_format
{
1189 my ($cfg, $storeid) = @_;
1191 my $scfg = storage_config
($cfg, $storeid);
1193 return PVE
::Storage
::Plugin
::default_format
($scfg);
1196 sub vgroup_is_used
{
1197 my ($cfg, $vgname) = @_;
1199 foreach my $storeid (keys %{$cfg->{ids
}}) {
1200 my $scfg = storage_config
($cfg, $storeid);
1201 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
1209 sub target_is_used
{
1210 my ($cfg, $target) = @_;
1212 foreach my $storeid (keys %{$cfg->{ids
}}) {
1213 my $scfg = storage_config
($cfg, $storeid);
1214 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
1222 sub volume_is_used
{
1223 my ($cfg, $volid) = @_;
1225 foreach my $storeid (keys %{$cfg->{ids
}}) {
1226 my $scfg = storage_config
($cfg, $storeid);
1227 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
1235 sub storage_is_used
{
1236 my ($cfg, $storeid) = @_;
1238 foreach my $sid (keys %{$cfg->{ids
}}) {
1239 my $scfg = storage_config
($cfg, $sid);
1240 next if !$scfg->{base
};
1241 my ($st) = parse_volume_id
($scfg->{base
});
1242 return 1 if $st && $st eq $storeid;
1249 my ($list, $func) = @_;
1253 foreach my $sid (keys %$list) {
1254 foreach my $info (@{$list->{$sid}}) {
1255 my $volid = $info->{volid
};
1256 my ($sid1, $volname) = parse_volume_id
($volid, 1);
1257 if ($sid1 && $sid1 eq $sid) {
1258 &$func ($volid, $sid, $info);
1260 warn "detected strange volid '$volid' in volume list for '$sid'\n";
1266 sub extract_vzdump_config_tar
{
1267 my ($archive, $conf_re) = @_;
1269 die "ERROR: file '$archive' does not exist\n" if ! -f
$archive;
1271 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
1272 die "unable to open file '$archive'\n";
1275 while (defined($file = <$fh>)) {
1276 if ($file =~ m!$conf_re!) {
1277 $file = $1; # untaint
1286 die "ERROR: archive contains no configuration file\n" if !$file;
1292 $raw .= "$output\n";
1295 PVE
::Tools
::run_command
(['tar', '-xpOf', $archive, $file, '--occurrence'], outfunc
=> $out);
1297 return wantarray ?
($raw, $file) : $raw;
1300 sub extract_vzdump_config_vma
{
1301 my ($archive, $comp) = @_;
1307 $raw .= "$output\n";
1313 if ($comp eq 'gz') {
1314 $uncomp = ["zcat", $archive];
1315 } elsif ($comp eq 'lzo') {
1316 $uncomp = ["lzop", "-d", "-c", $archive];
1318 die "unknown compression method '$comp'\n";
1320 $cmd = [$uncomp, ["vma", "config", "-"]];
1322 # in some cases, lzop/zcat exits with 1 when its stdout pipe is
1323 # closed early by vma, detect this and ignore the exit code later
1328 if ($output =~ m/lzop: Broken pipe: <stdout>/ || $output =~ m/gzip: stdout: Broken pipe/) {
1330 } elsif (!defined ($errstring) && $output !~ m/^\s*$/) {
1331 $errstring = "Failed to extract config from VMA archive: $output\n";
1335 # in other cases, the pipeline will exit with exit code 141
1336 # because of the broken pipe, handle / ignore this as well
1339 $rc = PVE
::Tools
::run_command
($cmd, outfunc
=> $out, errfunc
=> $err, noerr
=> 1);
1343 # use exit code if no stderr output and not just broken pipe
1344 if (!$errstring && !$broken_pipe && $rc > 0 && $rc != 141) {
1345 die "$rerr\n" if $rerr;
1346 die "config extraction failed with exit code $rc\n";
1348 die "$errstring\n" if $errstring;
1350 # simple case without compression and weird piping behaviour
1351 PVE
::Tools
::run_command
(["vma", "config", $archive], outfunc
=> $out);
1354 return wantarray ?
($raw, undef) : $raw;
1357 sub extract_vzdump_config
{
1358 my ($cfg, $volid) = @_;
1360 my $archive = abs_filesystem_path
($cfg, $volid);
1362 if ($volid =~ /\/vzdump-
(lxc
|openvz
)-\d
+-(\d
{4})_
(\d
{2})_
(\d
{2})-(\d
{2})_
(\d
{2})_
(\d
{2})\
.(tgz
|(tar
(\
.(gz
|lzo
))?
))$/) {
1363 return extract_vzdump_config_tar
($archive,'^(\./etc/vzdump/(pct|vps)\.conf)$');
1364 } elsif ($volid =~ /\/vzdump-qemu-\d
+-(\d
{4})_
(\d
{2})_
(\d
{2})-(\d
{2})_
(\d
{2})_
(\d
{2})\
.(tgz
|((tar
|vma
)(\
.(gz
|lzo
))?
))$/) {
1372 $comp = $11 if defined($11);
1375 if ($format eq 'tar') {
1376 return extract_vzdump_config_tar
($archive, qr!\(\./qemu-server\.conf\)!);
1378 return extract_vzdump_config_vma
($archive, $comp);
1381 die "cannot determine backup guest type for backup archive '$volid'\n";
1385 # bash completion helper
1387 sub complete_storage
{
1388 my ($cmdname, $pname, $cvalue) = @_;
1390 my $cfg = PVE
::Storage
::config
();
1392 return $cmdname eq 'add' ?
[] : [ PVE
::Storage
::storage_ids
($cfg) ];
1395 sub complete_storage_enabled
{
1396 my ($cmdname, $pname, $cvalue) = @_;
1400 my $cfg = PVE
::Storage
::config
();
1401 foreach my $sid (keys %{$cfg->{ids
}}) {
1402 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1408 sub complete_content_type
{
1409 my ($cmdname, $pname, $cvalue) = @_;
1411 return [qw(rootdir images vztmpl iso backup)];
1414 sub complete_volume
{
1415 my ($cmdname, $pname, $cvalue) = @_;
1419 my $storage_list = complete_storage_enabled
();
1421 if ($cvalue =~ m/^([^:]+):/) {
1422 $storage_list = [ $1 ];
1424 if (scalar(@$storage_list) > 1) {
1425 # only list storage IDs to avoid large listings
1427 foreach my $storeid (@$storage_list) {
1428 # Hack: simply return 2 artificial values, so that
1429 # completions does not finish
1430 push @$res, "$storeid:volname", "$storeid:...";
1437 foreach my $storeid (@$storage_list) {
1438 my $vollist = PVE
::Storage
::volume_list
($cfg, $storeid);
1440 foreach my $item (@$vollist) {
1441 push @$res, $item->{volid
};