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]);
465 run_command
(['/usr/bin/ssh', "root\@${target_host}",
466 '/bin/mkdir', '-p', $dirname]);
468 # we use rsync with --sparse, so we can't use --inplace,
469 # so we remove file on the target if it already exists to
471 my ($size, $format) = PVE
::Storage
::Plugin
::file_size_info
($src);
472 if ($format && ($format eq 'raw') && $size) {
473 run_command
(['/usr/bin/ssh', "root\@${target_host}",
478 my $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
479 $src, "root\@${target_host}:$dst"];
483 run_command
($cmd, outfunc
=> sub {
486 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
489 print "rsync status: $1\n";
499 die "$errstr - target type '$tcfg->{type}' not implemented\n";
502 } elsif ($scfg->{type
} eq 'zfspool') {
504 if ($tcfg->{type
} eq 'zfspool') {
506 die "$errstr - pool on target has not same name as source!"
507 if $tcfg->{pool
} ne $scfg->{pool
};
509 my (undef, $volname) = parse_volname
($cfg, $volid);
511 my $zfspath = "$scfg->{pool}\/$volname";
513 my $snap = "zfs snapshot $zfspath\@__migration__";
515 my $send = "zfs send -pv $zfspath\@__migration__ \| ssh root\@$target_host zfs recv $zfspath";
517 my $destroy_target = "ssh root\@$target_host zfs destroy $zfspath\@__migration__";
524 run_command
("zfs destroy $zfspath\@__migration__");
527 run_command
($destroy_target);
530 die "$errstr - target type $tcfg->{type} is not valid\n";
533 die "$errstr - source type '$scfg->{type}' not implemented\n";
538 my ($cfg, $volid, $vmid, $snap) = @_;
540 my ($storeid, $volname) = parse_volume_id
($volid);
542 my $scfg = storage_config
($cfg, $storeid);
544 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
546 activate_storage
($cfg, $storeid);
548 # lock shared storage
549 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
550 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
551 return "$storeid:$volname";
555 sub vdisk_create_base
{
556 my ($cfg, $volid) = @_;
558 my ($storeid, $volname) = parse_volume_id
($volid);
560 my $scfg = storage_config
($cfg, $storeid);
562 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
564 activate_storage
($cfg, $storeid);
566 # lock shared storage
567 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
568 my $volname = $plugin->create_base($storeid, $scfg, $volname);
569 return "$storeid:$volname";
574 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
576 die "no storage id specified\n" if !$storeid;
578 PVE
::JSONSchema
::parse_storage_id
($storeid);
580 my $scfg = storage_config
($cfg, $storeid);
582 die "no VMID specified\n" if !$vmid;
584 $vmid = parse_vmid
($vmid);
586 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
588 $fmt = $defformat if !$fmt;
590 activate_storage
($cfg, $storeid);
592 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
594 # lock shared storage
595 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
596 my $old_umask = umask(umask|0037);
597 my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) };
601 return "$storeid:$volname";
606 my ($cfg, $volid) = @_;
608 my ($storeid, $volname) = parse_volume_id
($volid);
610 my $scfg = storage_config
($cfg, $storeid);
612 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
614 activate_storage
($cfg, $storeid);
618 # lock shared storage
619 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
621 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
622 $plugin->parse_volname($volname);
624 my $vollist = $plugin->list_images($storeid, $scfg);
625 foreach my $info (@$vollist) {
626 my (undef, $tmpvolname) = parse_volume_id
($info->{volid
});
627 my $basename = undef;
628 my $basevmid = undef;
631 (undef, undef, undef, $basename, $basevmid) =
632 $plugin->parse_volname($tmpvolname);
635 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
636 die "base volume '$volname' is still in use " .
637 "(use by '$tmpvolname')\n";
641 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format);
644 return if !$cleanup_worker;
646 my $rpcenv = PVE
::RPCEnvironment
::get
();
647 my $authuser = $rpcenv->get_user();
649 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
652 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
654 my ($cfg, $storeid, $tt) = @_;
656 die "unknown template type '$tt'\n"
657 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
659 my $ids = $cfg->{ids
};
661 storage_check_enabled
($cfg, $storeid) if ($storeid);
667 foreach my $sid (keys %$ids) {
668 next if $storeid && $storeid ne $sid;
670 my $scfg = $ids->{$sid};
671 my $type = $scfg->{type
};
673 next if !storage_check_enabled
($cfg, $sid, undef, 1);
675 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
676 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
677 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
679 activate_storage
($cfg, $sid);
682 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
684 my $path = $plugin->get_subdir($scfg, $tt);
686 foreach my $fn (<$path/*>) {
691 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
693 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
695 } elsif ($tt eq 'vztmpl') {
696 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
698 $info = { volid
=> "$sid:vztmpl/$1", format
=> "t$2" };
700 } elsif ($tt eq 'backup') {
701 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
703 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
706 $info->{size
} = -s
$fn;
708 push @{$res->{$sid}}, $info;
713 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
721 my ($cfg, $storeid, $vmid, $vollist) = @_;
723 my $ids = $cfg->{ids
};
725 storage_check_enabled
($cfg, $storeid) if ($storeid);
729 # prepare/activate/refresh all storages
731 my $storage_list = [];
733 foreach my $volid (@$vollist) {
734 my ($sid, undef) = parse_volume_id
($volid);
735 next if !defined($ids->{$sid});
736 next if !storage_check_enabled
($cfg, $sid, undef, 1);
737 push @$storage_list, $sid;
740 foreach my $sid (keys %$ids) {
741 next if $storeid && $storeid ne $sid;
742 next if !storage_check_enabled
($cfg, $sid, undef, 1);
743 push @$storage_list, $sid;
749 activate_storage_list
($cfg, $storage_list, $cache);
751 foreach my $sid (keys %$ids) {
752 next if $storeid && $storeid ne $sid;
753 next if !storage_check_enabled
($cfg, $sid, undef, 1);
755 my $scfg = $ids->{$sid};
756 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
757 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
758 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
765 my ($cfg, $storeid, $vmid, $content) = @_;
767 my @ctypes = qw(images vztmpl iso backup);
769 my $cts = $content ?
[ $content ] : [ @ctypes ];
771 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
774 foreach my $ct (@$cts) {
776 if ($ct eq 'images') {
777 $data = vdisk_list
($cfg, $storeid, $vmid);
778 } elsif ($ct eq 'iso' && !defined($vmid)) {
779 $data = template_list
($cfg, $storeid, 'iso');
780 } elsif ($ct eq 'vztmpl'&& !defined($vmid)) {
781 $data = template_list
($cfg, $storeid, 'vztmpl');
782 } elsif ($ct eq 'backup') {
783 $data = template_list
($cfg, $storeid, 'backup');
784 foreach my $item (@{$data->{$storeid}}) {
785 if (defined($vmid)) {
786 @{$data->{$storeid}} = grep { $_->{volid
} =~ m/\S+-$vmid-\S+/ } @{$data->{$storeid}};
791 next if !$data || !$data->{$storeid};
793 foreach my $item (@{$data->{$storeid}}) {
794 $item->{content
} = $ct;
804 my $filename = "/sys/kernel/uevent_seqnum";
807 if (my $fh = IO
::File-
>new($filename, "r")) {
809 if ($line =~ m/^(\d+)$/) {
817 sub activate_storage
{
818 my ($cfg, $storeid, $cache) = @_;
820 $cache = {} if !$cache;
822 my $scfg = storage_check_enabled
($cfg, $storeid);
824 return if $cache->{activated
}->{$storeid};
826 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
828 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
831 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
832 activate_storage
($cfg, $baseid, $cache);
835 if (!$plugin->check_connection($storeid, $scfg)) {
836 die "storage '$storeid' is not online\n";
839 $plugin->activate_storage($storeid, $scfg, $cache);
841 my $newseq = uevent_seqnum
();
843 # only call udevsettle if there are events
844 if ($newseq > $cache->{uevent_seqnum
}) {
846 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
847 $cache->{uevent_seqnum
} = $newseq;
850 $cache->{activated
}->{$storeid} = 1;
853 sub activate_storage_list
{
854 my ($cfg, $storeid_list, $cache) = @_;
856 $cache = {} if !$cache;
858 foreach my $storeid (@$storeid_list) {
859 activate_storage
($cfg, $storeid, $cache);
863 sub deactivate_storage
{
864 my ($cfg, $storeid) = @_;
866 my $scfg = storage_config
($cfg, $storeid);
867 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
870 $plugin->deactivate_storage($storeid, $scfg, $cache);
873 sub activate_volumes
{
874 my ($cfg, $vollist, $snapname) = @_;
876 return if !($vollist && scalar(@$vollist));
878 my $storagehash = {};
879 foreach my $volid (@$vollist) {
880 my ($storeid, undef) = parse_volume_id
($volid);
881 $storagehash->{$storeid} = 1;
886 activate_storage_list
($cfg, [keys %$storagehash], $cache);
888 foreach my $volid (@$vollist) {
889 my ($storeid, $volname) = parse_volume_id
($volid);
890 my $scfg = storage_config
($cfg, $storeid);
891 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
892 $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache);
896 sub deactivate_volumes
{
897 my ($cfg, $vollist, $snapname) = @_;
899 return if !($vollist && scalar(@$vollist));
904 foreach my $volid (@$vollist) {
905 my ($storeid, $volname) = parse_volume_id
($volid);
907 my $scfg = storage_config
($cfg, $storeid);
908 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
911 $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache);
915 push @errlist, $volid;
919 die "volume deativation failed: " . join(' ', @errlist)
924 my ($cfg, $content) = @_;
926 my $ids = $cfg->{ids
};
930 my @ctypes = PVE
::Tools
::split_list
($content);
933 foreach my $storeid (keys %$ids) {
935 next if !storage_check_enabled
($cfg, $storeid, undef, 1);
937 if (defined($content)) {
939 foreach my $ctype (@ctypes) {
940 if ($ids->{$storeid}->{content
}->{$ctype}) {
945 next if !$want_ctype;
948 my $type = $ids->{$storeid}->{type
};
950 $info->{$storeid} = {
955 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
956 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
960 push @$slist, $storeid;
965 foreach my $storeid (keys %$ids) {
966 my $scfg = $ids->{$storeid};
967 next if !$info->{$storeid};
969 eval { activate_storage
($cfg, $storeid, $cache); };
975 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
976 my ($total, $avail, $used, $active);
977 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
980 $info->{$storeid}->{total
} = $total;
981 $info->{$storeid}->{avail
} = $avail;
982 $info->{$storeid}->{used
} = $used;
983 $info->{$storeid}->{active
} = $active;
992 my ($packed_ip, $family);
994 my @res = PVE
::Tools
::getaddrinfo_all
($server);
995 $family = $res[0]->{family
};
996 $packed_ip = (PVE
::Tools
::unpack_sockaddr_in46
($res[0]->{addr
}))[2];
998 if (defined $packed_ip) {
999 return Socket
::inet_ntop
($family, $packed_ip);
1005 my ($server_in) = @_;
1008 if (!($server = resolv_server
($server_in))) {
1009 die "unable to resolve address for server '${server_in}'\n";
1012 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
1015 run_command
($cmd, outfunc
=> sub {
1018 # note: howto handle white spaces in export path??
1019 if ($line =~ m!^(/\S+)\s+(.+)$!) {
1029 my $cmd = ['zfs', 'list', '-t', 'filesystem', '-H', '-o', 'name,avail,used'];
1032 run_command
($cmd, outfunc
=> sub {
1035 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
1036 my ($pool, $size_str, $used_str) = ($1, $2, $3);
1037 my $size = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($size_str);
1038 my $used = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($used_str);
1039 # ignore subvolumes generated by our ZFSPoolPlugin
1040 return if $pool =~ m!/subvol-\d+-[^/]+$!;
1041 push @$res, { pool
=> $pool, size
=> $size, free
=> $size-$used };
1049 my ($portal, $noerr) = @_;
1051 my ($server, $port) = PVE
::Tools
::parse_host_and_port
($portal);
1053 if (my $ip = resolv_server
($server)) {
1055 $server = "[$server]" if $server =~ /^$IPV6RE$/;
1056 return $port ?
"$server:$port" : $server;
1059 return undef if $noerr;
1061 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
1064 # idea is from usbutils package (/usr/bin/usb-devices) script
1065 sub __scan_usb_device
{
1066 my ($res, $devpath, $parent, $level) = @_;
1068 return if ! -d
$devpath;
1069 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
1070 my $port = $level ?
int($1 - 1) : 0;
1072 my $busnum = int(file_read_firstline
("$devpath/busnum"));
1073 my $devnum = int(file_read_firstline
("$devpath/devnum"));
1080 speed
=> file_read_firstline
("$devpath/speed"),
1081 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
1082 vendid
=> file_read_firstline
("$devpath/idVendor"),
1083 prodid
=> file_read_firstline
("$devpath/idProduct"),
1087 my $usbpath = $devpath;
1088 $usbpath =~ s
|^.*/\d
+\
-||;
1089 $d->{usbpath
} = $usbpath;
1092 my $product = file_read_firstline
("$devpath/product");
1093 $d->{product
} = $product if $product;
1095 my $manu = file_read_firstline
("$devpath/manufacturer");
1096 $d->{manufacturer
} = $manu if $manu;
1098 my $serial => file_read_firstline
("$devpath/serial");
1099 $d->{serial
} = $serial if $serial;
1103 foreach my $subdev (<$devpath/$busnum-*>) {
1104 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
1105 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
1114 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
1115 __scan_usb_device
($devlist, $device, 0, 0);
1122 my ($portal_in) = @_;
1125 if (!($portal = resolv_portal
($portal_in))) {
1126 die "unable to parse/resolve portal address '${portal_in}'\n";
1129 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
1132 sub storage_default_format
{
1133 my ($cfg, $storeid) = @_;
1135 my $scfg = storage_config
($cfg, $storeid);
1137 return PVE
::Storage
::Plugin
::default_format
($scfg);
1140 sub vgroup_is_used
{
1141 my ($cfg, $vgname) = @_;
1143 foreach my $storeid (keys %{$cfg->{ids
}}) {
1144 my $scfg = storage_config
($cfg, $storeid);
1145 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
1153 sub target_is_used
{
1154 my ($cfg, $target) = @_;
1156 foreach my $storeid (keys %{$cfg->{ids
}}) {
1157 my $scfg = storage_config
($cfg, $storeid);
1158 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
1166 sub volume_is_used
{
1167 my ($cfg, $volid) = @_;
1169 foreach my $storeid (keys %{$cfg->{ids
}}) {
1170 my $scfg = storage_config
($cfg, $storeid);
1171 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
1179 sub storage_is_used
{
1180 my ($cfg, $storeid) = @_;
1182 foreach my $sid (keys %{$cfg->{ids
}}) {
1183 my $scfg = storage_config
($cfg, $sid);
1184 next if !$scfg->{base
};
1185 my ($st) = parse_volume_id
($scfg->{base
});
1186 return 1 if $st && $st eq $storeid;
1193 my ($list, $func) = @_;
1197 foreach my $sid (keys %$list) {
1198 foreach my $info (@{$list->{$sid}}) {
1199 my $volid = $info->{volid
};
1200 my ($sid1, $volname) = parse_volume_id
($volid, 1);
1201 if ($sid1 && $sid1 eq $sid) {
1202 &$func ($volid, $sid, $info);
1204 warn "detected strange volid '$volid' in volume list for '$sid'\n";
1210 # bash completion helper
1212 sub complete_storage
{
1213 my ($cmdname, $pname, $cvalue) = @_;
1215 my $cfg = PVE
::Storage
::config
();
1217 return $cmdname eq 'add' ?
[] : [ PVE
::Storage
::storage_ids
($cfg) ];
1220 sub complete_storage_enabled
{
1221 my ($cmdname, $pname, $cvalue) = @_;
1225 my $cfg = PVE
::Storage
::config
();
1226 foreach my $sid (keys %{$cfg->{ids
}}) {
1227 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1233 sub complete_content_type
{
1234 my ($cmdname, $pname, $cvalue) = @_;
1236 return [qw(rootdir images vztmpl iso backup)];
1239 sub complete_volume
{
1240 my ($cmdname, $pname, $cvalue) = @_;
1244 my $storage_list = complete_storage_enabled
();
1246 if ($cvalue =~ m/^([^:]+):/) {
1247 $storage_list = [ $1 ];
1249 if (scalar(@$storage_list) > 1) {
1250 # only list storage IDs to avoid large listings
1252 foreach my $storeid (@$storage_list) {
1253 # Hack: simply return 2 artificial values, so that
1254 # completions does not finish
1255 push @$res, "$storeid:volname", "$storeid:...";
1262 foreach my $storeid (@$storage_list) {
1263 my $vollist = PVE
::Storage
::volume_list
($cfg, $storeid);
1265 foreach my $item (@$vollist) {
1266 push @$res, $item->{volid
};