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+$/;
324 # NOTE: basename and basevmid are always undef for LVM-thin, where the
325 # clone -> base reference is not encoded in the volume ID.
326 # see note in PVE::Storage::LvmThinPlugin for details.
328 my ($cfg, $volid) = @_;
330 my ($storeid, $volname) = parse_volume_id($volid);
332 my $scfg = storage_config($cfg, $storeid);
334 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
336 # returns ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format)
338 return $plugin->parse_volname($volname);
341 sub parse_volume_id {
342 my ($volid, $noerr) = @_;
344 return PVE::Storage::Plugin::parse_volume_id($volid, $noerr);
347 # test if we have read access to volid
348 sub check_volume_access {
349 my ($rpcenv, $user, $cfg, $vmid, $volid) = @_;
351 my ($sid, $volname) = parse_volume_id($volid, 1);
353 my ($vtype, undef, $ownervm) = parse_volname($cfg, $volid);
354 if ($vtype eq 'iso' || $vtype eq 'vztmpl') {
355 # we simply allow access
356 } elsif (defined($ownervm) && defined($vmid) && ($ownervm == $vmid)) {
357 # we are owner - allow access
358 } elsif ($vtype eq 'backup' && $ownervm) {
359 $rpcenv->check($user, "/storage/$sid", ['Datastore.AllocateSpace']);
360 $rpcenv->check($user, "/vms/$ownervm", ['VM.Backup']);
362 # allow if we are Datastore administrator
363 $rpcenv->check($user, "/storage/$sid", ['Datastore.Allocate']);
366 die "Only root can pass arbitrary filesystem paths
."
367 if $user ne 'root@pam';
373 my $volume_is_base_and_used__no_lock = sub {
374 my ($scfg, $storeid, $plugin, $volname) = @_;
376 my ($vtype, $name, $vmid, undef, undef, $isBase, undef) =
377 $plugin->parse_volname($volname);
380 my $vollist = $plugin->list_images($storeid, $scfg);
381 foreach my $info (@$vollist) {
382 my (undef, $tmpvolname) = parse_volume_id($info->{volid});
383 my $basename = undef;
384 my $basevmid = undef;
387 (undef, undef, undef, $basename, $basevmid) =
388 $plugin->parse_volname($tmpvolname);
391 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
399 # NOTE: this check does not work for LVM-thin, where the clone -> base
400 # reference is not encoded in the volume ID.
401 # see note in PVE::Storage::LvmThinPlugin for details.
402 sub volume_is_base_and_used {
403 my ($cfg, $volid) = @_;
405 my ($storeid, $volname) = parse_volume_id($volid);
406 my $scfg = storage_config($cfg, $storeid);
407 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
409 $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
410 return &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
414 # try to map a filesystem path to a volume identifier
415 sub path_to_volume_id {
416 my ($cfg, $path) = @_;
418 my $ids = $cfg->{ids};
420 my ($sid, $volname) = parse_volume_id($path, 1);
422 if (my $scfg = $ids->{$sid}) {
424 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
425 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
426 return ($vtype, $path);
432 # Note: abs_path() return undef if $path doesn not exist
433 # for example when nfs storage is not mounted
434 $path = abs_path($path) || $path;
436 foreach my $sid (keys %$ids) {
437 my $scfg = $ids->{$sid};
438 next if !$scfg->{path};
439 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
440 my $imagedir = $plugin->get_subdir($scfg, 'images');
441 my $isodir = $plugin->get_subdir($scfg, 'iso');
442 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
443 my $backupdir = $plugin->get_subdir($scfg, 'backup');
444 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
446 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
450 my $vollist = $plugin->list_images($sid, $scfg, $vmid);
451 foreach my $info (@$vollist) {
452 my ($storeid, $volname) = parse_volume_id($info->{volid});
453 my $volpath = $plugin->path($scfg, $volname, $storeid);
454 if ($volpath eq $path) {
455 return ('images', $info->{volid});
458 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
460 return ('iso', "$sid:iso
/$name");
461 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
463 return ('vztmpl', "$sid:vztmpl
/$name");
464 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
466 return ('rootdir', "$sid:rootdir
/$vmid");
467 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!) {
469 return ('iso', "$sid:backup
/$name");
473 # can't map path to volume id
478 my ($cfg, $volid, $snapname) = @_;
480 my ($storeid, $volname) = parse_volume_id($volid);
482 my $scfg = storage_config($cfg, $storeid);
484 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
485 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname);
486 return wantarray ? ($path, $owner, $vtype) : $path;
489 sub abs_filesystem_path {
490 my ($cfg, $volid) = @_;
493 if (PVE::Storage::parse_volume_id ($volid, 1)) {
494 PVE::Storage::activate_volumes($cfg, [ $volid ]);
495 $path = PVE::Storage::path($cfg, $volid);
498 my $abspath = abs_path($volid);
499 if ($abspath && $abspath =~ m|^(/.+)$|) {
500 $path = $1; # untaint any path
505 die "can
't find file '$volid'\n" if !($path && -f $path);
510 sub storage_migrate {
511 my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
513 my ($storeid, $volname) = parse_volume_id($volid);
514 $target_volname = $volname if !$target_volname;
516 my $scfg = storage_config($cfg, $storeid);
518 # no need to migrate shared content
519 return if $storeid eq $target_storeid && $scfg->{shared};
521 my $tcfg = storage_config($cfg, $target_storeid);
523 my $target_volid = "${target_storeid}:${target_volname}";
525 my $errstr = "unable to migrate '$volid' to '${target_volid
}' on host '$target_host'";
527 my $sshoptions = "-o 'BatchMode
=yes
'";
528 my $ssh = "/usr/bin/ssh $sshoptions";
530 local $ENV{RSYNC_RSH} = $ssh;
532 # only implemented for file system based storage
536 my $src_plugin = PVE::Storage::Plugin->lookup($scfg->{type});
537 my $dst_plugin = PVE::Storage::Plugin->lookup($tcfg->{type});
538 my $src = $src_plugin->path($scfg, $volname, $storeid);
539 my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid);
541 my $dirname = dirname($dst);
543 if ($tcfg->{shared}) { # we can do a local copy
545 run_command(['/bin/mkdir
', '-p
', $dirname]);
547 run_command(['/bin/cp', $src, $dst]);
550 run_command(['/usr/bin
/ssh
', "root\@${target_host}",
551 '/bin/mkdir
', '-p
', $dirname]);
553 # we use rsync with --sparse, so we can't
use --inplace
,
554 # so we remove file on the target if it already exists to
556 my ($size, $format) = PVE
::Storage
::Plugin
::file_size_info
($src);
557 if ($format && ($format eq 'raw') && $size) {
558 run_command
(['/usr/bin/ssh', "root\@${target_host}",
564 if ($format eq 'subvol') {
565 $cmd = ['/usr/bin/rsync', '--progress', '-X', '-A', '--numeric-ids',
566 '-aH', '--delete', '--no-whole-file', '--inplace',
567 '--one-file-system', "$src/", "[root\@${target_host}]:$dst"];
569 $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
570 $src, "[root\@${target_host}]:$dst"];
575 run_command
($cmd, outfunc
=> sub {
578 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
581 print "rsync status: $1\n";
591 die "$errstr - target type '$tcfg->{type}' not implemented\n";
594 } elsif ($scfg->{type
} eq 'zfspool') {
596 if ($tcfg->{type
} eq 'zfspool') {
598 die "$errstr - pool on target does not have the same name as on source!"
599 if $tcfg->{pool
} ne $scfg->{pool
};
601 my (undef, $volname) = parse_volname
($cfg, $volid);
603 my $zfspath = "$scfg->{pool}\/$volname";
605 my $snap = ['zfs', 'snapshot', "$zfspath\@__migration__"];
607 my $send = [['zfs', 'send', '-Rpv', "$zfspath\@__migration__"], ['ssh', "root\@$target_host",
608 'zfs', 'recv', $zfspath]];
610 my $destroy_target = ['ssh', "root\@$target_host", 'zfs', 'destroy', "$zfspath\@__migration__"];
616 warn "zfs send/receive failed, cleaning up snapshot(s)..\n" if $err;
617 eval { run_command
(['zfs', 'destroy', "$zfspath\@__migration__"]); };
618 warn "could not remove source snapshot: $@\n" if $@;
619 eval { run_command
($destroy_target); };
620 warn "could not remove target snapshot: $@\n" if $@;
624 die "$errstr - target type $tcfg->{type} is not valid\n";
627 } elsif ($scfg->{type
} eq 'lvmthin' || $scfg->{type
} eq 'lvm') {
629 if (($scfg->{type
} eq $tcfg->{type
}) &&
630 ($tcfg->{type
} eq 'lvmthin' || $tcfg->{type
} eq 'lvm')) {
632 my (undef, $volname, $vmid) = parse_volname
($cfg, $volid);
633 my $size = volume_size_info
($cfg, $volid, 5);
634 my $src = path
($cfg, $volid);
635 my $dst = path
($cfg, $target_volid);
637 run_command
(['/usr/bin/ssh', "root\@${target_host}",
638 'pvesm', 'alloc', $target_storeid, $vmid,
639 $target_volname, int($size/1024)]);
642 if ($tcfg->{type
} eq 'lvmthin') {
643 run_command
([["dd", "if=$src", "bs=4k"],["/usr/bin/ssh", "root\@${target_host}",
644 "dd", 'conv=sparse', "of=$dst", "bs=4k"]]);
646 run_command
([["dd", "if=$src", "bs=4k"],["/usr/bin/ssh", "root\@${target_host}",
647 "dd", "of=$dst", "bs=4k"]]);
651 run_command
(['/usr/bin/ssh', "root\@${target_host}",
652 'pvesm', 'free', $target_volid]);
656 die "$errstr - migrate from source type '$scfg->{type}' to '$tcfg->{type}' not implemented\n";
659 die "$errstr - source type '$scfg->{type}' not implemented\n";
664 my ($cfg, $volid, $vmid, $snap) = @_;
666 my ($storeid, $volname) = parse_volume_id
($volid);
668 my $scfg = storage_config
($cfg, $storeid);
670 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
672 activate_storage
($cfg, $storeid);
674 # lock shared storage
675 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
676 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
677 return "$storeid:$volname";
681 sub vdisk_create_base
{
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);
692 # lock shared storage
693 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
694 my $volname = $plugin->create_base($storeid, $scfg, $volname);
695 return "$storeid:$volname";
700 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
702 die "no storage ID specified\n" if !$storeid;
704 PVE
::JSONSchema
::parse_storage_id
($storeid);
706 my $scfg = storage_config
($cfg, $storeid);
708 die "no VMID specified\n" if !$vmid;
710 $vmid = parse_vmid
($vmid);
712 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
714 $fmt = $defformat if !$fmt;
716 activate_storage
($cfg, $storeid);
718 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
720 # lock shared storage
721 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
722 my $old_umask = umask(umask|0037);
723 my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) };
727 return "$storeid:$volname";
732 my ($cfg, $volid) = @_;
734 my ($storeid, $volname) = parse_volume_id
($volid);
735 my $scfg = storage_config
($cfg, $storeid);
736 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
738 activate_storage
($cfg, $storeid);
742 # lock shared storage
743 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
744 # LVM-thin allows deletion of still referenced base volumes!
745 die "base volume '$volname' is still in use by linked clones\n"
746 if &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
748 my (undef, undef, undef, undef, undef, $isBase, $format) =
749 $plugin->parse_volname($volname);
750 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format);
753 return if !$cleanup_worker;
755 my $rpcenv = PVE
::RPCEnvironment
::get
();
756 my $authuser = $rpcenv->get_user();
758 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
761 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
763 my ($cfg, $storeid, $tt) = @_;
765 die "unknown template type '$tt'\n"
766 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
768 my $ids = $cfg->{ids
};
770 storage_check_enabled
($cfg, $storeid) if ($storeid);
776 foreach my $sid (keys %$ids) {
777 next if $storeid && $storeid ne $sid;
779 my $scfg = $ids->{$sid};
780 my $type = $scfg->{type
};
782 next if !storage_check_enabled
($cfg, $sid, undef, 1);
784 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
785 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
786 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
788 activate_storage
($cfg, $sid);
791 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
793 my $path = $plugin->get_subdir($scfg, $tt);
795 foreach my $fn (<$path/*>) {
800 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
802 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
804 } elsif ($tt eq 'vztmpl') {
805 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
807 $info = { volid
=> "$sid:vztmpl/$1", format
=> "t$2" };
809 } elsif ($tt eq 'backup') {
810 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
812 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
815 $info->{size
} = -s
$fn;
817 push @{$res->{$sid}}, $info;
822 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
830 my ($cfg, $storeid, $vmid, $vollist) = @_;
832 my $ids = $cfg->{ids
};
834 storage_check_enabled
($cfg, $storeid) if ($storeid);
838 # prepare/activate/refresh all storages
840 my $storage_list = [];
842 foreach my $volid (@$vollist) {
843 my ($sid, undef) = parse_volume_id
($volid);
844 next if !defined($ids->{$sid});
845 next if !storage_check_enabled
($cfg, $sid, undef, 1);
846 push @$storage_list, $sid;
849 foreach my $sid (keys %$ids) {
850 next if $storeid && $storeid ne $sid;
851 next if !storage_check_enabled
($cfg, $sid, undef, 1);
852 push @$storage_list, $sid;
858 activate_storage_list
($cfg, $storage_list, $cache);
860 foreach my $sid (keys %$ids) {
861 next if $storeid && $storeid ne $sid;
862 next if !storage_check_enabled
($cfg, $sid, undef, 1);
864 my $scfg = $ids->{$sid};
865 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
866 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
867 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
874 my ($cfg, $storeid, $vmid, $content) = @_;
876 my @ctypes = qw(images vztmpl iso backup);
878 my $cts = $content ?
[ $content ] : [ @ctypes ];
880 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
883 foreach my $ct (@$cts) {
885 if ($ct eq 'images') {
886 $data = vdisk_list
($cfg, $storeid, $vmid);
887 } elsif ($ct eq 'iso' && !defined($vmid)) {
888 $data = template_list
($cfg, $storeid, 'iso');
889 } elsif ($ct eq 'vztmpl'&& !defined($vmid)) {
890 $data = template_list
($cfg, $storeid, 'vztmpl');
891 } elsif ($ct eq 'backup') {
892 $data = template_list
($cfg, $storeid, 'backup');
893 foreach my $item (@{$data->{$storeid}}) {
894 if (defined($vmid)) {
895 @{$data->{$storeid}} = grep { $_->{volid
} =~ m/\S+-$vmid-\S+/ } @{$data->{$storeid}};
900 next if !$data || !$data->{$storeid};
902 foreach my $item (@{$data->{$storeid}}) {
903 $item->{content
} = $ct;
913 my $filename = "/sys/kernel/uevent_seqnum";
916 if (my $fh = IO
::File-
>new($filename, "r")) {
918 if ($line =~ m/^(\d+)$/) {
926 sub activate_storage
{
927 my ($cfg, $storeid, $cache) = @_;
929 $cache = {} if !$cache;
931 my $scfg = storage_check_enabled
($cfg, $storeid);
933 return if $cache->{activated
}->{$storeid};
935 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
937 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
940 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
941 activate_storage
($cfg, $baseid, $cache);
944 if (!$plugin->check_connection($storeid, $scfg)) {
945 die "storage '$storeid' is not online\n";
948 $plugin->activate_storage($storeid, $scfg, $cache);
950 my $newseq = uevent_seqnum
();
952 # only call udevsettle if there are events
953 if ($newseq > $cache->{uevent_seqnum
}) {
955 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
956 $cache->{uevent_seqnum
} = $newseq;
959 $cache->{activated
}->{$storeid} = 1;
962 sub activate_storage_list
{
963 my ($cfg, $storeid_list, $cache) = @_;
965 $cache = {} if !$cache;
967 foreach my $storeid (@$storeid_list) {
968 activate_storage
($cfg, $storeid, $cache);
972 sub deactivate_storage
{
973 my ($cfg, $storeid) = @_;
975 my $scfg = storage_config
($cfg, $storeid);
976 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
979 $plugin->deactivate_storage($storeid, $scfg, $cache);
982 sub activate_volumes
{
983 my ($cfg, $vollist, $snapname) = @_;
985 return if !($vollist && scalar(@$vollist));
987 my $storagehash = {};
988 foreach my $volid (@$vollist) {
989 my ($storeid, undef) = parse_volume_id
($volid);
990 $storagehash->{$storeid} = 1;
995 activate_storage_list
($cfg, [keys %$storagehash], $cache);
997 foreach my $volid (@$vollist) {
998 my ($storeid, $volname) = parse_volume_id
($volid);
999 my $scfg = storage_config
($cfg, $storeid);
1000 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1001 $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache);
1005 sub deactivate_volumes
{
1006 my ($cfg, $vollist, $snapname) = @_;
1008 return if !($vollist && scalar(@$vollist));
1013 foreach my $volid (@$vollist) {
1014 my ($storeid, $volname) = parse_volume_id
($volid);
1016 my $scfg = storage_config
($cfg, $storeid);
1017 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1020 $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache);
1024 push @errlist, $volid;
1028 die "volume deactivation failed: " . join(' ', @errlist)
1029 if scalar(@errlist);
1033 my ($cfg, $content) = @_;
1035 my $ids = $cfg->{ids
};
1039 my @ctypes = PVE
::Tools
::split_list
($content);
1042 foreach my $storeid (keys %$ids) {
1044 next if !storage_check_enabled
($cfg, $storeid, undef, 1);
1046 if (defined($content)) {
1048 foreach my $ctype (@ctypes) {
1049 if ($ids->{$storeid}->{content
}->{$ctype}) {
1054 next if !$want_ctype;
1057 my $type = $ids->{$storeid}->{type
};
1059 $info->{$storeid} = {
1064 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
1065 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
1069 push @$slist, $storeid;
1074 foreach my $storeid (keys %$ids) {
1075 my $scfg = $ids->{$storeid};
1076 next if !$info->{$storeid};
1078 eval { activate_storage
($cfg, $storeid, $cache); };
1084 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1085 my ($total, $avail, $used, $active);
1086 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
1089 $info->{$storeid}->{total
} = int($total);
1090 $info->{$storeid}->{avail
} = int($avail);
1091 $info->{$storeid}->{used
} = int($used);
1092 $info->{$storeid}->{active
} = $active;
1101 my ($packed_ip, $family);
1103 my @res = PVE
::Tools
::getaddrinfo_all
($server);
1104 $family = $res[0]->{family
};
1105 $packed_ip = (PVE
::Tools
::unpack_sockaddr_in46
($res[0]->{addr
}))[2];
1107 if (defined $packed_ip) {
1108 return Socket
::inet_ntop
($family, $packed_ip);
1114 my ($server_in) = @_;
1117 if (!($server = resolv_server
($server_in))) {
1118 die "unable to resolve address for server '${server_in}'\n";
1121 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
1124 run_command
($cmd, outfunc
=> sub {
1127 # note: howto handle white spaces in export path??
1128 if ($line =~ m!^(/\S+)\s+(.+)$!) {
1138 my $cmd = ['zfs', 'list', '-t', 'filesystem', '-H', '-o', 'name,avail,used'];
1141 run_command
($cmd, outfunc
=> sub {
1144 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
1145 my ($pool, $size_str, $used_str) = ($1, $2, $3);
1146 my $size = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($size_str);
1147 my $used = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($used_str);
1148 # ignore subvolumes generated by our ZFSPoolPlugin
1149 return if $pool =~ m!/subvol-\d+-[^/]+$!;
1150 return if $pool =~ m!/basevol-\d+-[^/]+$!;
1151 push @$res, { pool
=> $pool, size
=> $size, free
=> $size-$used };
1159 my ($portal, $noerr) = @_;
1161 my ($server, $port) = PVE
::Tools
::parse_host_and_port
($portal);
1163 if (my $ip = resolv_server
($server)) {
1165 $server = "[$server]" if $server =~ /^$IPV6RE$/;
1166 return $port ?
"$server:$port" : $server;
1169 return undef if $noerr;
1171 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
1174 # idea is from usbutils package (/usr/bin/usb-devices) script
1175 sub __scan_usb_device
{
1176 my ($res, $devpath, $parent, $level) = @_;
1178 return if ! -d
$devpath;
1179 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
1180 my $port = $level ?
int($1 - 1) : 0;
1182 my $busnum = int(file_read_firstline
("$devpath/busnum"));
1183 my $devnum = int(file_read_firstline
("$devpath/devnum"));
1190 speed
=> file_read_firstline
("$devpath/speed"),
1191 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
1192 vendid
=> file_read_firstline
("$devpath/idVendor"),
1193 prodid
=> file_read_firstline
("$devpath/idProduct"),
1197 my $usbpath = $devpath;
1198 $usbpath =~ s
|^.*/\d
+\
-||;
1199 $d->{usbpath
} = $usbpath;
1202 my $product = file_read_firstline
("$devpath/product");
1203 $d->{product
} = $product if $product;
1205 my $manu = file_read_firstline
("$devpath/manufacturer");
1206 $d->{manufacturer
} = $manu if $manu;
1208 my $serial => file_read_firstline
("$devpath/serial");
1209 $d->{serial
} = $serial if $serial;
1213 foreach my $subdev (<$devpath/$busnum-*>) {
1214 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
1215 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
1224 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
1225 __scan_usb_device
($devlist, $device, 0, 0);
1232 my ($portal_in) = @_;
1235 if (!($portal = resolv_portal
($portal_in))) {
1236 die "unable to parse/resolve portal address '${portal_in}'\n";
1239 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
1242 sub storage_default_format
{
1243 my ($cfg, $storeid) = @_;
1245 my $scfg = storage_config
($cfg, $storeid);
1247 return PVE
::Storage
::Plugin
::default_format
($scfg);
1250 sub vgroup_is_used
{
1251 my ($cfg, $vgname) = @_;
1253 foreach my $storeid (keys %{$cfg->{ids
}}) {
1254 my $scfg = storage_config
($cfg, $storeid);
1255 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
1263 sub target_is_used
{
1264 my ($cfg, $target) = @_;
1266 foreach my $storeid (keys %{$cfg->{ids
}}) {
1267 my $scfg = storage_config
($cfg, $storeid);
1268 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
1276 sub volume_is_used
{
1277 my ($cfg, $volid) = @_;
1279 foreach my $storeid (keys %{$cfg->{ids
}}) {
1280 my $scfg = storage_config
($cfg, $storeid);
1281 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
1289 sub storage_is_used
{
1290 my ($cfg, $storeid) = @_;
1292 foreach my $sid (keys %{$cfg->{ids
}}) {
1293 my $scfg = storage_config
($cfg, $sid);
1294 next if !$scfg->{base
};
1295 my ($st) = parse_volume_id
($scfg->{base
});
1296 return 1 if $st && $st eq $storeid;
1303 my ($list, $func) = @_;
1307 foreach my $sid (keys %$list) {
1308 foreach my $info (@{$list->{$sid}}) {
1309 my $volid = $info->{volid
};
1310 my ($sid1, $volname) = parse_volume_id
($volid, 1);
1311 if ($sid1 && $sid1 eq $sid) {
1312 &$func ($volid, $sid, $info);
1314 warn "detected strange volid '$volid' in volume list for '$sid'\n";
1320 sub extract_vzdump_config_tar
{
1321 my ($archive, $conf_re) = @_;
1323 die "ERROR: file '$archive' does not exist\n" if ! -f
$archive;
1325 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
1326 die "unable to open file '$archive'\n";
1329 while (defined($file = <$fh>)) {
1330 if ($file =~ $conf_re) {
1331 $file = $1; # untaint
1340 die "ERROR: archive contains no configuration file\n" if !$file;
1346 $raw .= "$output\n";
1349 PVE
::Tools
::run_command
(['tar', '-xpOf', $archive, $file, '--occurrence'], outfunc
=> $out);
1351 return wantarray ?
($raw, $file) : $raw;
1354 sub extract_vzdump_config_vma
{
1355 my ($archive, $comp) = @_;
1361 $raw .= "$output\n";
1367 if ($comp eq 'gz') {
1368 $uncomp = ["zcat", $archive];
1369 } elsif ($comp eq 'lzo') {
1370 $uncomp = ["lzop", "-d", "-c", $archive];
1372 die "unknown compression method '$comp'\n";
1374 $cmd = [$uncomp, ["vma", "config", "-"]];
1376 # in some cases, lzop/zcat exits with 1 when its stdout pipe is
1377 # closed early by vma, detect this and ignore the exit code later
1382 if ($output =~ m/lzop: Broken pipe: <stdout>/ || $output =~ m/gzip: stdout: Broken pipe/) {
1384 } elsif (!defined ($errstring) && $output !~ m/^\s*$/) {
1385 $errstring = "Failed to extract config from VMA archive: $output\n";
1389 # in other cases, the pipeline will exit with exit code 141
1390 # because of the broken pipe, handle / ignore this as well
1393 $rc = PVE
::Tools
::run_command
($cmd, outfunc
=> $out, errfunc
=> $err, noerr
=> 1);
1397 # use exit code if no stderr output and not just broken pipe
1398 if (!$errstring && !$broken_pipe && $rc != 0 && $rc != 141) {
1399 die "$rerr\n" if $rerr;
1400 die "config extraction failed with exit code $rc\n";
1402 die "$errstring\n" if $errstring;
1404 # simple case without compression and weird piping behaviour
1405 PVE
::Tools
::run_command
(["vma", "config", $archive], outfunc
=> $out);
1408 return wantarray ?
($raw, undef) : $raw;
1411 sub extract_vzdump_config
{
1412 my ($cfg, $volid) = @_;
1414 my $archive = abs_filesystem_path
($cfg, $volid);
1416 if ($volid =~ /vzdump-(lxc|openvz)-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|(tar(\.(gz|lzo))?))$/) {
1417 return extract_vzdump_config_tar
($archive, qr!^(\./etc/vzdump/(pct|vps)\.conf)$!);
1418 } elsif ($volid =~ /vzdump-qemu-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|((tar|vma)(\.(gz|lzo))?))$/) {
1426 $comp = $11 if defined($11);
1429 if ($format eq 'tar') {
1430 return extract_vzdump_config_tar
($archive, qr!\(\./qemu-server\.conf\)!);
1432 return extract_vzdump_config_vma
($archive, $comp);
1435 die "cannot determine backup guest type for backup archive '$volid'\n";
1439 # bash completion helper
1441 sub complete_storage
{
1442 my ($cmdname, $pname, $cvalue) = @_;
1444 my $cfg = PVE
::Storage
::config
();
1446 return $cmdname eq 'add' ?
[] : [ PVE
::Storage
::storage_ids
($cfg) ];
1449 sub complete_storage_enabled
{
1450 my ($cmdname, $pname, $cvalue) = @_;
1454 my $cfg = PVE
::Storage
::config
();
1455 foreach my $sid (keys %{$cfg->{ids
}}) {
1456 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1462 sub complete_content_type
{
1463 my ($cmdname, $pname, $cvalue) = @_;
1465 return [qw(rootdir images vztmpl iso backup)];
1468 sub complete_volume
{
1469 my ($cmdname, $pname, $cvalue) = @_;
1473 my $storage_list = complete_storage_enabled
();
1475 if ($cvalue =~ m/^([^:]+):/) {
1476 $storage_list = [ $1 ];
1478 if (scalar(@$storage_list) > 1) {
1479 # only list storage IDs to avoid large listings
1481 foreach my $storeid (@$storage_list) {
1482 # Hack: simply return 2 artificial values, so that
1483 # completions does not finish
1484 push @$res, "$storeid:volname", "$storeid:...";
1491 foreach my $storeid (@$storage_list) {
1492 my $vollist = PVE
::Storage
::volume_list
($cfg, $storeid);
1494 foreach my $item (@$vollist) {
1495 push @$res, $item->{volid
};