15 use PVE
::Tools
qw(run_command file_read_firstline $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 # load and initialize all plugins
37 PVE
::Storage
::DirPlugin-
>register();
38 PVE
::Storage
::LVMPlugin-
>register();
39 PVE
::Storage
::LvmThinPlugin-
>register();
40 PVE
::Storage
::NFSPlugin-
>register();
41 PVE
::Storage
::ISCSIPlugin-
>register();
42 PVE
::Storage
::RBDPlugin-
>register();
43 PVE
::Storage
::SheepdogPlugin-
>register();
44 PVE
::Storage
::ISCSIDirectPlugin-
>register();
45 PVE
::Storage
::GlusterfsPlugin-
>register();
46 PVE
::Storage
::ZFSPoolPlugin-
>register();
47 PVE
::Storage
::ZFSPlugin-
>register();
48 PVE
::Storage
::DRBDPlugin-
>register();
49 PVE
::Storage
::Plugin-
>init();
51 my $UDEVADM = '/sbin/udevadm';
53 # PVE::Storage utility functions
56 return cfs_read_file
("storage.cfg");
62 cfs_write_file
('storage.cfg', $cfg);
65 sub lock_storage_config
{
66 my ($code, $errmsg) = @_;
68 cfs_lock_file
("storage.cfg", undef, $code);
71 $errmsg ?
die "$errmsg: $err" : die $err;
76 my ($cfg, $storeid, $noerr) = @_;
78 die "no storage id specified\n" if !$storeid;
80 my $scfg = $cfg->{ids
}->{$storeid};
82 die "storage '$storeid' does not exists\n" if (!$noerr && !$scfg);
87 sub storage_check_node
{
88 my ($cfg, $storeid, $node, $noerr) = @_;
90 my $scfg = storage_config
($cfg, $storeid);
93 $node = PVE
::INotify
::nodename
() if !$node || ($node eq 'localhost');
94 if (!$scfg->{nodes
}->{$node}) {
95 die "storage '$storeid' is not available on node '$node'\n" if !$noerr;
103 sub storage_check_enabled
{
104 my ($cfg, $storeid, $node, $noerr) = @_;
106 my $scfg = storage_config
($cfg, $storeid);
108 if ($scfg->{disable
}) {
109 die "storage '$storeid' is disabled\n" if !$noerr;
113 return storage_check_node
($cfg, $storeid, $node, $noerr);
119 return keys %{$cfg->{ids
}};
123 my ($filename, $timeout) = @_;
125 return PVE
::Storage
::Plugin
::file_size_info
($filename, $timeout);
128 sub volume_size_info
{
129 my ($cfg, $volid, $timeout) = @_;
131 my ($storeid, $volname) = parse_volume_id
($volid, 1);
133 my $scfg = storage_config
($cfg, $storeid);
134 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
135 return $plugin->volume_size_info($scfg, $storeid, $volname, $timeout);
136 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
137 return file_size_info
($volid, $timeout);
144 my ($cfg, $volid, $size, $running) = @_;
146 my ($storeid, $volname) = parse_volume_id
($volid, 1);
148 my $scfg = storage_config
($cfg, $storeid);
149 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
150 return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running);
151 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
152 die "resize file/device '$volid' is not possible\n";
154 die "unable to parse volume ID '$volid'\n";
158 sub volume_rollback_is_possible
{
159 my ($cfg, $volid, $snap) = @_;
161 my ($storeid, $volname) = parse_volume_id
($volid, 1);
163 my $scfg = storage_config
($cfg, $storeid);
164 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
165 return $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
166 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
167 die "snapshot rollback file/device '$volid' is not possible\n";
169 die "unable to parse volume ID '$volid'\n";
173 sub volume_snapshot
{
174 my ($cfg, $volid, $snap) = @_;
176 my ($storeid, $volname) = parse_volume_id
($volid, 1);
178 my $scfg = storage_config
($cfg, $storeid);
179 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
180 return $plugin->volume_snapshot($scfg, $storeid, $volname, $snap);
181 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
182 die "snapshot file/device '$volid' is not possible\n";
184 die "unable to parse volume ID '$volid'\n";
188 sub volume_snapshot_rollback
{
189 my ($cfg, $volid, $snap) = @_;
191 my ($storeid, $volname) = parse_volume_id
($volid, 1);
193 my $scfg = storage_config
($cfg, $storeid);
194 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
195 $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
196 return $plugin->volume_snapshot_rollback($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_delete
{
205 my ($cfg, $volid, $snap, $running) = @_;
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_delete($scfg, $storeid, $volname, $snap, $running);
212 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
213 die "snapshot delete file/device '$volid' is not possible\n";
215 die "unable to parse volume ID '$volid'\n";
219 sub volume_has_feature
{
220 my ($cfg, $feature, $volid, $snap, $running) = @_;
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 return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running);
227 } elsif ($volid =~ m
|^(/.+)$| && -e
$volid) {
235 my ($cfg, $storeid, $vmid) = @_;
237 my $scfg = storage_config
($cfg, $storeid);
238 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
240 my $path = $plugin->get_subdir($scfg, 'images');
242 return $vmid ?
"$path/$vmid" : $path;
245 sub get_private_dir
{
246 my ($cfg, $storeid, $vmid) = @_;
248 my $scfg = storage_config
($cfg, $storeid);
249 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
251 my $path = $plugin->get_subdir($scfg, 'rootdir');
253 return $vmid ?
"$path/$vmid" : $path;
257 my ($cfg, $storeid) = @_;
259 my $scfg = storage_config
($cfg, $storeid);
260 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
262 return $plugin->get_subdir($scfg, 'iso');
266 my ($cfg, $storeid) = @_;
268 my $scfg = storage_config
($cfg, $storeid);
269 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
271 return $plugin->get_subdir($scfg, 'vztmpl');
275 my ($cfg, $storeid) = @_;
277 my $scfg = storage_config
($cfg, $storeid);
278 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
280 return $plugin->get_subdir($scfg, 'backup');
283 # library implementation
288 die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/;
294 my ($cfg, $volid) = @_;
296 my ($storeid, $volname) = parse_volume_id
($volid);
298 my $scfg = storage_config
($cfg, $storeid);
300 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
302 # returns ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format)
304 return $plugin->parse_volname($volname);
307 sub parse_volume_id
{
308 my ($volid, $noerr) = @_;
310 return PVE
::Storage
::Plugin
::parse_volume_id
($volid, $noerr);
314 my ($cfg, $volid) = @_;
316 my ($sid, $volname) = parse_volume_id
($volid, 1);
319 if (my $scfg = $cfg->{ids
}->{$sid}) {
320 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
321 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
322 $plugin->parse_volname($volname);
323 return $isBase ?
1 : 0;
325 # stale volid with undefined storage - so we can just guess
326 if ($volid =~ m/base-/) {
334 # try to map a filesystem path to a volume identifier
335 sub path_to_volume_id
{
336 my ($cfg, $path) = @_;
338 my $ids = $cfg->{ids
};
340 my ($sid, $volname) = parse_volume_id
($path, 1);
342 if (my $scfg = $ids->{$sid}) {
344 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
345 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
346 return ($vtype, $path);
352 # Note: abs_path() return undef if $path doesn not exist
353 # for example when nfs storage is not mounted
354 $path = abs_path
($path) || $path;
356 foreach my $sid (keys %$ids) {
357 my $scfg = $ids->{$sid};
358 next if !$scfg->{path
};
359 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
360 my $imagedir = $plugin->get_subdir($scfg, 'images');
361 my $isodir = $plugin->get_subdir($scfg, 'iso');
362 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
363 my $backupdir = $plugin->get_subdir($scfg, 'backup');
364 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
366 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
370 my $vollist = $plugin->list_images($sid, $scfg, $vmid);
371 foreach my $info (@$vollist) {
372 my ($storeid, $volname) = parse_volume_id
($info->{volid
});
373 my $volpath = $plugin->path($scfg, $volname, $storeid);
374 if ($volpath eq $path) {
375 return ('images', $info->{volid
});
378 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
380 return ('iso', "$sid:iso/$name");
381 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
383 return ('vztmpl', "$sid:vztmpl/$name");
384 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
386 return ('rootdir', "$sid:rootdir/$vmid");
387 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!) {
389 return ('iso', "$sid:backup/$name");
393 # can't map path to volume id
398 my ($cfg, $volid, $snapname) = @_;
400 my ($storeid, $volname) = parse_volume_id
($volid);
402 my $scfg = storage_config
($cfg, $storeid);
404 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
405 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname);
406 return wantarray ?
($path, $owner, $vtype) : $path;
409 sub abs_filesystem_path
{
410 my ($cfg, $volid) = @_;
413 if (PVE
::Storage
::parse_volume_id
($volid, 1)) {
414 PVE
::Storage
::activate_volumes
($cfg, [ $volid ]);
415 $path = PVE
::Storage
::path
($cfg, $volid);
418 my $abspath = abs_path
($volid);
419 if ($abspath && $abspath =~ m
|^(/.+)$|) {
420 $path = $1; # untaint any path
425 die "can't find file '$volid'\n" if !($path && -f
$path);
430 sub storage_migrate
{
431 my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
433 my ($storeid, $volname) = parse_volume_id
($volid);
434 $target_volname = $volname if !$target_volname;
436 my $scfg = storage_config
($cfg, $storeid);
438 # no need to migrate shared content
439 return if $storeid eq $target_storeid && $scfg->{shared
};
441 my $tcfg = storage_config
($cfg, $target_storeid);
443 my $target_volid = "${target_storeid}:${target_volname}";
445 my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_host'";
447 my $sshoptions = "-o 'BatchMode=yes'";
448 my $ssh = "/usr/bin/ssh $sshoptions";
450 local $ENV{RSYNC_RSH
} = $ssh;
452 # only implemented for file system based storage
456 my $src_plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
457 my $dst_plugin = PVE
::Storage
::Plugin-
>lookup($tcfg->{type
});
458 my $src = $src_plugin->path($scfg, $volname, $storeid);
459 my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid);
461 my $dirname = dirname
($dst);
463 if ($tcfg->{shared
}) { # we can do a local copy
465 run_command
(['/bin/mkdir', '-p', $dirname]);
467 run_command
(['/bin/cp', $src, $dst]);
470 run_command
(['/usr/bin/ssh', "root\@${target_host}",
471 '/bin/mkdir', '-p', $dirname]);
473 # we use rsync with --sparse, so we can't use --inplace,
474 # so we remove file on the target if it already exists to
476 my ($size, $format) = PVE
::Storage
::Plugin
::file_size_info
($src);
477 if ($format && ($format eq 'raw') && $size) {
478 run_command
(['/usr/bin/ssh', "root\@${target_host}",
484 if ($format eq 'subvol') {
485 $cmd = ['/usr/bin/rsync', '--progress', '-X', '-A', '--numeric-ids',
486 '-aH', '--delete', '--no-whole-file', '--inplace',
487 '--one-file-system', "$src/", "[root\@${target_host}]:$dst"];
489 $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
490 $src, "[root\@${target_host}]:$dst"];
495 run_command
($cmd, outfunc
=> sub {
498 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
501 print "rsync status: $1\n";
511 die "$errstr - target type '$tcfg->{type}' not implemented\n";
514 } elsif ($scfg->{type
} eq 'zfspool') {
516 if ($tcfg->{type
} eq 'zfspool') {
518 die "$errstr - pool on target has not same name as source!"
519 if $tcfg->{pool
} ne $scfg->{pool
};
521 my (undef, $volname) = parse_volname
($cfg, $volid);
523 my $zfspath = "$scfg->{pool}\/$volname";
525 my $snap = ['zfs', 'snapshot', "$zfspath\@__migration__"];
527 my $send = [['zfs', 'send', '-Rpv', "$zfspath\@__migration__"], ['ssh', "root\@$target_host",
528 'zfs', 'recv', $zfspath]];
530 my $destroy_target = ['ssh', "root\@$target_host", 'zfs', 'destroy', "$zfspath\@__migration__"];
537 run_command
(['zfs', 'destroy', "$zfspath\@__migration__"]);
540 run_command
($destroy_target);
543 die "$errstr - target type $tcfg->{type} is not valid\n";
546 } elsif ($scfg->{type
} eq 'lvmthin' || $scfg->{type
} eq 'lvm') {
548 if (($scfg->{type
} eq $tcfg->{type
}) &&
549 ($tcfg->{type
} eq 'lvmthin' || $tcfg->{type
} eq 'lvm')) {
551 my (undef, $volname) = parse_volname
($cfg, $volid);
552 my $size = volume_size_info
($cfg, $volid, 5);
553 my $path = path
($cfg, $volid);
555 $volname =~ m/^vm-(\d+)-/;
558 run_command
(['/usr/bin/ssh', "root\@${target_host}",
559 'pvesm', 'alloc', $target_storeid, $vmid,
560 $volname, $size/1024]);
563 run_command
([["dd", "if=$path"],
564 ["/usr/bin/ssh", "root\@${target_host}", "-C",
565 "dd", 'conv=sparse', "of=$path"]]);
568 run_command
(['/usr/bin/ssh', "root\@${target_host}",
569 'pvesm', 'free', $volid]);
571 vdisk_free
($cfg, $volid);
574 die "$errstr - migrate from source type '$scfg->{type}' to '$tcfg->{type}' not implemented\n";
577 die "$errstr - source type '$scfg->{type}' not implemented\n";
582 my ($cfg, $volid, $vmid, $snap) = @_;
584 my ($storeid, $volname) = parse_volume_id
($volid);
586 my $scfg = storage_config
($cfg, $storeid);
588 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
590 activate_storage
($cfg, $storeid);
592 # lock shared storage
593 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
594 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
595 return "$storeid:$volname";
599 sub vdisk_create_base
{
600 my ($cfg, $volid) = @_;
602 my ($storeid, $volname) = parse_volume_id
($volid);
604 my $scfg = storage_config
($cfg, $storeid);
606 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
608 activate_storage
($cfg, $storeid);
610 # lock shared storage
611 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
612 my $volname = $plugin->create_base($storeid, $scfg, $volname);
613 return "$storeid:$volname";
618 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
620 die "no storage id specified\n" if !$storeid;
622 PVE
::JSONSchema
::parse_storage_id
($storeid);
624 my $scfg = storage_config
($cfg, $storeid);
626 die "no VMID specified\n" if !$vmid;
628 $vmid = parse_vmid
($vmid);
630 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
632 $fmt = $defformat if !$fmt;
634 activate_storage
($cfg, $storeid);
636 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
638 # lock shared storage
639 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
640 my $old_umask = umask(umask|0037);
641 my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) };
645 return "$storeid:$volname";
650 my ($cfg, $volid) = @_;
652 my ($storeid, $volname) = parse_volume_id
($volid);
654 my $scfg = storage_config
($cfg, $storeid);
656 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
658 activate_storage
($cfg, $storeid);
662 # lock shared storage
663 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
665 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
666 $plugin->parse_volname($volname);
668 my $vollist = $plugin->list_images($storeid, $scfg);
669 foreach my $info (@$vollist) {
670 my (undef, $tmpvolname) = parse_volume_id
($info->{volid
});
671 my $basename = undef;
672 my $basevmid = undef;
675 (undef, undef, undef, $basename, $basevmid) =
676 $plugin->parse_volname($tmpvolname);
679 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
680 die "base volume '$volname' is still in use " .
681 "(use by '$tmpvolname')\n";
685 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format);
688 return if !$cleanup_worker;
690 my $rpcenv = PVE
::RPCEnvironment
::get
();
691 my $authuser = $rpcenv->get_user();
693 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
696 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
698 my ($cfg, $storeid, $tt) = @_;
700 die "unknown template type '$tt'\n"
701 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
703 my $ids = $cfg->{ids
};
705 storage_check_enabled
($cfg, $storeid) if ($storeid);
711 foreach my $sid (keys %$ids) {
712 next if $storeid && $storeid ne $sid;
714 my $scfg = $ids->{$sid};
715 my $type = $scfg->{type
};
717 next if !storage_check_enabled
($cfg, $sid, undef, 1);
719 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
720 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
721 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
723 activate_storage
($cfg, $sid);
726 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
728 my $path = $plugin->get_subdir($scfg, $tt);
730 foreach my $fn (<$path/*>) {
735 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
737 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
739 } elsif ($tt eq 'vztmpl') {
740 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
742 $info = { volid
=> "$sid:vztmpl/$1", format
=> "t$2" };
744 } elsif ($tt eq 'backup') {
745 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
747 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
750 $info->{size
} = -s
$fn;
752 push @{$res->{$sid}}, $info;
757 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
765 my ($cfg, $storeid, $vmid, $vollist) = @_;
767 my $ids = $cfg->{ids
};
769 storage_check_enabled
($cfg, $storeid) if ($storeid);
773 # prepare/activate/refresh all storages
775 my $storage_list = [];
777 foreach my $volid (@$vollist) {
778 my ($sid, undef) = parse_volume_id
($volid);
779 next if !defined($ids->{$sid});
780 next if !storage_check_enabled
($cfg, $sid, undef, 1);
781 push @$storage_list, $sid;
784 foreach my $sid (keys %$ids) {
785 next if $storeid && $storeid ne $sid;
786 next if !storage_check_enabled
($cfg, $sid, undef, 1);
787 push @$storage_list, $sid;
793 activate_storage_list
($cfg, $storage_list, $cache);
795 foreach my $sid (keys %$ids) {
796 next if $storeid && $storeid ne $sid;
797 next if !storage_check_enabled
($cfg, $sid, undef, 1);
799 my $scfg = $ids->{$sid};
800 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
801 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
802 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
809 my ($cfg, $storeid, $vmid, $content) = @_;
811 my @ctypes = qw(images vztmpl iso backup);
813 my $cts = $content ?
[ $content ] : [ @ctypes ];
815 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
818 foreach my $ct (@$cts) {
820 if ($ct eq 'images') {
821 $data = vdisk_list
($cfg, $storeid, $vmid);
822 } elsif ($ct eq 'iso' && !defined($vmid)) {
823 $data = template_list
($cfg, $storeid, 'iso');
824 } elsif ($ct eq 'vztmpl'&& !defined($vmid)) {
825 $data = template_list
($cfg, $storeid, 'vztmpl');
826 } elsif ($ct eq 'backup') {
827 $data = template_list
($cfg, $storeid, 'backup');
828 foreach my $item (@{$data->{$storeid}}) {
829 if (defined($vmid)) {
830 @{$data->{$storeid}} = grep { $_->{volid
} =~ m/\S+-$vmid-\S+/ } @{$data->{$storeid}};
835 next if !$data || !$data->{$storeid};
837 foreach my $item (@{$data->{$storeid}}) {
838 $item->{content
} = $ct;
848 my $filename = "/sys/kernel/uevent_seqnum";
851 if (my $fh = IO
::File-
>new($filename, "r")) {
853 if ($line =~ m/^(\d+)$/) {
861 sub activate_storage
{
862 my ($cfg, $storeid, $cache) = @_;
864 $cache = {} if !$cache;
866 my $scfg = storage_check_enabled
($cfg, $storeid);
868 return if $cache->{activated
}->{$storeid};
870 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
872 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
875 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
876 activate_storage
($cfg, $baseid, $cache);
879 if (!$plugin->check_connection($storeid, $scfg)) {
880 die "storage '$storeid' is not online\n";
883 $plugin->activate_storage($storeid, $scfg, $cache);
885 my $newseq = uevent_seqnum
();
887 # only call udevsettle if there are events
888 if ($newseq > $cache->{uevent_seqnum
}) {
890 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
891 $cache->{uevent_seqnum
} = $newseq;
894 $cache->{activated
}->{$storeid} = 1;
897 sub activate_storage_list
{
898 my ($cfg, $storeid_list, $cache) = @_;
900 $cache = {} if !$cache;
902 foreach my $storeid (@$storeid_list) {
903 activate_storage
($cfg, $storeid, $cache);
907 sub deactivate_storage
{
908 my ($cfg, $storeid) = @_;
910 my $scfg = storage_config
($cfg, $storeid);
911 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
914 $plugin->deactivate_storage($storeid, $scfg, $cache);
917 sub activate_volumes
{
918 my ($cfg, $vollist, $snapname) = @_;
920 return if !($vollist && scalar(@$vollist));
922 my $storagehash = {};
923 foreach my $volid (@$vollist) {
924 my ($storeid, undef) = parse_volume_id
($volid);
925 $storagehash->{$storeid} = 1;
930 activate_storage_list
($cfg, [keys %$storagehash], $cache);
932 foreach my $volid (@$vollist) {
933 my ($storeid, $volname) = parse_volume_id
($volid);
934 my $scfg = storage_config
($cfg, $storeid);
935 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
936 $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache);
940 sub deactivate_volumes
{
941 my ($cfg, $vollist, $snapname) = @_;
943 return if !($vollist && scalar(@$vollist));
948 foreach my $volid (@$vollist) {
949 my ($storeid, $volname) = parse_volume_id
($volid);
951 my $scfg = storage_config
($cfg, $storeid);
952 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
955 $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache);
959 push @errlist, $volid;
963 die "volume deativation failed: " . join(' ', @errlist)
968 my ($cfg, $content) = @_;
970 my $ids = $cfg->{ids
};
974 my @ctypes = PVE
::Tools
::split_list
($content);
977 foreach my $storeid (keys %$ids) {
979 next if !storage_check_enabled
($cfg, $storeid, undef, 1);
981 if (defined($content)) {
983 foreach my $ctype (@ctypes) {
984 if ($ids->{$storeid}->{content
}->{$ctype}) {
989 next if !$want_ctype;
992 my $type = $ids->{$storeid}->{type
};
994 $info->{$storeid} = {
999 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
1000 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
1004 push @$slist, $storeid;
1009 foreach my $storeid (keys %$ids) {
1010 my $scfg = $ids->{$storeid};
1011 next if !$info->{$storeid};
1013 eval { activate_storage
($cfg, $storeid, $cache); };
1019 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1020 my ($total, $avail, $used, $active);
1021 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
1024 $info->{$storeid}->{total
} = $total;
1025 $info->{$storeid}->{avail
} = $avail;
1026 $info->{$storeid}->{used
} = $used;
1027 $info->{$storeid}->{active
} = $active;
1036 my ($packed_ip, $family);
1038 my @res = PVE
::Tools
::getaddrinfo_all
($server);
1039 $family = $res[0]->{family
};
1040 $packed_ip = (PVE
::Tools
::unpack_sockaddr_in46
($res[0]->{addr
}))[2];
1042 if (defined $packed_ip) {
1043 return Socket
::inet_ntop
($family, $packed_ip);
1049 my ($server_in) = @_;
1052 if (!($server = resolv_server
($server_in))) {
1053 die "unable to resolve address for server '${server_in}'\n";
1056 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
1059 run_command
($cmd, outfunc
=> sub {
1062 # note: howto handle white spaces in export path??
1063 if ($line =~ m!^(/\S+)\s+(.+)$!) {
1073 my $cmd = ['zfs', 'list', '-t', 'filesystem', '-H', '-o', 'name,avail,used'];
1076 run_command
($cmd, outfunc
=> sub {
1079 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
1080 my ($pool, $size_str, $used_str) = ($1, $2, $3);
1081 my $size = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($size_str);
1082 my $used = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($used_str);
1083 # ignore subvolumes generated by our ZFSPoolPlugin
1084 return if $pool =~ m!/subvol-\d+-[^/]+$!;
1085 return if $pool =~ m!/basevol-\d+-[^/]+$!;
1086 push @$res, { pool
=> $pool, size
=> $size, free
=> $size-$used };
1094 my ($portal, $noerr) = @_;
1096 my ($server, $port) = PVE
::Tools
::parse_host_and_port
($portal);
1098 if (my $ip = resolv_server
($server)) {
1100 $server = "[$server]" if $server =~ /^$IPV6RE$/;
1101 return $port ?
"$server:$port" : $server;
1104 return undef if $noerr;
1106 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
1109 # idea is from usbutils package (/usr/bin/usb-devices) script
1110 sub __scan_usb_device
{
1111 my ($res, $devpath, $parent, $level) = @_;
1113 return if ! -d
$devpath;
1114 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
1115 my $port = $level ?
int($1 - 1) : 0;
1117 my $busnum = int(file_read_firstline
("$devpath/busnum"));
1118 my $devnum = int(file_read_firstline
("$devpath/devnum"));
1125 speed
=> file_read_firstline
("$devpath/speed"),
1126 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
1127 vendid
=> file_read_firstline
("$devpath/idVendor"),
1128 prodid
=> file_read_firstline
("$devpath/idProduct"),
1132 my $usbpath = $devpath;
1133 $usbpath =~ s
|^.*/\d
+\
-||;
1134 $d->{usbpath
} = $usbpath;
1137 my $product = file_read_firstline
("$devpath/product");
1138 $d->{product
} = $product if $product;
1140 my $manu = file_read_firstline
("$devpath/manufacturer");
1141 $d->{manufacturer
} = $manu if $manu;
1143 my $serial => file_read_firstline
("$devpath/serial");
1144 $d->{serial
} = $serial if $serial;
1148 foreach my $subdev (<$devpath/$busnum-*>) {
1149 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
1150 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
1159 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
1160 __scan_usb_device
($devlist, $device, 0, 0);
1167 my ($portal_in) = @_;
1170 if (!($portal = resolv_portal
($portal_in))) {
1171 die "unable to parse/resolve portal address '${portal_in}'\n";
1174 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
1177 sub storage_default_format
{
1178 my ($cfg, $storeid) = @_;
1180 my $scfg = storage_config
($cfg, $storeid);
1182 return PVE
::Storage
::Plugin
::default_format
($scfg);
1185 sub vgroup_is_used
{
1186 my ($cfg, $vgname) = @_;
1188 foreach my $storeid (keys %{$cfg->{ids
}}) {
1189 my $scfg = storage_config
($cfg, $storeid);
1190 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
1198 sub target_is_used
{
1199 my ($cfg, $target) = @_;
1201 foreach my $storeid (keys %{$cfg->{ids
}}) {
1202 my $scfg = storage_config
($cfg, $storeid);
1203 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
1211 sub volume_is_used
{
1212 my ($cfg, $volid) = @_;
1214 foreach my $storeid (keys %{$cfg->{ids
}}) {
1215 my $scfg = storage_config
($cfg, $storeid);
1216 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
1224 sub storage_is_used
{
1225 my ($cfg, $storeid) = @_;
1227 foreach my $sid (keys %{$cfg->{ids
}}) {
1228 my $scfg = storage_config
($cfg, $sid);
1229 next if !$scfg->{base
};
1230 my ($st) = parse_volume_id
($scfg->{base
});
1231 return 1 if $st && $st eq $storeid;
1238 my ($list, $func) = @_;
1242 foreach my $sid (keys %$list) {
1243 foreach my $info (@{$list->{$sid}}) {
1244 my $volid = $info->{volid
};
1245 my ($sid1, $volname) = parse_volume_id
($volid, 1);
1246 if ($sid1 && $sid1 eq $sid) {
1247 &$func ($volid, $sid, $info);
1249 warn "detected strange volid '$volid' in volume list for '$sid'\n";
1255 sub extract_vzdump_config_tar
{
1256 my ($archive, $conf_re) = @_;
1258 die "ERROR: file '$archive' does not exist\n" if ! -f
$archive;
1260 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
1261 die "unable to open file '$archive'\n";
1264 while (defined($file = <$fh>)) {
1265 if ($file =~ m!$conf_re!) {
1266 $file = $1; # untaint
1275 die "ERROR: archive contains no configuration file\n" if !$file;
1281 $raw .= "$output\n";
1284 PVE
::Tools
::run_command
(['tar', '-xpOf', $archive, $file, '--occurrence'], outfunc
=> $out);
1286 return wantarray ?
($raw, $file) : $raw;
1289 sub extract_vzdump_config_vma
{
1290 my ($archive, $comp) = @_;
1296 $raw .= "$output\n";
1302 if ($comp eq 'gz') {
1303 $uncomp = ["zcat", $archive];
1304 } elsif ($comp eq 'lzo') {
1305 $uncomp = ["lzop", "-d", "-c", $archive];
1307 die "unknown compression method '$comp'\n";
1309 $cmd = [$uncomp, ["vma", "config", "-"]];
1311 # in some cases, lzop/zcat exits with 1 when its stdout pipe is
1312 # closed early by vma, detect this and ignore the exit code later
1317 if ($output =~ m/lzop: Broken pipe: <stdout>/ || $output =~ m/gzip: stdout: Broken pipe/) {
1319 } elsif (!defined ($errstring) && $output !~ m/^\s*$/) {
1320 $errstring = "Failed to extract config from VMA archive: $output\n";
1324 # in other cases, the pipeline will exit with exit code 141
1325 # because of the broken pipe, handle / ignore this as well
1328 $rc = PVE
::Tools
::run_command
($cmd, outfunc
=> $out, errfunc
=> $err, noerr
=> 1);
1332 # use exit code if no stderr output and not just broken pipe
1333 if (!$errstring && !$broken_pipe && $rc > 0 && $rc != 141) {
1334 die "$rerr\n" if $rerr;
1335 die "config extraction failed with exit code $rc\n";
1337 die "$errstring\n" if $errstring;
1339 # simple case without compression and weird piping behaviour
1340 PVE
::Tools
::run_command
(["vma", "config", $archive], outfunc
=> $out);
1343 return wantarray ?
($raw, undef) : $raw;
1346 sub extract_vzdump_config
{
1347 my ($cfg, $volid) = @_;
1349 my $archive = abs_filesystem_path
($cfg, $volid);
1351 if ($volid =~ /\/vzdump-
(lxc
|openvz
)-\d
+-(\d
{4})_
(\d
{2})_
(\d
{2})-(\d
{2})_
(\d
{2})_
(\d
{2})\
.(tgz
|(tar
(\
.(gz
|lzo
))?
))$/) {
1352 return extract_vzdump_config_tar
($archive,'^(\./etc/vzdump/(pct|vps)\.conf)$');
1353 } elsif ($volid =~ /\/vzdump-qemu-\d
+-(\d
{4})_
(\d
{2})_
(\d
{2})-(\d
{2})_
(\d
{2})_
(\d
{2})\
.(tgz
|((tar
|vma
)(\
.(gz
|lzo
))?
))$/) {
1361 $comp = $11 if defined($11);
1364 if ($format eq 'tar') {
1365 return extract_vzdump_config_tar
($archive, qr!\(\./qemu-server\.conf\)!);
1367 return extract_vzdump_config_vma
($archive, $comp);
1370 die "cannot determine backup guest type for backup archive '$volid'\n";
1374 # bash completion helper
1376 sub complete_storage
{
1377 my ($cmdname, $pname, $cvalue) = @_;
1379 my $cfg = PVE
::Storage
::config
();
1381 return $cmdname eq 'add' ?
[] : [ PVE
::Storage
::storage_ids
($cfg) ];
1384 sub complete_storage_enabled
{
1385 my ($cmdname, $pname, $cvalue) = @_;
1389 my $cfg = PVE
::Storage
::config
();
1390 foreach my $sid (keys %{$cfg->{ids
}}) {
1391 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1397 sub complete_content_type
{
1398 my ($cmdname, $pname, $cvalue) = @_;
1400 return [qw(rootdir images vztmpl iso backup)];
1403 sub complete_volume
{
1404 my ($cmdname, $pname, $cvalue) = @_;
1408 my $storage_list = complete_storage_enabled
();
1410 if ($cvalue =~ m/^([^:]+):/) {
1411 $storage_list = [ $1 ];
1413 if (scalar(@$storage_list) > 1) {
1414 # only list storage IDs to avoid large listings
1416 foreach my $storeid (@$storage_list) {
1417 # Hack: simply return 2 artificial values, so that
1418 # completions does not finish
1419 push @$res, "$storeid:volname", "$storeid:...";
1426 foreach my $storeid (@$storage_list) {
1427 my $vollist = PVE
::Storage
::volume_list
($cfg, $storeid);
1429 foreach my $item (@$vollist) {
1430 push @$res, $item->{volid
};