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) {
265 sub volume_snapshot_list {
266 my ($cfg, $volid, $prefix) = @_;
268 my ($storeid, $volname) = parse_volume_id($volid, 1);
270 my $scfg = storage_config($cfg, $storeid);
271 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
272 return $plugin->volume_snapshot_list($scfg, $storeid, $volname, $prefix);
273 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
274 die "send file
/device
'$volid' is not possible
\n";
276 die "unable to parse volume ID
'$volid'\n";
278 # return an empty array if dataset does not exist.
279 # youngest snap first
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) = @_;
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_host = $target_sshinfo->{name};
543 my $errstr = "unable to migrate '$volid' to '${target_volid
}' on host '$target_host'";
545 my $ssh = PVE::Cluster::ssh_info_to_command($target_sshinfo);
546 local $ENV{RSYNC_RSH} = PVE::Tools::cmd2string($ssh);
548 my $no_incremental = sub {
550 die "incremental migration not supported on storage type $type\n"
551 if defined($base_snapshot);
553 my $no_snapshot = sub {
555 # $snapshot is currently only used by replication
556 die "replicating storage migration not supported on storage type $type\n"
557 if defined($snapshot);
560 my @cstream = ([ '/usr/bin
/cstream
', '-t
', $ratelimit_bps ])
561 if defined($ratelimit_bps);
563 # only implemented for file system based storage
565 $no_incremental->($scfg->{type});
566 $no_snapshot->($scfg->{type});
569 my $src_plugin = PVE::Storage::Plugin->lookup($scfg->{type});
570 my $dst_plugin = PVE::Storage::Plugin->lookup($tcfg->{type});
571 my $src = $src_plugin->path($scfg, $volname, $storeid);
572 my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid);
574 my $dirname = dirname($dst);
576 if ($tcfg->{shared}) { # we can do a local copy
578 run_command(['/bin/mkdir
', '-p
', $dirname]);
580 run_command(['/bin/cp', $src, $dst]);
583 run_command([@$ssh, '/bin/mkdir
', '-p
', $dirname]);
585 # we use rsync with --sparse, so we can't
use --inplace
,
586 # so we remove file on the target if it already exists to
588 my ($size, $format) = PVE
::Storage
::Plugin
::file_size_info
($src);
589 if ($format && ($format eq 'raw') && $size) {
590 run_command
([@$ssh, 'rm', '-f', $dst],
595 my @bwlimit = ("--bwlimit=${ratelimit_bps}b") if defined($ratelimit_bps);
596 if ($format eq 'subvol') {
597 $cmd = ['/usr/bin/rsync', '--progress', '-X', '-A', '--numeric-ids',
598 '-aH', '--delete', '--no-whole-file', '--inplace',
599 '--one-file-system', @bwlimit,
600 "$src/", "[root\@${target_host}]:$dst"];
602 $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
604 $src, "[root\@${target_host}]:$dst"];
609 run_command
($cmd, outfunc
=> sub {
612 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
615 print "rsync status: $1\n";
625 die "$errstr - target type '$tcfg->{type}' not implemented\n";
628 } elsif ($scfg->{type
} eq 'zfspool') {
630 if ($tcfg->{type
} eq 'zfspool') {
632 die "$errstr - pool on target does not have the same name as on source!"
633 if $tcfg->{pool
} ne $scfg->{pool
};
635 my $migration_snapshot;
636 if (!defined($snapshot)) {
637 $migration_snapshot = 1;
638 $snapshot = '__migration__';
641 my (undef, $volname) = parse_volname
($cfg, $volid);
642 my $zfspath = "$scfg->{pool}\/$volname";
644 my @formats = volume_transfer_formats
($cfg, $volid, $volid, $snapshot, $base_snapshot, 1);
645 die "cannot migrate from storage type '$scfg->{type}' to '$tcfg->{type}'\n" if !@formats;
646 my $format = $formats[0];
648 my $send = ['pvesm', 'export', $volid, $format, '-', '-snapshot', $snapshot, '-with-snapshots', '1'];
649 my $recv = [@$ssh, '--', 'pvesm', 'import', $volid, $format, '-', '-with-snapshots', '1'];
650 if ($migration_snapshot) {
651 push @$recv, '-delete-snapshot', $snapshot;
654 if (defined($base_snapshot)) {
655 # Check if the snapshot exists on the remote side:
656 push @$send, '-base', $base_snapshot;
657 push @$recv, '-base', $base_snapshot;
660 volume_snapshot
($cfg, $volid, $snapshot) if $migration_snapshot;
662 run_command
([$send, @cstream, $recv]);
665 warn "send/receive failed, cleaning up snapshot(s)..\n" if $err;
666 if ($migration_snapshot) {
667 eval { volume_snapshot_delete
($cfg, $volid, $snapshot, 0) };
668 warn "could not remove source snapshot: $@\n" if $@;
672 die "$errstr - target type $tcfg->{type} is not valid\n";
675 } elsif ($scfg->{type
} eq 'lvmthin' || $scfg->{type
} eq 'lvm') {
676 $no_incremental->($scfg->{type
});
677 $no_snapshot->($scfg->{type
});
679 if (($scfg->{type
} eq $tcfg->{type
}) &&
680 ($tcfg->{type
} eq 'lvmthin' || $tcfg->{type
} eq 'lvm')) {
682 my (undef, $volname, $vmid) = parse_volname
($cfg, $volid);
683 my $size = volume_size_info
($cfg, $volid, 5);
684 my $src = path
($cfg, $volid);
685 my $dst = path
($cfg, $target_volid);
687 run_command
([@$ssh, '--',
688 'pvesm', 'alloc', $target_storeid, $vmid,
689 $target_volname, int($size/1024)]);
692 if ($tcfg->{type
} eq 'lvmthin') {
693 run_command
([["dd", "if=$src", "bs=4k"], @cstream,
694 [@$ssh, "dd", 'conv=sparse', "of=$dst", "bs=4k"]]);
696 run_command
([["dd", "if=$src", "bs=4k"], @cstream,
697 [@$ssh, "dd", "of=$dst", "bs=4k"]]);
701 run_command
([@$ssh, 'pvesm', 'free', $target_volid]);
705 die "$errstr - migrate from source type '$scfg->{type}' to '$tcfg->{type}' not implemented\n";
708 die "$errstr - source type '$scfg->{type}' not implemented\n";
713 my ($cfg, $volid, $vmid, $snap) = @_;
715 my ($storeid, $volname) = parse_volume_id
($volid);
717 my $scfg = storage_config
($cfg, $storeid);
719 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
721 activate_storage
($cfg, $storeid);
723 # lock shared storage
724 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
725 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
726 return "$storeid:$volname";
730 sub vdisk_create_base
{
731 my ($cfg, $volid) = @_;
733 my ($storeid, $volname) = parse_volume_id
($volid);
735 my $scfg = storage_config
($cfg, $storeid);
737 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
739 activate_storage
($cfg, $storeid);
741 # lock shared storage
742 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
743 my $volname = $plugin->create_base($storeid, $scfg, $volname);
744 return "$storeid:$volname";
749 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
751 die "no storage ID specified\n" if !$storeid;
753 PVE
::JSONSchema
::parse_storage_id
($storeid);
755 my $scfg = storage_config
($cfg, $storeid);
757 die "no VMID specified\n" if !$vmid;
759 $vmid = parse_vmid
($vmid);
761 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
763 $fmt = $defformat if !$fmt;
765 activate_storage
($cfg, $storeid);
767 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
769 # lock shared storage
770 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
771 my $old_umask = umask(umask|0037);
772 my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) };
776 return "$storeid:$volname";
781 my ($cfg, $volid) = @_;
783 my ($storeid, $volname) = parse_volume_id
($volid);
784 my $scfg = storage_config
($cfg, $storeid);
785 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
787 activate_storage
($cfg, $storeid);
791 # lock shared storage
792 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
793 # LVM-thin allows deletion of still referenced base volumes!
794 die "base volume '$volname' is still in use by linked clones\n"
795 if &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
797 my (undef, undef, undef, undef, undef, $isBase, $format) =
798 $plugin->parse_volname($volname);
799 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format);
802 return if !$cleanup_worker;
804 my $rpcenv = PVE
::RPCEnvironment
::get
();
805 my $authuser = $rpcenv->get_user();
807 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
810 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
812 my ($cfg, $storeid, $tt) = @_;
814 die "unknown template type '$tt'\n"
815 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
817 my $ids = $cfg->{ids
};
819 storage_check_enabled
($cfg, $storeid) if ($storeid);
825 foreach my $sid (keys %$ids) {
826 next if $storeid && $storeid ne $sid;
828 my $scfg = $ids->{$sid};
829 my $type = $scfg->{type
};
831 next if !storage_check_enabled
($cfg, $sid, undef, 1);
833 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
834 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
835 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
837 activate_storage
($cfg, $sid);
840 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
842 my $path = $plugin->get_subdir($scfg, $tt);
844 foreach my $fn (<$path/*>) {
849 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
851 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
853 } elsif ($tt eq 'vztmpl') {
854 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
856 $info = { volid
=> "$sid:vztmpl/$1", format
=> "t$2" };
858 } elsif ($tt eq 'backup') {
859 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
861 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
864 $info->{size
} = -s
$fn;
866 push @{$res->{$sid}}, $info;
871 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
879 my ($cfg, $storeid, $vmid, $vollist) = @_;
881 my $ids = $cfg->{ids
};
883 storage_check_enabled
($cfg, $storeid) if ($storeid);
887 # prepare/activate/refresh all storages
889 my $storage_list = [];
891 foreach my $volid (@$vollist) {
892 my ($sid, undef) = parse_volume_id
($volid);
893 next if !defined($ids->{$sid});
894 next if !storage_check_enabled
($cfg, $sid, undef, 1);
895 push @$storage_list, $sid;
898 foreach my $sid (keys %$ids) {
899 next if $storeid && $storeid ne $sid;
900 next if !storage_check_enabled
($cfg, $sid, undef, 1);
901 push @$storage_list, $sid;
907 activate_storage_list
($cfg, $storage_list, $cache);
909 foreach my $sid (keys %$ids) {
910 next if $storeid && $storeid ne $sid;
911 next if !storage_check_enabled
($cfg, $sid, undef, 1);
913 my $scfg = $ids->{$sid};
914 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
915 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
916 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
923 my ($cfg, $storeid, $vmid, $content) = @_;
925 my @ctypes = qw(images vztmpl iso backup);
927 my $cts = $content ?
[ $content ] : [ @ctypes ];
929 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
932 foreach my $ct (@$cts) {
934 if ($ct eq 'images') {
935 $data = vdisk_list
($cfg, $storeid, $vmid);
936 } elsif ($ct eq 'iso' && !defined($vmid)) {
937 $data = template_list
($cfg, $storeid, 'iso');
938 } elsif ($ct eq 'vztmpl'&& !defined($vmid)) {
939 $data = template_list
($cfg, $storeid, 'vztmpl');
940 } elsif ($ct eq 'backup') {
941 $data = template_list
($cfg, $storeid, 'backup');
942 foreach my $item (@{$data->{$storeid}}) {
943 if (defined($vmid)) {
944 @{$data->{$storeid}} = grep { $_->{volid
} =~ m/\S+-$vmid-\S+/ } @{$data->{$storeid}};
949 next if !$data || !$data->{$storeid};
951 foreach my $item (@{$data->{$storeid}}) {
952 $item->{content
} = $ct;
962 my $filename = "/sys/kernel/uevent_seqnum";
965 if (my $fh = IO
::File-
>new($filename, "r")) {
967 if ($line =~ m/^(\d+)$/) {
975 sub activate_storage
{
976 my ($cfg, $storeid, $cache) = @_;
978 $cache = {} if !$cache;
980 my $scfg = storage_check_enabled
($cfg, $storeid);
982 return if $cache->{activated
}->{$storeid};
984 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
986 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
989 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
990 activate_storage
($cfg, $baseid, $cache);
993 if (!$plugin->check_connection($storeid, $scfg)) {
994 die "storage '$storeid' is not online\n";
997 $plugin->activate_storage($storeid, $scfg, $cache);
999 my $newseq = uevent_seqnum
();
1001 # only call udevsettle if there are events
1002 if ($newseq > $cache->{uevent_seqnum
}) {
1004 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
1005 $cache->{uevent_seqnum
} = $newseq;
1008 $cache->{activated
}->{$storeid} = 1;
1011 sub activate_storage_list
{
1012 my ($cfg, $storeid_list, $cache) = @_;
1014 $cache = {} if !$cache;
1016 foreach my $storeid (@$storeid_list) {
1017 activate_storage
($cfg, $storeid, $cache);
1021 sub deactivate_storage
{
1022 my ($cfg, $storeid) = @_;
1024 my $scfg = storage_config
($cfg, $storeid);
1025 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1028 $plugin->deactivate_storage($storeid, $scfg, $cache);
1031 sub activate_volumes
{
1032 my ($cfg, $vollist, $snapname) = @_;
1034 return if !($vollist && scalar(@$vollist));
1036 my $storagehash = {};
1037 foreach my $volid (@$vollist) {
1038 my ($storeid, undef) = parse_volume_id
($volid);
1039 $storagehash->{$storeid} = 1;
1044 activate_storage_list
($cfg, [keys %$storagehash], $cache);
1046 foreach my $volid (@$vollist) {
1047 my ($storeid, $volname) = parse_volume_id
($volid);
1048 my $scfg = storage_config
($cfg, $storeid);
1049 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1050 $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache);
1054 sub deactivate_volumes
{
1055 my ($cfg, $vollist, $snapname) = @_;
1057 return if !($vollist && scalar(@$vollist));
1062 foreach my $volid (@$vollist) {
1063 my ($storeid, $volname) = parse_volume_id
($volid);
1065 my $scfg = storage_config
($cfg, $storeid);
1066 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1069 $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache);
1073 push @errlist, $volid;
1077 die "volume deactivation failed: " . join(' ', @errlist)
1078 if scalar(@errlist);
1082 my ($cfg, $content) = @_;
1084 my $ids = $cfg->{ids
};
1088 my @ctypes = PVE
::Tools
::split_list
($content);
1091 foreach my $storeid (keys %$ids) {
1093 next if !storage_check_enabled
($cfg, $storeid, undef, 1);
1095 if (defined($content)) {
1097 foreach my $ctype (@ctypes) {
1098 if ($ids->{$storeid}->{content
}->{$ctype}) {
1103 next if !$want_ctype;
1106 my $type = $ids->{$storeid}->{type
};
1108 $info->{$storeid} = {
1113 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
1114 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
1118 push @$slist, $storeid;
1123 foreach my $storeid (keys %$ids) {
1124 my $scfg = $ids->{$storeid};
1125 next if !$info->{$storeid};
1127 eval { activate_storage
($cfg, $storeid, $cache); };
1133 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1134 my ($total, $avail, $used, $active);
1135 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
1138 $info->{$storeid}->{total
} = int($total);
1139 $info->{$storeid}->{avail
} = int($avail);
1140 $info->{$storeid}->{used
} = int($used);
1141 $info->{$storeid}->{active
} = $active;
1150 my ($packed_ip, $family);
1152 my @res = PVE
::Tools
::getaddrinfo_all
($server);
1153 $family = $res[0]->{family
};
1154 $packed_ip = (PVE
::Tools
::unpack_sockaddr_in46
($res[0]->{addr
}))[2];
1156 if (defined $packed_ip) {
1157 return Socket
::inet_ntop
($family, $packed_ip);
1163 my ($server_in) = @_;
1166 if (!($server = resolv_server
($server_in))) {
1167 die "unable to resolve address for server '${server_in}'\n";
1170 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
1173 run_command
($cmd, outfunc
=> sub {
1176 # note: howto handle white spaces in export path??
1177 if ($line =~ m!^(/\S+)\s+(.+)$!) {
1187 my $cmd = ['zfs', 'list', '-t', 'filesystem', '-H', '-o', 'name,avail,used'];
1190 run_command
($cmd, outfunc
=> sub {
1193 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
1194 my ($pool, $size_str, $used_str) = ($1, $2, $3);
1195 my $size = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($size_str);
1196 my $used = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($used_str);
1197 # ignore subvolumes generated by our ZFSPoolPlugin
1198 return if $pool =~ m!/subvol-\d+-[^/]+$!;
1199 return if $pool =~ m!/basevol-\d+-[^/]+$!;
1200 push @$res, { pool
=> $pool, size
=> $size, free
=> $size-$used };
1208 my ($portal, $noerr) = @_;
1210 my ($server, $port) = PVE
::Tools
::parse_host_and_port
($portal);
1212 if (my $ip = resolv_server
($server)) {
1214 $server = "[$server]" if $server =~ /^$IPV6RE$/;
1215 return $port ?
"$server:$port" : $server;
1218 return undef if $noerr;
1220 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
1223 # idea is from usbutils package (/usr/bin/usb-devices) script
1224 sub __scan_usb_device
{
1225 my ($res, $devpath, $parent, $level) = @_;
1227 return if ! -d
$devpath;
1228 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
1229 my $port = $level ?
int($1 - 1) : 0;
1231 my $busnum = int(file_read_firstline
("$devpath/busnum"));
1232 my $devnum = int(file_read_firstline
("$devpath/devnum"));
1239 speed
=> file_read_firstline
("$devpath/speed"),
1240 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
1241 vendid
=> file_read_firstline
("$devpath/idVendor"),
1242 prodid
=> file_read_firstline
("$devpath/idProduct"),
1246 my $usbpath = $devpath;
1247 $usbpath =~ s
|^.*/\d
+\
-||;
1248 $d->{usbpath
} = $usbpath;
1251 my $product = file_read_firstline
("$devpath/product");
1252 $d->{product
} = $product if $product;
1254 my $manu = file_read_firstline
("$devpath/manufacturer");
1255 $d->{manufacturer
} = $manu if $manu;
1257 my $serial => file_read_firstline
("$devpath/serial");
1258 $d->{serial
} = $serial if $serial;
1262 foreach my $subdev (<$devpath/$busnum-*>) {
1263 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
1264 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
1273 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
1274 __scan_usb_device
($devlist, $device, 0, 0);
1281 my ($portal_in) = @_;
1284 if (!($portal = resolv_portal
($portal_in))) {
1285 die "unable to parse/resolve portal address '${portal_in}'\n";
1288 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
1291 sub storage_default_format
{
1292 my ($cfg, $storeid) = @_;
1294 my $scfg = storage_config
($cfg, $storeid);
1296 return PVE
::Storage
::Plugin
::default_format
($scfg);
1299 sub vgroup_is_used
{
1300 my ($cfg, $vgname) = @_;
1302 foreach my $storeid (keys %{$cfg->{ids
}}) {
1303 my $scfg = storage_config
($cfg, $storeid);
1304 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
1312 sub target_is_used
{
1313 my ($cfg, $target) = @_;
1315 foreach my $storeid (keys %{$cfg->{ids
}}) {
1316 my $scfg = storage_config
($cfg, $storeid);
1317 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
1325 sub volume_is_used
{
1326 my ($cfg, $volid) = @_;
1328 foreach my $storeid (keys %{$cfg->{ids
}}) {
1329 my $scfg = storage_config
($cfg, $storeid);
1330 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
1338 sub storage_is_used
{
1339 my ($cfg, $storeid) = @_;
1341 foreach my $sid (keys %{$cfg->{ids
}}) {
1342 my $scfg = storage_config
($cfg, $sid);
1343 next if !$scfg->{base
};
1344 my ($st) = parse_volume_id
($scfg->{base
});
1345 return 1 if $st && $st eq $storeid;
1352 my ($list, $func) = @_;
1356 foreach my $sid (keys %$list) {
1357 foreach my $info (@{$list->{$sid}}) {
1358 my $volid = $info->{volid
};
1359 my ($sid1, $volname) = parse_volume_id
($volid, 1);
1360 if ($sid1 && $sid1 eq $sid) {
1361 &$func ($volid, $sid, $info);
1363 warn "detected strange volid '$volid' in volume list for '$sid'\n";
1369 sub extract_vzdump_config_tar
{
1370 my ($archive, $conf_re) = @_;
1372 die "ERROR: file '$archive' does not exist\n" if ! -f
$archive;
1374 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
1375 die "unable to open file '$archive'\n";
1378 while (defined($file = <$fh>)) {
1379 if ($file =~ $conf_re) {
1380 $file = $1; # untaint
1389 die "ERROR: archive contains no configuration file\n" if !$file;
1395 $raw .= "$output\n";
1398 PVE
::Tools
::run_command
(['tar', '-xpOf', $archive, $file, '--occurrence'], outfunc
=> $out);
1400 return wantarray ?
($raw, $file) : $raw;
1403 sub extract_vzdump_config_vma
{
1404 my ($archive, $comp) = @_;
1410 $raw .= "$output\n";
1416 if ($comp eq 'gz') {
1417 $uncomp = ["zcat", $archive];
1418 } elsif ($comp eq 'lzo') {
1419 $uncomp = ["lzop", "-d", "-c", $archive];
1421 die "unknown compression method '$comp'\n";
1423 $cmd = [$uncomp, ["vma", "config", "-"]];
1425 # in some cases, lzop/zcat exits with 1 when its stdout pipe is
1426 # closed early by vma, detect this and ignore the exit code later
1431 if ($output =~ m/lzop: Broken pipe: <stdout>/ || $output =~ m/gzip: stdout: Broken pipe/) {
1433 } elsif (!defined ($errstring) && $output !~ m/^\s*$/) {
1434 $errstring = "Failed to extract config from VMA archive: $output\n";
1438 # in other cases, the pipeline will exit with exit code 141
1439 # because of the broken pipe, handle / ignore this as well
1442 $rc = PVE
::Tools
::run_command
($cmd, outfunc
=> $out, errfunc
=> $err, noerr
=> 1);
1446 # use exit code if no stderr output and not just broken pipe
1447 if (!$errstring && !$broken_pipe && $rc != 0 && $rc != 141) {
1448 die "$rerr\n" if $rerr;
1449 die "config extraction failed with exit code $rc\n";
1451 die "$errstring\n" if $errstring;
1453 # simple case without compression and weird piping behaviour
1454 PVE
::Tools
::run_command
(["vma", "config", $archive], outfunc
=> $out);
1457 return wantarray ?
($raw, undef) : $raw;
1460 sub extract_vzdump_config
{
1461 my ($cfg, $volid) = @_;
1463 my $archive = abs_filesystem_path
($cfg, $volid);
1465 if ($volid =~ /vzdump-(lxc|openvz)-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|(tar(\.(gz|lzo))?))$/) {
1466 return extract_vzdump_config_tar
($archive, qr!^(\./etc/vzdump/(pct|vps)\.conf)$!);
1467 } elsif ($volid =~ /vzdump-qemu-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|((tar|vma)(\.(gz|lzo))?))$/) {
1475 $comp = $11 if defined($11);
1478 if ($format eq 'tar') {
1479 return extract_vzdump_config_tar
($archive, qr!\(\./qemu-server\.conf\)!);
1481 return extract_vzdump_config_vma
($archive, $comp);
1484 die "cannot determine backup guest type for backup archive '$volid'\n";
1489 my ($cfg, $fh, $volid, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
1491 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1492 die "cannot export volume '$volid'\n" if !$storeid;
1493 my $scfg = storage_config
($cfg, $storeid);
1494 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1495 return $plugin->volume_export($scfg, $storeid, $fh, $volname, $format,
1496 $snapshot, $base_snapshot, $with_snapshots);
1500 my ($cfg, $fh, $volid, $format, $base_snapshot, $with_snapshots) = @_;
1502 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1503 die "cannot import into volume '$volid'\n" if !$storeid;
1504 my $scfg = storage_config
($cfg, $storeid);
1505 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1506 return $plugin->volume_import($scfg, $storeid, $fh, $volname, $format,
1507 $base_snapshot, $with_snapshots);
1510 sub volume_export_formats
{
1511 my ($cfg, $volid, $snapshot, $base_snapshot, $with_snapshots) = @_;
1513 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1514 return if !$storeid;
1515 my $scfg = storage_config
($cfg, $storeid);
1516 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1517 return $plugin->volume_export_formats($scfg, $storeid, $volname,
1518 $base_snapshot, $with_snapshots);
1521 sub volume_import_formats
{
1522 my ($cfg, $volid, $base_snapshot, $with_snapshots) = @_;
1524 my ($storeid, $volname) = parse_volume_id
($volid, 1);
1525 return if !$storeid;
1526 my $scfg = storage_config
($cfg, $storeid);
1527 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1528 return $plugin->volume_import_formats($scfg, $storeid, $volname,
1529 $base_snapshot, $with_snapshots);
1532 sub volume_transfer_formats
{
1533 my ($cfg, $src_volid, $dst_volid, $snapshot, $base_snapshot, $with_snapshots) = @_;
1534 my @export_formats = volume_export_formats
($cfg, $src_volid, $snapshot, $base_snapshot, $with_snapshots);
1535 my @import_formats = volume_import_formats
($cfg, $dst_volid, $base_snapshot, $with_snapshots);
1536 my %import_hash = map { $_ => 1 } @import_formats;
1537 my @common = grep { $import_hash{$_} } @export_formats;
1541 # bash completion helper
1543 sub complete_storage
{
1544 my ($cmdname, $pname, $cvalue) = @_;
1546 my $cfg = PVE
::Storage
::config
();
1548 return $cmdname eq 'add' ?
[] : [ PVE
::Storage
::storage_ids
($cfg) ];
1551 sub complete_storage_enabled
{
1552 my ($cmdname, $pname, $cvalue) = @_;
1556 my $cfg = PVE
::Storage
::config
();
1557 foreach my $sid (keys %{$cfg->{ids
}}) {
1558 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1564 sub complete_content_type
{
1565 my ($cmdname, $pname, $cvalue) = @_;
1567 return [qw(rootdir images vztmpl iso backup)];
1570 sub complete_volume
{
1571 my ($cmdname, $pname, $cvalue) = @_;
1575 my $storage_list = complete_storage_enabled
();
1577 if ($cvalue =~ m/^([^:]+):/) {
1578 $storage_list = [ $1 ];
1580 if (scalar(@$storage_list) > 1) {
1581 # only list storage IDs to avoid large listings
1583 foreach my $storeid (@$storage_list) {
1584 # Hack: simply return 2 artificial values, so that
1585 # completions does not finish
1586 push @$res, "$storeid:volname", "$storeid:...";
1593 foreach my $storeid (@$storage_list) {
1594 my $vollist = PVE
::Storage
::volume_list
($cfg, $storeid);
1596 foreach my $item (@$vollist) {
1597 push @$res, $item->{volid
};