16 use PVE
::Tools
qw(run_command file_read_firstline dir_glob_foreach $IPV6RE);
17 use PVE
::Cluster
qw(cfs_read_file cfs_write_file cfs_lock_file);
18 use PVE
::Exception
qw(raise_param_exc);
21 use PVE
::RPCEnvironment
;
23 use PVE
::Storage
::Plugin
;
24 use PVE
::Storage
::DirPlugin
;
25 use PVE
::Storage
::LVMPlugin
;
26 use PVE
::Storage
::LvmThinPlugin
;
27 use PVE
::Storage
::NFSPlugin
;
28 use PVE
::Storage
::ISCSIPlugin
;
29 use PVE
::Storage
::RBDPlugin
;
30 use PVE
::Storage
::SheepdogPlugin
;
31 use PVE
::Storage
::ISCSIDirectPlugin
;
32 use PVE
::Storage
::GlusterfsPlugin
;
33 use PVE
::Storage
::ZFSPoolPlugin
;
34 use PVE
::Storage
::ZFSPlugin
;
35 use PVE
::Storage
::DRBDPlugin
;
37 # Storage API version. Icrement it on changes in storage API interface.
38 use constant APIVER
=> 1;
40 # load standard plugins
41 PVE
::Storage
::DirPlugin-
>register();
42 PVE
::Storage
::LVMPlugin-
>register();
43 PVE
::Storage
::LvmThinPlugin-
>register();
44 PVE
::Storage
::NFSPlugin-
>register();
45 PVE
::Storage
::ISCSIPlugin-
>register();
46 PVE
::Storage
::RBDPlugin-
>register();
47 PVE
::Storage
::SheepdogPlugin-
>register();
48 PVE
::Storage
::ISCSIDirectPlugin-
>register();
49 PVE
::Storage
::GlusterfsPlugin-
>register();
50 PVE
::Storage
::ZFSPoolPlugin-
>register();
51 PVE
::Storage
::ZFSPlugin-
>register();
52 PVE
::Storage
::DRBDPlugin-
>register();
54 # load third-party plugins
55 if ( -d
'/usr/share/perl5/PVE/Storage/Custom' ) {
56 dir_glob_foreach
('/usr/share/perl5/PVE/Storage/Custom', '.*\.pm$', sub {
58 my $modname = 'PVE::Storage::Custom::' . $file;
59 $modname =~ s!\.pm$!!;
60 $file = 'PVE/Storage/Custom/' . $file;
67 # Check storage API version and that file is really storage plugin.
68 } elsif ($modname->isa('PVE::Storage::Plugin') && $modname->can('api') && $modname->api() == APIVER
) {
75 warn "Error loading storage plugin \"$modname\" because of API version mismatch
. Please
, update it
.\n"
80 # initialize all plugins
81 PVE::Storage::Plugin->init();
83 my $UDEVADM = '/sbin/udevadm';
85 # PVE::Storage utility functions
88 return cfs_read_file("storage
.cfg
");
94 cfs_write_file('storage.cfg', $cfg);
97 sub lock_storage_config {
98 my ($code, $errmsg) = @_;
100 cfs_lock_file("storage
.cfg
", undef, $code);
103 $errmsg ? die "$errmsg: $err" : die $err;
108 my ($cfg, $storeid, $noerr) = @_;
110 die "no storage ID specified
\n" if !$storeid;
112 my $scfg = $cfg->{ids}->{$storeid};
114 die "storage
'$storeid' does not exists\n" if (!$noerr && !$scfg);
119 sub storage_check_node {
120 my ($cfg, $storeid, $node, $noerr) = @_;
122 my $scfg = storage_config($cfg, $storeid);
124 if ($scfg->{nodes}) {
125 $node = PVE::INotify::nodename() if !$node || ($node eq 'localhost');
126 if (!$scfg->{nodes}->{$node}) {
127 die "storage
'$storeid' is not available on node
'$node'\n" if !$noerr;
135 sub storage_check_enabled {
136 my ($cfg, $storeid, $node, $noerr) = @_;
138 my $scfg = storage_config($cfg, $storeid);
140 if ($scfg->{disable}) {
141 die "storage
'$storeid' is disabled
\n" if !$noerr;
145 return storage_check_node($cfg, $storeid, $node, $noerr);
148 # storage_can_replicate:
149 # return true if storage supports replication
150 # (volumes alocated with vdisk_alloc() has replication feature)
151 sub storage_can_replicate {
152 my ($cfg, $storeid, $format) = @_;
154 my $scfg = storage_config($cfg, $storeid);
155 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
156 return $plugin->storage_can_replicate($scfg, $storeid, $format);
162 return keys %{$cfg->{ids}};
166 my ($filename, $timeout) = @_;
168 return PVE::Storage::Plugin::file_size_info($filename, $timeout);
171 sub volume_size_info {
172 my ($cfg, $volid, $timeout) = @_;
174 my ($storeid, $volname) = parse_volume_id($volid, 1);
176 my $scfg = storage_config($cfg, $storeid);
177 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
178 return $plugin->volume_size_info($scfg, $storeid, $volname, $timeout);
179 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
180 return file_size_info($volid, $timeout);
187 my ($cfg, $volid, $size, $running) = @_;
189 my ($storeid, $volname) = parse_volume_id($volid, 1);
191 my $scfg = storage_config($cfg, $storeid);
192 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
193 return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running);
194 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
195 die "resize file
/device
'$volid' is not possible
\n";
197 die "unable to parse volume ID
'$volid'\n";
201 sub volume_rollback_is_possible {
202 my ($cfg, $volid, $snap) = @_;
204 my ($storeid, $volname) = parse_volume_id($volid, 1);
206 my $scfg = storage_config($cfg, $storeid);
207 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
208 return $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
209 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
210 die "snapshot rollback file
/device
'$volid' is not possible
\n";
212 die "unable to parse volume ID
'$volid'\n";
216 sub volume_snapshot {
217 my ($cfg, $volid, $snap) = @_;
219 my ($storeid, $volname) = parse_volume_id($volid, 1);
221 my $scfg = storage_config($cfg, $storeid);
222 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
223 return $plugin->volume_snapshot($scfg, $storeid, $volname, $snap);
224 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
225 die "snapshot file
/device
'$volid' is not possible
\n";
227 die "unable to parse volume ID
'$volid'\n";
231 sub volume_snapshot_rollback {
232 my ($cfg, $volid, $snap) = @_;
234 my ($storeid, $volname) = parse_volume_id($volid, 1);
236 my $scfg = storage_config($cfg, $storeid);
237 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
238 $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
239 return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap);
240 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
241 die "snapshot rollback file
/device
'$volid' is not possible
\n";
243 die "unable to parse volume ID
'$volid'\n";
247 sub volume_snapshot_delete {
248 my ($cfg, $volid, $snap, $running) = @_;
250 my ($storeid, $volname) = parse_volume_id($volid, 1);
252 my $scfg = storage_config($cfg, $storeid);
253 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
254 return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running);
255 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
256 die "snapshot
delete file
/device
'$volid' is not possible
\n";
258 die "unable to parse volume ID
'$volid'\n";
262 sub volume_has_feature {
263 my ($cfg, $feature, $volid, $snap, $running) = @_;
265 my ($storeid, $volname) = parse_volume_id($volid, 1);
267 my $scfg = storage_config($cfg, $storeid);
268 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
269 return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running);
270 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
277 sub volume_snapshot_list {
278 my ($cfg, $volid) = @_;
280 my ($storeid, $volname) = parse_volume_id($volid, 1);
282 my $scfg = storage_config($cfg, $storeid);
283 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
284 return $plugin->volume_snapshot_list($scfg, $storeid, $volname);
285 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
286 die "send file
/device
'$volid' is not possible
\n";
288 die "unable to parse volume ID
'$volid'\n";
290 # return an empty array if dataset does not exist.
294 my ($cfg, $storeid, $vmid) = @_;
296 my $scfg = storage_config($cfg, $storeid);
297 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
299 my $path = $plugin->get_subdir($scfg, 'images');
301 return $vmid ? "$path/$vmid" : $path;
304 sub get_private_dir {
305 my ($cfg, $storeid, $vmid) = @_;
307 my $scfg = storage_config($cfg, $storeid);
308 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
310 my $path = $plugin->get_subdir($scfg, 'rootdir');
312 return $vmid ? "$path/$vmid" : $path;
316 my ($cfg, $storeid) = @_;
318 my $scfg = storage_config($cfg, $storeid);
319 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
321 return $plugin->get_subdir($scfg, 'iso');
325 my ($cfg, $storeid) = @_;
327 my $scfg = storage_config($cfg, $storeid);
328 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
330 return $plugin->get_subdir($scfg, 'vztmpl');
334 my ($cfg, $storeid) = @_;
336 my $scfg = storage_config($cfg, $storeid);
337 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
339 return $plugin->get_subdir($scfg, 'backup');
342 # library implementation
347 die "VMID
'$vmid' contains illegal characters
\n" if $vmid !~ m/^\d+$/;
352 # NOTE: basename and basevmid are always undef for LVM-thin, where the
353 # clone -> base reference is not encoded in the volume ID.
354 # see note in PVE::Storage::LvmThinPlugin for details.
356 my ($cfg, $volid) = @_;
358 my ($storeid, $volname) = parse_volume_id($volid);
360 my $scfg = storage_config($cfg, $storeid);
362 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
364 # returns ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format)
366 return $plugin->parse_volname($volname);
369 sub parse_volume_id {
370 my ($volid, $noerr) = @_;
372 return PVE::Storage::Plugin::parse_volume_id($volid, $noerr);
375 # test if we have read access to volid
376 sub check_volume_access {
377 my ($rpcenv, $user, $cfg, $vmid, $volid) = @_;
379 my ($sid, $volname) = parse_volume_id($volid, 1);
381 my ($vtype, undef, $ownervm) = parse_volname($cfg, $volid);
382 if ($vtype eq 'iso' || $vtype eq 'vztmpl') {
383 # we simply allow access
384 } elsif (defined($ownervm) && defined($vmid) && ($ownervm == $vmid)) {
385 # we are owner - allow access
386 } elsif ($vtype eq 'backup' && $ownervm) {
387 $rpcenv->check($user, "/storage/$sid", ['Datastore.AllocateSpace']);
388 $rpcenv->check($user, "/vms/$ownervm", ['VM.Backup']);
390 # allow if we are Datastore administrator
391 $rpcenv->check($user, "/storage/$sid", ['Datastore.Allocate']);
394 die "Only root can pass arbitrary filesystem paths
."
395 if $user ne 'root@pam';
401 my $volume_is_base_and_used__no_lock = sub {
402 my ($scfg, $storeid, $plugin, $volname) = @_;
404 my ($vtype, $name, $vmid, undef, undef, $isBase, undef) =
405 $plugin->parse_volname($volname);
408 my $vollist = $plugin->list_images($storeid, $scfg);
409 foreach my $info (@$vollist) {
410 my (undef, $tmpvolname) = parse_volume_id($info->{volid});
411 my $basename = undef;
412 my $basevmid = undef;
415 (undef, undef, undef, $basename, $basevmid) =
416 $plugin->parse_volname($tmpvolname);
419 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
427 # NOTE: this check does not work for LVM-thin, where the clone -> base
428 # reference is not encoded in the volume ID.
429 # see note in PVE::Storage::LvmThinPlugin for details.
430 sub volume_is_base_and_used {
431 my ($cfg, $volid) = @_;
433 my ($storeid, $volname) = parse_volume_id($volid);
434 my $scfg = storage_config($cfg, $storeid);
435 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
437 $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
438 return &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
442 # try to map a filesystem path to a volume identifier
443 sub path_to_volume_id {
444 my ($cfg, $path) = @_;
446 my $ids = $cfg->{ids};
448 my ($sid, $volname) = parse_volume_id($path, 1);
450 if (my $scfg = $ids->{$sid}) {
452 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
453 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
454 return ($vtype, $path);
460 # Note: abs_path() return undef if $path doesn not exist
461 # for example when nfs storage is not mounted
462 $path = abs_path($path) || $path;
464 foreach my $sid (keys %$ids) {
465 my $scfg = $ids->{$sid};
466 next if !$scfg->{path};
467 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
468 my $imagedir = $plugin->get_subdir($scfg, 'images');
469 my $isodir = $plugin->get_subdir($scfg, 'iso');
470 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
471 my $backupdir = $plugin->get_subdir($scfg, 'backup');
472 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
474 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
478 my $vollist = $plugin->list_images($sid, $scfg, $vmid);
479 foreach my $info (@$vollist) {
480 my ($storeid, $volname) = parse_volume_id($info->{volid});
481 my $volpath = $plugin->path($scfg, $volname, $storeid);
482 if ($volpath eq $path) {
483 return ('images', $info->{volid});
486 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
488 return ('iso', "$sid:iso
/$name");
489 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
491 return ('vztmpl', "$sid:vztmpl
/$name");
492 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
494 return ('rootdir', "$sid:rootdir
/$vmid");
495 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!) {
497 return ('iso', "$sid:backup
/$name");
501 # can't map path to volume id
506 my ($cfg, $volid, $snapname) = @_;
508 my ($storeid, $volname) = parse_volume_id($volid);
510 my $scfg = storage_config($cfg, $storeid);
512 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
513 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname);
514 return wantarray ? ($path, $owner, $vtype) : $path;
517 sub abs_filesystem_path {
518 my ($cfg, $volid) = @_;
521 if (PVE::Storage::parse_volume_id ($volid, 1)) {
522 PVE::Storage::activate_volumes($cfg, [ $volid ]);
523 $path = PVE::Storage::path($cfg, $volid);
526 my $abspath = abs_path($volid);
527 if ($abspath && $abspath =~ m|^(/.+)$|) {
528 $path = $1; # untaint any path
533 die "can
't find file '$volid'\n" if !($path && -f $path);
538 sub storage_migrate {
539 my ($cfg, $volid, $target_sshinfo, $target_storeid, $target_volname, $base_snapshot, $snapshot, $ratelimit_bps, $insecure, $with_snapshots) = @_;
541 my ($storeid, $volname) = parse_volume_id($volid);
542 $target_volname = $volname if !$target_volname;
544 my $scfg = storage_config($cfg, $storeid);
546 # no need to migrate shared content
547 return if $storeid eq $target_storeid && $scfg->{shared};
549 my $tcfg = storage_config($cfg, $target_storeid);
551 my $target_volid = "${target_storeid}:${target_volname}";
553 my $target_ip = $target_sshinfo->{ip};
554 my $errstr = "unable to migrate '$volid' to '${target_volid
}' on host '$target_sshinfo->{name
}'";
556 my $ssh = PVE::Cluster::ssh_info_to_command($target_sshinfo);
557 my $ssh_base = PVE::Cluster::ssh_info_to_command_base($target_sshinfo);
558 local $ENV{RSYNC_RSH} = PVE::Tools::cmd2string($ssh_base);
560 my @cstream = ([ '/usr/bin
/cstream
', '-t
', $ratelimit_bps ])
561 if defined($ratelimit_bps);
563 my $migration_snapshot;
564 if (!defined($snapshot)) {
565 if ($scfg->{type} eq 'zfspool
') {
566 $migration_snapshot = 1;
567 $snapshot = '__migration__
';
571 my @formats = volume_transfer_formats($cfg, $volid, $volid, $snapshot, $base_snapshot, $with_snapshots);
572 die "cannot migrate from storage type '$scfg->{type
}' to '$tcfg->{type
}'\n" if !@formats;
573 my $format = $formats[0];
577 @insecurecmd = ('pvecm
', 'mtunnel
', '-run-command
', 1);
578 if (my $network = $target_sshinfo->{network}) {
579 push @insecurecmd, '-migration_network
', $network;
583 $with_snapshots = $with_snapshots ? 1 : 0; # sanitize for passing as cli parameter
584 my $send = ['pvesm
', 'export
', $volid, $format, '-', '-with-snapshots
', $with_snapshots];
585 my $recv = [@$ssh, @insecurecmd, '--', 'pvesm
', 'import
', $volid, $format, '-', '-with-snapshots
', $with_snapshots];
586 if (defined($snapshot)) {
587 push @$send, '-snapshot
', $snapshot
589 if ($migration_snapshot) {
590 push @$recv, '-delete-snapshot
', $snapshot;
593 if (defined($base_snapshot)) {
594 # Check if the snapshot exists on the remote side:
595 push @$send, '-base
', $base_snapshot;
596 push @$recv, '-base
', $base_snapshot;
599 volume_snapshot($cfg, $volid, $snapshot) if $migration_snapshot;
602 open(my $info, '-|', @$recv)
603 or die "receive command failed: $!\n";
604 my ($ip) = <$info> =~ /^($PVE::Tools::IPRE)$/ or die "no tunnel IP received\n";
605 my ($port) = <$info> =~ /^(\d+)$/ or die "no tunnel port received\n";
606 my $socket = IO::Socket::IP->new(PeerHost => $ip, PeerPort => $port, Type => SOCK_STREAM)
607 or die "failed to connect to tunnel at $ip:$port\n";
608 # we won't be reading from the
socket
609 shutdown($socket, 0);
610 run_command
([$send, @cstream], output
=> '>&'.fileno($socket));
611 # don't close the connection entirely otherwise the receiving end
612 # might not get all buffered data (and fails with 'connection reset by peer')
613 shutdown($socket, 1);
614 1 while <$info>; # wait for the remote process to finish
615 # now close the socket
617 if (!close($info)) { # does waitpid()
618 die "import failed: $!\n" if $!;
619 die "import failed: exit code ".($?>>8)."\n";
622 run_command
([$send, @cstream, $recv]);
626 warn "send/receive failed, cleaning up snapshot(s)..\n" if $err;
627 if ($migration_snapshot) {
628 eval { volume_snapshot_delete
($cfg, $volid, $snapshot, 0) };
629 warn "could not remove source snapshot: $@\n" if $@;
635 my ($cfg, $volid, $vmid, $snap) = @_;
637 my ($storeid, $volname) = parse_volume_id
($volid);
639 my $scfg = storage_config
($cfg, $storeid);
641 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
643 activate_storage
($cfg, $storeid);
645 # lock shared storage
646 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
647 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
648 return "$storeid:$volname";
652 sub vdisk_create_base
{
653 my ($cfg, $volid) = @_;
655 my ($storeid, $volname) = parse_volume_id
($volid);
657 my $scfg = storage_config
($cfg, $storeid);
659 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
661 activate_storage
($cfg, $storeid);
663 # lock shared storage
664 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
665 my $volname = $plugin->create_base($storeid, $scfg, $volname);
666 return "$storeid:$volname";
671 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
673 die "no storage ID specified\n" if !$storeid;
675 PVE
::JSONSchema
::parse_storage_id
($storeid);
677 my $scfg = storage_config
($cfg, $storeid);
679 die "no VMID specified\n" if !$vmid;
681 $vmid = parse_vmid
($vmid);
683 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
685 $fmt = $defformat if !$fmt;
687 activate_storage
($cfg, $storeid);
689 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
691 # lock shared storage
692 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
693 my $old_umask = umask(umask|0037);
694 my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) };
698 return "$storeid:$volname";
703 my ($cfg, $volid) = @_;
705 my ($storeid, $volname) = parse_volume_id
($volid);
706 my $scfg = storage_config
($cfg, $storeid);
707 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
709 activate_storage
($cfg, $storeid);
713 # lock shared storage
714 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
715 # LVM-thin allows deletion of still referenced base volumes!
716 die "base volume '$volname' is still in use by linked clones\n"
717 if &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
719 my (undef, undef, undef, undef, undef, $isBase, $format) =
720 $plugin->parse_volname($volname);
721 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format);
724 return if !$cleanup_worker;
726 my $rpcenv = PVE
::RPCEnvironment
::get
();
727 my $authuser = $rpcenv->get_user();
729 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
732 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
734 my ($cfg, $storeid, $tt) = @_;
736 die "unknown template type '$tt'\n"
737 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
739 my $ids = $cfg->{ids
};
741 storage_check_enabled
($cfg, $storeid) if ($storeid);
747 foreach my $sid (keys %$ids) {
748 next if $storeid && $storeid ne $sid;
750 my $scfg = $ids->{$sid};
751 my $type = $scfg->{type
};
753 next if !storage_check_enabled
($cfg, $sid, undef, 1);
755 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
756 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
757 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
759 activate_storage
($cfg, $sid);
762 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
764 my $path = $plugin->get_subdir($scfg, $tt);
766 foreach my $fn (<$path/*>) {
771 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
773 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
775 } elsif ($tt eq 'vztmpl') {
776 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
778 $info = { volid
=> "$sid:vztmpl/$1", format
=> "t$2" };
780 } elsif ($tt eq 'backup') {
781 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
783 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
786 $info->{size
} = -s
$fn;
788 push @{$res->{$sid}}, $info;
793 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
801 my ($cfg, $storeid, $vmid, $vollist) = @_;
803 my $ids = $cfg->{ids
};
805 storage_check_enabled
($cfg, $storeid) if ($storeid);
809 # prepare/activate/refresh all storages
811 my $storage_list = [];
813 foreach my $volid (@$vollist) {
814 my ($sid, undef) = parse_volume_id
($volid);
815 next if !defined($ids->{$sid});
816 next if !storage_check_enabled
($cfg, $sid, undef, 1);
817 push @$storage_list, $sid;
820 foreach my $sid (keys %$ids) {
821 next if $storeid && $storeid ne $sid;
822 next if !storage_check_enabled
($cfg, $sid, undef, 1);
823 push @$storage_list, $sid;
829 activate_storage_list
($cfg, $storage_list, $cache);
831 foreach my $sid (keys %$ids) {
832 next if $storeid && $storeid ne $sid;
833 next if !storage_check_enabled
($cfg, $sid, undef, 1);
835 my $scfg = $ids->{$sid};
836 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
837 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
838 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
845 my ($cfg, $storeid, $vmid, $content) = @_;
847 my @ctypes = qw(images vztmpl iso backup);
849 my $cts = $content ?
[ $content ] : [ @ctypes ];
851 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
854 foreach my $ct (@$cts) {
856 if ($ct eq 'images') {
857 $data = vdisk_list
($cfg, $storeid, $vmid);
858 } elsif ($ct eq 'iso' && !defined($vmid)) {
859 $data = template_list
($cfg, $storeid, 'iso');
860 } elsif ($ct eq 'vztmpl'&& !defined($vmid)) {
861 $data = template_list
($cfg, $storeid, 'vztmpl');
862 } elsif ($ct eq 'backup') {
863 $data = template_list
($cfg, $storeid, 'backup');
864 foreach my $item (@{$data->{$storeid}}) {
865 if (defined($vmid)) {
866 @{$data->{$storeid}} = grep { $_->{volid
} =~ m/\S+-$vmid-\S+/ } @{$data->{$storeid}};
871 next if !$data || !$data->{$storeid};
873 foreach my $item (@{$data->{$storeid}}) {
874 $item->{content
} = $ct;
884 my $filename = "/sys/kernel/uevent_seqnum";
887 if (my $fh = IO
::File-
>new($filename, "r")) {
889 if ($line =~ m/^(\d+)$/) {
897 sub activate_storage
{
898 my ($cfg, $storeid, $cache) = @_;
900 $cache = {} if !$cache;
902 my $scfg = storage_check_enabled
($cfg, $storeid);
904 return if $cache->{activated
}->{$storeid};
906 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
908 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
911 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
912 activate_storage
($cfg, $baseid, $cache);
915 if (!$plugin->check_connection($storeid, $scfg)) {
916 die "storage '$storeid' is not online\n";
919 $plugin->activate_storage($storeid, $scfg, $cache);
921 my $newseq = uevent_seqnum
();
923 # only call udevsettle if there are events
924 if ($newseq > $cache->{uevent_seqnum
}) {
926 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
927 $cache->{uevent_seqnum
} = $newseq;
930 $cache->{activated
}->{$storeid} = 1;
933 sub activate_storage_list
{
934 my ($cfg, $storeid_list, $cache) = @_;
936 $cache = {} if !$cache;
938 foreach my $storeid (@$storeid_list) {
939 activate_storage
($cfg, $storeid, $cache);
943 sub deactivate_storage
{
944 my ($cfg, $storeid) = @_;
946 my $scfg = storage_config
($cfg, $storeid);
947 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
950 $plugin->deactivate_storage($storeid, $scfg, $cache);
953 sub activate_volumes
{
954 my ($cfg, $vollist, $snapname) = @_;
956 return if !($vollist && scalar(@$vollist));
958 my $storagehash = {};
959 foreach my $volid (@$vollist) {
960 my ($storeid, undef) = parse_volume_id
($volid);
961 $storagehash->{$storeid} = 1;
966 activate_storage_list
($cfg, [keys %$storagehash], $cache);
968 foreach my $volid (@$vollist) {
969 my ($storeid, $volname) = parse_volume_id
($volid);
970 my $scfg = storage_config
($cfg, $storeid);
971 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
972 $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache);
976 sub deactivate_volumes
{
977 my ($cfg, $vollist, $snapname) = @_;
979 return if !($vollist && scalar(@$vollist));
984 foreach my $volid (@$vollist) {
985 my ($storeid, $volname) = parse_volume_id
($volid);
987 my $scfg = storage_config
($cfg, $storeid);
988 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
991 $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache);
995 push @errlist, $volid;
999 die "volume deactivation failed: " . join(' ', @errlist)
1000 if scalar(@errlist);
1004 my ($cfg, $content) = @_;
1006 my $ids = $cfg->{ids
};
1010 my @ctypes = PVE
::Tools
::split_list
($content);
1013 foreach my $storeid (keys %$ids) {
1014 my $storage_enabled = defined(storage_check_enabled
($cfg, $storeid, undef, 1));
1016 if (defined($content)) {
1018 foreach my $ctype (@ctypes) {
1019 if ($ids->{$storeid}->{content
}->{$ctype}) {
1024 next if !$want_ctype || !$storage_enabled;
1027 my $type = $ids->{$storeid}->{type
};
1029 $info->{$storeid} = {
1034 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
1035 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
1037 enabled
=> $storage_enabled ?
1 : 0,
1040 push @$slist, $storeid;
1045 foreach my $storeid (keys %$ids) {
1046 my $scfg = $ids->{$storeid};
1048 next if !$info->{$storeid};
1049 next if !$info->{$storeid}->{enabled
};
1051 eval { activate_storage
($cfg, $storeid, $cache); };
1057 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1058 my ($total, $avail, $used, $active);
1059 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
1062 $info->{$storeid}->{total
} = int($total);
1063 $info->{$storeid}->{avail
} = int($avail);
1064 $info->{$storeid}->{used
} = int($used);
1065 $info->{$storeid}->{active
} = $active;
1074 my ($packed_ip, $family);
1076 my @res = PVE
::Tools
::getaddrinfo_all
($server);
1077 $family = $res[0]->{family
};
1078 $packed_ip = (PVE
::Tools
::unpack_sockaddr_in46
($res[0]->{addr
}))[2];
1080 if (defined $packed_ip) {
1081 return Socket
::inet_ntop
($family, $packed_ip);
1087 my ($server_in) = @_;
1090 if (!($server = resolv_server
($server_in))) {
1091 die "unable to resolve address for server '${server_in}'\n";
1094 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
1097 run_command
($cmd, outfunc
=> sub {
1100 # note: howto handle white spaces in export path??
1101 if ($line =~ m!^(/\S+)\s+(.+)$!) {
1111 my $cmd = ['zfs', 'list', '-t', 'filesystem', '-H', '-o', 'name,avail,used'];
1114 run_command
($cmd, outfunc
=> sub {
1117 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
1118 my ($pool, $size_str, $used_str) = ($1, $2, $3);
1119 my $size = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($size_str);
1120 my $used = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($used_str);
1121 # ignore subvolumes generated by our ZFSPoolPlugin
1122 return if $pool =~ m!/subvol-\d+-[^/]+$!;
1123 return if $pool =~ m!/basevol-\d+-[^/]+$!;
1124 push @$res, { pool
=> $pool, size
=> $size, free
=> $size-$used };
1132 my ($portal, $noerr) = @_;
1134 my ($server, $port) = PVE
::Tools
::parse_host_and_port
($portal);
1136 if (my $ip = resolv_server
($server)) {
1138 $server = "[$server]" if $server =~ /^$IPV6RE$/;
1139 return $port ?
"$server:$port" : $server;
1142 return undef if $noerr;
1144 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
1147 # idea is from usbutils package (/usr/bin/usb-devices) script
1148 sub __scan_usb_device
{
1149 my ($res, $devpath, $parent, $level) = @_;
1151 return if ! -d
$devpath;
1152 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
1153 my $port = $level ?
int($1 - 1) : 0;
1155 my $busnum = int(file_read_firstline
("$devpath/busnum"));
1156 my $devnum = int(file_read_firstline
("$devpath/devnum"));
1163 speed
=> file_read_firstline
("$devpath/speed"),
1164 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
1165 vendid
=> file_read_firstline
("$devpath/idVendor"),
1166 prodid
=> file_read_firstline
("$devpath/idProduct"),
1170 my $usbpath = $devpath;
1171 $usbpath =~ s
|^.*/\d
+\
-||;
1172 $d->{usbpath
} = $usbpath;
1175 my $product = file_read_firstline
("$devpath/product");
1176 $d->{product
} = $product if $product;
1178 my $manu = file_read_firstline
("$devpath/manufacturer");
1179 $d->{manufacturer
} = $manu if $manu;
1181 my $serial => file_read_firstline
("$devpath/serial");
1182 $d->{serial
} = $serial if $serial;
1186 foreach my $subdev (<$devpath/$busnum-*>) {
1187 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
1188 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
1197 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
1198 __scan_usb_device
($devlist, $device, 0, 0);
1205 my ($portal_in) = @_;
1208 if (!($portal = resolv_portal
($portal_in))) {
1209 die "unable to parse/resolve portal address '${portal_in}'\n";
1212 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
1215 sub storage_default_format
{
1216 my ($cfg, $storeid) = @_;
1218 my $scfg = storage_config
($cfg, $storeid);
1220 return PVE
::Storage
::Plugin
::default_format
($scfg);
1223 sub vgroup_is_used
{
1224 my ($cfg, $vgname) = @_;
1226 foreach my $storeid (keys %{$cfg->{ids
}}) {
1227 my $scfg = storage_config
($cfg, $storeid);
1228 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
1236 sub target_is_used
{
1237 my ($cfg, $target) = @_;
1239 foreach my $storeid (keys %{$cfg->{ids
}}) {
1240 my $scfg = storage_config
($cfg, $storeid);
1241 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
1249 sub volume_is_used
{
1250 my ($cfg, $volid) = @_;
1252 foreach my $storeid (keys %{$cfg->{ids
}}) {
1253 my $scfg = storage_config
($cfg, $storeid);
1254 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
1262 sub storage_is_used
{
1263 my ($cfg, $storeid) = @_;
1265 foreach my $sid (keys %{$cfg->{ids
}}) {
1266 my $scfg = storage_config
($cfg, $sid);
1267 next if !$scfg->{base
};
1268 my ($st) = parse_volume_id
($scfg->{base
});
1269 return 1 if $st && $st eq $storeid;
1276 my ($list, $func) = @_;
1280 foreach my $sid (keys %$list) {
1281 foreach my $info (@{$list->{$sid}}) {
1282 my $volid = $info->{volid
};
1283 my ($sid1, $volname) = parse_volume_id
($volid, 1);
1284 if ($sid1 && $sid1 eq $sid) {
1285 &$func ($volid, $sid, $info);
1287 warn "detected strange volid '$volid' in volume list for '$sid'\n";
1293 sub extract_vzdump_config_tar
{
1294 my ($archive, $conf_re) = @_;
1296 die "ERROR: file '$archive' does not exist\n" if ! -f
$archive;
1298 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
1299 die "unable to open file '$archive'\n";
1302 while (defined($file = <$fh>)) {
1303 if ($file =~ $conf_re) {
1304 $file = $1; # untaint
1313 die "ERROR: archive contains no configuration file\n" if !$file;
1319 $raw .= "$output\n";
1322 PVE
::Tools
::run_command
(['tar', '-xpOf', $archive, $file, '--occurrence'], outfunc
=> $out);
1324 return wantarray ?
($raw, $file) : $raw;
1327 sub extract_vzdump_config_vma
{
1328 my ($archive, $comp) = @_;
1334 $raw .= "$output\n";
1340 if ($comp eq 'gz') {
1341 $uncomp = ["zcat", $archive];
1342 } elsif ($comp eq 'lzo') {
1343 $uncomp = ["lzop", "-d", "-c", $archive];
1345 die "unknown compression method '$comp'\n";
1347 $cmd = [$uncomp, ["vma", "config", "-"]];
1349 # in some cases, lzop/zcat exits with 1 when its stdout pipe is
1350 # closed early by vma, detect this and ignore the exit code later
1355 if ($output =~ m/lzop: Broken pipe: <stdout>/ || $output =~ m/gzip: stdout: Broken pipe/) {
1357 } elsif (!defined ($errstring) && $output !~ m/^\s*$/) {
1358 $errstring = "Failed to extract config from VMA archive: $output\n";
1362 # in other cases, the pipeline will exit with exit code 141
1363 # because of the broken pipe, handle / ignore this as well
1366 $rc = PVE
::Tools
::run_command
($cmd, outfunc
=> $out, errfunc
=> $err, noerr
=> 1);
1370 # use exit code if no stderr output and not just broken pipe
1371 if (!$errstring && !$broken_pipe && $rc != 0 && $rc != 141) {
1372 die "$rerr\n" if $rerr;
1373 die "config extraction failed with exit code $rc\n";
1375 die "$errstring\n" if $errstring;
1377 # simple case without compression and weird piping behaviour
1378 PVE
::Tools
::run_command
(["vma", "config", $archive], outfunc
=> $out);
1381 return wantarray ?
($raw, undef) : $raw;
1384 sub extract_vzdump_config
{
1385 my ($cfg, $volid) = @_;
1387 my $archive = abs_filesystem_path
($cfg, $volid);
1389 if ($volid =~ /vzdump-(lxc|openvz)-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|(tar(\.(gz|lzo))?))$/) {
1390 return extract_vzdump_config_tar
($archive, qr!^(\./etc/vzdump/(pct|vps)\.conf)$!);
1391 } elsif ($volid =~ /vzdump-qemu-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|((tar|vma)(\.(gz|lzo))?))$/) {
1399 $comp = $11 if defined($11);
1402 if ($format eq 'tar') {
1403 return extract_vzdump_config_tar
($archive, qr!\(\./qemu-server\.conf\)!);
1405 return extract_vzdump_config_vma
($archive, $comp);
1408 die "cannot determine backup guest type for backup archive '$volid'\n";
1413 my ($cfg, $fh, $volid, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
1415 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1416 die "cannot export volume '$volid'\n" if !$storeid;
1417 my $scfg = storage_config
($cfg, $storeid);
1418 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1419 return $plugin->volume_export($scfg, $storeid, $fh, $volname, $format,
1420 $snapshot, $base_snapshot, $with_snapshots);
1424 my ($cfg, $fh, $volid, $format, $base_snapshot, $with_snapshots) = @_;
1426 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1427 die "cannot import into volume '$volid'\n" if !$storeid;
1428 my $scfg = storage_config
($cfg, $storeid);
1429 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1430 return $plugin->volume_import($scfg, $storeid, $fh, $volname, $format,
1431 $base_snapshot, $with_snapshots);
1434 sub volume_export_formats
{
1435 my ($cfg, $volid, $snapshot, $base_snapshot, $with_snapshots) = @_;
1437 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1438 return if !$storeid;
1439 my $scfg = storage_config
($cfg, $storeid);
1440 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1441 return $plugin->volume_export_formats($scfg, $storeid, $volname,
1442 $snapshot, $base_snapshot,
1446 sub volume_import_formats
{
1447 my ($cfg, $volid, $base_snapshot, $with_snapshots) = @_;
1449 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1450 return if !$storeid;
1451 my $scfg = storage_config
($cfg, $storeid);
1452 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1453 return $plugin->volume_import_formats($scfg, $storeid, $volname,
1454 $base_snapshot, $with_snapshots);
1457 sub volume_transfer_formats
{
1458 my ($cfg, $src_volid, $dst_volid, $snapshot, $base_snapshot, $with_snapshots) = @_;
1459 my @export_formats = volume_export_formats
($cfg, $src_volid, $snapshot, $base_snapshot, $with_snapshots);
1460 my @import_formats = volume_import_formats
($cfg, $dst_volid, $base_snapshot, $with_snapshots);
1461 my %import_hash = map { $_ => 1 } @import_formats;
1462 my @common = grep { $import_hash{$_} } @export_formats;
1466 # bash completion helper
1468 sub complete_storage
{
1469 my ($cmdname, $pname, $cvalue) = @_;
1471 my $cfg = PVE
::Storage
::config
();
1473 return $cmdname eq 'add' ?
[] : [ PVE
::Storage
::storage_ids
($cfg) ];
1476 sub complete_storage_enabled
{
1477 my ($cmdname, $pname, $cvalue) = @_;
1481 my $cfg = PVE
::Storage
::config
();
1482 foreach my $sid (keys %{$cfg->{ids
}}) {
1483 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1489 sub complete_content_type
{
1490 my ($cmdname, $pname, $cvalue) = @_;
1492 return [qw(rootdir images vztmpl iso backup)];
1495 sub complete_volume
{
1496 my ($cmdname, $pname, $cvalue) = @_;
1500 my $storage_list = complete_storage_enabled
();
1502 if ($cvalue =~ m/^([^:]+):/) {
1503 $storage_list = [ $1 ];
1505 if (scalar(@$storage_list) > 1) {
1506 # only list storage IDs to avoid large listings
1508 foreach my $storeid (@$storage_list) {
1509 # Hack: simply return 2 artificial values, so that
1510 # completions does not finish
1511 push @$res, "$storeid:volname", "$storeid:...";
1518 foreach my $storeid (@$storage_list) {
1519 my $vollist = PVE
::Storage
::volume_list
($cfg, $storeid);
1521 foreach my $item (@$vollist) {
1522 push @$res, $item->{volid
};