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";
220 my ($cfg, $volid, $snap, $ip, $incremental_snap, $verbose, $limit,
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 return $plugin->volume_send($scfg, $storeid, $volname, $ip, $snap,
228 $incremental_snap, $verbose, $limit, $target_path);
230 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
231 die "send file
/device
'$volid' is not possible
\n";
233 die "unable to parse volume ID
'$volid'\n";
237 sub volume_snapshot_rollback {
238 my ($cfg, $volid, $snap) = @_;
240 my ($storeid, $volname) = parse_volume_id($volid, 1);
242 my $scfg = storage_config($cfg, $storeid);
243 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
244 $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
245 return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap);
246 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
247 die "snapshot rollback file
/device
'$volid' is not possible
\n";
249 die "unable to parse volume ID
'$volid'\n";
253 sub volume_snapshot_delete {
254 my ($cfg, $volid, $snap, $running) = @_;
256 my ($storeid, $volname) = parse_volume_id($volid, 1);
258 my $scfg = storage_config($cfg, $storeid);
259 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
260 return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running);
261 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
262 die "snapshot
delete file
/device
'$volid' is not possible
\n";
264 die "unable to parse volume ID
'$volid'\n";
268 sub volume_has_feature {
269 my ($cfg, $feature, $volid, $snap, $running) = @_;
271 my ($storeid, $volname) = parse_volume_id($volid, 1);
273 my $scfg = storage_config($cfg, $storeid);
274 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
275 return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running);
276 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
283 sub volume_snapshot_list {
284 my ($cfg, $volid, $prefix, $ip) = @_;
286 my ($storeid, $volname) = parse_volume_id($volid, 1);
288 my $scfg = storage_config($cfg, $storeid);
289 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
290 return $plugin->volume_snapshot_list($scfg, $storeid, $volname, $prefix, $ip);
291 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
292 die "send file
/device
'$volid' is not possible
\n";
294 die "unable to parse volume ID
'$volid'\n";
296 # return an empty array if dataset does not exist.
297 # youngest snap first
301 my ($cfg, $storeid, $vmid) = @_;
303 my $scfg = storage_config($cfg, $storeid);
304 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
306 my $path = $plugin->get_subdir($scfg, 'images');
308 return $vmid ? "$path/$vmid" : $path;
311 sub get_private_dir {
312 my ($cfg, $storeid, $vmid) = @_;
314 my $scfg = storage_config($cfg, $storeid);
315 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
317 my $path = $plugin->get_subdir($scfg, 'rootdir');
319 return $vmid ? "$path/$vmid" : $path;
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, 'iso');
332 my ($cfg, $storeid) = @_;
334 my $scfg = storage_config($cfg, $storeid);
335 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
337 return $plugin->get_subdir($scfg, 'vztmpl');
341 my ($cfg, $storeid) = @_;
343 my $scfg = storage_config($cfg, $storeid);
344 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
346 return $plugin->get_subdir($scfg, 'backup');
349 # library implementation
354 die "VMID
'$vmid' contains illegal characters
\n" if $vmid !~ m/^\d+$/;
359 # NOTE: basename and basevmid are always undef for LVM-thin, where the
360 # clone -> base reference is not encoded in the volume ID.
361 # see note in PVE::Storage::LvmThinPlugin for details.
363 my ($cfg, $volid) = @_;
365 my ($storeid, $volname) = parse_volume_id($volid);
367 my $scfg = storage_config($cfg, $storeid);
369 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
371 # returns ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format)
373 return $plugin->parse_volname($volname);
376 sub parse_volume_id {
377 my ($volid, $noerr) = @_;
379 return PVE::Storage::Plugin::parse_volume_id($volid, $noerr);
382 # test if we have read access to volid
383 sub check_volume_access {
384 my ($rpcenv, $user, $cfg, $vmid, $volid) = @_;
386 my ($sid, $volname) = parse_volume_id($volid, 1);
388 my ($vtype, undef, $ownervm) = parse_volname($cfg, $volid);
389 if ($vtype eq 'iso' || $vtype eq 'vztmpl') {
390 # we simply allow access
391 } elsif (defined($ownervm) && defined($vmid) && ($ownervm == $vmid)) {
392 # we are owner - allow access
393 } elsif ($vtype eq 'backup' && $ownervm) {
394 $rpcenv->check($user, "/storage/$sid", ['Datastore.AllocateSpace']);
395 $rpcenv->check($user, "/vms/$ownervm", ['VM.Backup']);
397 # allow if we are Datastore administrator
398 $rpcenv->check($user, "/storage/$sid", ['Datastore.Allocate']);
401 die "Only root can pass arbitrary filesystem paths
."
402 if $user ne 'root@pam';
408 my $volume_is_base_and_used__no_lock = sub {
409 my ($scfg, $storeid, $plugin, $volname) = @_;
411 my ($vtype, $name, $vmid, undef, undef, $isBase, undef) =
412 $plugin->parse_volname($volname);
415 my $vollist = $plugin->list_images($storeid, $scfg);
416 foreach my $info (@$vollist) {
417 my (undef, $tmpvolname) = parse_volume_id($info->{volid});
418 my $basename = undef;
419 my $basevmid = undef;
422 (undef, undef, undef, $basename, $basevmid) =
423 $plugin->parse_volname($tmpvolname);
426 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
434 # NOTE: this check does not work for LVM-thin, where the clone -> base
435 # reference is not encoded in the volume ID.
436 # see note in PVE::Storage::LvmThinPlugin for details.
437 sub volume_is_base_and_used {
438 my ($cfg, $volid) = @_;
440 my ($storeid, $volname) = parse_volume_id($volid);
441 my $scfg = storage_config($cfg, $storeid);
442 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
444 $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
445 return &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
449 # try to map a filesystem path to a volume identifier
450 sub path_to_volume_id {
451 my ($cfg, $path) = @_;
453 my $ids = $cfg->{ids};
455 my ($sid, $volname) = parse_volume_id($path, 1);
457 if (my $scfg = $ids->{$sid}) {
459 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
460 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
461 return ($vtype, $path);
467 # Note: abs_path() return undef if $path doesn not exist
468 # for example when nfs storage is not mounted
469 $path = abs_path($path) || $path;
471 foreach my $sid (keys %$ids) {
472 my $scfg = $ids->{$sid};
473 next if !$scfg->{path};
474 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
475 my $imagedir = $plugin->get_subdir($scfg, 'images');
476 my $isodir = $plugin->get_subdir($scfg, 'iso');
477 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
478 my $backupdir = $plugin->get_subdir($scfg, 'backup');
479 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
481 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
485 my $vollist = $plugin->list_images($sid, $scfg, $vmid);
486 foreach my $info (@$vollist) {
487 my ($storeid, $volname) = parse_volume_id($info->{volid});
488 my $volpath = $plugin->path($scfg, $volname, $storeid);
489 if ($volpath eq $path) {
490 return ('images', $info->{volid});
493 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
495 return ('iso', "$sid:iso
/$name");
496 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
498 return ('vztmpl', "$sid:vztmpl
/$name");
499 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
501 return ('rootdir', "$sid:rootdir
/$vmid");
502 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!) {
504 return ('iso', "$sid:backup
/$name");
508 # can't map path to volume id
513 my ($cfg, $volid, $snapname) = @_;
515 my ($storeid, $volname) = parse_volume_id($volid);
517 my $scfg = storage_config($cfg, $storeid);
519 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
520 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname);
521 return wantarray ? ($path, $owner, $vtype) : $path;
524 sub abs_filesystem_path {
525 my ($cfg, $volid) = @_;
528 if (PVE::Storage::parse_volume_id ($volid, 1)) {
529 PVE::Storage::activate_volumes($cfg, [ $volid ]);
530 $path = PVE::Storage::path($cfg, $volid);
533 my $abspath = abs_path($volid);
534 if ($abspath && $abspath =~ m|^(/.+)$|) {
535 $path = $1; # untaint any path
540 die "can
't find file '$volid'\n" if !($path && -f $path);
545 sub storage_migrate {
546 my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
548 my ($storeid, $volname) = parse_volume_id($volid);
549 $target_volname = $volname if !$target_volname;
551 my $scfg = storage_config($cfg, $storeid);
553 # no need to migrate shared content
554 return if $storeid eq $target_storeid && $scfg->{shared};
556 my $tcfg = storage_config($cfg, $target_storeid);
558 my $target_volid = "${target_storeid}:${target_volname}";
560 my $errstr = "unable to migrate '$volid' to '${target_volid
}' on host '$target_host'";
562 my $sshoptions = "-o 'BatchMode
=yes
'";
563 my $ssh = "/usr/bin/ssh $sshoptions";
565 local $ENV{RSYNC_RSH} = $ssh;
567 # only implemented for file system based storage
571 my $src_plugin = PVE::Storage::Plugin->lookup($scfg->{type});
572 my $dst_plugin = PVE::Storage::Plugin->lookup($tcfg->{type});
573 my $src = $src_plugin->path($scfg, $volname, $storeid);
574 my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid);
576 my $dirname = dirname($dst);
578 if ($tcfg->{shared}) { # we can do a local copy
580 run_command(['/bin/mkdir
', '-p
', $dirname]);
582 run_command(['/bin/cp', $src, $dst]);
585 run_command(['/usr/bin
/ssh
', "root\@${target_host}",
586 '/bin/mkdir
', '-p
', $dirname]);
588 # we use rsync with --sparse, so we can't
use --inplace
,
589 # so we remove file on the target if it already exists to
591 my ($size, $format) = PVE
::Storage
::Plugin
::file_size_info
($src);
592 if ($format && ($format eq 'raw') && $size) {
593 run_command
(['/usr/bin/ssh', "root\@${target_host}",
599 if ($format eq 'subvol') {
600 $cmd = ['/usr/bin/rsync', '--progress', '-X', '-A', '--numeric-ids',
601 '-aH', '--delete', '--no-whole-file', '--inplace',
602 '--one-file-system', "$src/", "[root\@${target_host}]:$dst"];
604 $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
605 $src, "[root\@${target_host}]:$dst"];
610 run_command
($cmd, outfunc
=> sub {
613 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
616 print "rsync status: $1\n";
626 die "$errstr - target type '$tcfg->{type}' not implemented\n";
629 } elsif ($scfg->{type
} eq 'zfspool') {
631 if ($tcfg->{type
} eq 'zfspool') {
633 die "$errstr - pool on target does not have the same name as on source!"
634 if $tcfg->{pool
} ne $scfg->{pool
};
636 my (undef, $volname) = parse_volname
($cfg, $volid);
638 my $zfspath = "$scfg->{pool}\/$volname";
640 my $snap = ['zfs', 'snapshot', "$zfspath\@__migration__"];
642 my $send = [['zfs', 'send', '-Rpv', "$zfspath\@__migration__"], ['ssh', "root\@$target_host",
643 'zfs', 'recv', $zfspath]];
645 my $destroy_target = ['ssh', "root\@$target_host", 'zfs', 'destroy', "$zfspath\@__migration__"];
651 warn "zfs send/receive failed, cleaning up snapshot(s)..\n" if $err;
652 eval { run_command
(['zfs', 'destroy', "$zfspath\@__migration__"]); };
653 warn "could not remove source snapshot: $@\n" if $@;
654 eval { run_command
($destroy_target); };
655 warn "could not remove target snapshot: $@\n" if $@;
659 die "$errstr - target type $tcfg->{type} is not valid\n";
662 } elsif ($scfg->{type
} eq 'lvmthin' || $scfg->{type
} eq 'lvm') {
664 if (($scfg->{type
} eq $tcfg->{type
}) &&
665 ($tcfg->{type
} eq 'lvmthin' || $tcfg->{type
} eq 'lvm')) {
667 my (undef, $volname, $vmid) = parse_volname
($cfg, $volid);
668 my $size = volume_size_info
($cfg, $volid, 5);
669 my $src = path
($cfg, $volid);
670 my $dst = path
($cfg, $target_volid);
672 run_command
(['/usr/bin/ssh', "root\@${target_host}",
673 'pvesm', 'alloc', $target_storeid, $vmid,
674 $target_volname, int($size/1024)]);
677 if ($tcfg->{type
} eq 'lvmthin') {
678 run_command
([["dd", "if=$src", "bs=4k"],["/usr/bin/ssh", "root\@${target_host}",
679 "dd", 'conv=sparse', "of=$dst", "bs=4k"]]);
681 run_command
([["dd", "if=$src", "bs=4k"],["/usr/bin/ssh", "root\@${target_host}",
682 "dd", "of=$dst", "bs=4k"]]);
686 run_command
(['/usr/bin/ssh', "root\@${target_host}",
687 'pvesm', 'free', $target_volid]);
691 die "$errstr - migrate from source type '$scfg->{type}' to '$tcfg->{type}' not implemented\n";
694 die "$errstr - source type '$scfg->{type}' not implemented\n";
699 my ($cfg, $volid, $vmid, $snap) = @_;
701 my ($storeid, $volname) = parse_volume_id
($volid);
703 my $scfg = storage_config
($cfg, $storeid);
705 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
707 activate_storage
($cfg, $storeid);
709 # lock shared storage
710 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
711 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
712 return "$storeid:$volname";
716 sub vdisk_create_base
{
717 my ($cfg, $volid) = @_;
719 my ($storeid, $volname) = parse_volume_id
($volid);
721 my $scfg = storage_config
($cfg, $storeid);
723 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
725 activate_storage
($cfg, $storeid);
727 # lock shared storage
728 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
729 my $volname = $plugin->create_base($storeid, $scfg, $volname);
730 return "$storeid:$volname";
735 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
737 die "no storage ID specified\n" if !$storeid;
739 PVE
::JSONSchema
::parse_storage_id
($storeid);
741 my $scfg = storage_config
($cfg, $storeid);
743 die "no VMID specified\n" if !$vmid;
745 $vmid = parse_vmid
($vmid);
747 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
749 $fmt = $defformat if !$fmt;
751 activate_storage
($cfg, $storeid);
753 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
755 # lock shared storage
756 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
757 my $old_umask = umask(umask|0037);
758 my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) };
762 return "$storeid:$volname";
767 my ($cfg, $volid) = @_;
769 my ($storeid, $volname) = parse_volume_id
($volid);
770 my $scfg = storage_config
($cfg, $storeid);
771 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
773 activate_storage
($cfg, $storeid);
777 # lock shared storage
778 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
779 # LVM-thin allows deletion of still referenced base volumes!
780 die "base volume '$volname' is still in use by linked clones\n"
781 if &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
783 my (undef, undef, undef, undef, undef, $isBase, $format) =
784 $plugin->parse_volname($volname);
785 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format);
788 return if !$cleanup_worker;
790 my $rpcenv = PVE
::RPCEnvironment
::get
();
791 my $authuser = $rpcenv->get_user();
793 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
796 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
798 my ($cfg, $storeid, $tt) = @_;
800 die "unknown template type '$tt'\n"
801 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
803 my $ids = $cfg->{ids
};
805 storage_check_enabled
($cfg, $storeid) if ($storeid);
811 foreach my $sid (keys %$ids) {
812 next if $storeid && $storeid ne $sid;
814 my $scfg = $ids->{$sid};
815 my $type = $scfg->{type
};
817 next if !storage_check_enabled
($cfg, $sid, undef, 1);
819 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
820 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
821 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
823 activate_storage
($cfg, $sid);
826 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
828 my $path = $plugin->get_subdir($scfg, $tt);
830 foreach my $fn (<$path/*>) {
835 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
837 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
839 } elsif ($tt eq 'vztmpl') {
840 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
842 $info = { volid
=> "$sid:vztmpl/$1", format
=> "t$2" };
844 } elsif ($tt eq 'backup') {
845 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
847 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
850 $info->{size
} = -s
$fn;
852 push @{$res->{$sid}}, $info;
857 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
865 my ($cfg, $storeid, $vmid, $vollist) = @_;
867 my $ids = $cfg->{ids
};
869 storage_check_enabled
($cfg, $storeid) if ($storeid);
873 # prepare/activate/refresh all storages
875 my $storage_list = [];
877 foreach my $volid (@$vollist) {
878 my ($sid, undef) = parse_volume_id
($volid);
879 next if !defined($ids->{$sid});
880 next if !storage_check_enabled
($cfg, $sid, undef, 1);
881 push @$storage_list, $sid;
884 foreach my $sid (keys %$ids) {
885 next if $storeid && $storeid ne $sid;
886 next if !storage_check_enabled
($cfg, $sid, undef, 1);
887 push @$storage_list, $sid;
893 activate_storage_list
($cfg, $storage_list, $cache);
895 foreach my $sid (keys %$ids) {
896 next if $storeid && $storeid ne $sid;
897 next if !storage_check_enabled
($cfg, $sid, undef, 1);
899 my $scfg = $ids->{$sid};
900 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
901 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
902 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
909 my ($cfg, $storeid, $vmid, $content) = @_;
911 my @ctypes = qw(images vztmpl iso backup);
913 my $cts = $content ?
[ $content ] : [ @ctypes ];
915 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
918 foreach my $ct (@$cts) {
920 if ($ct eq 'images') {
921 $data = vdisk_list
($cfg, $storeid, $vmid);
922 } elsif ($ct eq 'iso' && !defined($vmid)) {
923 $data = template_list
($cfg, $storeid, 'iso');
924 } elsif ($ct eq 'vztmpl'&& !defined($vmid)) {
925 $data = template_list
($cfg, $storeid, 'vztmpl');
926 } elsif ($ct eq 'backup') {
927 $data = template_list
($cfg, $storeid, 'backup');
928 foreach my $item (@{$data->{$storeid}}) {
929 if (defined($vmid)) {
930 @{$data->{$storeid}} = grep { $_->{volid
} =~ m/\S+-$vmid-\S+/ } @{$data->{$storeid}};
935 next if !$data || !$data->{$storeid};
937 foreach my $item (@{$data->{$storeid}}) {
938 $item->{content
} = $ct;
948 my $filename = "/sys/kernel/uevent_seqnum";
951 if (my $fh = IO
::File-
>new($filename, "r")) {
953 if ($line =~ m/^(\d+)$/) {
961 sub activate_storage
{
962 my ($cfg, $storeid, $cache) = @_;
964 $cache = {} if !$cache;
966 my $scfg = storage_check_enabled
($cfg, $storeid);
968 return if $cache->{activated
}->{$storeid};
970 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
972 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
975 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
976 activate_storage
($cfg, $baseid, $cache);
979 if (!$plugin->check_connection($storeid, $scfg)) {
980 die "storage '$storeid' is not online\n";
983 $plugin->activate_storage($storeid, $scfg, $cache);
985 my $newseq = uevent_seqnum
();
987 # only call udevsettle if there are events
988 if ($newseq > $cache->{uevent_seqnum
}) {
990 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
991 $cache->{uevent_seqnum
} = $newseq;
994 $cache->{activated
}->{$storeid} = 1;
997 sub activate_storage_list
{
998 my ($cfg, $storeid_list, $cache) = @_;
1000 $cache = {} if !$cache;
1002 foreach my $storeid (@$storeid_list) {
1003 activate_storage
($cfg, $storeid, $cache);
1007 sub deactivate_storage
{
1008 my ($cfg, $storeid) = @_;
1010 my $scfg = storage_config
($cfg, $storeid);
1011 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1014 $plugin->deactivate_storage($storeid, $scfg, $cache);
1017 sub activate_volumes
{
1018 my ($cfg, $vollist, $snapname) = @_;
1020 return if !($vollist && scalar(@$vollist));
1022 my $storagehash = {};
1023 foreach my $volid (@$vollist) {
1024 my ($storeid, undef) = parse_volume_id
($volid);
1025 $storagehash->{$storeid} = 1;
1030 activate_storage_list
($cfg, [keys %$storagehash], $cache);
1032 foreach my $volid (@$vollist) {
1033 my ($storeid, $volname) = parse_volume_id
($volid);
1034 my $scfg = storage_config
($cfg, $storeid);
1035 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1036 $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache);
1040 sub deactivate_volumes
{
1041 my ($cfg, $vollist, $snapname) = @_;
1043 return if !($vollist && scalar(@$vollist));
1048 foreach my $volid (@$vollist) {
1049 my ($storeid, $volname) = parse_volume_id
($volid);
1051 my $scfg = storage_config
($cfg, $storeid);
1052 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1055 $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache);
1059 push @errlist, $volid;
1063 die "volume deactivation failed: " . join(' ', @errlist)
1064 if scalar(@errlist);
1068 my ($cfg, $content) = @_;
1070 my $ids = $cfg->{ids
};
1074 my @ctypes = PVE
::Tools
::split_list
($content);
1077 foreach my $storeid (keys %$ids) {
1079 next if !storage_check_enabled
($cfg, $storeid, undef, 1);
1081 if (defined($content)) {
1083 foreach my $ctype (@ctypes) {
1084 if ($ids->{$storeid}->{content
}->{$ctype}) {
1089 next if !$want_ctype;
1092 my $type = $ids->{$storeid}->{type
};
1094 $info->{$storeid} = {
1099 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
1100 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
1104 push @$slist, $storeid;
1109 foreach my $storeid (keys %$ids) {
1110 my $scfg = $ids->{$storeid};
1111 next if !$info->{$storeid};
1113 eval { activate_storage
($cfg, $storeid, $cache); };
1119 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1120 my ($total, $avail, $used, $active);
1121 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
1124 $info->{$storeid}->{total
} = int($total);
1125 $info->{$storeid}->{avail
} = int($avail);
1126 $info->{$storeid}->{used
} = int($used);
1127 $info->{$storeid}->{active
} = $active;
1136 my ($packed_ip, $family);
1138 my @res = PVE
::Tools
::getaddrinfo_all
($server);
1139 $family = $res[0]->{family
};
1140 $packed_ip = (PVE
::Tools
::unpack_sockaddr_in46
($res[0]->{addr
}))[2];
1142 if (defined $packed_ip) {
1143 return Socket
::inet_ntop
($family, $packed_ip);
1149 my ($server_in) = @_;
1152 if (!($server = resolv_server
($server_in))) {
1153 die "unable to resolve address for server '${server_in}'\n";
1156 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
1159 run_command
($cmd, outfunc
=> sub {
1162 # note: howto handle white spaces in export path??
1163 if ($line =~ m!^(/\S+)\s+(.+)$!) {
1173 my $cmd = ['zfs', 'list', '-t', 'filesystem', '-H', '-o', 'name,avail,used'];
1176 run_command
($cmd, outfunc
=> sub {
1179 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
1180 my ($pool, $size_str, $used_str) = ($1, $2, $3);
1181 my $size = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($size_str);
1182 my $used = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($used_str);
1183 # ignore subvolumes generated by our ZFSPoolPlugin
1184 return if $pool =~ m!/subvol-\d+-[^/]+$!;
1185 return if $pool =~ m!/basevol-\d+-[^/]+$!;
1186 push @$res, { pool
=> $pool, size
=> $size, free
=> $size-$used };
1194 my ($portal, $noerr) = @_;
1196 my ($server, $port) = PVE
::Tools
::parse_host_and_port
($portal);
1198 if (my $ip = resolv_server
($server)) {
1200 $server = "[$server]" if $server =~ /^$IPV6RE$/;
1201 return $port ?
"$server:$port" : $server;
1204 return undef if $noerr;
1206 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
1209 # idea is from usbutils package (/usr/bin/usb-devices) script
1210 sub __scan_usb_device
{
1211 my ($res, $devpath, $parent, $level) = @_;
1213 return if ! -d
$devpath;
1214 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
1215 my $port = $level ?
int($1 - 1) : 0;
1217 my $busnum = int(file_read_firstline
("$devpath/busnum"));
1218 my $devnum = int(file_read_firstline
("$devpath/devnum"));
1225 speed
=> file_read_firstline
("$devpath/speed"),
1226 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
1227 vendid
=> file_read_firstline
("$devpath/idVendor"),
1228 prodid
=> file_read_firstline
("$devpath/idProduct"),
1232 my $usbpath = $devpath;
1233 $usbpath =~ s
|^.*/\d
+\
-||;
1234 $d->{usbpath
} = $usbpath;
1237 my $product = file_read_firstline
("$devpath/product");
1238 $d->{product
} = $product if $product;
1240 my $manu = file_read_firstline
("$devpath/manufacturer");
1241 $d->{manufacturer
} = $manu if $manu;
1243 my $serial => file_read_firstline
("$devpath/serial");
1244 $d->{serial
} = $serial if $serial;
1248 foreach my $subdev (<$devpath/$busnum-*>) {
1249 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
1250 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
1259 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
1260 __scan_usb_device
($devlist, $device, 0, 0);
1267 my ($portal_in) = @_;
1270 if (!($portal = resolv_portal
($portal_in))) {
1271 die "unable to parse/resolve portal address '${portal_in}'\n";
1274 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
1277 sub storage_default_format
{
1278 my ($cfg, $storeid) = @_;
1280 my $scfg = storage_config
($cfg, $storeid);
1282 return PVE
::Storage
::Plugin
::default_format
($scfg);
1285 sub vgroup_is_used
{
1286 my ($cfg, $vgname) = @_;
1288 foreach my $storeid (keys %{$cfg->{ids
}}) {
1289 my $scfg = storage_config
($cfg, $storeid);
1290 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
1298 sub target_is_used
{
1299 my ($cfg, $target) = @_;
1301 foreach my $storeid (keys %{$cfg->{ids
}}) {
1302 my $scfg = storage_config
($cfg, $storeid);
1303 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
1311 sub volume_is_used
{
1312 my ($cfg, $volid) = @_;
1314 foreach my $storeid (keys %{$cfg->{ids
}}) {
1315 my $scfg = storage_config
($cfg, $storeid);
1316 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
1324 sub storage_is_used
{
1325 my ($cfg, $storeid) = @_;
1327 foreach my $sid (keys %{$cfg->{ids
}}) {
1328 my $scfg = storage_config
($cfg, $sid);
1329 next if !$scfg->{base
};
1330 my ($st) = parse_volume_id
($scfg->{base
});
1331 return 1 if $st && $st eq $storeid;
1338 my ($list, $func) = @_;
1342 foreach my $sid (keys %$list) {
1343 foreach my $info (@{$list->{$sid}}) {
1344 my $volid = $info->{volid
};
1345 my ($sid1, $volname) = parse_volume_id
($volid, 1);
1346 if ($sid1 && $sid1 eq $sid) {
1347 &$func ($volid, $sid, $info);
1349 warn "detected strange volid '$volid' in volume list for '$sid'\n";
1355 sub extract_vzdump_config_tar
{
1356 my ($archive, $conf_re) = @_;
1358 die "ERROR: file '$archive' does not exist\n" if ! -f
$archive;
1360 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
1361 die "unable to open file '$archive'\n";
1364 while (defined($file = <$fh>)) {
1365 if ($file =~ $conf_re) {
1366 $file = $1; # untaint
1375 die "ERROR: archive contains no configuration file\n" if !$file;
1381 $raw .= "$output\n";
1384 PVE
::Tools
::run_command
(['tar', '-xpOf', $archive, $file, '--occurrence'], outfunc
=> $out);
1386 return wantarray ?
($raw, $file) : $raw;
1389 sub extract_vzdump_config_vma
{
1390 my ($archive, $comp) = @_;
1396 $raw .= "$output\n";
1402 if ($comp eq 'gz') {
1403 $uncomp = ["zcat", $archive];
1404 } elsif ($comp eq 'lzo') {
1405 $uncomp = ["lzop", "-d", "-c", $archive];
1407 die "unknown compression method '$comp'\n";
1409 $cmd = [$uncomp, ["vma", "config", "-"]];
1411 # in some cases, lzop/zcat exits with 1 when its stdout pipe is
1412 # closed early by vma, detect this and ignore the exit code later
1417 if ($output =~ m/lzop: Broken pipe: <stdout>/ || $output =~ m/gzip: stdout: Broken pipe/) {
1419 } elsif (!defined ($errstring) && $output !~ m/^\s*$/) {
1420 $errstring = "Failed to extract config from VMA archive: $output\n";
1424 # in other cases, the pipeline will exit with exit code 141
1425 # because of the broken pipe, handle / ignore this as well
1428 $rc = PVE
::Tools
::run_command
($cmd, outfunc
=> $out, errfunc
=> $err, noerr
=> 1);
1432 # use exit code if no stderr output and not just broken pipe
1433 if (!$errstring && !$broken_pipe && $rc != 0 && $rc != 141) {
1434 die "$rerr\n" if $rerr;
1435 die "config extraction failed with exit code $rc\n";
1437 die "$errstring\n" if $errstring;
1439 # simple case without compression and weird piping behaviour
1440 PVE
::Tools
::run_command
(["vma", "config", $archive], outfunc
=> $out);
1443 return wantarray ?
($raw, undef) : $raw;
1446 sub extract_vzdump_config
{
1447 my ($cfg, $volid) = @_;
1449 my $archive = abs_filesystem_path
($cfg, $volid);
1451 if ($volid =~ /vzdump-(lxc|openvz)-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|(tar(\.(gz|lzo))?))$/) {
1452 return extract_vzdump_config_tar
($archive, qr!^(\./etc/vzdump/(pct|vps)\.conf)$!);
1453 } elsif ($volid =~ /vzdump-qemu-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|((tar|vma)(\.(gz|lzo))?))$/) {
1461 $comp = $11 if defined($11);
1464 if ($format eq 'tar') {
1465 return extract_vzdump_config_tar
($archive, qr!\(\./qemu-server\.conf\)!);
1467 return extract_vzdump_config_vma
($archive, $comp);
1470 die "cannot determine backup guest type for backup archive '$volid'\n";
1474 # bash completion helper
1476 sub complete_storage
{
1477 my ($cmdname, $pname, $cvalue) = @_;
1479 my $cfg = PVE
::Storage
::config
();
1481 return $cmdname eq 'add' ?
[] : [ PVE
::Storage
::storage_ids
($cfg) ];
1484 sub complete_storage_enabled
{
1485 my ($cmdname, $pname, $cvalue) = @_;
1489 my $cfg = PVE
::Storage
::config
();
1490 foreach my $sid (keys %{$cfg->{ids
}}) {
1491 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1497 sub complete_content_type
{
1498 my ($cmdname, $pname, $cvalue) = @_;
1500 return [qw(rootdir images vztmpl iso backup)];
1503 sub complete_volume
{
1504 my ($cmdname, $pname, $cvalue) = @_;
1508 my $storage_list = complete_storage_enabled
();
1510 if ($cvalue =~ m/^([^:]+):/) {
1511 $storage_list = [ $1 ];
1513 if (scalar(@$storage_list) > 1) {
1514 # only list storage IDs to avoid large listings
1516 foreach my $storeid (@$storage_list) {
1517 # Hack: simply return 2 artificial values, so that
1518 # completions does not finish
1519 push @$res, "$storeid:volname", "$storeid:...";
1526 foreach my $storeid (@$storage_list) {
1527 my $vollist = PVE
::Storage
::volume_list
($cfg, $storeid);
1529 foreach my $item (@$vollist) {
1530 push @$res, $item->{volid
};