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);
345 my ($cfg, $volid) = @_;
347 my ($sid, $volname) = parse_volume_id($volid, 1);
350 if (my $scfg = $cfg->{ids}->{$sid}) {
351 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
352 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
353 $plugin->parse_volname($volname);
354 return $isBase ? 1 : 0;
356 # stale volid with undefined storage - so we can just guess
357 if ($volid =~ m/base-/) {
365 # try to map a filesystem path to a volume identifier
366 sub path_to_volume_id {
367 my ($cfg, $path) = @_;
369 my $ids = $cfg->{ids};
371 my ($sid, $volname) = parse_volume_id($path, 1);
373 if (my $scfg = $ids->{$sid}) {
375 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
376 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
377 return ($vtype, $path);
383 # Note: abs_path() return undef if $path doesn not exist
384 # for example when nfs storage is not mounted
385 $path = abs_path($path) || $path;
387 foreach my $sid (keys %$ids) {
388 my $scfg = $ids->{$sid};
389 next if !$scfg->{path};
390 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
391 my $imagedir = $plugin->get_subdir($scfg, 'images');
392 my $isodir = $plugin->get_subdir($scfg, 'iso');
393 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
394 my $backupdir = $plugin->get_subdir($scfg, 'backup');
395 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
397 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
401 my $vollist = $plugin->list_images($sid, $scfg, $vmid);
402 foreach my $info (@$vollist) {
403 my ($storeid, $volname) = parse_volume_id($info->{volid});
404 my $volpath = $plugin->path($scfg, $volname, $storeid);
405 if ($volpath eq $path) {
406 return ('images', $info->{volid});
409 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
411 return ('iso', "$sid:iso
/$name");
412 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
414 return ('vztmpl', "$sid:vztmpl
/$name");
415 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
417 return ('rootdir', "$sid:rootdir
/$vmid");
418 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!) {
420 return ('iso', "$sid:backup
/$name");
424 # can't map path to volume id
429 my ($cfg, $volid, $snapname) = @_;
431 my ($storeid, $volname) = parse_volume_id($volid);
433 my $scfg = storage_config($cfg, $storeid);
435 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
436 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname);
437 return wantarray ? ($path, $owner, $vtype) : $path;
440 sub abs_filesystem_path {
441 my ($cfg, $volid) = @_;
444 if (PVE::Storage::parse_volume_id ($volid, 1)) {
445 PVE::Storage::activate_volumes($cfg, [ $volid ]);
446 $path = PVE::Storage::path($cfg, $volid);
449 my $abspath = abs_path($volid);
450 if ($abspath && $abspath =~ m|^(/.+)$|) {
451 $path = $1; # untaint any path
456 die "can
't find file '$volid'\n" if !($path && -f $path);
461 sub storage_migrate {
462 my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
464 my ($storeid, $volname) = parse_volume_id($volid);
465 $target_volname = $volname if !$target_volname;
467 my $scfg = storage_config($cfg, $storeid);
469 # no need to migrate shared content
470 return if $storeid eq $target_storeid && $scfg->{shared};
472 my $tcfg = storage_config($cfg, $target_storeid);
474 my $target_volid = "${target_storeid}:${target_volname}";
476 my $errstr = "unable to migrate '$volid' to '${target_volid
}' on host '$target_host'";
478 my $sshoptions = "-o 'BatchMode
=yes
'";
479 my $ssh = "/usr/bin/ssh $sshoptions";
481 local $ENV{RSYNC_RSH} = $ssh;
483 # only implemented for file system based storage
487 my $src_plugin = PVE::Storage::Plugin->lookup($scfg->{type});
488 my $dst_plugin = PVE::Storage::Plugin->lookup($tcfg->{type});
489 my $src = $src_plugin->path($scfg, $volname, $storeid);
490 my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid);
492 my $dirname = dirname($dst);
494 if ($tcfg->{shared}) { # we can do a local copy
496 run_command(['/bin/mkdir
', '-p
', $dirname]);
498 run_command(['/bin/cp', $src, $dst]);
501 run_command(['/usr/bin
/ssh
', "root\@${target_host}",
502 '/bin/mkdir
', '-p
', $dirname]);
504 # we use rsync with --sparse, so we can't
use --inplace
,
505 # so we remove file on the target if it already exists to
507 my ($size, $format) = PVE
::Storage
::Plugin
::file_size_info
($src);
508 if ($format && ($format eq 'raw') && $size) {
509 run_command
(['/usr/bin/ssh', "root\@${target_host}",
515 if ($format eq 'subvol') {
516 $cmd = ['/usr/bin/rsync', '--progress', '-X', '-A', '--numeric-ids',
517 '-aH', '--delete', '--no-whole-file', '--inplace',
518 '--one-file-system', "$src/", "[root\@${target_host}]:$dst"];
520 $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
521 $src, "[root\@${target_host}]:$dst"];
526 run_command
($cmd, outfunc
=> sub {
529 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
532 print "rsync status: $1\n";
542 die "$errstr - target type '$tcfg->{type}' not implemented\n";
545 } elsif ($scfg->{type
} eq 'zfspool') {
547 if ($tcfg->{type
} eq 'zfspool') {
549 die "$errstr - pool on target does not have the same name as on source!"
550 if $tcfg->{pool
} ne $scfg->{pool
};
552 my (undef, $volname) = parse_volname
($cfg, $volid);
554 my $zfspath = "$scfg->{pool}\/$volname";
556 my $snap = ['zfs', 'snapshot', "$zfspath\@__migration__"];
558 my $send = [['zfs', 'send', '-Rpv', "$zfspath\@__migration__"], ['ssh', "root\@$target_host",
559 'zfs', 'recv', $zfspath]];
561 my $destroy_target = ['ssh', "root\@$target_host", 'zfs', 'destroy', "$zfspath\@__migration__"];
568 run_command
(['zfs', 'destroy', "$zfspath\@__migration__"]);
571 run_command
($destroy_target);
574 die "$errstr - target type $tcfg->{type} is not valid\n";
577 } elsif ($scfg->{type
} eq 'lvmthin' || $scfg->{type
} eq 'lvm') {
579 if (($scfg->{type
} eq $tcfg->{type
}) &&
580 ($tcfg->{type
} eq 'lvmthin' || $tcfg->{type
} eq 'lvm')) {
582 my (undef, $volname, $vmid) = parse_volname
($cfg, $volid);
583 my $size = volume_size_info
($cfg, $volid, 5);
584 my $src = path
($cfg, $volid);
585 my $dst = path
($cfg, $target_volid);
587 run_command
(['/usr/bin/ssh', "root\@${target_host}",
588 'pvesm', 'alloc', $target_storeid, $vmid,
589 $target_volname, int($size/1024)]);
592 if ($tcfg->{type
} eq 'lvmthin') {
593 run_command
([["dd", "if=$src"],["/usr/bin/ssh", "root\@${target_host}",
594 "dd", 'conv=sparse', "of=$dst"]]);
596 run_command
([["dd", "if=$src"],["/usr/bin/ssh", "root\@${target_host}",
601 run_command
(['/usr/bin/ssh', "root\@${target_host}",
602 'pvesm', 'free', $target_volid]);
606 die "$errstr - migrate from source type '$scfg->{type}' to '$tcfg->{type}' not implemented\n";
609 die "$errstr - source type '$scfg->{type}' not implemented\n";
614 my ($cfg, $volid, $vmid, $snap) = @_;
616 my ($storeid, $volname) = parse_volume_id
($volid);
618 my $scfg = storage_config
($cfg, $storeid);
620 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
622 activate_storage
($cfg, $storeid);
624 # lock shared storage
625 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
626 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
627 return "$storeid:$volname";
631 sub vdisk_create_base
{
632 my ($cfg, $volid) = @_;
634 my ($storeid, $volname) = parse_volume_id
($volid);
636 my $scfg = storage_config
($cfg, $storeid);
638 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
640 activate_storage
($cfg, $storeid);
642 # lock shared storage
643 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
644 my $volname = $plugin->create_base($storeid, $scfg, $volname);
645 return "$storeid:$volname";
650 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
652 die "no storage ID specified\n" if !$storeid;
654 PVE
::JSONSchema
::parse_storage_id
($storeid);
656 my $scfg = storage_config
($cfg, $storeid);
658 die "no VMID specified\n" if !$vmid;
660 $vmid = parse_vmid
($vmid);
662 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
664 $fmt = $defformat if !$fmt;
666 activate_storage
($cfg, $storeid);
668 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
670 # lock shared storage
671 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
672 my $old_umask = umask(umask|0037);
673 my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) };
677 return "$storeid:$volname";
682 my ($cfg, $volid) = @_;
684 my ($storeid, $volname) = parse_volume_id
($volid);
686 my $scfg = storage_config
($cfg, $storeid);
688 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
690 activate_storage
($cfg, $storeid);
694 # lock shared storage
695 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
697 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
698 $plugin->parse_volname($volname);
700 my $vollist = $plugin->list_images($storeid, $scfg);
701 foreach my $info (@$vollist) {
702 my (undef, $tmpvolname) = parse_volume_id
($info->{volid
});
703 my $basename = undef;
704 my $basevmid = undef;
707 (undef, undef, undef, $basename, $basevmid) =
708 $plugin->parse_volname($tmpvolname);
711 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
712 die "base volume '$volname' is still in use " .
713 "(used by '$tmpvolname')\n";
717 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format);
720 return if !$cleanup_worker;
722 my $rpcenv = PVE
::RPCEnvironment
::get
();
723 my $authuser = $rpcenv->get_user();
725 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
728 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
730 my ($cfg, $storeid, $tt) = @_;
732 die "unknown template type '$tt'\n"
733 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
735 my $ids = $cfg->{ids
};
737 storage_check_enabled
($cfg, $storeid) if ($storeid);
743 foreach my $sid (keys %$ids) {
744 next if $storeid && $storeid ne $sid;
746 my $scfg = $ids->{$sid};
747 my $type = $scfg->{type
};
749 next if !storage_check_enabled
($cfg, $sid, undef, 1);
751 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
752 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
753 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
755 activate_storage
($cfg, $sid);
758 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
760 my $path = $plugin->get_subdir($scfg, $tt);
762 foreach my $fn (<$path/*>) {
767 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
769 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
771 } elsif ($tt eq 'vztmpl') {
772 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
774 $info = { volid
=> "$sid:vztmpl/$1", format
=> "t$2" };
776 } elsif ($tt eq 'backup') {
777 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
779 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
782 $info->{size
} = -s
$fn;
784 push @{$res->{$sid}}, $info;
789 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
797 my ($cfg, $storeid, $vmid, $vollist) = @_;
799 my $ids = $cfg->{ids
};
801 storage_check_enabled
($cfg, $storeid) if ($storeid);
805 # prepare/activate/refresh all storages
807 my $storage_list = [];
809 foreach my $volid (@$vollist) {
810 my ($sid, undef) = parse_volume_id
($volid);
811 next if !defined($ids->{$sid});
812 next if !storage_check_enabled
($cfg, $sid, undef, 1);
813 push @$storage_list, $sid;
816 foreach my $sid (keys %$ids) {
817 next if $storeid && $storeid ne $sid;
818 next if !storage_check_enabled
($cfg, $sid, undef, 1);
819 push @$storage_list, $sid;
825 activate_storage_list
($cfg, $storage_list, $cache);
827 foreach my $sid (keys %$ids) {
828 next if $storeid && $storeid ne $sid;
829 next if !storage_check_enabled
($cfg, $sid, undef, 1);
831 my $scfg = $ids->{$sid};
832 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
833 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
834 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
841 my ($cfg, $storeid, $vmid, $content) = @_;
843 my @ctypes = qw(images vztmpl iso backup);
845 my $cts = $content ?
[ $content ] : [ @ctypes ];
847 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
850 foreach my $ct (@$cts) {
852 if ($ct eq 'images') {
853 $data = vdisk_list
($cfg, $storeid, $vmid);
854 } elsif ($ct eq 'iso' && !defined($vmid)) {
855 $data = template_list
($cfg, $storeid, 'iso');
856 } elsif ($ct eq 'vztmpl'&& !defined($vmid)) {
857 $data = template_list
($cfg, $storeid, 'vztmpl');
858 } elsif ($ct eq 'backup') {
859 $data = template_list
($cfg, $storeid, 'backup');
860 foreach my $item (@{$data->{$storeid}}) {
861 if (defined($vmid)) {
862 @{$data->{$storeid}} = grep { $_->{volid
} =~ m/\S+-$vmid-\S+/ } @{$data->{$storeid}};
867 next if !$data || !$data->{$storeid};
869 foreach my $item (@{$data->{$storeid}}) {
870 $item->{content
} = $ct;
880 my $filename = "/sys/kernel/uevent_seqnum";
883 if (my $fh = IO
::File-
>new($filename, "r")) {
885 if ($line =~ m/^(\d+)$/) {
893 sub activate_storage
{
894 my ($cfg, $storeid, $cache) = @_;
896 $cache = {} if !$cache;
898 my $scfg = storage_check_enabled
($cfg, $storeid);
900 return if $cache->{activated
}->{$storeid};
902 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
904 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
907 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
908 activate_storage
($cfg, $baseid, $cache);
911 if (!$plugin->check_connection($storeid, $scfg)) {
912 die "storage '$storeid' is not online\n";
915 $plugin->activate_storage($storeid, $scfg, $cache);
917 my $newseq = uevent_seqnum
();
919 # only call udevsettle if there are events
920 if ($newseq > $cache->{uevent_seqnum
}) {
922 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
923 $cache->{uevent_seqnum
} = $newseq;
926 $cache->{activated
}->{$storeid} = 1;
929 sub activate_storage_list
{
930 my ($cfg, $storeid_list, $cache) = @_;
932 $cache = {} if !$cache;
934 foreach my $storeid (@$storeid_list) {
935 activate_storage
($cfg, $storeid, $cache);
939 sub deactivate_storage
{
940 my ($cfg, $storeid) = @_;
942 my $scfg = storage_config
($cfg, $storeid);
943 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
946 $plugin->deactivate_storage($storeid, $scfg, $cache);
949 sub activate_volumes
{
950 my ($cfg, $vollist, $snapname) = @_;
952 return if !($vollist && scalar(@$vollist));
954 my $storagehash = {};
955 foreach my $volid (@$vollist) {
956 my ($storeid, undef) = parse_volume_id
($volid);
957 $storagehash->{$storeid} = 1;
962 activate_storage_list
($cfg, [keys %$storagehash], $cache);
964 foreach my $volid (@$vollist) {
965 my ($storeid, $volname) = parse_volume_id
($volid);
966 my $scfg = storage_config
($cfg, $storeid);
967 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
968 $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache);
972 sub deactivate_volumes
{
973 my ($cfg, $vollist, $snapname) = @_;
975 return if !($vollist && scalar(@$vollist));
980 foreach my $volid (@$vollist) {
981 my ($storeid, $volname) = parse_volume_id
($volid);
983 my $scfg = storage_config
($cfg, $storeid);
984 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
987 $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache);
991 push @errlist, $volid;
995 die "volume deactivation failed: " . join(' ', @errlist)
1000 my ($cfg, $content) = @_;
1002 my $ids = $cfg->{ids
};
1006 my @ctypes = PVE
::Tools
::split_list
($content);
1009 foreach my $storeid (keys %$ids) {
1011 next if !storage_check_enabled
($cfg, $storeid, undef, 1);
1013 if (defined($content)) {
1015 foreach my $ctype (@ctypes) {
1016 if ($ids->{$storeid}->{content
}->{$ctype}) {
1021 next if !$want_ctype;
1024 my $type = $ids->{$storeid}->{type
};
1026 $info->{$storeid} = {
1031 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
1032 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
1036 push @$slist, $storeid;
1041 foreach my $storeid (keys %$ids) {
1042 my $scfg = $ids->{$storeid};
1043 next if !$info->{$storeid};
1045 eval { activate_storage
($cfg, $storeid, $cache); };
1051 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1052 my ($total, $avail, $used, $active);
1053 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
1056 $info->{$storeid}->{total
} = $total;
1057 $info->{$storeid}->{avail
} = $avail;
1058 $info->{$storeid}->{used
} = $used;
1059 $info->{$storeid}->{active
} = $active;
1068 my ($packed_ip, $family);
1070 my @res = PVE
::Tools
::getaddrinfo_all
($server);
1071 $family = $res[0]->{family
};
1072 $packed_ip = (PVE
::Tools
::unpack_sockaddr_in46
($res[0]->{addr
}))[2];
1074 if (defined $packed_ip) {
1075 return Socket
::inet_ntop
($family, $packed_ip);
1081 my ($server_in) = @_;
1084 if (!($server = resolv_server
($server_in))) {
1085 die "unable to resolve address for server '${server_in}'\n";
1088 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
1091 run_command
($cmd, outfunc
=> sub {
1094 # note: howto handle white spaces in export path??
1095 if ($line =~ m!^(/\S+)\s+(.+)$!) {
1105 my $cmd = ['zfs', 'list', '-t', 'filesystem', '-H', '-o', 'name,avail,used'];
1108 run_command
($cmd, outfunc
=> sub {
1111 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
1112 my ($pool, $size_str, $used_str) = ($1, $2, $3);
1113 my $size = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($size_str);
1114 my $used = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($used_str);
1115 # ignore subvolumes generated by our ZFSPoolPlugin
1116 return if $pool =~ m!/subvol-\d+-[^/]+$!;
1117 return if $pool =~ m!/basevol-\d+-[^/]+$!;
1118 push @$res, { pool
=> $pool, size
=> $size, free
=> $size-$used };
1126 my ($portal, $noerr) = @_;
1128 my ($server, $port) = PVE
::Tools
::parse_host_and_port
($portal);
1130 if (my $ip = resolv_server
($server)) {
1132 $server = "[$server]" if $server =~ /^$IPV6RE$/;
1133 return $port ?
"$server:$port" : $server;
1136 return undef if $noerr;
1138 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
1141 # idea is from usbutils package (/usr/bin/usb-devices) script
1142 sub __scan_usb_device
{
1143 my ($res, $devpath, $parent, $level) = @_;
1145 return if ! -d
$devpath;
1146 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
1147 my $port = $level ?
int($1 - 1) : 0;
1149 my $busnum = int(file_read_firstline
("$devpath/busnum"));
1150 my $devnum = int(file_read_firstline
("$devpath/devnum"));
1157 speed
=> file_read_firstline
("$devpath/speed"),
1158 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
1159 vendid
=> file_read_firstline
("$devpath/idVendor"),
1160 prodid
=> file_read_firstline
("$devpath/idProduct"),
1164 my $usbpath = $devpath;
1165 $usbpath =~ s
|^.*/\d
+\
-||;
1166 $d->{usbpath
} = $usbpath;
1169 my $product = file_read_firstline
("$devpath/product");
1170 $d->{product
} = $product if $product;
1172 my $manu = file_read_firstline
("$devpath/manufacturer");
1173 $d->{manufacturer
} = $manu if $manu;
1175 my $serial => file_read_firstline
("$devpath/serial");
1176 $d->{serial
} = $serial if $serial;
1180 foreach my $subdev (<$devpath/$busnum-*>) {
1181 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
1182 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
1191 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
1192 __scan_usb_device
($devlist, $device, 0, 0);
1199 my ($portal_in) = @_;
1202 if (!($portal = resolv_portal
($portal_in))) {
1203 die "unable to parse/resolve portal address '${portal_in}'\n";
1206 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
1209 sub storage_default_format
{
1210 my ($cfg, $storeid) = @_;
1212 my $scfg = storage_config
($cfg, $storeid);
1214 return PVE
::Storage
::Plugin
::default_format
($scfg);
1217 sub vgroup_is_used
{
1218 my ($cfg, $vgname) = @_;
1220 foreach my $storeid (keys %{$cfg->{ids
}}) {
1221 my $scfg = storage_config
($cfg, $storeid);
1222 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
1230 sub target_is_used
{
1231 my ($cfg, $target) = @_;
1233 foreach my $storeid (keys %{$cfg->{ids
}}) {
1234 my $scfg = storage_config
($cfg, $storeid);
1235 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
1243 sub volume_is_used
{
1244 my ($cfg, $volid) = @_;
1246 foreach my $storeid (keys %{$cfg->{ids
}}) {
1247 my $scfg = storage_config
($cfg, $storeid);
1248 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
1256 sub storage_is_used
{
1257 my ($cfg, $storeid) = @_;
1259 foreach my $sid (keys %{$cfg->{ids
}}) {
1260 my $scfg = storage_config
($cfg, $sid);
1261 next if !$scfg->{base
};
1262 my ($st) = parse_volume_id
($scfg->{base
});
1263 return 1 if $st && $st eq $storeid;
1270 my ($list, $func) = @_;
1274 foreach my $sid (keys %$list) {
1275 foreach my $info (@{$list->{$sid}}) {
1276 my $volid = $info->{volid
};
1277 my ($sid1, $volname) = parse_volume_id
($volid, 1);
1278 if ($sid1 && $sid1 eq $sid) {
1279 &$func ($volid, $sid, $info);
1281 warn "detected strange volid '$volid' in volume list for '$sid'\n";
1287 sub extract_vzdump_config_tar
{
1288 my ($archive, $conf_re) = @_;
1290 die "ERROR: file '$archive' does not exist\n" if ! -f
$archive;
1292 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
1293 die "unable to open file '$archive'\n";
1296 while (defined($file = <$fh>)) {
1297 if ($file =~ m!$conf_re!) {
1298 $file = $1; # untaint
1307 die "ERROR: archive contains no configuration file\n" if !$file;
1313 $raw .= "$output\n";
1316 PVE
::Tools
::run_command
(['tar', '-xpOf', $archive, $file, '--occurrence'], outfunc
=> $out);
1318 return wantarray ?
($raw, $file) : $raw;
1321 sub extract_vzdump_config_vma
{
1322 my ($archive, $comp) = @_;
1328 $raw .= "$output\n";
1334 if ($comp eq 'gz') {
1335 $uncomp = ["zcat", $archive];
1336 } elsif ($comp eq 'lzo') {
1337 $uncomp = ["lzop", "-d", "-c", $archive];
1339 die "unknown compression method '$comp'\n";
1341 $cmd = [$uncomp, ["vma", "config", "-"]];
1343 # in some cases, lzop/zcat exits with 1 when its stdout pipe is
1344 # closed early by vma, detect this and ignore the exit code later
1349 if ($output =~ m/lzop: Broken pipe: <stdout>/ || $output =~ m/gzip: stdout: Broken pipe/) {
1351 } elsif (!defined ($errstring) && $output !~ m/^\s*$/) {
1352 $errstring = "Failed to extract config from VMA archive: $output\n";
1356 # in other cases, the pipeline will exit with exit code 141
1357 # because of the broken pipe, handle / ignore this as well
1360 $rc = PVE
::Tools
::run_command
($cmd, outfunc
=> $out, errfunc
=> $err, noerr
=> 1);
1364 # use exit code if no stderr output and not just broken pipe
1365 if (!$errstring && !$broken_pipe && $rc > 0 && $rc != 141) {
1366 die "$rerr\n" if $rerr;
1367 die "config extraction failed with exit code $rc\n";
1369 die "$errstring\n" if $errstring;
1371 # simple case without compression and weird piping behaviour
1372 PVE
::Tools
::run_command
(["vma", "config", $archive], outfunc
=> $out);
1375 return wantarray ?
($raw, undef) : $raw;
1378 sub extract_vzdump_config
{
1379 my ($cfg, $volid) = @_;
1381 my $archive = abs_filesystem_path
($cfg, $volid);
1383 if ($volid =~ /\/vzdump-
(lxc
|openvz
)-\d
+-(\d
{4})_
(\d
{2})_
(\d
{2})-(\d
{2})_
(\d
{2})_
(\d
{2})\
.(tgz
|(tar
(\
.(gz
|lzo
))?
))$/) {
1384 return extract_vzdump_config_tar
($archive,'^(\./etc/vzdump/(pct|vps)\.conf)$');
1385 } elsif ($volid =~ /\/vzdump-qemu-\d
+-(\d
{4})_
(\d
{2})_
(\d
{2})-(\d
{2})_
(\d
{2})_
(\d
{2})\
.(tgz
|((tar
|vma
)(\
.(gz
|lzo
))?
))$/) {
1393 $comp = $11 if defined($11);
1396 if ($format eq 'tar') {
1397 return extract_vzdump_config_tar
($archive, qr!\(\./qemu-server\.conf\)!);
1399 return extract_vzdump_config_vma
($archive, $comp);
1402 die "cannot determine backup guest type for backup archive '$volid'\n";
1406 # bash completion helper
1408 sub complete_storage
{
1409 my ($cmdname, $pname, $cvalue) = @_;
1411 my $cfg = PVE
::Storage
::config
();
1413 return $cmdname eq 'add' ?
[] : [ PVE
::Storage
::storage_ids
($cfg) ];
1416 sub complete_storage_enabled
{
1417 my ($cmdname, $pname, $cvalue) = @_;
1421 my $cfg = PVE
::Storage
::config
();
1422 foreach my $sid (keys %{$cfg->{ids
}}) {
1423 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1429 sub complete_content_type
{
1430 my ($cmdname, $pname, $cvalue) = @_;
1432 return [qw(rootdir images vztmpl iso backup)];
1435 sub complete_volume
{
1436 my ($cmdname, $pname, $cvalue) = @_;
1440 my $storage_list = complete_storage_enabled
();
1442 if ($cvalue =~ m/^([^:]+):/) {
1443 $storage_list = [ $1 ];
1445 if (scalar(@$storage_list) > 1) {
1446 # only list storage IDs to avoid large listings
1448 foreach my $storeid (@$storage_list) {
1449 # Hack: simply return 2 artificial values, so that
1450 # completions does not finish
1451 push @$res, "$storeid:volname", "$storeid:...";
1458 foreach my $storeid (@$storage_list) {
1459 my $vollist = PVE
::Storage
::volume_list
($cfg, $storeid);
1461 foreach my $item (@$vollist) {
1462 push @$res, $item->{volid
};