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);
151 return keys %{$cfg->{ids}};
155 my ($filename, $timeout) = @_;
157 return PVE::Storage::Plugin::file_size_info($filename, $timeout);
160 sub volume_size_info {
161 my ($cfg, $volid, $timeout) = @_;
163 my ($storeid, $volname) = parse_volume_id($volid, 1);
165 my $scfg = storage_config($cfg, $storeid);
166 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
167 return $plugin->volume_size_info($scfg, $storeid, $volname, $timeout);
168 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
169 return file_size_info($volid, $timeout);
176 my ($cfg, $volid, $size, $running) = @_;
178 my ($storeid, $volname) = parse_volume_id($volid, 1);
180 my $scfg = storage_config($cfg, $storeid);
181 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
182 return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running);
183 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
184 die "resize file
/device
'$volid' is not possible
\n";
186 die "unable to parse volume ID
'$volid'\n";
190 sub volume_rollback_is_possible {
191 my ($cfg, $volid, $snap) = @_;
193 my ($storeid, $volname) = parse_volume_id($volid, 1);
195 my $scfg = storage_config($cfg, $storeid);
196 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
197 return $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
198 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
199 die "snapshot rollback file
/device
'$volid' is not possible
\n";
201 die "unable to parse volume ID
'$volid'\n";
205 sub volume_snapshot {
206 my ($cfg, $volid, $snap) = @_;
208 my ($storeid, $volname) = parse_volume_id($volid, 1);
210 my $scfg = storage_config($cfg, $storeid);
211 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
212 return $plugin->volume_snapshot($scfg, $storeid, $volname, $snap);
213 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
214 die "snapshot file
/device
'$volid' is not possible
\n";
216 die "unable to parse volume ID
'$volid'\n";
220 sub volume_snapshot_rollback {
221 my ($cfg, $volid, $snap) = @_;
223 my ($storeid, $volname) = parse_volume_id($volid, 1);
225 my $scfg = storage_config($cfg, $storeid);
226 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
227 $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
228 return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap);
229 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
230 die "snapshot rollback file
/device
'$volid' is not possible
\n";
232 die "unable to parse volume ID
'$volid'\n";
236 sub volume_snapshot_delete {
237 my ($cfg, $volid, $snap, $running) = @_;
239 my ($storeid, $volname) = parse_volume_id($volid, 1);
241 my $scfg = storage_config($cfg, $storeid);
242 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
243 return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running);
244 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
245 die "snapshot
delete file
/device
'$volid' is not possible
\n";
247 die "unable to parse volume ID
'$volid'\n";
251 sub volume_has_feature {
252 my ($cfg, $feature, $volid, $snap, $running) = @_;
254 my ($storeid, $volname) = parse_volume_id($volid, 1);
256 my $scfg = storage_config($cfg, $storeid);
257 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
258 return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running);
259 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
266 sub volume_snapshot_list {
267 my ($cfg, $volid) = @_;
269 my ($storeid, $volname) = parse_volume_id($volid, 1);
271 my $scfg = storage_config($cfg, $storeid);
272 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
273 return $plugin->volume_snapshot_list($scfg, $storeid, $volname);
274 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
275 die "send file
/device
'$volid' is not possible
\n";
277 die "unable to parse volume ID
'$volid'\n";
279 # return an empty array if dataset does not exist.
283 my ($cfg, $storeid, $vmid) = @_;
285 my $scfg = storage_config($cfg, $storeid);
286 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
288 my $path = $plugin->get_subdir($scfg, 'images');
290 return $vmid ? "$path/$vmid" : $path;
293 sub get_private_dir {
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, 'rootdir');
301 return $vmid ? "$path/$vmid" : $path;
305 my ($cfg, $storeid) = @_;
307 my $scfg = storage_config($cfg, $storeid);
308 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
310 return $plugin->get_subdir($scfg, 'iso');
314 my ($cfg, $storeid) = @_;
316 my $scfg = storage_config($cfg, $storeid);
317 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
319 return $plugin->get_subdir($scfg, 'vztmpl');
323 my ($cfg, $storeid) = @_;
325 my $scfg = storage_config($cfg, $storeid);
326 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
328 return $plugin->get_subdir($scfg, 'backup');
331 # library implementation
336 die "VMID
'$vmid' contains illegal characters
\n" if $vmid !~ m/^\d+$/;
341 # NOTE: basename and basevmid are always undef for LVM-thin, where the
342 # clone -> base reference is not encoded in the volume ID.
343 # see note in PVE::Storage::LvmThinPlugin for details.
345 my ($cfg, $volid) = @_;
347 my ($storeid, $volname) = parse_volume_id($volid);
349 my $scfg = storage_config($cfg, $storeid);
351 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
353 # returns ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format)
355 return $plugin->parse_volname($volname);
358 sub parse_volume_id {
359 my ($volid, $noerr) = @_;
361 return PVE::Storage::Plugin::parse_volume_id($volid, $noerr);
364 # test if we have read access to volid
365 sub check_volume_access {
366 my ($rpcenv, $user, $cfg, $vmid, $volid) = @_;
368 my ($sid, $volname) = parse_volume_id($volid, 1);
370 my ($vtype, undef, $ownervm) = parse_volname($cfg, $volid);
371 if ($vtype eq 'iso' || $vtype eq 'vztmpl') {
372 # we simply allow access
373 } elsif (defined($ownervm) && defined($vmid) && ($ownervm == $vmid)) {
374 # we are owner - allow access
375 } elsif ($vtype eq 'backup' && $ownervm) {
376 $rpcenv->check($user, "/storage/$sid", ['Datastore.AllocateSpace']);
377 $rpcenv->check($user, "/vms/$ownervm", ['VM.Backup']);
379 # allow if we are Datastore administrator
380 $rpcenv->check($user, "/storage/$sid", ['Datastore.Allocate']);
383 die "Only root can pass arbitrary filesystem paths
."
384 if $user ne 'root@pam';
390 my $volume_is_base_and_used__no_lock = sub {
391 my ($scfg, $storeid, $plugin, $volname) = @_;
393 my ($vtype, $name, $vmid, undef, undef, $isBase, undef) =
394 $plugin->parse_volname($volname);
397 my $vollist = $plugin->list_images($storeid, $scfg);
398 foreach my $info (@$vollist) {
399 my (undef, $tmpvolname) = parse_volume_id($info->{volid});
400 my $basename = undef;
401 my $basevmid = undef;
404 (undef, undef, undef, $basename, $basevmid) =
405 $plugin->parse_volname($tmpvolname);
408 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
416 # NOTE: this check does not work for LVM-thin, where the clone -> base
417 # reference is not encoded in the volume ID.
418 # see note in PVE::Storage::LvmThinPlugin for details.
419 sub volume_is_base_and_used {
420 my ($cfg, $volid) = @_;
422 my ($storeid, $volname) = parse_volume_id($volid);
423 my $scfg = storage_config($cfg, $storeid);
424 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
426 $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
427 return &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
431 # try to map a filesystem path to a volume identifier
432 sub path_to_volume_id {
433 my ($cfg, $path) = @_;
435 my $ids = $cfg->{ids};
437 my ($sid, $volname) = parse_volume_id($path, 1);
439 if (my $scfg = $ids->{$sid}) {
441 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
442 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
443 return ($vtype, $path);
449 # Note: abs_path() return undef if $path doesn not exist
450 # for example when nfs storage is not mounted
451 $path = abs_path($path) || $path;
453 foreach my $sid (keys %$ids) {
454 my $scfg = $ids->{$sid};
455 next if !$scfg->{path};
456 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
457 my $imagedir = $plugin->get_subdir($scfg, 'images');
458 my $isodir = $plugin->get_subdir($scfg, 'iso');
459 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
460 my $backupdir = $plugin->get_subdir($scfg, 'backup');
461 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
463 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
467 my $vollist = $plugin->list_images($sid, $scfg, $vmid);
468 foreach my $info (@$vollist) {
469 my ($storeid, $volname) = parse_volume_id($info->{volid});
470 my $volpath = $plugin->path($scfg, $volname, $storeid);
471 if ($volpath eq $path) {
472 return ('images', $info->{volid});
475 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
477 return ('iso', "$sid:iso
/$name");
478 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
480 return ('vztmpl', "$sid:vztmpl
/$name");
481 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
483 return ('rootdir', "$sid:rootdir
/$vmid");
484 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!) {
486 return ('iso', "$sid:backup
/$name");
490 # can't map path to volume id
495 my ($cfg, $volid, $snapname) = @_;
497 my ($storeid, $volname) = parse_volume_id($volid);
499 my $scfg = storage_config($cfg, $storeid);
501 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
502 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname);
503 return wantarray ? ($path, $owner, $vtype) : $path;
506 sub abs_filesystem_path {
507 my ($cfg, $volid) = @_;
510 if (PVE::Storage::parse_volume_id ($volid, 1)) {
511 PVE::Storage::activate_volumes($cfg, [ $volid ]);
512 $path = PVE::Storage::path($cfg, $volid);
515 my $abspath = abs_path($volid);
516 if ($abspath && $abspath =~ m|^(/.+)$|) {
517 $path = $1; # untaint any path
522 die "can
't find file '$volid'\n" if !($path && -f $path);
527 sub storage_migrate {
528 my ($cfg, $volid, $target_sshinfo, $target_storeid, $target_volname, $base_snapshot, $snapshot, $ratelimit_bps, $insecure, $with_snapshots) = @_;
530 my ($storeid, $volname) = parse_volume_id($volid);
531 $target_volname = $volname if !$target_volname;
533 my $scfg = storage_config($cfg, $storeid);
535 # no need to migrate shared content
536 return if $storeid eq $target_storeid && $scfg->{shared};
538 my $tcfg = storage_config($cfg, $target_storeid);
540 my $target_volid = "${target_storeid}:${target_volname}";
542 my $target_ip = $target_sshinfo->{ip};
543 my $errstr = "unable to migrate '$volid' to '${target_volid
}' on host '$target_sshinfo->{name
}'";
545 my $ssh = PVE::Cluster::ssh_info_to_command($target_sshinfo);
546 my $ssh_base = PVE::Cluster::ssh_info_to_command_base($target_sshinfo);
547 local $ENV{RSYNC_RSH} = PVE::Tools::cmd2string($ssh_base);
549 my @cstream = ([ '/usr/bin
/cstream
', '-t
', $ratelimit_bps ])
550 if defined($ratelimit_bps);
552 my $migration_snapshot;
553 if (!defined($snapshot)) {
554 if ($scfg->{type} eq 'zfspool
') {
555 $migration_snapshot = 1;
556 $snapshot = '__migration__
';
560 my @formats = volume_transfer_formats($cfg, $volid, $volid, $snapshot, $base_snapshot, $with_snapshots);
561 die "cannot migrate from storage type '$scfg->{type
}' to '$tcfg->{type
}'\n" if !@formats;
562 my $format = $formats[0];
566 @insecurecmd = ('pvecm
', 'mtunnel
', '-run-command
', 1);
567 if (my $network = $target_sshinfo->{network}) {
568 push @insecurecmd, '-migration_network
', $network;
572 $with_snapshots = $with_snapshots ? 1 : 0; # sanitize for passing as cli parameter
573 my $send = ['pvesm
', 'export
', $volid, $format, '-', '-with-snapshots
', $with_snapshots];
574 my $recv = [@$ssh, @insecurecmd, '--', 'pvesm
', 'import
', $volid, $format, '-', '-with-snapshots
', $with_snapshots];
575 if (defined($snapshot)) {
576 push @$send, '-snapshot
', $snapshot
578 if ($migration_snapshot) {
579 push @$recv, '-delete-snapshot
', $snapshot;
582 if (defined($base_snapshot)) {
583 # Check if the snapshot exists on the remote side:
584 push @$send, '-base
', $base_snapshot;
585 push @$recv, '-base
', $base_snapshot;
588 volume_snapshot($cfg, $volid, $snapshot) if $migration_snapshot;
591 open(my $info, '-|', @$recv)
592 or die "receive command failed: $!\n";
593 my ($ip) = <$info> =~ /^($PVE::Tools::IPRE)$/ or die "no tunnel IP received\n";
594 my ($port) = <$info> =~ /^(\d+)$/ or die "no tunnel port received\n";
595 my $socket = IO::Socket::IP->new(PeerHost => $ip, PeerPort => $port, Type => SOCK_STREAM)
596 or die "failed to connect to tunnel at $ip:$port\n";
597 # we won't be reading from the
socket
598 shutdown($socket, 0);
599 run_command
([$send, @cstream], output
=> '>&'.fileno($socket));
600 # don't close the connection entirely otherwise the receiving end
601 # might not get all buffered data (and fails with 'connection reset by peer')
602 shutdown($socket, 1);
603 1 while <$info>; # wait for the remote process to finish
604 # now close the socket
606 if (!close($info)) { # does waitpid()
607 die "import failed: $!\n" if $!;
608 die "import failed: exit code ".($?>>8)."\n";
611 run_command
([$send, @cstream, $recv]);
615 warn "send/receive failed, cleaning up snapshot(s)..\n" if $err;
616 if ($migration_snapshot) {
617 eval { volume_snapshot_delete
($cfg, $volid, $snapshot, 0) };
618 warn "could not remove source snapshot: $@\n" if $@;
624 my ($cfg, $volid, $vmid, $snap) = @_;
626 my ($storeid, $volname) = parse_volume_id
($volid);
628 my $scfg = storage_config
($cfg, $storeid);
630 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
632 activate_storage
($cfg, $storeid);
634 # lock shared storage
635 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
636 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
637 return "$storeid:$volname";
641 sub vdisk_create_base
{
642 my ($cfg, $volid) = @_;
644 my ($storeid, $volname) = parse_volume_id
($volid);
646 my $scfg = storage_config
($cfg, $storeid);
648 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
650 activate_storage
($cfg, $storeid);
652 # lock shared storage
653 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
654 my $volname = $plugin->create_base($storeid, $scfg, $volname);
655 return "$storeid:$volname";
660 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
662 die "no storage ID specified\n" if !$storeid;
664 PVE
::JSONSchema
::parse_storage_id
($storeid);
666 my $scfg = storage_config
($cfg, $storeid);
668 die "no VMID specified\n" if !$vmid;
670 $vmid = parse_vmid
($vmid);
672 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
674 $fmt = $defformat if !$fmt;
676 activate_storage
($cfg, $storeid);
678 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
680 # lock shared storage
681 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
682 my $old_umask = umask(umask|0037);
683 my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) };
687 return "$storeid:$volname";
692 my ($cfg, $volid) = @_;
694 my ($storeid, $volname) = parse_volume_id
($volid);
695 my $scfg = storage_config
($cfg, $storeid);
696 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
698 activate_storage
($cfg, $storeid);
702 # lock shared storage
703 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
704 # LVM-thin allows deletion of still referenced base volumes!
705 die "base volume '$volname' is still in use by linked clones\n"
706 if &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
708 my (undef, undef, undef, undef, undef, $isBase, $format) =
709 $plugin->parse_volname($volname);
710 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format);
713 return if !$cleanup_worker;
715 my $rpcenv = PVE
::RPCEnvironment
::get
();
716 my $authuser = $rpcenv->get_user();
718 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
721 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
723 my ($cfg, $storeid, $tt) = @_;
725 die "unknown template type '$tt'\n"
726 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
728 my $ids = $cfg->{ids
};
730 storage_check_enabled
($cfg, $storeid) if ($storeid);
736 foreach my $sid (keys %$ids) {
737 next if $storeid && $storeid ne $sid;
739 my $scfg = $ids->{$sid};
740 my $type = $scfg->{type
};
742 next if !storage_check_enabled
($cfg, $sid, undef, 1);
744 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
745 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
746 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
748 activate_storage
($cfg, $sid);
751 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
753 my $path = $plugin->get_subdir($scfg, $tt);
755 foreach my $fn (<$path/*>) {
760 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
762 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
764 } elsif ($tt eq 'vztmpl') {
765 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
767 $info = { volid
=> "$sid:vztmpl/$1", format
=> "t$2" };
769 } elsif ($tt eq 'backup') {
770 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
772 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
775 $info->{size
} = -s
$fn;
777 push @{$res->{$sid}}, $info;
782 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
790 my ($cfg, $storeid, $vmid, $vollist) = @_;
792 my $ids = $cfg->{ids
};
794 storage_check_enabled
($cfg, $storeid) if ($storeid);
798 # prepare/activate/refresh all storages
800 my $storage_list = [];
802 foreach my $volid (@$vollist) {
803 my ($sid, undef) = parse_volume_id
($volid);
804 next if !defined($ids->{$sid});
805 next if !storage_check_enabled
($cfg, $sid, undef, 1);
806 push @$storage_list, $sid;
809 foreach my $sid (keys %$ids) {
810 next if $storeid && $storeid ne $sid;
811 next if !storage_check_enabled
($cfg, $sid, undef, 1);
812 push @$storage_list, $sid;
818 activate_storage_list
($cfg, $storage_list, $cache);
820 foreach my $sid (keys %$ids) {
821 next if $storeid && $storeid ne $sid;
822 next if !storage_check_enabled
($cfg, $sid, undef, 1);
824 my $scfg = $ids->{$sid};
825 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
826 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
827 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
834 my ($cfg, $storeid, $vmid, $content) = @_;
836 my @ctypes = qw(images vztmpl iso backup);
838 my $cts = $content ?
[ $content ] : [ @ctypes ];
840 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
843 foreach my $ct (@$cts) {
845 if ($ct eq 'images') {
846 $data = vdisk_list
($cfg, $storeid, $vmid);
847 } elsif ($ct eq 'iso' && !defined($vmid)) {
848 $data = template_list
($cfg, $storeid, 'iso');
849 } elsif ($ct eq 'vztmpl'&& !defined($vmid)) {
850 $data = template_list
($cfg, $storeid, 'vztmpl');
851 } elsif ($ct eq 'backup') {
852 $data = template_list
($cfg, $storeid, 'backup');
853 foreach my $item (@{$data->{$storeid}}) {
854 if (defined($vmid)) {
855 @{$data->{$storeid}} = grep { $_->{volid
} =~ m/\S+-$vmid-\S+/ } @{$data->{$storeid}};
860 next if !$data || !$data->{$storeid};
862 foreach my $item (@{$data->{$storeid}}) {
863 $item->{content
} = $ct;
873 my $filename = "/sys/kernel/uevent_seqnum";
876 if (my $fh = IO
::File-
>new($filename, "r")) {
878 if ($line =~ m/^(\d+)$/) {
886 sub activate_storage
{
887 my ($cfg, $storeid, $cache) = @_;
889 $cache = {} if !$cache;
891 my $scfg = storage_check_enabled
($cfg, $storeid);
893 return if $cache->{activated
}->{$storeid};
895 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
897 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
900 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
901 activate_storage
($cfg, $baseid, $cache);
904 if (!$plugin->check_connection($storeid, $scfg)) {
905 die "storage '$storeid' is not online\n";
908 $plugin->activate_storage($storeid, $scfg, $cache);
910 my $newseq = uevent_seqnum
();
912 # only call udevsettle if there are events
913 if ($newseq > $cache->{uevent_seqnum
}) {
915 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
916 $cache->{uevent_seqnum
} = $newseq;
919 $cache->{activated
}->{$storeid} = 1;
922 sub activate_storage_list
{
923 my ($cfg, $storeid_list, $cache) = @_;
925 $cache = {} if !$cache;
927 foreach my $storeid (@$storeid_list) {
928 activate_storage
($cfg, $storeid, $cache);
932 sub deactivate_storage
{
933 my ($cfg, $storeid) = @_;
935 my $scfg = storage_config
($cfg, $storeid);
936 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
939 $plugin->deactivate_storage($storeid, $scfg, $cache);
942 sub activate_volumes
{
943 my ($cfg, $vollist, $snapname) = @_;
945 return if !($vollist && scalar(@$vollist));
947 my $storagehash = {};
948 foreach my $volid (@$vollist) {
949 my ($storeid, undef) = parse_volume_id
($volid);
950 $storagehash->{$storeid} = 1;
955 activate_storage_list
($cfg, [keys %$storagehash], $cache);
957 foreach my $volid (@$vollist) {
958 my ($storeid, $volname) = parse_volume_id
($volid);
959 my $scfg = storage_config
($cfg, $storeid);
960 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
961 $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache);
965 sub deactivate_volumes
{
966 my ($cfg, $vollist, $snapname) = @_;
968 return if !($vollist && scalar(@$vollist));
973 foreach my $volid (@$vollist) {
974 my ($storeid, $volname) = parse_volume_id
($volid);
976 my $scfg = storage_config
($cfg, $storeid);
977 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
980 $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache);
984 push @errlist, $volid;
988 die "volume deactivation failed: " . join(' ', @errlist)
993 my ($cfg, $content) = @_;
995 my $ids = $cfg->{ids
};
999 my @ctypes = PVE
::Tools
::split_list
($content);
1002 foreach my $storeid (keys %$ids) {
1004 next if !storage_check_enabled
($cfg, $storeid, undef, 1);
1006 if (defined($content)) {
1008 foreach my $ctype (@ctypes) {
1009 if ($ids->{$storeid}->{content
}->{$ctype}) {
1014 next if !$want_ctype;
1017 my $type = $ids->{$storeid}->{type
};
1019 $info->{$storeid} = {
1024 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
1025 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
1029 push @$slist, $storeid;
1034 foreach my $storeid (keys %$ids) {
1035 my $scfg = $ids->{$storeid};
1036 next if !$info->{$storeid};
1038 eval { activate_storage
($cfg, $storeid, $cache); };
1044 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1045 my ($total, $avail, $used, $active);
1046 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
1049 $info->{$storeid}->{total
} = int($total);
1050 $info->{$storeid}->{avail
} = int($avail);
1051 $info->{$storeid}->{used
} = int($used);
1052 $info->{$storeid}->{active
} = $active;
1061 my ($packed_ip, $family);
1063 my @res = PVE
::Tools
::getaddrinfo_all
($server);
1064 $family = $res[0]->{family
};
1065 $packed_ip = (PVE
::Tools
::unpack_sockaddr_in46
($res[0]->{addr
}))[2];
1067 if (defined $packed_ip) {
1068 return Socket
::inet_ntop
($family, $packed_ip);
1074 my ($server_in) = @_;
1077 if (!($server = resolv_server
($server_in))) {
1078 die "unable to resolve address for server '${server_in}'\n";
1081 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
1084 run_command
($cmd, outfunc
=> sub {
1087 # note: howto handle white spaces in export path??
1088 if ($line =~ m!^(/\S+)\s+(.+)$!) {
1098 my $cmd = ['zfs', 'list', '-t', 'filesystem', '-H', '-o', 'name,avail,used'];
1101 run_command
($cmd, outfunc
=> sub {
1104 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
1105 my ($pool, $size_str, $used_str) = ($1, $2, $3);
1106 my $size = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($size_str);
1107 my $used = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($used_str);
1108 # ignore subvolumes generated by our ZFSPoolPlugin
1109 return if $pool =~ m!/subvol-\d+-[^/]+$!;
1110 return if $pool =~ m!/basevol-\d+-[^/]+$!;
1111 push @$res, { pool
=> $pool, size
=> $size, free
=> $size-$used };
1119 my ($portal, $noerr) = @_;
1121 my ($server, $port) = PVE
::Tools
::parse_host_and_port
($portal);
1123 if (my $ip = resolv_server
($server)) {
1125 $server = "[$server]" if $server =~ /^$IPV6RE$/;
1126 return $port ?
"$server:$port" : $server;
1129 return undef if $noerr;
1131 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
1134 # idea is from usbutils package (/usr/bin/usb-devices) script
1135 sub __scan_usb_device
{
1136 my ($res, $devpath, $parent, $level) = @_;
1138 return if ! -d
$devpath;
1139 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
1140 my $port = $level ?
int($1 - 1) : 0;
1142 my $busnum = int(file_read_firstline
("$devpath/busnum"));
1143 my $devnum = int(file_read_firstline
("$devpath/devnum"));
1150 speed
=> file_read_firstline
("$devpath/speed"),
1151 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
1152 vendid
=> file_read_firstline
("$devpath/idVendor"),
1153 prodid
=> file_read_firstline
("$devpath/idProduct"),
1157 my $usbpath = $devpath;
1158 $usbpath =~ s
|^.*/\d
+\
-||;
1159 $d->{usbpath
} = $usbpath;
1162 my $product = file_read_firstline
("$devpath/product");
1163 $d->{product
} = $product if $product;
1165 my $manu = file_read_firstline
("$devpath/manufacturer");
1166 $d->{manufacturer
} = $manu if $manu;
1168 my $serial => file_read_firstline
("$devpath/serial");
1169 $d->{serial
} = $serial if $serial;
1173 foreach my $subdev (<$devpath/$busnum-*>) {
1174 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
1175 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
1184 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
1185 __scan_usb_device
($devlist, $device, 0, 0);
1192 my ($portal_in) = @_;
1195 if (!($portal = resolv_portal
($portal_in))) {
1196 die "unable to parse/resolve portal address '${portal_in}'\n";
1199 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
1202 sub storage_default_format
{
1203 my ($cfg, $storeid) = @_;
1205 my $scfg = storage_config
($cfg, $storeid);
1207 return PVE
::Storage
::Plugin
::default_format
($scfg);
1210 sub vgroup_is_used
{
1211 my ($cfg, $vgname) = @_;
1213 foreach my $storeid (keys %{$cfg->{ids
}}) {
1214 my $scfg = storage_config
($cfg, $storeid);
1215 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
1223 sub target_is_used
{
1224 my ($cfg, $target) = @_;
1226 foreach my $storeid (keys %{$cfg->{ids
}}) {
1227 my $scfg = storage_config
($cfg, $storeid);
1228 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
1236 sub volume_is_used
{
1237 my ($cfg, $volid) = @_;
1239 foreach my $storeid (keys %{$cfg->{ids
}}) {
1240 my $scfg = storage_config
($cfg, $storeid);
1241 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
1249 sub storage_is_used
{
1250 my ($cfg, $storeid) = @_;
1252 foreach my $sid (keys %{$cfg->{ids
}}) {
1253 my $scfg = storage_config
($cfg, $sid);
1254 next if !$scfg->{base
};
1255 my ($st) = parse_volume_id
($scfg->{base
});
1256 return 1 if $st && $st eq $storeid;
1263 my ($list, $func) = @_;
1267 foreach my $sid (keys %$list) {
1268 foreach my $info (@{$list->{$sid}}) {
1269 my $volid = $info->{volid
};
1270 my ($sid1, $volname) = parse_volume_id
($volid, 1);
1271 if ($sid1 && $sid1 eq $sid) {
1272 &$func ($volid, $sid, $info);
1274 warn "detected strange volid '$volid' in volume list for '$sid'\n";
1280 sub extract_vzdump_config_tar
{
1281 my ($archive, $conf_re) = @_;
1283 die "ERROR: file '$archive' does not exist\n" if ! -f
$archive;
1285 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
1286 die "unable to open file '$archive'\n";
1289 while (defined($file = <$fh>)) {
1290 if ($file =~ $conf_re) {
1291 $file = $1; # untaint
1300 die "ERROR: archive contains no configuration file\n" if !$file;
1306 $raw .= "$output\n";
1309 PVE
::Tools
::run_command
(['tar', '-xpOf', $archive, $file, '--occurrence'], outfunc
=> $out);
1311 return wantarray ?
($raw, $file) : $raw;
1314 sub extract_vzdump_config_vma
{
1315 my ($archive, $comp) = @_;
1321 $raw .= "$output\n";
1327 if ($comp eq 'gz') {
1328 $uncomp = ["zcat", $archive];
1329 } elsif ($comp eq 'lzo') {
1330 $uncomp = ["lzop", "-d", "-c", $archive];
1332 die "unknown compression method '$comp'\n";
1334 $cmd = [$uncomp, ["vma", "config", "-"]];
1336 # in some cases, lzop/zcat exits with 1 when its stdout pipe is
1337 # closed early by vma, detect this and ignore the exit code later
1342 if ($output =~ m/lzop: Broken pipe: <stdout>/ || $output =~ m/gzip: stdout: Broken pipe/) {
1344 } elsif (!defined ($errstring) && $output !~ m/^\s*$/) {
1345 $errstring = "Failed to extract config from VMA archive: $output\n";
1349 # in other cases, the pipeline will exit with exit code 141
1350 # because of the broken pipe, handle / ignore this as well
1353 $rc = PVE
::Tools
::run_command
($cmd, outfunc
=> $out, errfunc
=> $err, noerr
=> 1);
1357 # use exit code if no stderr output and not just broken pipe
1358 if (!$errstring && !$broken_pipe && $rc != 0 && $rc != 141) {
1359 die "$rerr\n" if $rerr;
1360 die "config extraction failed with exit code $rc\n";
1362 die "$errstring\n" if $errstring;
1364 # simple case without compression and weird piping behaviour
1365 PVE
::Tools
::run_command
(["vma", "config", $archive], outfunc
=> $out);
1368 return wantarray ?
($raw, undef) : $raw;
1371 sub extract_vzdump_config
{
1372 my ($cfg, $volid) = @_;
1374 my $archive = abs_filesystem_path
($cfg, $volid);
1376 if ($volid =~ /vzdump-(lxc|openvz)-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|(tar(\.(gz|lzo))?))$/) {
1377 return extract_vzdump_config_tar
($archive, qr!^(\./etc/vzdump/(pct|vps)\.conf)$!);
1378 } elsif ($volid =~ /vzdump-qemu-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|((tar|vma)(\.(gz|lzo))?))$/) {
1386 $comp = $11 if defined($11);
1389 if ($format eq 'tar') {
1390 return extract_vzdump_config_tar
($archive, qr!\(\./qemu-server\.conf\)!);
1392 return extract_vzdump_config_vma
($archive, $comp);
1395 die "cannot determine backup guest type for backup archive '$volid'\n";
1400 my ($cfg, $fh, $volid, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
1402 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1403 die "cannot export volume '$volid'\n" if !$storeid;
1404 my $scfg = storage_config
($cfg, $storeid);
1405 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1406 return $plugin->volume_export($scfg, $storeid, $fh, $volname, $format,
1407 $snapshot, $base_snapshot, $with_snapshots);
1411 my ($cfg, $fh, $volid, $format, $base_snapshot, $with_snapshots) = @_;
1413 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1414 die "cannot import into volume '$volid'\n" if !$storeid;
1415 my $scfg = storage_config
($cfg, $storeid);
1416 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1417 return $plugin->volume_import($scfg, $storeid, $fh, $volname, $format,
1418 $base_snapshot, $with_snapshots);
1421 sub volume_export_formats
{
1422 my ($cfg, $volid, $snapshot, $base_snapshot, $with_snapshots) = @_;
1424 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1425 return if !$storeid;
1426 my $scfg = storage_config
($cfg, $storeid);
1427 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1428 return $plugin->volume_export_formats($scfg, $storeid, $volname,
1429 $snapshot, $base_snapshot,
1433 sub volume_import_formats
{
1434 my ($cfg, $volid, $base_snapshot, $with_snapshots) = @_;
1436 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1437 return if !$storeid;
1438 my $scfg = storage_config
($cfg, $storeid);
1439 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1440 return $plugin->volume_import_formats($scfg, $storeid, $volname,
1441 $base_snapshot, $with_snapshots);
1444 sub volume_transfer_formats
{
1445 my ($cfg, $src_volid, $dst_volid, $snapshot, $base_snapshot, $with_snapshots) = @_;
1446 my @export_formats = volume_export_formats
($cfg, $src_volid, $snapshot, $base_snapshot, $with_snapshots);
1447 my @import_formats = volume_import_formats
($cfg, $dst_volid, $base_snapshot, $with_snapshots);
1448 my %import_hash = map { $_ => 1 } @import_formats;
1449 my @common = grep { $import_hash{$_} } @export_formats;
1453 # bash completion helper
1455 sub complete_storage
{
1456 my ($cmdname, $pname, $cvalue) = @_;
1458 my $cfg = PVE
::Storage
::config
();
1460 return $cmdname eq 'add' ?
[] : [ PVE
::Storage
::storage_ids
($cfg) ];
1463 sub complete_storage_enabled
{
1464 my ($cmdname, $pname, $cvalue) = @_;
1468 my $cfg = PVE
::Storage
::config
();
1469 foreach my $sid (keys %{$cfg->{ids
}}) {
1470 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1476 sub complete_content_type
{
1477 my ($cmdname, $pname, $cvalue) = @_;
1479 return [qw(rootdir images vztmpl iso backup)];
1482 sub complete_volume
{
1483 my ($cmdname, $pname, $cvalue) = @_;
1487 my $storage_list = complete_storage_enabled
();
1489 if ($cvalue =~ m/^([^:]+):/) {
1490 $storage_list = [ $1 ];
1492 if (scalar(@$storage_list) > 1) {
1493 # only list storage IDs to avoid large listings
1495 foreach my $storeid (@$storage_list) {
1496 # Hack: simply return 2 artificial values, so that
1497 # completions does not finish
1498 push @$res, "$storeid:volname", "$storeid:...";
1505 foreach my $storeid (@$storage_list) {
1506 my $vollist = PVE
::Storage
::volume_list
($cfg, $storeid);
1508 foreach my $item (@$vollist) {
1509 push @$res, $item->{volid
};