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
::NFSPlugin
;
26 use PVE
::Storage
::ISCSIPlugin
;
27 use PVE
::Storage
::RBDPlugin
;
28 use PVE
::Storage
::SheepdogPlugin
;
29 use PVE
::Storage
::ISCSIDirectPlugin
;
30 use PVE
::Storage
::GlusterfsPlugin
;
31 use PVE
::Storage
::ZFSPoolPlugin
;
32 use PVE
::Storage
::ZFSPlugin
;
33 use PVE
::Storage
::DRBDPlugin
;
35 # load and initialize all plugins
36 PVE
::Storage
::DirPlugin-
>register();
37 PVE
::Storage
::LVMPlugin-
>register();
38 PVE
::Storage
::NFSPlugin-
>register();
39 PVE
::Storage
::ISCSIPlugin-
>register();
40 PVE
::Storage
::RBDPlugin-
>register();
41 PVE
::Storage
::SheepdogPlugin-
>register();
42 PVE
::Storage
::ISCSIDirectPlugin-
>register();
43 PVE
::Storage
::GlusterfsPlugin-
>register();
44 PVE
::Storage
::ZFSPoolPlugin-
>register();
45 PVE
::Storage
::ZFSPlugin-
>register();
46 PVE
::Storage
::DRBDPlugin-
>register();
47 PVE
::Storage
::Plugin-
>init();
49 my $UDEVADM = '/sbin/udevadm';
51 # PVE::Storage utility functions
54 return cfs_read_file
("storage.cfg");
57 sub lock_storage_config
{
58 my ($code, $errmsg) = @_;
60 cfs_lock_file
("storage.cfg", undef, $code);
63 $errmsg ?
die "$errmsg: $err" : die $err;
68 my ($cfg, $storeid, $noerr) = @_;
70 die "no storage id specified\n" if !$storeid;
72 my $scfg = $cfg->{ids
}->{$storeid};
74 die "storage '$storeid' does not exists\n" if (!$noerr && !$scfg);
79 sub storage_check_node
{
80 my ($cfg, $storeid, $node, $noerr) = @_;
82 my $scfg = storage_config
($cfg, $storeid);
85 $node = PVE
::INotify
::nodename
() if !$node || ($node eq 'localhost');
86 if (!$scfg->{nodes
}->{$node}) {
87 die "storage '$storeid' is not available on node '$node'\n" if !$noerr;
95 sub storage_check_enabled
{
96 my ($cfg, $storeid, $node, $noerr) = @_;
98 my $scfg = storage_config
($cfg, $storeid);
100 if ($scfg->{disable
}) {
101 die "storage '$storeid' is disabled\n" if !$noerr;
105 return storage_check_node
($cfg, $storeid, $node, $noerr);
111 return keys %{$cfg->{ids
}};
115 my ($filename, $timeout) = @_;
117 return PVE
::Storage
::Plugin
::file_size_info
($filename, $timeout);
120 sub volume_size_info
{
121 my ($cfg, $volid, $timeout) = @_;
123 my ($storeid, $volname) = parse_volume_id
($volid, 1);
125 my $scfg = storage_config
($cfg, $storeid);
126 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
127 return $plugin->volume_size_info($scfg, $storeid, $volname, $timeout);
128 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
129 return file_size_info
($volid, $timeout);
136 my ($cfg, $volid, $size, $running) = @_;
138 my ($storeid, $volname) = parse_volume_id
($volid, 1);
140 my $scfg = storage_config
($cfg, $storeid);
141 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
142 return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running);
143 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
144 die "resize file/device '$volid' is not possible\n";
146 die "unable to parse volume ID '$volid'\n";
150 sub volume_rollback_is_possible
{
151 my ($cfg, $volid, $snap) = @_;
153 my ($storeid, $volname) = parse_volume_id
($volid, 1);
155 my $scfg = storage_config
($cfg, $storeid);
156 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
157 return $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
158 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
159 die "snapshot rollback file/device '$volid' is not possible\n";
161 die "unable to parse volume ID '$volid'\n";
165 sub volume_snapshot
{
166 my ($cfg, $volid, $snap) = @_;
168 my ($storeid, $volname) = parse_volume_id
($volid, 1);
170 my $scfg = storage_config
($cfg, $storeid);
171 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
172 return $plugin->volume_snapshot($scfg, $storeid, $volname, $snap);
173 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
174 die "snapshot file/device '$volid' is not possible\n";
176 die "unable to parse volume ID '$volid'\n";
180 sub volume_snapshot_rollback
{
181 my ($cfg, $volid, $snap) = @_;
183 my ($storeid, $volname) = parse_volume_id
($volid, 1);
185 my $scfg = storage_config
($cfg, $storeid);
186 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
187 $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
188 return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap);
189 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
190 die "snapshot rollback file/device '$volid' is not possible\n";
192 die "unable to parse volume ID '$volid'\n";
196 sub volume_snapshot_delete
{
197 my ($cfg, $volid, $snap, $running) = @_;
199 my ($storeid, $volname) = parse_volume_id
($volid, 1);
201 my $scfg = storage_config
($cfg, $storeid);
202 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
203 return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running);
204 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
205 die "snapshot delete file/device '$volid' is not possible\n";
207 die "unable to parse volume ID '$volid'\n";
211 sub volume_has_feature
{
212 my ($cfg, $feature, $volid, $snap, $running) = @_;
214 my ($storeid, $volname) = parse_volume_id
($volid, 1);
216 my $scfg = storage_config
($cfg, $storeid);
217 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
218 return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running);
219 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
227 my ($cfg, $storeid, $vmid) = @_;
229 my $scfg = storage_config
($cfg, $storeid);
230 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
232 my $path = $plugin->get_subdir($scfg, 'images');
234 return $vmid ?
"$path/$vmid" : $path;
237 sub get_private_dir
{
238 my ($cfg, $storeid, $vmid) = @_;
240 my $scfg = storage_config
($cfg, $storeid);
241 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
243 my $path = $plugin->get_subdir($scfg, 'rootdir');
245 return $vmid ?
"$path/$vmid" : $path;
249 my ($cfg, $storeid) = @_;
251 my $scfg = storage_config
($cfg, $storeid);
252 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
254 return $plugin->get_subdir($scfg, 'iso');
258 my ($cfg, $storeid) = @_;
260 my $scfg = storage_config
($cfg, $storeid);
261 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
263 return $plugin->get_subdir($scfg, 'vztmpl');
267 my ($cfg, $storeid) = @_;
269 my $scfg = storage_config
($cfg, $storeid);
270 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
272 return $plugin->get_subdir($scfg, 'backup');
275 # library implementation
280 die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/;
286 my ($cfg, $volid) = @_;
288 my ($storeid, $volname) = parse_volume_id
($volid);
290 my $scfg = storage_config
($cfg, $storeid);
292 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
294 # returns ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format)
296 return $plugin->parse_volname($volname);
299 sub parse_volume_id
{
300 my ($volid, $noerr) = @_;
302 return PVE
::Storage
::Plugin
::parse_volume_id
($volid, $noerr);
306 my ($cfg, $volid) = @_;
308 my ($sid, $volname) = parse_volume_id
($volid, 1);
311 if (my $scfg = $cfg->{ids
}->{$sid}) {
312 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
313 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
314 $plugin->parse_volname($volname);
315 return $isBase ?
1 : 0;
317 # stale volid with undefined storage - so we can just guess
318 if ($volid =~ m/base-/) {
326 # try to map a filesystem path to a volume identifier
327 sub path_to_volume_id
{
328 my ($cfg, $path) = @_;
330 my $ids = $cfg->{ids
};
332 my ($sid, $volname) = parse_volume_id
($path, 1);
334 if (my $scfg = $ids->{$sid}) {
336 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
337 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
338 return ($vtype, $path);
344 # Note: abs_path() return undef if $path doesn not exist
345 # for example when nfs storage is not mounted
346 $path = abs_path
($path) || $path;
348 foreach my $sid (keys %$ids) {
349 my $scfg = $ids->{$sid};
350 next if !$scfg->{path
};
351 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
352 my $imagedir = $plugin->get_subdir($scfg, 'images');
353 my $isodir = $plugin->get_subdir($scfg, 'iso');
354 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
355 my $backupdir = $plugin->get_subdir($scfg, 'backup');
356 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
358 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
362 my $vollist = $plugin->list_images($sid, $scfg, $vmid);
363 foreach my $info (@$vollist) {
364 my ($storeid, $volname) = parse_volume_id
($info->{volid
});
365 my $volpath = $plugin->path($scfg, $volname, $storeid);
366 if ($volpath eq $path) {
367 return ('images', $info->{volid
});
370 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
372 return ('iso', "$sid:iso/$name");
373 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
375 return ('vztmpl', "$sid:vztmpl/$name");
376 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
378 return ('rootdir', "$sid:rootdir/$vmid");
379 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!) {
381 return ('iso', "$sid:backup/$name");
385 # can't map path to volume id
390 my ($cfg, $volid, $snapname) = @_;
392 my ($storeid, $volname) = parse_volume_id
($volid);
394 my $scfg = storage_config
($cfg, $storeid);
396 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
397 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname);
398 return wantarray ?
($path, $owner, $vtype) : $path;
401 sub abs_filesystem_path
{
402 my ($cfg, $volid) = @_;
405 if (PVE
::Storage
::parse_volume_id
($volid, 1)) {
406 PVE
::Storage
::activate_volumes
($cfg, [ $volid ]);
407 $path = PVE
::Storage
::path
($cfg, $volid);
410 my $abspath = abs_path
($volid);
411 if ($abspath && $abspath =~ m
|^(/.+)$|) {
412 $path = $1; # untaint any path
417 die "can't find file '$volid'\n" if !($path && -f
$path);
422 sub storage_migrate
{
423 my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
425 my ($storeid, $volname) = parse_volume_id
($volid);
426 $target_volname = $volname if !$target_volname;
428 my $scfg = storage_config
($cfg, $storeid);
430 # no need to migrate shared content
431 return if $storeid eq $target_storeid && $scfg->{shared
};
433 my $tcfg = storage_config
($cfg, $target_storeid);
435 my $target_volid = "${target_storeid}:${target_volname}";
437 my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_host'";
439 my $sshoptions = "-o 'BatchMode=yes'";
440 my $ssh = "/usr/bin/ssh $sshoptions";
442 local $ENV{RSYNC_RSH
} = $ssh;
444 # only implemented for file system based storage
448 my $src_plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
449 my $dst_plugin = PVE
::Storage
::Plugin-
>lookup($tcfg->{type
});
450 my $src = $src_plugin->path($scfg, $volname, $storeid);
451 my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid);
453 my $dirname = dirname
($dst);
455 if ($tcfg->{shared
}) { # we can do a local copy
457 run_command
(['/bin/mkdir', '-p', $dirname]);
459 run_command
(['/bin/cp', $src, $dst]);
463 run_command
(['/usr/bin/ssh', "root\@${target_host}",
464 '/bin/mkdir', '-p', $dirname]);
466 # we use rsync with --sparse, so we can't use --inplace,
467 # so we remove file on the target if it already exists to
469 my ($size, $format) = PVE
::Storage
::Plugin
::file_size_info
($src);
470 if ($format && ($format eq 'raw') && $size) {
471 run_command
(['/usr/bin/ssh', "root\@${target_host}",
476 my $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
477 $src, "root\@${target_host}:$dst"];
481 run_command
($cmd, outfunc
=> sub {
484 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
487 print "rsync status: $1\n";
497 die "$errstr - target type '$tcfg->{type}' not implemented\n";
500 } elsif ($scfg->{type
} eq 'zfspool') {
502 if ($tcfg->{type
} eq 'zfspool') {
504 die "$errstr - pool on target has not same name as source!"
505 if $tcfg->{pool
} ne $scfg->{pool
};
507 my (undef, $volname) = parse_volname
($cfg, $volid);
509 my $zfspath = "$scfg->{pool}\/$volname";
511 my $snap = "zfs snapshot $zfspath\@__migration__";
513 my $send = "zfs send -v $zfspath\@__migration__ \| ssh root\@$target_host zfs recv $zfspath";
515 my $destroy_target = "ssh root\@$target_host zfs destroy $zfspath\@__migration__";
522 run_command
("zfs destroy $zfspath\@__migration__");
525 run_command
($destroy_target);
528 die "$errstr - target type $tcfg->{type} is not valid\n";
531 die "$errstr - source type '$scfg->{type}' not implemented\n";
536 my ($cfg, $volid, $vmid, $snap) = @_;
538 my ($storeid, $volname) = parse_volume_id
($volid);
540 my $scfg = storage_config
($cfg, $storeid);
542 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
544 activate_storage
($cfg, $storeid);
546 # lock shared storage
547 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
548 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
549 return "$storeid:$volname";
553 sub vdisk_create_base
{
554 my ($cfg, $volid) = @_;
556 my ($storeid, $volname) = parse_volume_id
($volid);
558 my $scfg = storage_config
($cfg, $storeid);
560 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
562 activate_storage
($cfg, $storeid);
564 # lock shared storage
565 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
566 my $volname = $plugin->create_base($storeid, $scfg, $volname);
567 return "$storeid:$volname";
572 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
574 die "no storage id specified\n" if !$storeid;
576 PVE
::JSONSchema
::parse_storage_id
($storeid);
578 my $scfg = storage_config
($cfg, $storeid);
580 die "no VMID specified\n" if !$vmid;
582 $vmid = parse_vmid
($vmid);
584 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
586 $fmt = $defformat if !$fmt;
588 activate_storage
($cfg, $storeid);
590 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
592 # lock shared storage
593 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
594 my $old_umask = umask(umask|0037);
595 my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) };
599 return "$storeid:$volname";
604 my ($cfg, $volid) = @_;
606 my ($storeid, $volname) = parse_volume_id
($volid);
608 my $scfg = storage_config
($cfg, $storeid);
610 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
612 activate_storage
($cfg, $storeid);
616 # lock shared storage
617 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
619 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
620 $plugin->parse_volname($volname);
622 my $vollist = $plugin->list_images($storeid, $scfg);
623 foreach my $info (@$vollist) {
624 my (undef, $tmpvolname) = parse_volume_id
($info->{volid
});
625 my $basename = undef;
626 my $basevmid = undef;
629 (undef, undef, undef, $basename, $basevmid) =
630 $plugin->parse_volname($tmpvolname);
633 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
634 die "base volume '$volname' is still in use " .
635 "(use by '$tmpvolname')\n";
639 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format);
642 return if !$cleanup_worker;
644 my $rpcenv = PVE
::RPCEnvironment
::get
();
645 my $authuser = $rpcenv->get_user();
647 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
650 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
652 my ($cfg, $storeid, $tt) = @_;
654 die "unknown template type '$tt'\n"
655 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
657 my $ids = $cfg->{ids
};
659 storage_check_enabled
($cfg, $storeid) if ($storeid);
665 foreach my $sid (keys %$ids) {
666 next if $storeid && $storeid ne $sid;
668 my $scfg = $ids->{$sid};
669 my $type = $scfg->{type
};
671 next if !storage_check_enabled
($cfg, $sid, undef, 1);
673 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
674 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
675 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
677 activate_storage
($cfg, $sid);
680 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
682 my $path = $plugin->get_subdir($scfg, $tt);
684 foreach my $fn (<$path/*>) {
689 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
691 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
693 } elsif ($tt eq 'vztmpl') {
694 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
696 $info = { volid
=> "$sid:vztmpl/$1", format
=> "t$2" };
698 } elsif ($tt eq 'backup') {
699 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
701 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
704 $info->{size
} = -s
$fn;
706 push @{$res->{$sid}}, $info;
711 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
719 my ($cfg, $storeid, $vmid, $vollist) = @_;
721 my $ids = $cfg->{ids
};
723 storage_check_enabled
($cfg, $storeid) if ($storeid);
727 # prepare/activate/refresh all storages
729 my $storage_list = [];
731 foreach my $volid (@$vollist) {
732 my ($sid, undef) = parse_volume_id
($volid);
733 next if !defined($ids->{$sid});
734 next if !storage_check_enabled
($cfg, $sid, undef, 1);
735 push @$storage_list, $sid;
738 foreach my $sid (keys %$ids) {
739 next if $storeid && $storeid ne $sid;
740 next if !storage_check_enabled
($cfg, $sid, undef, 1);
741 push @$storage_list, $sid;
747 activate_storage_list
($cfg, $storage_list, $cache);
749 foreach my $sid (keys %$ids) {
750 next if $storeid && $storeid ne $sid;
751 next if !storage_check_enabled
($cfg, $sid, undef, 1);
753 my $scfg = $ids->{$sid};
754 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
755 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
756 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
763 my ($cfg, $storeid, $vmid, $content) = @_;
765 my @ctypes = qw(images vztmpl iso backup);
767 my $cts = $content ?
[ $content ] : [ @ctypes ];
769 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
772 foreach my $ct (@$cts) {
774 if ($ct eq 'images') {
775 $data = vdisk_list
($cfg, $storeid, $vmid);
776 } elsif ($ct eq 'iso' && !defined($vmid)) {
777 $data = template_list
($cfg, $storeid, 'iso');
778 } elsif ($ct eq 'vztmpl'&& !defined($vmid)) {
779 $data = template_list
($cfg, $storeid, 'vztmpl');
780 } elsif ($ct eq 'backup') {
781 $data = template_list
($cfg, $storeid, 'backup');
782 foreach my $item (@{$data->{$storeid}}) {
783 if (defined($vmid)) {
784 @{$data->{$storeid}} = grep { $_->{volid
} =~ m/\S+-$vmid-\S+/ } @{$data->{$storeid}};
789 next if !$data || !$data->{$storeid};
791 foreach my $item (@{$data->{$storeid}}) {
792 $item->{content
} = $ct;
802 my $filename = "/sys/kernel/uevent_seqnum";
805 if (my $fh = IO
::File-
>new($filename, "r")) {
807 if ($line =~ m/^(\d+)$/) {
815 sub activate_storage
{
816 my ($cfg, $storeid, $cache) = @_;
818 $cache = {} if !$cache;
820 my $scfg = storage_check_enabled
($cfg, $storeid);
822 return if $cache->{activated
}->{$storeid};
824 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
826 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
829 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
830 activate_storage
($cfg, $baseid, $cache);
833 if (!$plugin->check_connection($storeid, $scfg)) {
834 die "storage '$storeid' is not online\n";
837 $plugin->activate_storage($storeid, $scfg, $cache);
839 my $newseq = uevent_seqnum
();
841 # only call udevsettle if there are events
842 if ($newseq > $cache->{uevent_seqnum
}) {
844 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
845 $cache->{uevent_seqnum
} = $newseq;
848 $cache->{activated
}->{$storeid} = 1;
851 sub activate_storage_list
{
852 my ($cfg, $storeid_list, $cache) = @_;
854 $cache = {} if !$cache;
856 foreach my $storeid (@$storeid_list) {
857 activate_storage
($cfg, $storeid, $cache);
861 sub deactivate_storage
{
862 my ($cfg, $storeid) = @_;
864 my $scfg = storage_config
($cfg, $storeid);
865 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
868 $plugin->deactivate_storage($storeid, $scfg, $cache);
871 sub activate_volumes
{
872 my ($cfg, $vollist, $snapname) = @_;
874 return if !($vollist && scalar(@$vollist));
876 my $storagehash = {};
877 foreach my $volid (@$vollist) {
878 my ($storeid, undef) = parse_volume_id
($volid);
879 $storagehash->{$storeid} = 1;
884 activate_storage_list
($cfg, [keys %$storagehash], $cache);
886 foreach my $volid (@$vollist) {
887 my ($storeid, $volname) = parse_volume_id
($volid);
888 my $scfg = storage_config
($cfg, $storeid);
889 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
890 $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache);
894 sub deactivate_volumes
{
895 my ($cfg, $vollist, $snapname) = @_;
897 return if !($vollist && scalar(@$vollist));
902 foreach my $volid (@$vollist) {
903 my ($storeid, $volname) = parse_volume_id
($volid);
905 my $scfg = storage_config
($cfg, $storeid);
906 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
909 $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache);
913 push @errlist, $volid;
917 die "volume deativation failed: " . join(' ', @errlist)
922 my ($cfg, $content) = @_;
924 my $ids = $cfg->{ids
};
928 my @ctypes = PVE
::Tools
::split_list
($content);
931 foreach my $storeid (keys %$ids) {
933 next if !storage_check_enabled
($cfg, $storeid, undef, 1);
935 if (defined($content)) {
937 foreach my $ctype (@ctypes) {
938 if ($ids->{$storeid}->{content
}->{$ctype}) {
943 next if !$want_ctype;
946 my $type = $ids->{$storeid}->{type
};
948 $info->{$storeid} = {
953 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
954 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
958 push @$slist, $storeid;
963 foreach my $storeid (keys %$ids) {
964 my $scfg = $ids->{$storeid};
965 next if !$info->{$storeid};
967 eval { activate_storage
($cfg, $storeid, $cache); };
973 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
974 my ($total, $avail, $used, $active);
975 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
978 $info->{$storeid}->{total
} = $total;
979 $info->{$storeid}->{avail
} = $avail;
980 $info->{$storeid}->{used
} = $used;
981 $info->{$storeid}->{active
} = $active;
990 my ($packed_ip, $family);
992 my @res = PVE
::Tools
::getaddrinfo_all
($server);
993 $family = $res[0]->{family
};
994 $packed_ip = (PVE
::Tools
::unpack_sockaddr_in46
($res[0]->{addr
}))[2];
996 if (defined $packed_ip) {
997 return Socket
::inet_ntop
($family, $packed_ip);
1003 my ($server_in) = @_;
1006 if (!($server = resolv_server
($server_in))) {
1007 die "unable to resolve address for server '${server_in}'\n";
1010 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
1013 run_command
($cmd, outfunc
=> sub {
1016 # note: howto handle white spaces in export path??
1017 if ($line =~ m!^(/\S+)\s+(.+)$!) {
1027 my $cmd = ['zfs', 'list', '-t', 'filesystem', '-H', '-o', 'name,avail,used'];
1030 run_command
($cmd, outfunc
=> sub {
1033 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
1034 my ($pool, $size_str, $used_str) = ($1, $2, $3);
1035 my $size = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($size_str);
1036 my $used = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($used_str);
1037 # ignore subvolumes generated by our ZFSPoolPlugin
1038 return if $pool =~ m!/subvol-\d+-[^/]+$!;
1039 push @$res, { pool
=> $pool, size
=> $size, free
=> $size-$used };
1047 my ($portal, $noerr) = @_;
1049 my ($server, $port) = PVE
::Tools
::parse_host_and_port
($portal);
1051 if (my $ip = resolv_server
($server)) {
1053 $server = "[$server]" if $server =~ /^$IPV6RE$/;
1054 return $port ?
"$server:$port" : $server;
1057 return undef if $noerr;
1059 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
1062 # idea is from usbutils package (/usr/bin/usb-devices) script
1063 sub __scan_usb_device
{
1064 my ($res, $devpath, $parent, $level) = @_;
1066 return if ! -d
$devpath;
1067 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
1068 my $port = $level ?
int($1 - 1) : 0;
1070 my $busnum = int(file_read_firstline
("$devpath/busnum"));
1071 my $devnum = int(file_read_firstline
("$devpath/devnum"));
1078 speed
=> file_read_firstline
("$devpath/speed"),
1079 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
1080 vendid
=> file_read_firstline
("$devpath/idVendor"),
1081 prodid
=> file_read_firstline
("$devpath/idProduct"),
1085 my $usbpath = $devpath;
1086 $usbpath =~ s
|^.*/\d
+\
-||;
1087 $d->{usbpath
} = $usbpath;
1090 my $product = file_read_firstline
("$devpath/product");
1091 $d->{product
} = $product if $product;
1093 my $manu = file_read_firstline
("$devpath/manufacturer");
1094 $d->{manufacturer
} = $manu if $manu;
1096 my $serial => file_read_firstline
("$devpath/serial");
1097 $d->{serial
} = $serial if $serial;
1101 foreach my $subdev (<$devpath/$busnum-*>) {
1102 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
1103 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
1112 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
1113 __scan_usb_device
($devlist, $device, 0, 0);
1120 my ($portal_in) = @_;
1123 if (!($portal = resolv_portal
($portal_in))) {
1124 die "unable to parse/resolve portal address '${portal_in}'\n";
1127 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
1130 sub storage_default_format
{
1131 my ($cfg, $storeid) = @_;
1133 my $scfg = storage_config
($cfg, $storeid);
1135 return PVE
::Storage
::Plugin
::default_format
($scfg);
1138 sub vgroup_is_used
{
1139 my ($cfg, $vgname) = @_;
1141 foreach my $storeid (keys %{$cfg->{ids
}}) {
1142 my $scfg = storage_config
($cfg, $storeid);
1143 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
1151 sub target_is_used
{
1152 my ($cfg, $target) = @_;
1154 foreach my $storeid (keys %{$cfg->{ids
}}) {
1155 my $scfg = storage_config
($cfg, $storeid);
1156 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
1164 sub volume_is_used
{
1165 my ($cfg, $volid) = @_;
1167 foreach my $storeid (keys %{$cfg->{ids
}}) {
1168 my $scfg = storage_config
($cfg, $storeid);
1169 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
1177 sub storage_is_used
{
1178 my ($cfg, $storeid) = @_;
1180 foreach my $sid (keys %{$cfg->{ids
}}) {
1181 my $scfg = storage_config
($cfg, $sid);
1182 next if !$scfg->{base
};
1183 my ($st) = parse_volume_id
($scfg->{base
});
1184 return 1 if $st && $st eq $storeid;
1191 my ($list, $func) = @_;
1195 foreach my $sid (keys %$list) {
1196 foreach my $info (@{$list->{$sid}}) {
1197 my $volid = $info->{volid
};
1198 my ($sid1, $volname) = parse_volume_id
($volid, 1);
1199 if ($sid1 && $sid1 eq $sid) {
1200 &$func ($volid, $sid, $info);
1202 warn "detected strange volid '$volid' in volume list for '$sid'\n";
1208 # bash completion helper
1210 sub complete_storage
{
1211 my ($cmdname, $pname, $cvalue) = @_;
1213 my $cfg = PVE
::Storage
::config
();
1215 return $cmdname eq 'add' ?
[] : [ PVE
::Storage
::storage_ids
($cfg) ];
1218 sub complete_storage_enabled
{
1219 my ($cmdname, $pname, $cvalue) = @_;
1223 my $cfg = PVE
::Storage
::config
();
1224 foreach my $sid (keys %{$cfg->{ids
}}) {
1225 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1231 sub complete_content_type
{
1232 my ($cmdname, $pname, $cvalue) = @_;
1234 return [qw(rootdir images vztmpl iso backup)];
1237 sub complete_volume
{
1238 my ($cmdname, $pname, $cvalue) = @_;
1242 my $storage_list = complete_storage_enabled
();
1245 foreach my $storeid (@$storage_list) {
1246 my $vollist = PVE
::Storage
::volume_list
($cfg, $storeid);
1248 foreach my $item (@$vollist) {
1249 push @$res, $item->{volid
};