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
;
35 use PVE
::ReplicationTools
;
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, $prefix, $ip) = @_;
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, $prefix, $ip);
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.
280 # youngest snap first
284 my ($cfg, $storeid, $vmid) = @_;
286 my $scfg = storage_config($cfg, $storeid);
287 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
289 my $path = $plugin->get_subdir($scfg, 'images');
291 return $vmid ? "$path/$vmid" : $path;
294 sub get_private_dir {
295 my ($cfg, $storeid, $vmid) = @_;
297 my $scfg = storage_config($cfg, $storeid);
298 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
300 my $path = $plugin->get_subdir($scfg, 'rootdir');
302 return $vmid ? "$path/$vmid" : $path;
306 my ($cfg, $storeid) = @_;
308 my $scfg = storage_config($cfg, $storeid);
309 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
311 return $plugin->get_subdir($scfg, 'iso');
315 my ($cfg, $storeid) = @_;
317 my $scfg = storage_config($cfg, $storeid);
318 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
320 return $plugin->get_subdir($scfg, 'vztmpl');
324 my ($cfg, $storeid) = @_;
326 my $scfg = storage_config($cfg, $storeid);
327 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
329 return $plugin->get_subdir($scfg, 'backup');
332 # library implementation
337 die "VMID
'$vmid' contains illegal characters
\n" if $vmid !~ m/^\d+$/;
342 # NOTE: basename and basevmid are always undef for LVM-thin, where the
343 # clone -> base reference is not encoded in the volume ID.
344 # see note in PVE::Storage::LvmThinPlugin for details.
346 my ($cfg, $volid) = @_;
348 my ($storeid, $volname) = parse_volume_id($volid);
350 my $scfg = storage_config($cfg, $storeid);
352 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
354 # returns ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format)
356 return $plugin->parse_volname($volname);
359 sub parse_volume_id {
360 my ($volid, $noerr) = @_;
362 return PVE::Storage::Plugin::parse_volume_id($volid, $noerr);
365 # test if we have read access to volid
366 sub check_volume_access {
367 my ($rpcenv, $user, $cfg, $vmid, $volid) = @_;
369 my ($sid, $volname) = parse_volume_id($volid, 1);
371 my ($vtype, undef, $ownervm) = parse_volname($cfg, $volid);
372 if ($vtype eq 'iso' || $vtype eq 'vztmpl') {
373 # we simply allow access
374 } elsif (defined($ownervm) && defined($vmid) && ($ownervm == $vmid)) {
375 # we are owner - allow access
376 } elsif ($vtype eq 'backup' && $ownervm) {
377 $rpcenv->check($user, "/storage/$sid", ['Datastore.AllocateSpace']);
378 $rpcenv->check($user, "/vms/$ownervm", ['VM.Backup']);
380 # allow if we are Datastore administrator
381 $rpcenv->check($user, "/storage/$sid", ['Datastore.Allocate']);
384 die "Only root can pass arbitrary filesystem paths
."
385 if $user ne 'root@pam';
391 my $volume_is_base_and_used__no_lock = sub {
392 my ($scfg, $storeid, $plugin, $volname) = @_;
394 my ($vtype, $name, $vmid, undef, undef, $isBase, undef) =
395 $plugin->parse_volname($volname);
398 my $vollist = $plugin->list_images($storeid, $scfg);
399 foreach my $info (@$vollist) {
400 my (undef, $tmpvolname) = parse_volume_id($info->{volid});
401 my $basename = undef;
402 my $basevmid = undef;
405 (undef, undef, undef, $basename, $basevmid) =
406 $plugin->parse_volname($tmpvolname);
409 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
417 # NOTE: this check does not work for LVM-thin, where the clone -> base
418 # reference is not encoded in the volume ID.
419 # see note in PVE::Storage::LvmThinPlugin for details.
420 sub volume_is_base_and_used {
421 my ($cfg, $volid) = @_;
423 my ($storeid, $volname) = parse_volume_id($volid);
424 my $scfg = storage_config($cfg, $storeid);
425 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
427 $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
428 return &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
432 # try to map a filesystem path to a volume identifier
433 sub path_to_volume_id {
434 my ($cfg, $path) = @_;
436 my $ids = $cfg->{ids};
438 my ($sid, $volname) = parse_volume_id($path, 1);
440 if (my $scfg = $ids->{$sid}) {
442 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
443 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
444 return ($vtype, $path);
450 # Note: abs_path() return undef if $path doesn not exist
451 # for example when nfs storage is not mounted
452 $path = abs_path($path) || $path;
454 foreach my $sid (keys %$ids) {
455 my $scfg = $ids->{$sid};
456 next if !$scfg->{path};
457 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
458 my $imagedir = $plugin->get_subdir($scfg, 'images');
459 my $isodir = $plugin->get_subdir($scfg, 'iso');
460 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
461 my $backupdir = $plugin->get_subdir($scfg, 'backup');
462 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
464 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
468 my $vollist = $plugin->list_images($sid, $scfg, $vmid);
469 foreach my $info (@$vollist) {
470 my ($storeid, $volname) = parse_volume_id($info->{volid});
471 my $volpath = $plugin->path($scfg, $volname, $storeid);
472 if ($volpath eq $path) {
473 return ('images', $info->{volid});
476 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
478 return ('iso', "$sid:iso
/$name");
479 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
481 return ('vztmpl', "$sid:vztmpl
/$name");
482 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
484 return ('rootdir', "$sid:rootdir
/$vmid");
485 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!) {
487 return ('iso', "$sid:backup
/$name");
491 # can't map path to volume id
496 my ($cfg, $volid, $snapname) = @_;
498 my ($storeid, $volname) = parse_volume_id($volid);
500 my $scfg = storage_config($cfg, $storeid);
502 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
503 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname);
504 return wantarray ? ($path, $owner, $vtype) : $path;
507 sub abs_filesystem_path {
508 my ($cfg, $volid) = @_;
511 if (PVE::Storage::parse_volume_id ($volid, 1)) {
512 PVE::Storage::activate_volumes($cfg, [ $volid ]);
513 $path = PVE::Storage::path($cfg, $volid);
516 my $abspath = abs_path($volid);
517 if ($abspath && $abspath =~ m|^(/.+)$|) {
518 $path = $1; # untaint any path
523 die "can
't find file '$volid'\n" if !($path && -f $path);
528 sub storage_migrate {
529 my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
531 my ($storeid, $volname) = parse_volume_id($volid);
532 $target_volname = $volname if !$target_volname;
534 my $scfg = storage_config($cfg, $storeid);
536 # no need to migrate shared content
537 return if $storeid eq $target_storeid && $scfg->{shared};
539 my $tcfg = storage_config($cfg, $target_storeid);
541 my $target_volid = "${target_storeid}:${target_volname}";
543 my $errstr = "unable to migrate '$volid' to '${target_volid
}' on host '$target_host'";
545 my $sshoptions = "-o 'BatchMode
=yes
'";
546 my $ssh = "/usr/bin/ssh $sshoptions";
548 local $ENV{RSYNC_RSH} = $ssh;
550 # only implemented for file system based storage
554 my $src_plugin = PVE::Storage::Plugin->lookup($scfg->{type});
555 my $dst_plugin = PVE::Storage::Plugin->lookup($tcfg->{type});
556 my $src = $src_plugin->path($scfg, $volname, $storeid);
557 my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid);
559 my $dirname = dirname($dst);
561 if ($tcfg->{shared}) { # we can do a local copy
563 run_command(['/bin/mkdir
', '-p
', $dirname]);
565 run_command(['/bin/cp', $src, $dst]);
568 run_command(['/usr/bin
/ssh
', "root\@${target_host}",
569 '/bin/mkdir
', '-p
', $dirname]);
571 # we use rsync with --sparse, so we can't
use --inplace
,
572 # so we remove file on the target if it already exists to
574 my ($size, $format) = PVE
::Storage
::Plugin
::file_size_info
($src);
575 if ($format && ($format eq 'raw') && $size) {
576 run_command
(['/usr/bin/ssh', "root\@${target_host}",
582 if ($format eq 'subvol') {
583 $cmd = ['/usr/bin/rsync', '--progress', '-X', '-A', '--numeric-ids',
584 '-aH', '--delete', '--no-whole-file', '--inplace',
585 '--one-file-system', "$src/", "[root\@${target_host}]:$dst"];
587 $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
588 $src, "[root\@${target_host}]:$dst"];
593 run_command
($cmd, outfunc
=> sub {
596 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
599 print "rsync status: $1\n";
609 die "$errstr - target type '$tcfg->{type}' not implemented\n";
612 } elsif ($scfg->{type
} eq 'zfspool') {
614 if ($tcfg->{type
} eq 'zfspool') {
616 die "$errstr - pool on target does not have the same name as on source!"
617 if $tcfg->{pool
} ne $scfg->{pool
};
619 my (undef, $volname) = parse_volname
($cfg, $volid);
621 my $zfspath = "$scfg->{pool}\/$volname";
623 my $snap = ['zfs', 'snapshot', "$zfspath\@__migration__"];
624 my $send = ['zfs', 'send', '-Rpv'];
625 my $rec = ['ssh', "root\@$target_host", 'zfs', 'recv','-F' ,$zfspath];
627 if (my $snapname = PVE
::ReplicationTools
::get_last_replica_snap
($volid)) {
629 #check if target snapshot exists.
630 my $checksnap = ['/usr/bin/ssh', "root\@${target_host}", "-o",
631 'BatchMode=yes', 'zfs', 'list', '-Hrt', 'snap',
632 "$zfspath\@$snapname "];
634 run_command
($checksnap);
635 push @$send, '-I', "$zfspath\@$snapname";
639 push @$send, "--", "$zfspath\@__migration__";
641 my $destroy_target = ['ssh', "root\@$target_host", 'zfs', 'destroy', "$zfspath\@__migration__"];
644 run_command
([$send,$rec]);
647 warn "zfs send/receive failed, cleaning up snapshot(s)..\n" if $err;
648 eval { run_command
(['zfs', 'destroy', "$zfspath\@__migration__"]); };
649 warn "could not remove source snapshot: $@\n" if $@;
650 eval { run_command
($destroy_target); };
651 warn "could not remove target snapshot: $@\n" if $@;
655 die "$errstr - target type $tcfg->{type} is not valid\n";
658 } elsif ($scfg->{type
} eq 'lvmthin' || $scfg->{type
} eq 'lvm') {
660 if (($scfg->{type
} eq $tcfg->{type
}) &&
661 ($tcfg->{type
} eq 'lvmthin' || $tcfg->{type
} eq 'lvm')) {
663 my (undef, $volname, $vmid) = parse_volname
($cfg, $volid);
664 my $size = volume_size_info
($cfg, $volid, 5);
665 my $src = path
($cfg, $volid);
666 my $dst = path
($cfg, $target_volid);
668 run_command
(['/usr/bin/ssh', "root\@${target_host}",
669 'pvesm', 'alloc', $target_storeid, $vmid,
670 $target_volname, int($size/1024)]);
673 if ($tcfg->{type
} eq 'lvmthin') {
674 run_command
([["dd", "if=$src", "bs=4k"],["/usr/bin/ssh", "root\@${target_host}",
675 "dd", 'conv=sparse', "of=$dst", "bs=4k"]]);
677 run_command
([["dd", "if=$src", "bs=4k"],["/usr/bin/ssh", "root\@${target_host}",
678 "dd", "of=$dst", "bs=4k"]]);
682 run_command
(['/usr/bin/ssh', "root\@${target_host}",
683 'pvesm', 'free', $target_volid]);
687 die "$errstr - migrate from source type '$scfg->{type}' to '$tcfg->{type}' not implemented\n";
690 die "$errstr - source type '$scfg->{type}' not implemented\n";
695 my ($cfg, $volid, $vmid, $snap) = @_;
697 my ($storeid, $volname) = parse_volume_id
($volid);
699 my $scfg = storage_config
($cfg, $storeid);
701 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
703 activate_storage
($cfg, $storeid);
705 # lock shared storage
706 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
707 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
708 return "$storeid:$volname";
712 sub vdisk_create_base
{
713 my ($cfg, $volid) = @_;
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->create_base($storeid, $scfg, $volname);
726 return "$storeid:$volname";
731 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
733 die "no storage ID specified\n" if !$storeid;
735 PVE
::JSONSchema
::parse_storage_id
($storeid);
737 my $scfg = storage_config
($cfg, $storeid);
739 die "no VMID specified\n" if !$vmid;
741 $vmid = parse_vmid
($vmid);
743 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
745 $fmt = $defformat if !$fmt;
747 activate_storage
($cfg, $storeid);
749 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
751 # lock shared storage
752 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
753 my $old_umask = umask(umask|0037);
754 my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) };
758 return "$storeid:$volname";
763 my ($cfg, $volid) = @_;
765 my ($storeid, $volname) = parse_volume_id
($volid);
766 my $scfg = storage_config
($cfg, $storeid);
767 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
769 activate_storage
($cfg, $storeid);
773 # lock shared storage
774 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
775 # LVM-thin allows deletion of still referenced base volumes!
776 die "base volume '$volname' is still in use by linked clones\n"
777 if &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
779 my (undef, undef, undef, undef, undef, $isBase, $format) =
780 $plugin->parse_volname($volname);
781 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format);
784 return if !$cleanup_worker;
786 my $rpcenv = PVE
::RPCEnvironment
::get
();
787 my $authuser = $rpcenv->get_user();
789 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
792 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
794 my ($cfg, $storeid, $tt) = @_;
796 die "unknown template type '$tt'\n"
797 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
799 my $ids = $cfg->{ids
};
801 storage_check_enabled
($cfg, $storeid) if ($storeid);
807 foreach my $sid (keys %$ids) {
808 next if $storeid && $storeid ne $sid;
810 my $scfg = $ids->{$sid};
811 my $type = $scfg->{type
};
813 next if !storage_check_enabled
($cfg, $sid, undef, 1);
815 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
816 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
817 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
819 activate_storage
($cfg, $sid);
822 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
824 my $path = $plugin->get_subdir($scfg, $tt);
826 foreach my $fn (<$path/*>) {
831 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
833 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
835 } elsif ($tt eq 'vztmpl') {
836 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
838 $info = { volid
=> "$sid:vztmpl/$1", format
=> "t$2" };
840 } elsif ($tt eq 'backup') {
841 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
843 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
846 $info->{size
} = -s
$fn;
848 push @{$res->{$sid}}, $info;
853 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
861 my ($cfg, $storeid, $vmid, $vollist) = @_;
863 my $ids = $cfg->{ids
};
865 storage_check_enabled
($cfg, $storeid) if ($storeid);
869 # prepare/activate/refresh all storages
871 my $storage_list = [];
873 foreach my $volid (@$vollist) {
874 my ($sid, undef) = parse_volume_id
($volid);
875 next if !defined($ids->{$sid});
876 next if !storage_check_enabled
($cfg, $sid, undef, 1);
877 push @$storage_list, $sid;
880 foreach my $sid (keys %$ids) {
881 next if $storeid && $storeid ne $sid;
882 next if !storage_check_enabled
($cfg, $sid, undef, 1);
883 push @$storage_list, $sid;
889 activate_storage_list
($cfg, $storage_list, $cache);
891 foreach my $sid (keys %$ids) {
892 next if $storeid && $storeid ne $sid;
893 next if !storage_check_enabled
($cfg, $sid, undef, 1);
895 my $scfg = $ids->{$sid};
896 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
897 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
898 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
905 my ($cfg, $storeid, $vmid, $content) = @_;
907 my @ctypes = qw(images vztmpl iso backup);
909 my $cts = $content ?
[ $content ] : [ @ctypes ];
911 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
914 foreach my $ct (@$cts) {
916 if ($ct eq 'images') {
917 $data = vdisk_list
($cfg, $storeid, $vmid);
918 } elsif ($ct eq 'iso' && !defined($vmid)) {
919 $data = template_list
($cfg, $storeid, 'iso');
920 } elsif ($ct eq 'vztmpl'&& !defined($vmid)) {
921 $data = template_list
($cfg, $storeid, 'vztmpl');
922 } elsif ($ct eq 'backup') {
923 $data = template_list
($cfg, $storeid, 'backup');
924 foreach my $item (@{$data->{$storeid}}) {
925 if (defined($vmid)) {
926 @{$data->{$storeid}} = grep { $_->{volid
} =~ m/\S+-$vmid-\S+/ } @{$data->{$storeid}};
931 next if !$data || !$data->{$storeid};
933 foreach my $item (@{$data->{$storeid}}) {
934 $item->{content
} = $ct;
944 my $filename = "/sys/kernel/uevent_seqnum";
947 if (my $fh = IO
::File-
>new($filename, "r")) {
949 if ($line =~ m/^(\d+)$/) {
957 sub activate_storage
{
958 my ($cfg, $storeid, $cache) = @_;
960 $cache = {} if !$cache;
962 my $scfg = storage_check_enabled
($cfg, $storeid);
964 return if $cache->{activated
}->{$storeid};
966 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
968 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
971 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
972 activate_storage
($cfg, $baseid, $cache);
975 if (!$plugin->check_connection($storeid, $scfg)) {
976 die "storage '$storeid' is not online\n";
979 $plugin->activate_storage($storeid, $scfg, $cache);
981 my $newseq = uevent_seqnum
();
983 # only call udevsettle if there are events
984 if ($newseq > $cache->{uevent_seqnum
}) {
986 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
987 $cache->{uevent_seqnum
} = $newseq;
990 $cache->{activated
}->{$storeid} = 1;
993 sub activate_storage_list
{
994 my ($cfg, $storeid_list, $cache) = @_;
996 $cache = {} if !$cache;
998 foreach my $storeid (@$storeid_list) {
999 activate_storage
($cfg, $storeid, $cache);
1003 sub deactivate_storage
{
1004 my ($cfg, $storeid) = @_;
1006 my $scfg = storage_config
($cfg, $storeid);
1007 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1010 $plugin->deactivate_storage($storeid, $scfg, $cache);
1013 sub activate_volumes
{
1014 my ($cfg, $vollist, $snapname) = @_;
1016 return if !($vollist && scalar(@$vollist));
1018 my $storagehash = {};
1019 foreach my $volid (@$vollist) {
1020 my ($storeid, undef) = parse_volume_id
($volid);
1021 $storagehash->{$storeid} = 1;
1026 activate_storage_list
($cfg, [keys %$storagehash], $cache);
1028 foreach my $volid (@$vollist) {
1029 my ($storeid, $volname) = parse_volume_id
($volid);
1030 my $scfg = storage_config
($cfg, $storeid);
1031 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1032 $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache);
1036 sub deactivate_volumes
{
1037 my ($cfg, $vollist, $snapname) = @_;
1039 return if !($vollist && scalar(@$vollist));
1044 foreach my $volid (@$vollist) {
1045 my ($storeid, $volname) = parse_volume_id
($volid);
1047 my $scfg = storage_config
($cfg, $storeid);
1048 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1051 $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache);
1055 push @errlist, $volid;
1059 die "volume deactivation failed: " . join(' ', @errlist)
1060 if scalar(@errlist);
1064 my ($cfg, $content) = @_;
1066 my $ids = $cfg->{ids
};
1070 my @ctypes = PVE
::Tools
::split_list
($content);
1073 foreach my $storeid (keys %$ids) {
1075 next if !storage_check_enabled
($cfg, $storeid, undef, 1);
1077 if (defined($content)) {
1079 foreach my $ctype (@ctypes) {
1080 if ($ids->{$storeid}->{content
}->{$ctype}) {
1085 next if !$want_ctype;
1088 my $type = $ids->{$storeid}->{type
};
1090 $info->{$storeid} = {
1095 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
1096 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
1100 push @$slist, $storeid;
1105 foreach my $storeid (keys %$ids) {
1106 my $scfg = $ids->{$storeid};
1107 next if !$info->{$storeid};
1109 eval { activate_storage
($cfg, $storeid, $cache); };
1115 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1116 my ($total, $avail, $used, $active);
1117 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
1120 $info->{$storeid}->{total
} = int($total);
1121 $info->{$storeid}->{avail
} = int($avail);
1122 $info->{$storeid}->{used
} = int($used);
1123 $info->{$storeid}->{active
} = $active;
1132 my ($packed_ip, $family);
1134 my @res = PVE
::Tools
::getaddrinfo_all
($server);
1135 $family = $res[0]->{family
};
1136 $packed_ip = (PVE
::Tools
::unpack_sockaddr_in46
($res[0]->{addr
}))[2];
1138 if (defined $packed_ip) {
1139 return Socket
::inet_ntop
($family, $packed_ip);
1145 my ($server_in) = @_;
1148 if (!($server = resolv_server
($server_in))) {
1149 die "unable to resolve address for server '${server_in}'\n";
1152 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
1155 run_command
($cmd, outfunc
=> sub {
1158 # note: howto handle white spaces in export path??
1159 if ($line =~ m!^(/\S+)\s+(.+)$!) {
1169 my $cmd = ['zfs', 'list', '-t', 'filesystem', '-H', '-o', 'name,avail,used'];
1172 run_command
($cmd, outfunc
=> sub {
1175 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
1176 my ($pool, $size_str, $used_str) = ($1, $2, $3);
1177 my $size = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($size_str);
1178 my $used = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($used_str);
1179 # ignore subvolumes generated by our ZFSPoolPlugin
1180 return if $pool =~ m!/subvol-\d+-[^/]+$!;
1181 return if $pool =~ m!/basevol-\d+-[^/]+$!;
1182 push @$res, { pool
=> $pool, size
=> $size, free
=> $size-$used };
1190 my ($portal, $noerr) = @_;
1192 my ($server, $port) = PVE
::Tools
::parse_host_and_port
($portal);
1194 if (my $ip = resolv_server
($server)) {
1196 $server = "[$server]" if $server =~ /^$IPV6RE$/;
1197 return $port ?
"$server:$port" : $server;
1200 return undef if $noerr;
1202 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
1205 # idea is from usbutils package (/usr/bin/usb-devices) script
1206 sub __scan_usb_device
{
1207 my ($res, $devpath, $parent, $level) = @_;
1209 return if ! -d
$devpath;
1210 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
1211 my $port = $level ?
int($1 - 1) : 0;
1213 my $busnum = int(file_read_firstline
("$devpath/busnum"));
1214 my $devnum = int(file_read_firstline
("$devpath/devnum"));
1221 speed
=> file_read_firstline
("$devpath/speed"),
1222 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
1223 vendid
=> file_read_firstline
("$devpath/idVendor"),
1224 prodid
=> file_read_firstline
("$devpath/idProduct"),
1228 my $usbpath = $devpath;
1229 $usbpath =~ s
|^.*/\d
+\
-||;
1230 $d->{usbpath
} = $usbpath;
1233 my $product = file_read_firstline
("$devpath/product");
1234 $d->{product
} = $product if $product;
1236 my $manu = file_read_firstline
("$devpath/manufacturer");
1237 $d->{manufacturer
} = $manu if $manu;
1239 my $serial => file_read_firstline
("$devpath/serial");
1240 $d->{serial
} = $serial if $serial;
1244 foreach my $subdev (<$devpath/$busnum-*>) {
1245 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
1246 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
1255 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
1256 __scan_usb_device
($devlist, $device, 0, 0);
1263 my ($portal_in) = @_;
1266 if (!($portal = resolv_portal
($portal_in))) {
1267 die "unable to parse/resolve portal address '${portal_in}'\n";
1270 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
1273 sub storage_default_format
{
1274 my ($cfg, $storeid) = @_;
1276 my $scfg = storage_config
($cfg, $storeid);
1278 return PVE
::Storage
::Plugin
::default_format
($scfg);
1281 sub vgroup_is_used
{
1282 my ($cfg, $vgname) = @_;
1284 foreach my $storeid (keys %{$cfg->{ids
}}) {
1285 my $scfg = storage_config
($cfg, $storeid);
1286 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
1294 sub target_is_used
{
1295 my ($cfg, $target) = @_;
1297 foreach my $storeid (keys %{$cfg->{ids
}}) {
1298 my $scfg = storage_config
($cfg, $storeid);
1299 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
1307 sub volume_is_used
{
1308 my ($cfg, $volid) = @_;
1310 foreach my $storeid (keys %{$cfg->{ids
}}) {
1311 my $scfg = storage_config
($cfg, $storeid);
1312 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
1320 sub storage_is_used
{
1321 my ($cfg, $storeid) = @_;
1323 foreach my $sid (keys %{$cfg->{ids
}}) {
1324 my $scfg = storage_config
($cfg, $sid);
1325 next if !$scfg->{base
};
1326 my ($st) = parse_volume_id
($scfg->{base
});
1327 return 1 if $st && $st eq $storeid;
1334 my ($list, $func) = @_;
1338 foreach my $sid (keys %$list) {
1339 foreach my $info (@{$list->{$sid}}) {
1340 my $volid = $info->{volid
};
1341 my ($sid1, $volname) = parse_volume_id
($volid, 1);
1342 if ($sid1 && $sid1 eq $sid) {
1343 &$func ($volid, $sid, $info);
1345 warn "detected strange volid '$volid' in volume list for '$sid'\n";
1351 sub extract_vzdump_config_tar
{
1352 my ($archive, $conf_re) = @_;
1354 die "ERROR: file '$archive' does not exist\n" if ! -f
$archive;
1356 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
1357 die "unable to open file '$archive'\n";
1360 while (defined($file = <$fh>)) {
1361 if ($file =~ $conf_re) {
1362 $file = $1; # untaint
1371 die "ERROR: archive contains no configuration file\n" if !$file;
1377 $raw .= "$output\n";
1380 PVE
::Tools
::run_command
(['tar', '-xpOf', $archive, $file, '--occurrence'], outfunc
=> $out);
1382 return wantarray ?
($raw, $file) : $raw;
1385 sub extract_vzdump_config_vma
{
1386 my ($archive, $comp) = @_;
1392 $raw .= "$output\n";
1398 if ($comp eq 'gz') {
1399 $uncomp = ["zcat", $archive];
1400 } elsif ($comp eq 'lzo') {
1401 $uncomp = ["lzop", "-d", "-c", $archive];
1403 die "unknown compression method '$comp'\n";
1405 $cmd = [$uncomp, ["vma", "config", "-"]];
1407 # in some cases, lzop/zcat exits with 1 when its stdout pipe is
1408 # closed early by vma, detect this and ignore the exit code later
1413 if ($output =~ m/lzop: Broken pipe: <stdout>/ || $output =~ m/gzip: stdout: Broken pipe/) {
1415 } elsif (!defined ($errstring) && $output !~ m/^\s*$/) {
1416 $errstring = "Failed to extract config from VMA archive: $output\n";
1420 # in other cases, the pipeline will exit with exit code 141
1421 # because of the broken pipe, handle / ignore this as well
1424 $rc = PVE
::Tools
::run_command
($cmd, outfunc
=> $out, errfunc
=> $err, noerr
=> 1);
1428 # use exit code if no stderr output and not just broken pipe
1429 if (!$errstring && !$broken_pipe && $rc != 0 && $rc != 141) {
1430 die "$rerr\n" if $rerr;
1431 die "config extraction failed with exit code $rc\n";
1433 die "$errstring\n" if $errstring;
1435 # simple case without compression and weird piping behaviour
1436 PVE
::Tools
::run_command
(["vma", "config", $archive], outfunc
=> $out);
1439 return wantarray ?
($raw, undef) : $raw;
1442 sub extract_vzdump_config
{
1443 my ($cfg, $volid) = @_;
1445 my $archive = abs_filesystem_path
($cfg, $volid);
1447 if ($volid =~ /vzdump-(lxc|openvz)-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|(tar(\.(gz|lzo))?))$/) {
1448 return extract_vzdump_config_tar
($archive, qr!^(\./etc/vzdump/(pct|vps)\.conf)$!);
1449 } elsif ($volid =~ /vzdump-qemu-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|((tar|vma)(\.(gz|lzo))?))$/) {
1457 $comp = $11 if defined($11);
1460 if ($format eq 'tar') {
1461 return extract_vzdump_config_tar
($archive, qr!\(\./qemu-server\.conf\)!);
1463 return extract_vzdump_config_vma
($archive, $comp);
1466 die "cannot determine backup guest type for backup archive '$volid'\n";
1470 # bash completion helper
1472 sub complete_storage
{
1473 my ($cmdname, $pname, $cvalue) = @_;
1475 my $cfg = PVE
::Storage
::config
();
1477 return $cmdname eq 'add' ?
[] : [ PVE
::Storage
::storage_ids
($cfg) ];
1480 sub complete_storage_enabled
{
1481 my ($cmdname, $pname, $cvalue) = @_;
1485 my $cfg = PVE
::Storage
::config
();
1486 foreach my $sid (keys %{$cfg->{ids
}}) {
1487 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1493 sub complete_content_type
{
1494 my ($cmdname, $pname, $cvalue) = @_;
1496 return [qw(rootdir images vztmpl iso backup)];
1499 sub complete_volume
{
1500 my ($cmdname, $pname, $cvalue) = @_;
1504 my $storage_list = complete_storage_enabled
();
1506 if ($cvalue =~ m/^([^:]+):/) {
1507 $storage_list = [ $1 ];
1509 if (scalar(@$storage_list) > 1) {
1510 # only list storage IDs to avoid large listings
1512 foreach my $storeid (@$storage_list) {
1513 # Hack: simply return 2 artificial values, so that
1514 # completions does not finish
1515 push @$res, "$storeid:volname", "$storeid:...";
1522 foreach my $storeid (@$storage_list) {
1523 my $vollist = PVE
::Storage
::volume_list
($cfg, $storeid);
1525 foreach my $item (@$vollist) {
1526 push @$res, $item->{volid
};