15 use PVE
::Tools
qw(run_command file_read_firstline $IPV6RE);
16 use PVE
::Cluster
qw(cfs_read_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 # load and initialize all plugins
37 PVE
::Storage
::DirPlugin-
>register();
38 PVE
::Storage
::LVMPlugin-
>register();
39 PVE
::Storage
::LvmThinPlugin-
>register();
40 PVE
::Storage
::NFSPlugin-
>register();
41 PVE
::Storage
::ISCSIPlugin-
>register();
42 PVE
::Storage
::RBDPlugin-
>register();
43 PVE
::Storage
::SheepdogPlugin-
>register();
44 PVE
::Storage
::ISCSIDirectPlugin-
>register();
45 PVE
::Storage
::GlusterfsPlugin-
>register();
46 PVE
::Storage
::ZFSPoolPlugin-
>register();
47 PVE
::Storage
::ZFSPlugin-
>register();
48 PVE
::Storage
::DRBDPlugin-
>register();
49 PVE
::Storage
::Plugin-
>init();
51 my $UDEVADM = '/sbin/udevadm';
53 # PVE::Storage utility functions
56 return cfs_read_file
("storage.cfg");
59 sub lock_storage_config
{
60 my ($code, $errmsg) = @_;
62 cfs_lock_file
("storage.cfg", undef, $code);
65 $errmsg ?
die "$errmsg: $err" : die $err;
70 my ($cfg, $storeid, $noerr) = @_;
72 die "no storage id specified\n" if !$storeid;
74 my $scfg = $cfg->{ids
}->{$storeid};
76 die "storage '$storeid' does not exists\n" if (!$noerr && !$scfg);
81 sub storage_check_node
{
82 my ($cfg, $storeid, $node, $noerr) = @_;
84 my $scfg = storage_config
($cfg, $storeid);
87 $node = PVE
::INotify
::nodename
() if !$node || ($node eq 'localhost');
88 if (!$scfg->{nodes
}->{$node}) {
89 die "storage '$storeid' is not available on node '$node'\n" if !$noerr;
97 sub storage_check_enabled
{
98 my ($cfg, $storeid, $node, $noerr) = @_;
100 my $scfg = storage_config
($cfg, $storeid);
102 if ($scfg->{disable
}) {
103 die "storage '$storeid' is disabled\n" if !$noerr;
107 return storage_check_node
($cfg, $storeid, $node, $noerr);
113 return keys %{$cfg->{ids
}};
117 my ($filename, $timeout) = @_;
119 return PVE
::Storage
::Plugin
::file_size_info
($filename, $timeout);
122 sub volume_size_info
{
123 my ($cfg, $volid, $timeout) = @_;
125 my ($storeid, $volname) = parse_volume_id
($volid, 1);
127 my $scfg = storage_config
($cfg, $storeid);
128 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
129 return $plugin->volume_size_info($scfg, $storeid, $volname, $timeout);
130 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
131 return file_size_info
($volid, $timeout);
138 my ($cfg, $volid, $size, $running) = @_;
140 my ($storeid, $volname) = parse_volume_id
($volid, 1);
142 my $scfg = storage_config
($cfg, $storeid);
143 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
144 return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running);
145 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
146 die "resize file/device '$volid' is not possible\n";
148 die "unable to parse volume ID '$volid'\n";
152 sub volume_rollback_is_possible
{
153 my ($cfg, $volid, $snap) = @_;
155 my ($storeid, $volname) = parse_volume_id
($volid, 1);
157 my $scfg = storage_config
($cfg, $storeid);
158 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
159 return $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
160 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
161 die "snapshot rollback file/device '$volid' is not possible\n";
163 die "unable to parse volume ID '$volid'\n";
167 sub volume_snapshot
{
168 my ($cfg, $volid, $snap) = @_;
170 my ($storeid, $volname) = parse_volume_id
($volid, 1);
172 my $scfg = storage_config
($cfg, $storeid);
173 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
174 return $plugin->volume_snapshot($scfg, $storeid, $volname, $snap);
175 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
176 die "snapshot file/device '$volid' is not possible\n";
178 die "unable to parse volume ID '$volid'\n";
182 sub volume_snapshot_rollback
{
183 my ($cfg, $volid, $snap) = @_;
185 my ($storeid, $volname) = parse_volume_id
($volid, 1);
187 my $scfg = storage_config
($cfg, $storeid);
188 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
189 $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
190 return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap);
191 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
192 die "snapshot rollback file/device '$volid' is not possible\n";
194 die "unable to parse volume ID '$volid'\n";
198 sub volume_snapshot_delete
{
199 my ($cfg, $volid, $snap, $running) = @_;
201 my ($storeid, $volname) = parse_volume_id
($volid, 1);
203 my $scfg = storage_config
($cfg, $storeid);
204 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
205 return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running);
206 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
207 die "snapshot delete file/device '$volid' is not possible\n";
209 die "unable to parse volume ID '$volid'\n";
213 sub volume_has_feature
{
214 my ($cfg, $feature, $volid, $snap, $running) = @_;
216 my ($storeid, $volname) = parse_volume_id
($volid, 1);
218 my $scfg = storage_config
($cfg, $storeid);
219 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
220 return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running);
221 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
229 my ($cfg, $storeid, $vmid) = @_;
231 my $scfg = storage_config
($cfg, $storeid);
232 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
234 my $path = $plugin->get_subdir($scfg, 'images');
236 return $vmid ?
"$path/$vmid" : $path;
239 sub get_private_dir
{
240 my ($cfg, $storeid, $vmid) = @_;
242 my $scfg = storage_config
($cfg, $storeid);
243 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
245 my $path = $plugin->get_subdir($scfg, 'rootdir');
247 return $vmid ?
"$path/$vmid" : $path;
251 my ($cfg, $storeid) = @_;
253 my $scfg = storage_config
($cfg, $storeid);
254 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
256 return $plugin->get_subdir($scfg, 'iso');
260 my ($cfg, $storeid) = @_;
262 my $scfg = storage_config
($cfg, $storeid);
263 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
265 return $plugin->get_subdir($scfg, 'vztmpl');
269 my ($cfg, $storeid) = @_;
271 my $scfg = storage_config
($cfg, $storeid);
272 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
274 return $plugin->get_subdir($scfg, 'backup');
277 # library implementation
282 die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/;
288 my ($cfg, $volid) = @_;
290 my ($storeid, $volname) = parse_volume_id
($volid);
292 my $scfg = storage_config
($cfg, $storeid);
294 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
296 # returns ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format)
298 return $plugin->parse_volname($volname);
301 sub parse_volume_id
{
302 my ($volid, $noerr) = @_;
304 return PVE
::Storage
::Plugin
::parse_volume_id
($volid, $noerr);
308 my ($cfg, $volid) = @_;
310 my ($sid, $volname) = parse_volume_id
($volid, 1);
313 if (my $scfg = $cfg->{ids
}->{$sid}) {
314 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
315 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
316 $plugin->parse_volname($volname);
317 return $isBase ?
1 : 0;
319 # stale volid with undefined storage - so we can just guess
320 if ($volid =~ m/base-/) {
328 # try to map a filesystem path to a volume identifier
329 sub path_to_volume_id
{
330 my ($cfg, $path) = @_;
332 my $ids = $cfg->{ids
};
334 my ($sid, $volname) = parse_volume_id
($path, 1);
336 if (my $scfg = $ids->{$sid}) {
338 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
339 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
340 return ($vtype, $path);
346 # Note: abs_path() return undef if $path doesn not exist
347 # for example when nfs storage is not mounted
348 $path = abs_path
($path) || $path;
350 foreach my $sid (keys %$ids) {
351 my $scfg = $ids->{$sid};
352 next if !$scfg->{path
};
353 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
354 my $imagedir = $plugin->get_subdir($scfg, 'images');
355 my $isodir = $plugin->get_subdir($scfg, 'iso');
356 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
357 my $backupdir = $plugin->get_subdir($scfg, 'backup');
358 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
360 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
364 my $vollist = $plugin->list_images($sid, $scfg, $vmid);
365 foreach my $info (@$vollist) {
366 my ($storeid, $volname) = parse_volume_id
($info->{volid
});
367 my $volpath = $plugin->path($scfg, $volname, $storeid);
368 if ($volpath eq $path) {
369 return ('images', $info->{volid
});
372 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
374 return ('iso', "$sid:iso/$name");
375 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
377 return ('vztmpl', "$sid:vztmpl/$name");
378 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
380 return ('rootdir', "$sid:rootdir/$vmid");
381 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!) {
383 return ('iso', "$sid:backup/$name");
387 # can't map path to volume id
392 my ($cfg, $volid, $snapname) = @_;
394 my ($storeid, $volname) = parse_volume_id
($volid);
396 my $scfg = storage_config
($cfg, $storeid);
398 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
399 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname);
400 return wantarray ?
($path, $owner, $vtype) : $path;
403 sub abs_filesystem_path
{
404 my ($cfg, $volid) = @_;
407 if (PVE
::Storage
::parse_volume_id
($volid, 1)) {
408 PVE
::Storage
::activate_volumes
($cfg, [ $volid ]);
409 $path = PVE
::Storage
::path
($cfg, $volid);
412 my $abspath = abs_path
($volid);
413 if ($abspath && $abspath =~ m
|^(/.+)$|) {
414 $path = $1; # untaint any path
419 die "can't find file '$volid'\n" if !($path && -f
$path);
424 sub storage_migrate
{
425 my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
427 my ($storeid, $volname) = parse_volume_id
($volid);
428 $target_volname = $volname if !$target_volname;
430 my $scfg = storage_config
($cfg, $storeid);
432 # no need to migrate shared content
433 return if $storeid eq $target_storeid && $scfg->{shared
};
435 my $tcfg = storage_config
($cfg, $target_storeid);
437 my $target_volid = "${target_storeid}:${target_volname}";
439 my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_host'";
441 my $sshoptions = "-o 'BatchMode=yes'";
442 my $ssh = "/usr/bin/ssh $sshoptions";
444 local $ENV{RSYNC_RSH
} = $ssh;
446 # only implemented for file system based storage
450 my $src_plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
451 my $dst_plugin = PVE
::Storage
::Plugin-
>lookup($tcfg->{type
});
452 my $src = $src_plugin->path($scfg, $volname, $storeid);
453 my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid);
455 my $dirname = dirname
($dst);
457 if ($tcfg->{shared
}) { # we can do a local copy
459 run_command
(['/bin/mkdir', '-p', $dirname]);
461 run_command
(['/bin/cp', $src, $dst]);
464 run_command
(['/usr/bin/ssh', "root\@${target_host}",
465 '/bin/mkdir', '-p', $dirname]);
467 # we use rsync with --sparse, so we can't use --inplace,
468 # so we remove file on the target if it already exists to
470 my ($size, $format) = PVE
::Storage
::Plugin
::file_size_info
($src);
471 if ($format && ($format eq 'raw') && $size) {
472 run_command
(['/usr/bin/ssh', "root\@${target_host}",
478 if ($format eq 'subvol') {
479 $cmd = ['/usr/bin/rsync', '--progress', '-X', '-A', '--numeric-ids',
480 '-aH', '--delete', '--no-whole-file', '--inplace',
481 '--one-file-system', "$src/", "[root\@${target_host}]:$dst"];
483 $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
484 $src, "[root\@${target_host}]:$dst"];
489 run_command
($cmd, outfunc
=> sub {
492 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
495 print "rsync status: $1\n";
505 die "$errstr - target type '$tcfg->{type}' not implemented\n";
508 } elsif ($scfg->{type
} eq 'zfspool') {
510 if ($tcfg->{type
} eq 'zfspool') {
512 die "$errstr - pool on target has not same name as source!"
513 if $tcfg->{pool
} ne $scfg->{pool
};
515 my (undef, $volname) = parse_volname
($cfg, $volid);
517 my $zfspath = "$scfg->{pool}\/$volname";
519 my $snap = ['zfs', 'snapshot', "$zfspath\@__migration__"];
521 my $send = [['zfs', 'send', '-Rpv', "$zfspath\@__migration__"], ['ssh', "root\@$target_host",
522 'zfs', 'recv', $zfspath]];
524 my $destroy_target = ['ssh', "root\@$target_host", 'zfs', 'destroy', "$zfspath\@__migration__"];
531 run_command
(['zfs', 'destroy', "$zfspath\@__migration__"]);
534 run_command
($destroy_target);
537 die "$errstr - target type $tcfg->{type} is not valid\n";
540 die "$errstr - source type '$scfg->{type}' not implemented\n";
545 my ($cfg, $volid, $vmid, $snap) = @_;
547 my ($storeid, $volname) = parse_volume_id
($volid);
549 my $scfg = storage_config
($cfg, $storeid);
551 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
553 activate_storage
($cfg, $storeid);
555 # lock shared storage
556 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
557 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
558 return "$storeid:$volname";
562 sub vdisk_create_base
{
563 my ($cfg, $volid) = @_;
565 my ($storeid, $volname) = parse_volume_id
($volid);
567 my $scfg = storage_config
($cfg, $storeid);
569 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
571 activate_storage
($cfg, $storeid);
573 # lock shared storage
574 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
575 my $volname = $plugin->create_base($storeid, $scfg, $volname);
576 return "$storeid:$volname";
581 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
583 die "no storage id specified\n" if !$storeid;
585 PVE
::JSONSchema
::parse_storage_id
($storeid);
587 my $scfg = storage_config
($cfg, $storeid);
589 die "no VMID specified\n" if !$vmid;
591 $vmid = parse_vmid
($vmid);
593 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
595 $fmt = $defformat if !$fmt;
597 activate_storage
($cfg, $storeid);
599 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
601 # lock shared storage
602 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
603 my $old_umask = umask(umask|0037);
604 my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) };
608 return "$storeid:$volname";
613 my ($cfg, $volid) = @_;
615 my ($storeid, $volname) = parse_volume_id
($volid);
617 my $scfg = storage_config
($cfg, $storeid);
619 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
621 activate_storage
($cfg, $storeid);
625 # lock shared storage
626 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
628 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
629 $plugin->parse_volname($volname);
631 my $vollist = $plugin->list_images($storeid, $scfg);
632 foreach my $info (@$vollist) {
633 my (undef, $tmpvolname) = parse_volume_id
($info->{volid
});
634 my $basename = undef;
635 my $basevmid = undef;
638 (undef, undef, undef, $basename, $basevmid) =
639 $plugin->parse_volname($tmpvolname);
642 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
643 die "base volume '$volname' is still in use " .
644 "(use by '$tmpvolname')\n";
648 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format);
651 return if !$cleanup_worker;
653 my $rpcenv = PVE
::RPCEnvironment
::get
();
654 my $authuser = $rpcenv->get_user();
656 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
659 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
661 my ($cfg, $storeid, $tt) = @_;
663 die "unknown template type '$tt'\n"
664 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
666 my $ids = $cfg->{ids
};
668 storage_check_enabled
($cfg, $storeid) if ($storeid);
674 foreach my $sid (keys %$ids) {
675 next if $storeid && $storeid ne $sid;
677 my $scfg = $ids->{$sid};
678 my $type = $scfg->{type
};
680 next if !storage_check_enabled
($cfg, $sid, undef, 1);
682 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
683 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
684 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
686 activate_storage
($cfg, $sid);
689 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
691 my $path = $plugin->get_subdir($scfg, $tt);
693 foreach my $fn (<$path/*>) {
698 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
700 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
702 } elsif ($tt eq 'vztmpl') {
703 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
705 $info = { volid
=> "$sid:vztmpl/$1", format
=> "t$2" };
707 } elsif ($tt eq 'backup') {
708 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
710 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
713 $info->{size
} = -s
$fn;
715 push @{$res->{$sid}}, $info;
720 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
728 my ($cfg, $storeid, $vmid, $vollist) = @_;
730 my $ids = $cfg->{ids
};
732 storage_check_enabled
($cfg, $storeid) if ($storeid);
736 # prepare/activate/refresh all storages
738 my $storage_list = [];
740 foreach my $volid (@$vollist) {
741 my ($sid, undef) = parse_volume_id
($volid);
742 next if !defined($ids->{$sid});
743 next if !storage_check_enabled
($cfg, $sid, undef, 1);
744 push @$storage_list, $sid;
747 foreach my $sid (keys %$ids) {
748 next if $storeid && $storeid ne $sid;
749 next if !storage_check_enabled
($cfg, $sid, undef, 1);
750 push @$storage_list, $sid;
756 activate_storage_list
($cfg, $storage_list, $cache);
758 foreach my $sid (keys %$ids) {
759 next if $storeid && $storeid ne $sid;
760 next if !storage_check_enabled
($cfg, $sid, undef, 1);
762 my $scfg = $ids->{$sid};
763 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
764 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
765 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
772 my ($cfg, $storeid, $vmid, $content) = @_;
774 my @ctypes = qw(images vztmpl iso backup);
776 my $cts = $content ?
[ $content ] : [ @ctypes ];
778 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
781 foreach my $ct (@$cts) {
783 if ($ct eq 'images') {
784 $data = vdisk_list
($cfg, $storeid, $vmid);
785 } elsif ($ct eq 'iso' && !defined($vmid)) {
786 $data = template_list
($cfg, $storeid, 'iso');
787 } elsif ($ct eq 'vztmpl'&& !defined($vmid)) {
788 $data = template_list
($cfg, $storeid, 'vztmpl');
789 } elsif ($ct eq 'backup') {
790 $data = template_list
($cfg, $storeid, 'backup');
791 foreach my $item (@{$data->{$storeid}}) {
792 if (defined($vmid)) {
793 @{$data->{$storeid}} = grep { $_->{volid
} =~ m/\S+-$vmid-\S+/ } @{$data->{$storeid}};
798 next if !$data || !$data->{$storeid};
800 foreach my $item (@{$data->{$storeid}}) {
801 $item->{content
} = $ct;
811 my $filename = "/sys/kernel/uevent_seqnum";
814 if (my $fh = IO
::File-
>new($filename, "r")) {
816 if ($line =~ m/^(\d+)$/) {
824 sub activate_storage
{
825 my ($cfg, $storeid, $cache) = @_;
827 $cache = {} if !$cache;
829 my $scfg = storage_check_enabled
($cfg, $storeid);
831 return if $cache->{activated
}->{$storeid};
833 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
835 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
838 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
839 activate_storage
($cfg, $baseid, $cache);
842 if (!$plugin->check_connection($storeid, $scfg)) {
843 die "storage '$storeid' is not online\n";
846 $plugin->activate_storage($storeid, $scfg, $cache);
848 my $newseq = uevent_seqnum
();
850 # only call udevsettle if there are events
851 if ($newseq > $cache->{uevent_seqnum
}) {
853 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
854 $cache->{uevent_seqnum
} = $newseq;
857 $cache->{activated
}->{$storeid} = 1;
860 sub activate_storage_list
{
861 my ($cfg, $storeid_list, $cache) = @_;
863 $cache = {} if !$cache;
865 foreach my $storeid (@$storeid_list) {
866 activate_storage
($cfg, $storeid, $cache);
870 sub deactivate_storage
{
871 my ($cfg, $storeid) = @_;
873 my $scfg = storage_config
($cfg, $storeid);
874 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
877 $plugin->deactivate_storage($storeid, $scfg, $cache);
880 sub activate_volumes
{
881 my ($cfg, $vollist, $snapname) = @_;
883 return if !($vollist && scalar(@$vollist));
885 my $storagehash = {};
886 foreach my $volid (@$vollist) {
887 my ($storeid, undef) = parse_volume_id
($volid);
888 $storagehash->{$storeid} = 1;
893 activate_storage_list
($cfg, [keys %$storagehash], $cache);
895 foreach my $volid (@$vollist) {
896 my ($storeid, $volname) = parse_volume_id
($volid);
897 my $scfg = storage_config
($cfg, $storeid);
898 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
899 $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache);
903 sub deactivate_volumes
{
904 my ($cfg, $vollist, $snapname) = @_;
906 return if !($vollist && scalar(@$vollist));
911 foreach my $volid (@$vollist) {
912 my ($storeid, $volname) = parse_volume_id
($volid);
914 my $scfg = storage_config
($cfg, $storeid);
915 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
918 $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache);
922 push @errlist, $volid;
926 die "volume deativation failed: " . join(' ', @errlist)
931 my ($cfg, $content) = @_;
933 my $ids = $cfg->{ids
};
937 my @ctypes = PVE
::Tools
::split_list
($content);
940 foreach my $storeid (keys %$ids) {
942 next if !storage_check_enabled
($cfg, $storeid, undef, 1);
944 if (defined($content)) {
946 foreach my $ctype (@ctypes) {
947 if ($ids->{$storeid}->{content
}->{$ctype}) {
952 next if !$want_ctype;
955 my $type = $ids->{$storeid}->{type
};
957 $info->{$storeid} = {
962 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
963 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
967 push @$slist, $storeid;
972 foreach my $storeid (keys %$ids) {
973 my $scfg = $ids->{$storeid};
974 next if !$info->{$storeid};
976 eval { activate_storage
($cfg, $storeid, $cache); };
982 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
983 my ($total, $avail, $used, $active);
984 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
987 $info->{$storeid}->{total
} = $total;
988 $info->{$storeid}->{avail
} = $avail;
989 $info->{$storeid}->{used
} = $used;
990 $info->{$storeid}->{active
} = $active;
999 my ($packed_ip, $family);
1001 my @res = PVE
::Tools
::getaddrinfo_all
($server);
1002 $family = $res[0]->{family
};
1003 $packed_ip = (PVE
::Tools
::unpack_sockaddr_in46
($res[0]->{addr
}))[2];
1005 if (defined $packed_ip) {
1006 return Socket
::inet_ntop
($family, $packed_ip);
1012 my ($server_in) = @_;
1015 if (!($server = resolv_server
($server_in))) {
1016 die "unable to resolve address for server '${server_in}'\n";
1019 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
1022 run_command
($cmd, outfunc
=> sub {
1025 # note: howto handle white spaces in export path??
1026 if ($line =~ m!^(/\S+)\s+(.+)$!) {
1036 my $cmd = ['zfs', 'list', '-t', 'filesystem', '-H', '-o', 'name,avail,used'];
1039 run_command
($cmd, outfunc
=> sub {
1042 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
1043 my ($pool, $size_str, $used_str) = ($1, $2, $3);
1044 my $size = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($size_str);
1045 my $used = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($used_str);
1046 # ignore subvolumes generated by our ZFSPoolPlugin
1047 return if $pool =~ m!/subvol-\d+-[^/]+$!;
1048 push @$res, { pool
=> $pool, size
=> $size, free
=> $size-$used };
1056 my ($portal, $noerr) = @_;
1058 my ($server, $port) = PVE
::Tools
::parse_host_and_port
($portal);
1060 if (my $ip = resolv_server
($server)) {
1062 $server = "[$server]" if $server =~ /^$IPV6RE$/;
1063 return $port ?
"$server:$port" : $server;
1066 return undef if $noerr;
1068 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
1071 # idea is from usbutils package (/usr/bin/usb-devices) script
1072 sub __scan_usb_device
{
1073 my ($res, $devpath, $parent, $level) = @_;
1075 return if ! -d
$devpath;
1076 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
1077 my $port = $level ?
int($1 - 1) : 0;
1079 my $busnum = int(file_read_firstline
("$devpath/busnum"));
1080 my $devnum = int(file_read_firstline
("$devpath/devnum"));
1087 speed
=> file_read_firstline
("$devpath/speed"),
1088 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
1089 vendid
=> file_read_firstline
("$devpath/idVendor"),
1090 prodid
=> file_read_firstline
("$devpath/idProduct"),
1094 my $usbpath = $devpath;
1095 $usbpath =~ s
|^.*/\d
+\
-||;
1096 $d->{usbpath
} = $usbpath;
1099 my $product = file_read_firstline
("$devpath/product");
1100 $d->{product
} = $product if $product;
1102 my $manu = file_read_firstline
("$devpath/manufacturer");
1103 $d->{manufacturer
} = $manu if $manu;
1105 my $serial => file_read_firstline
("$devpath/serial");
1106 $d->{serial
} = $serial if $serial;
1110 foreach my $subdev (<$devpath/$busnum-*>) {
1111 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
1112 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
1121 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
1122 __scan_usb_device
($devlist, $device, 0, 0);
1129 my ($portal_in) = @_;
1132 if (!($portal = resolv_portal
($portal_in))) {
1133 die "unable to parse/resolve portal address '${portal_in}'\n";
1136 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
1139 sub storage_default_format
{
1140 my ($cfg, $storeid) = @_;
1142 my $scfg = storage_config
($cfg, $storeid);
1144 return PVE
::Storage
::Plugin
::default_format
($scfg);
1147 sub vgroup_is_used
{
1148 my ($cfg, $vgname) = @_;
1150 foreach my $storeid (keys %{$cfg->{ids
}}) {
1151 my $scfg = storage_config
($cfg, $storeid);
1152 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
1160 sub target_is_used
{
1161 my ($cfg, $target) = @_;
1163 foreach my $storeid (keys %{$cfg->{ids
}}) {
1164 my $scfg = storage_config
($cfg, $storeid);
1165 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
1173 sub volume_is_used
{
1174 my ($cfg, $volid) = @_;
1176 foreach my $storeid (keys %{$cfg->{ids
}}) {
1177 my $scfg = storage_config
($cfg, $storeid);
1178 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
1186 sub storage_is_used
{
1187 my ($cfg, $storeid) = @_;
1189 foreach my $sid (keys %{$cfg->{ids
}}) {
1190 my $scfg = storage_config
($cfg, $sid);
1191 next if !$scfg->{base
};
1192 my ($st) = parse_volume_id
($scfg->{base
});
1193 return 1 if $st && $st eq $storeid;
1200 my ($list, $func) = @_;
1204 foreach my $sid (keys %$list) {
1205 foreach my $info (@{$list->{$sid}}) {
1206 my $volid = $info->{volid
};
1207 my ($sid1, $volname) = parse_volume_id
($volid, 1);
1208 if ($sid1 && $sid1 eq $sid) {
1209 &$func ($volid, $sid, $info);
1211 warn "detected strange volid '$volid' in volume list for '$sid'\n";
1217 # bash completion helper
1219 sub complete_storage
{
1220 my ($cmdname, $pname, $cvalue) = @_;
1222 my $cfg = PVE
::Storage
::config
();
1224 return $cmdname eq 'add' ?
[] : [ PVE
::Storage
::storage_ids
($cfg) ];
1227 sub complete_storage_enabled
{
1228 my ($cmdname, $pname, $cvalue) = @_;
1232 my $cfg = PVE
::Storage
::config
();
1233 foreach my $sid (keys %{$cfg->{ids
}}) {
1234 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1240 sub complete_content_type
{
1241 my ($cmdname, $pname, $cvalue) = @_;
1243 return [qw(rootdir images vztmpl iso backup)];
1246 sub complete_volume
{
1247 my ($cmdname, $pname, $cvalue) = @_;
1251 my $storage_list = complete_storage_enabled
();
1253 if ($cvalue =~ m/^([^:]+):/) {
1254 $storage_list = [ $1 ];
1256 if (scalar(@$storage_list) > 1) {
1257 # only list storage IDs to avoid large listings
1259 foreach my $storeid (@$storage_list) {
1260 # Hack: simply return 2 artificial values, so that
1261 # completions does not finish
1262 push @$res, "$storeid:volname", "$storeid:...";
1269 foreach my $storeid (@$storage_list) {
1270 my $vollist = PVE
::Storage
::volume_list
($cfg, $storeid);
1272 foreach my $item (@$vollist) {
1273 push @$res, $item->{volid
};