15 use PVE
::Tools
qw(run_command file_read_firstline dir_glob_foreach $IPV6RE);
16 use PVE
::Cluster
qw(cfs_read_file cfs_write_file cfs_lock_file);
17 use PVE
::Exception
qw(raise_param_exc);
20 use PVE
::RPCEnvironment
;
22 use PVE
::Storage
::Plugin
;
23 use PVE
::Storage
::DirPlugin
;
24 use PVE
::Storage
::LVMPlugin
;
25 use PVE
::Storage
::LvmThinPlugin
;
26 use PVE
::Storage
::NFSPlugin
;
27 use PVE
::Storage
::ISCSIPlugin
;
28 use PVE
::Storage
::RBDPlugin
;
29 use PVE
::Storage
::SheepdogPlugin
;
30 use PVE
::Storage
::ISCSIDirectPlugin
;
31 use PVE
::Storage
::GlusterfsPlugin
;
32 use PVE
::Storage
::ZFSPoolPlugin
;
33 use PVE
::Storage
::ZFSPlugin
;
34 use PVE
::Storage
::DRBDPlugin
;
36 # Storage API version. Icrement it on changes in storage API interface.
37 use constant APIVER
=> 1;
39 # load standard plugins
40 PVE
::Storage
::DirPlugin-
>register();
41 PVE
::Storage
::LVMPlugin-
>register();
42 PVE
::Storage
::LvmThinPlugin-
>register();
43 PVE
::Storage
::NFSPlugin-
>register();
44 PVE
::Storage
::ISCSIPlugin-
>register();
45 PVE
::Storage
::RBDPlugin-
>register();
46 PVE
::Storage
::SheepdogPlugin-
>register();
47 PVE
::Storage
::ISCSIDirectPlugin-
>register();
48 PVE
::Storage
::GlusterfsPlugin-
>register();
49 PVE
::Storage
::ZFSPoolPlugin-
>register();
50 PVE
::Storage
::ZFSPlugin-
>register();
51 PVE
::Storage
::DRBDPlugin-
>register();
53 # load third-party plugins
54 if ( -d
'/usr/share/perl5/PVE/Storage/Custom' ) {
55 dir_glob_foreach
('/usr/share/perl5/PVE/Storage/Custom', '.*\.pm$', sub {
57 my $modname = 'PVE::Storage::Custom::' . $file;
58 $modname =~ s!\.pm$!!;
59 $file = 'PVE/Storage/Custom/' . $file;
66 # Check storage API version and that file is really storage plugin.
67 } elsif ($modname->isa('PVE::Storage::Plugin') && $modname->can('api') && $modname->api() == APIVER
) {
74 warn "Error loading storage plugin \"$modname\" because of API version mismatch
. Please
, update it
.\n"
79 # initialize all plugins
80 PVE::Storage::Plugin->init();
82 my $UDEVADM = '/sbin/udevadm';
84 # PVE::Storage utility functions
87 return cfs_read_file("storage
.cfg
");
93 cfs_write_file('storage.cfg', $cfg);
96 sub lock_storage_config {
97 my ($code, $errmsg) = @_;
99 cfs_lock_file("storage
.cfg
", undef, $code);
102 $errmsg ? die "$errmsg: $err" : die $err;
107 my ($cfg, $storeid, $noerr) = @_;
109 die "no storage ID specified
\n" if !$storeid;
111 my $scfg = $cfg->{ids}->{$storeid};
113 die "storage
'$storeid' does not exists\n" if (!$noerr && !$scfg);
118 sub storage_check_node {
119 my ($cfg, $storeid, $node, $noerr) = @_;
121 my $scfg = storage_config($cfg, $storeid);
123 if ($scfg->{nodes}) {
124 $node = PVE::INotify::nodename() if !$node || ($node eq 'localhost');
125 if (!$scfg->{nodes}->{$node}) {
126 die "storage
'$storeid' is not available on node
'$node'\n" if !$noerr;
134 sub storage_check_enabled {
135 my ($cfg, $storeid, $node, $noerr) = @_;
137 my $scfg = storage_config($cfg, $storeid);
139 if ($scfg->{disable}) {
140 die "storage
'$storeid' is disabled
\n" if !$noerr;
144 return storage_check_node($cfg, $storeid, $node, $noerr);
150 return keys %{$cfg->{ids}};
154 my ($filename, $timeout) = @_;
156 return PVE::Storage::Plugin::file_size_info($filename, $timeout);
159 sub volume_size_info {
160 my ($cfg, $volid, $timeout) = @_;
162 my ($storeid, $volname) = parse_volume_id($volid, 1);
164 my $scfg = storage_config($cfg, $storeid);
165 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
166 return $plugin->volume_size_info($scfg, $storeid, $volname, $timeout);
167 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
168 return file_size_info($volid, $timeout);
175 my ($cfg, $volid, $size, $running) = @_;
177 my ($storeid, $volname) = parse_volume_id($volid, 1);
179 my $scfg = storage_config($cfg, $storeid);
180 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
181 return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running);
182 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
183 die "resize file
/device
'$volid' is not possible
\n";
185 die "unable to parse volume ID
'$volid'\n";
189 sub volume_rollback_is_possible {
190 my ($cfg, $volid, $snap) = @_;
192 my ($storeid, $volname) = parse_volume_id($volid, 1);
194 my $scfg = storage_config($cfg, $storeid);
195 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
196 return $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
197 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
198 die "snapshot rollback file
/device
'$volid' is not possible
\n";
200 die "unable to parse volume ID
'$volid'\n";
204 sub volume_snapshot {
205 my ($cfg, $volid, $snap) = @_;
207 my ($storeid, $volname) = parse_volume_id($volid, 1);
209 my $scfg = storage_config($cfg, $storeid);
210 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
211 return $plugin->volume_snapshot($scfg, $storeid, $volname, $snap);
212 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
213 die "snapshot file
/device
'$volid' is not possible
\n";
215 die "unable to parse volume ID
'$volid'\n";
219 sub volume_snapshot_rollback {
220 my ($cfg, $volid, $snap) = @_;
222 my ($storeid, $volname) = parse_volume_id($volid, 1);
224 my $scfg = storage_config($cfg, $storeid);
225 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
226 $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
227 return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap);
228 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
229 die "snapshot rollback file
/device
'$volid' is not possible
\n";
231 die "unable to parse volume ID
'$volid'\n";
235 sub volume_snapshot_delete {
236 my ($cfg, $volid, $snap, $running) = @_;
238 my ($storeid, $volname) = parse_volume_id($volid, 1);
240 my $scfg = storage_config($cfg, $storeid);
241 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
242 return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running);
243 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
244 die "snapshot
delete file
/device
'$volid' is not possible
\n";
246 die "unable to parse volume ID
'$volid'\n";
250 sub volume_has_feature {
251 my ($cfg, $feature, $volid, $snap, $running) = @_;
253 my ($storeid, $volname) = parse_volume_id($volid, 1);
255 my $scfg = storage_config($cfg, $storeid);
256 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
257 return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running);
258 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
266 my ($cfg, $storeid, $vmid) = @_;
268 my $scfg = storage_config($cfg, $storeid);
269 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
271 my $path = $plugin->get_subdir($scfg, 'images');
273 return $vmid ? "$path/$vmid" : $path;
276 sub get_private_dir {
277 my ($cfg, $storeid, $vmid) = @_;
279 my $scfg = storage_config($cfg, $storeid);
280 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
282 my $path = $plugin->get_subdir($scfg, 'rootdir');
284 return $vmid ? "$path/$vmid" : $path;
288 my ($cfg, $storeid) = @_;
290 my $scfg = storage_config($cfg, $storeid);
291 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
293 return $plugin->get_subdir($scfg, 'iso');
297 my ($cfg, $storeid) = @_;
299 my $scfg = storage_config($cfg, $storeid);
300 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
302 return $plugin->get_subdir($scfg, 'vztmpl');
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, 'backup');
314 # library implementation
319 die "VMID
'$vmid' contains illegal characters
\n" if $vmid !~ m/^\d+$/;
324 # NOTE: basename and basevmid are always undef for LVM-thin, where the
325 # clone -> base reference is not encoded in the volume ID.
326 # see note in PVE::Storage::LvmThinPlugin for details.
328 my ($cfg, $volid) = @_;
330 my ($storeid, $volname) = parse_volume_id($volid);
332 my $scfg = storage_config($cfg, $storeid);
334 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
336 # returns ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format)
338 return $plugin->parse_volname($volname);
341 sub parse_volume_id {
342 my ($volid, $noerr) = @_;
344 return PVE::Storage::Plugin::parse_volume_id($volid, $noerr);
347 my $volume_is_base_and_used__no_lock = sub {
348 my ($scfg, $storeid, $plugin, $volname) = @_;
350 my ($vtype, $name, $vmid, undef, undef, $isBase, undef) =
351 $plugin->parse_volname($volname);
354 my $vollist = $plugin->list_images($storeid, $scfg);
355 foreach my $info (@$vollist) {
356 my (undef, $tmpvolname) = parse_volume_id($info->{volid});
357 my $basename = undef;
358 my $basevmid = undef;
361 (undef, undef, undef, $basename, $basevmid) =
362 $plugin->parse_volname($tmpvolname);
365 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
373 # NOTE: this check does not work for LVM-thin, where the clone -> base
374 # reference is not encoded in the volume ID.
375 # see note in PVE::Storage::LvmThinPlugin for details.
376 sub volume_is_base_and_used {
377 my ($cfg, $volid) = @_;
379 my ($storeid, $volname) = parse_volume_id($volid);
380 my $scfg = storage_config($cfg, $storeid);
381 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
383 $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
384 return &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
388 # try to map a filesystem path to a volume identifier
389 sub path_to_volume_id {
390 my ($cfg, $path) = @_;
392 my $ids = $cfg->{ids};
394 my ($sid, $volname) = parse_volume_id($path, 1);
396 if (my $scfg = $ids->{$sid}) {
398 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
399 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
400 return ($vtype, $path);
406 # Note: abs_path() return undef if $path doesn not exist
407 # for example when nfs storage is not mounted
408 $path = abs_path($path) || $path;
410 foreach my $sid (keys %$ids) {
411 my $scfg = $ids->{$sid};
412 next if !$scfg->{path};
413 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
414 my $imagedir = $plugin->get_subdir($scfg, 'images');
415 my $isodir = $plugin->get_subdir($scfg, 'iso');
416 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
417 my $backupdir = $plugin->get_subdir($scfg, 'backup');
418 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
420 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
424 my $vollist = $plugin->list_images($sid, $scfg, $vmid);
425 foreach my $info (@$vollist) {
426 my ($storeid, $volname) = parse_volume_id($info->{volid});
427 my $volpath = $plugin->path($scfg, $volname, $storeid);
428 if ($volpath eq $path) {
429 return ('images', $info->{volid});
432 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
434 return ('iso', "$sid:iso
/$name");
435 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
437 return ('vztmpl', "$sid:vztmpl
/$name");
438 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
440 return ('rootdir', "$sid:rootdir
/$vmid");
441 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!) {
443 return ('iso', "$sid:backup
/$name");
447 # can't map path to volume id
452 my ($cfg, $volid, $snapname) = @_;
454 my ($storeid, $volname) = parse_volume_id($volid);
456 my $scfg = storage_config($cfg, $storeid);
458 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
459 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname);
460 return wantarray ? ($path, $owner, $vtype) : $path;
463 sub abs_filesystem_path {
464 my ($cfg, $volid) = @_;
467 if (PVE::Storage::parse_volume_id ($volid, 1)) {
468 PVE::Storage::activate_volumes($cfg, [ $volid ]);
469 $path = PVE::Storage::path($cfg, $volid);
472 my $abspath = abs_path($volid);
473 if ($abspath && $abspath =~ m|^(/.+)$|) {
474 $path = $1; # untaint any path
479 die "can
't find file '$volid'\n" if !($path && -f $path);
484 sub storage_migrate {
485 my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
487 my ($storeid, $volname) = parse_volume_id($volid);
488 $target_volname = $volname if !$target_volname;
490 my $scfg = storage_config($cfg, $storeid);
492 # no need to migrate shared content
493 return if $storeid eq $target_storeid && $scfg->{shared};
495 my $tcfg = storage_config($cfg, $target_storeid);
497 my $target_volid = "${target_storeid}:${target_volname}";
499 my $errstr = "unable to migrate '$volid' to '${target_volid
}' on host '$target_host'";
501 my $sshoptions = "-o 'BatchMode
=yes
'";
502 my $ssh = "/usr/bin/ssh $sshoptions";
504 local $ENV{RSYNC_RSH} = $ssh;
506 # only implemented for file system based storage
510 my $src_plugin = PVE::Storage::Plugin->lookup($scfg->{type});
511 my $dst_plugin = PVE::Storage::Plugin->lookup($tcfg->{type});
512 my $src = $src_plugin->path($scfg, $volname, $storeid);
513 my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid);
515 my $dirname = dirname($dst);
517 if ($tcfg->{shared}) { # we can do a local copy
519 run_command(['/bin/mkdir
', '-p
', $dirname]);
521 run_command(['/bin/cp', $src, $dst]);
524 run_command(['/usr/bin
/ssh
', "root\@${target_host}",
525 '/bin/mkdir
', '-p
', $dirname]);
527 # we use rsync with --sparse, so we can't
use --inplace
,
528 # so we remove file on the target if it already exists to
530 my ($size, $format) = PVE
::Storage
::Plugin
::file_size_info
($src);
531 if ($format && ($format eq 'raw') && $size) {
532 run_command
(['/usr/bin/ssh', "root\@${target_host}",
538 if ($format eq 'subvol') {
539 $cmd = ['/usr/bin/rsync', '--progress', '-X', '-A', '--numeric-ids',
540 '-aH', '--delete', '--no-whole-file', '--inplace',
541 '--one-file-system', "$src/", "[root\@${target_host}]:$dst"];
543 $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
544 $src, "[root\@${target_host}]:$dst"];
549 run_command
($cmd, outfunc
=> sub {
552 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
555 print "rsync status: $1\n";
565 die "$errstr - target type '$tcfg->{type}' not implemented\n";
568 } elsif ($scfg->{type
} eq 'zfspool') {
570 if ($tcfg->{type
} eq 'zfspool') {
572 die "$errstr - pool on target does not have the same name as on source!"
573 if $tcfg->{pool
} ne $scfg->{pool
};
575 my (undef, $volname) = parse_volname
($cfg, $volid);
577 my $zfspath = "$scfg->{pool}\/$volname";
579 my $snap = ['zfs', 'snapshot', "$zfspath\@__migration__"];
581 my $send = [['zfs', 'send', '-Rpv', "$zfspath\@__migration__"], ['ssh', "root\@$target_host",
582 'zfs', 'recv', $zfspath]];
584 my $destroy_target = ['ssh', "root\@$target_host", 'zfs', 'destroy', "$zfspath\@__migration__"];
591 run_command
(['zfs', 'destroy', "$zfspath\@__migration__"]);
594 run_command
($destroy_target);
597 die "$errstr - target type $tcfg->{type} is not valid\n";
600 } elsif ($scfg->{type
} eq 'lvmthin' || $scfg->{type
} eq 'lvm') {
602 if (($scfg->{type
} eq $tcfg->{type
}) &&
603 ($tcfg->{type
} eq 'lvmthin' || $tcfg->{type
} eq 'lvm')) {
605 my (undef, $volname, $vmid) = parse_volname
($cfg, $volid);
606 my $size = volume_size_info
($cfg, $volid, 5);
607 my $src = path
($cfg, $volid);
608 my $dst = path
($cfg, $target_volid);
610 run_command
(['/usr/bin/ssh', "root\@${target_host}",
611 'pvesm', 'alloc', $target_storeid, $vmid,
612 $target_volname, int($size/1024)]);
615 if ($tcfg->{type
} eq 'lvmthin') {
616 run_command
([["dd", "if=$src"],["/usr/bin/ssh", "root\@${target_host}",
617 "dd", 'conv=sparse', "of=$dst"]]);
619 run_command
([["dd", "if=$src"],["/usr/bin/ssh", "root\@${target_host}",
624 run_command
(['/usr/bin/ssh', "root\@${target_host}",
625 'pvesm', 'free', $target_volid]);
629 die "$errstr - migrate from source type '$scfg->{type}' to '$tcfg->{type}' not implemented\n";
632 die "$errstr - source type '$scfg->{type}' not implemented\n";
637 my ($cfg, $volid, $vmid, $snap) = @_;
639 my ($storeid, $volname) = parse_volume_id
($volid);
641 my $scfg = storage_config
($cfg, $storeid);
643 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
645 activate_storage
($cfg, $storeid);
647 # lock shared storage
648 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
649 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
650 return "$storeid:$volname";
654 sub vdisk_create_base
{
655 my ($cfg, $volid) = @_;
657 my ($storeid, $volname) = parse_volume_id
($volid);
659 my $scfg = storage_config
($cfg, $storeid);
661 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
663 activate_storage
($cfg, $storeid);
665 # lock shared storage
666 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
667 my $volname = $plugin->create_base($storeid, $scfg, $volname);
668 return "$storeid:$volname";
673 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
675 die "no storage ID specified\n" if !$storeid;
677 PVE
::JSONSchema
::parse_storage_id
($storeid);
679 my $scfg = storage_config
($cfg, $storeid);
681 die "no VMID specified\n" if !$vmid;
683 $vmid = parse_vmid
($vmid);
685 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
687 $fmt = $defformat if !$fmt;
689 activate_storage
($cfg, $storeid);
691 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
693 # lock shared storage
694 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
695 my $old_umask = umask(umask|0037);
696 my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) };
700 return "$storeid:$volname";
705 my ($cfg, $volid) = @_;
707 my ($storeid, $volname) = parse_volume_id
($volid);
708 my $scfg = storage_config
($cfg, $storeid);
709 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
711 activate_storage
($cfg, $storeid);
715 # lock shared storage
716 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
717 # LVM-thin allows deletion of still referenced base volumes!
718 die "base volume '$volname' is still in use by linked clones\n"
719 if &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
721 my (undef, undef, undef, undef, undef, $isBase, $format) =
722 $plugin->parse_volname($volname);
723 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format);
726 return if !$cleanup_worker;
728 my $rpcenv = PVE
::RPCEnvironment
::get
();
729 my $authuser = $rpcenv->get_user();
731 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
734 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
736 my ($cfg, $storeid, $tt) = @_;
738 die "unknown template type '$tt'\n"
739 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
741 my $ids = $cfg->{ids
};
743 storage_check_enabled
($cfg, $storeid) if ($storeid);
749 foreach my $sid (keys %$ids) {
750 next if $storeid && $storeid ne $sid;
752 my $scfg = $ids->{$sid};
753 my $type = $scfg->{type
};
755 next if !storage_check_enabled
($cfg, $sid, undef, 1);
757 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
758 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
759 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
761 activate_storage
($cfg, $sid);
764 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
766 my $path = $plugin->get_subdir($scfg, $tt);
768 foreach my $fn (<$path/*>) {
773 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
775 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
777 } elsif ($tt eq 'vztmpl') {
778 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
780 $info = { volid
=> "$sid:vztmpl/$1", format
=> "t$2" };
782 } elsif ($tt eq 'backup') {
783 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
785 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
788 $info->{size
} = -s
$fn;
790 push @{$res->{$sid}}, $info;
795 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
803 my ($cfg, $storeid, $vmid, $vollist) = @_;
805 my $ids = $cfg->{ids
};
807 storage_check_enabled
($cfg, $storeid) if ($storeid);
811 # prepare/activate/refresh all storages
813 my $storage_list = [];
815 foreach my $volid (@$vollist) {
816 my ($sid, undef) = parse_volume_id
($volid);
817 next if !defined($ids->{$sid});
818 next if !storage_check_enabled
($cfg, $sid, undef, 1);
819 push @$storage_list, $sid;
822 foreach my $sid (keys %$ids) {
823 next if $storeid && $storeid ne $sid;
824 next if !storage_check_enabled
($cfg, $sid, undef, 1);
825 push @$storage_list, $sid;
831 activate_storage_list
($cfg, $storage_list, $cache);
833 foreach my $sid (keys %$ids) {
834 next if $storeid && $storeid ne $sid;
835 next if !storage_check_enabled
($cfg, $sid, undef, 1);
837 my $scfg = $ids->{$sid};
838 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
839 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
840 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
847 my ($cfg, $storeid, $vmid, $content) = @_;
849 my @ctypes = qw(images vztmpl iso backup);
851 my $cts = $content ?
[ $content ] : [ @ctypes ];
853 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
856 foreach my $ct (@$cts) {
858 if ($ct eq 'images') {
859 $data = vdisk_list
($cfg, $storeid, $vmid);
860 } elsif ($ct eq 'iso' && !defined($vmid)) {
861 $data = template_list
($cfg, $storeid, 'iso');
862 } elsif ($ct eq 'vztmpl'&& !defined($vmid)) {
863 $data = template_list
($cfg, $storeid, 'vztmpl');
864 } elsif ($ct eq 'backup') {
865 $data = template_list
($cfg, $storeid, 'backup');
866 foreach my $item (@{$data->{$storeid}}) {
867 if (defined($vmid)) {
868 @{$data->{$storeid}} = grep { $_->{volid
} =~ m/\S+-$vmid-\S+/ } @{$data->{$storeid}};
873 next if !$data || !$data->{$storeid};
875 foreach my $item (@{$data->{$storeid}}) {
876 $item->{content
} = $ct;
886 my $filename = "/sys/kernel/uevent_seqnum";
889 if (my $fh = IO
::File-
>new($filename, "r")) {
891 if ($line =~ m/^(\d+)$/) {
899 sub activate_storage
{
900 my ($cfg, $storeid, $cache) = @_;
902 $cache = {} if !$cache;
904 my $scfg = storage_check_enabled
($cfg, $storeid);
906 return if $cache->{activated
}->{$storeid};
908 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
910 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
913 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
914 activate_storage
($cfg, $baseid, $cache);
917 if (!$plugin->check_connection($storeid, $scfg)) {
918 die "storage '$storeid' is not online\n";
921 $plugin->activate_storage($storeid, $scfg, $cache);
923 my $newseq = uevent_seqnum
();
925 # only call udevsettle if there are events
926 if ($newseq > $cache->{uevent_seqnum
}) {
928 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
929 $cache->{uevent_seqnum
} = $newseq;
932 $cache->{activated
}->{$storeid} = 1;
935 sub activate_storage_list
{
936 my ($cfg, $storeid_list, $cache) = @_;
938 $cache = {} if !$cache;
940 foreach my $storeid (@$storeid_list) {
941 activate_storage
($cfg, $storeid, $cache);
945 sub deactivate_storage
{
946 my ($cfg, $storeid) = @_;
948 my $scfg = storage_config
($cfg, $storeid);
949 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
952 $plugin->deactivate_storage($storeid, $scfg, $cache);
955 sub activate_volumes
{
956 my ($cfg, $vollist, $snapname) = @_;
958 return if !($vollist && scalar(@$vollist));
960 my $storagehash = {};
961 foreach my $volid (@$vollist) {
962 my ($storeid, undef) = parse_volume_id
($volid);
963 $storagehash->{$storeid} = 1;
968 activate_storage_list
($cfg, [keys %$storagehash], $cache);
970 foreach my $volid (@$vollist) {
971 my ($storeid, $volname) = parse_volume_id
($volid);
972 my $scfg = storage_config
($cfg, $storeid);
973 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
974 $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache);
978 sub deactivate_volumes
{
979 my ($cfg, $vollist, $snapname) = @_;
981 return if !($vollist && scalar(@$vollist));
986 foreach my $volid (@$vollist) {
987 my ($storeid, $volname) = parse_volume_id
($volid);
989 my $scfg = storage_config
($cfg, $storeid);
990 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
993 $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache);
997 push @errlist, $volid;
1001 die "volume deactivation failed: " . join(' ', @errlist)
1002 if scalar(@errlist);
1006 my ($cfg, $content) = @_;
1008 my $ids = $cfg->{ids
};
1012 my @ctypes = PVE
::Tools
::split_list
($content);
1015 foreach my $storeid (keys %$ids) {
1017 next if !storage_check_enabled
($cfg, $storeid, undef, 1);
1019 if (defined($content)) {
1021 foreach my $ctype (@ctypes) {
1022 if ($ids->{$storeid}->{content
}->{$ctype}) {
1027 next if !$want_ctype;
1030 my $type = $ids->{$storeid}->{type
};
1032 $info->{$storeid} = {
1037 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
1038 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
1042 push @$slist, $storeid;
1047 foreach my $storeid (keys %$ids) {
1048 my $scfg = $ids->{$storeid};
1049 next if !$info->{$storeid};
1051 eval { activate_storage
($cfg, $storeid, $cache); };
1057 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1058 my ($total, $avail, $used, $active);
1059 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
1062 $info->{$storeid}->{total
} = $total;
1063 $info->{$storeid}->{avail
} = $avail;
1064 $info->{$storeid}->{used
} = $used;
1065 $info->{$storeid}->{active
} = $active;
1074 my ($packed_ip, $family);
1076 my @res = PVE
::Tools
::getaddrinfo_all
($server);
1077 $family = $res[0]->{family
};
1078 $packed_ip = (PVE
::Tools
::unpack_sockaddr_in46
($res[0]->{addr
}))[2];
1080 if (defined $packed_ip) {
1081 return Socket
::inet_ntop
($family, $packed_ip);
1087 my ($server_in) = @_;
1090 if (!($server = resolv_server
($server_in))) {
1091 die "unable to resolve address for server '${server_in}'\n";
1094 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
1097 run_command
($cmd, outfunc
=> sub {
1100 # note: howto handle white spaces in export path??
1101 if ($line =~ m!^(/\S+)\s+(.+)$!) {
1111 my $cmd = ['zfs', 'list', '-t', 'filesystem', '-H', '-o', 'name,avail,used'];
1114 run_command
($cmd, outfunc
=> sub {
1117 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
1118 my ($pool, $size_str, $used_str) = ($1, $2, $3);
1119 my $size = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($size_str);
1120 my $used = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($used_str);
1121 # ignore subvolumes generated by our ZFSPoolPlugin
1122 return if $pool =~ m!/subvol-\d+-[^/]+$!;
1123 return if $pool =~ m!/basevol-\d+-[^/]+$!;
1124 push @$res, { pool
=> $pool, size
=> $size, free
=> $size-$used };
1132 my ($portal, $noerr) = @_;
1134 my ($server, $port) = PVE
::Tools
::parse_host_and_port
($portal);
1136 if (my $ip = resolv_server
($server)) {
1138 $server = "[$server]" if $server =~ /^$IPV6RE$/;
1139 return $port ?
"$server:$port" : $server;
1142 return undef if $noerr;
1144 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
1147 # idea is from usbutils package (/usr/bin/usb-devices) script
1148 sub __scan_usb_device
{
1149 my ($res, $devpath, $parent, $level) = @_;
1151 return if ! -d
$devpath;
1152 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
1153 my $port = $level ?
int($1 - 1) : 0;
1155 my $busnum = int(file_read_firstline
("$devpath/busnum"));
1156 my $devnum = int(file_read_firstline
("$devpath/devnum"));
1163 speed
=> file_read_firstline
("$devpath/speed"),
1164 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
1165 vendid
=> file_read_firstline
("$devpath/idVendor"),
1166 prodid
=> file_read_firstline
("$devpath/idProduct"),
1170 my $usbpath = $devpath;
1171 $usbpath =~ s
|^.*/\d
+\
-||;
1172 $d->{usbpath
} = $usbpath;
1175 my $product = file_read_firstline
("$devpath/product");
1176 $d->{product
} = $product if $product;
1178 my $manu = file_read_firstline
("$devpath/manufacturer");
1179 $d->{manufacturer
} = $manu if $manu;
1181 my $serial => file_read_firstline
("$devpath/serial");
1182 $d->{serial
} = $serial if $serial;
1186 foreach my $subdev (<$devpath/$busnum-*>) {
1187 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
1188 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
1197 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
1198 __scan_usb_device
($devlist, $device, 0, 0);
1205 my ($portal_in) = @_;
1208 if (!($portal = resolv_portal
($portal_in))) {
1209 die "unable to parse/resolve portal address '${portal_in}'\n";
1212 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
1215 sub storage_default_format
{
1216 my ($cfg, $storeid) = @_;
1218 my $scfg = storage_config
($cfg, $storeid);
1220 return PVE
::Storage
::Plugin
::default_format
($scfg);
1223 sub vgroup_is_used
{
1224 my ($cfg, $vgname) = @_;
1226 foreach my $storeid (keys %{$cfg->{ids
}}) {
1227 my $scfg = storage_config
($cfg, $storeid);
1228 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
1236 sub target_is_used
{
1237 my ($cfg, $target) = @_;
1239 foreach my $storeid (keys %{$cfg->{ids
}}) {
1240 my $scfg = storage_config
($cfg, $storeid);
1241 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
1249 sub volume_is_used
{
1250 my ($cfg, $volid) = @_;
1252 foreach my $storeid (keys %{$cfg->{ids
}}) {
1253 my $scfg = storage_config
($cfg, $storeid);
1254 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
1262 sub storage_is_used
{
1263 my ($cfg, $storeid) = @_;
1265 foreach my $sid (keys %{$cfg->{ids
}}) {
1266 my $scfg = storage_config
($cfg, $sid);
1267 next if !$scfg->{base
};
1268 my ($st) = parse_volume_id
($scfg->{base
});
1269 return 1 if $st && $st eq $storeid;
1276 my ($list, $func) = @_;
1280 foreach my $sid (keys %$list) {
1281 foreach my $info (@{$list->{$sid}}) {
1282 my $volid = $info->{volid
};
1283 my ($sid1, $volname) = parse_volume_id
($volid, 1);
1284 if ($sid1 && $sid1 eq $sid) {
1285 &$func ($volid, $sid, $info);
1287 warn "detected strange volid '$volid' in volume list for '$sid'\n";
1293 sub extract_vzdump_config_tar
{
1294 my ($archive, $conf_re) = @_;
1296 die "ERROR: file '$archive' does not exist\n" if ! -f
$archive;
1298 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
1299 die "unable to open file '$archive'\n";
1302 while (defined($file = <$fh>)) {
1303 if ($file =~ m!$conf_re!) {
1304 $file = $1; # untaint
1313 die "ERROR: archive contains no configuration file\n" if !$file;
1319 $raw .= "$output\n";
1322 PVE
::Tools
::run_command
(['tar', '-xpOf', $archive, $file, '--occurrence'], outfunc
=> $out);
1324 return wantarray ?
($raw, $file) : $raw;
1327 sub extract_vzdump_config_vma
{
1328 my ($archive, $comp) = @_;
1334 $raw .= "$output\n";
1340 if ($comp eq 'gz') {
1341 $uncomp = ["zcat", $archive];
1342 } elsif ($comp eq 'lzo') {
1343 $uncomp = ["lzop", "-d", "-c", $archive];
1345 die "unknown compression method '$comp'\n";
1347 $cmd = [$uncomp, ["vma", "config", "-"]];
1349 # in some cases, lzop/zcat exits with 1 when its stdout pipe is
1350 # closed early by vma, detect this and ignore the exit code later
1355 if ($output =~ m/lzop: Broken pipe: <stdout>/ || $output =~ m/gzip: stdout: Broken pipe/) {
1357 } elsif (!defined ($errstring) && $output !~ m/^\s*$/) {
1358 $errstring = "Failed to extract config from VMA archive: $output\n";
1362 # in other cases, the pipeline will exit with exit code 141
1363 # because of the broken pipe, handle / ignore this as well
1366 $rc = PVE
::Tools
::run_command
($cmd, outfunc
=> $out, errfunc
=> $err, noerr
=> 1);
1370 # use exit code if no stderr output and not just broken pipe
1371 if (!$errstring && !$broken_pipe && $rc > 0 && $rc != 141) {
1372 die "$rerr\n" if $rerr;
1373 die "config extraction failed with exit code $rc\n";
1375 die "$errstring\n" if $errstring;
1377 # simple case without compression and weird piping behaviour
1378 PVE
::Tools
::run_command
(["vma", "config", $archive], outfunc
=> $out);
1381 return wantarray ?
($raw, undef) : $raw;
1384 sub extract_vzdump_config
{
1385 my ($cfg, $volid) = @_;
1387 my $archive = abs_filesystem_path
($cfg, $volid);
1389 if ($volid =~ /\/vzdump-
(lxc
|openvz
)-\d
+-(\d
{4})_
(\d
{2})_
(\d
{2})-(\d
{2})_
(\d
{2})_
(\d
{2})\
.(tgz
|(tar
(\
.(gz
|lzo
))?
))$/) {
1390 return extract_vzdump_config_tar
($archive,'^(\./etc/vzdump/(pct|vps)\.conf)$');
1391 } elsif ($volid =~ /\/vzdump-qemu-\d
+-(\d
{4})_
(\d
{2})_
(\d
{2})-(\d
{2})_
(\d
{2})_
(\d
{2})\
.(tgz
|((tar
|vma
)(\
.(gz
|lzo
))?
))$/) {
1399 $comp = $11 if defined($11);
1402 if ($format eq 'tar') {
1403 return extract_vzdump_config_tar
($archive, qr!\(\./qemu-server\.conf\)!);
1405 return extract_vzdump_config_vma
($archive, $comp);
1408 die "cannot determine backup guest type for backup archive '$volid'\n";
1412 # bash completion helper
1414 sub complete_storage
{
1415 my ($cmdname, $pname, $cvalue) = @_;
1417 my $cfg = PVE
::Storage
::config
();
1419 return $cmdname eq 'add' ?
[] : [ PVE
::Storage
::storage_ids
($cfg) ];
1422 sub complete_storage_enabled
{
1423 my ($cmdname, $pname, $cvalue) = @_;
1427 my $cfg = PVE
::Storage
::config
();
1428 foreach my $sid (keys %{$cfg->{ids
}}) {
1429 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1435 sub complete_content_type
{
1436 my ($cmdname, $pname, $cvalue) = @_;
1438 return [qw(rootdir images vztmpl iso backup)];
1441 sub complete_volume
{
1442 my ($cmdname, $pname, $cvalue) = @_;
1446 my $storage_list = complete_storage_enabled
();
1448 if ($cvalue =~ m/^([^:]+):/) {
1449 $storage_list = [ $1 ];
1451 if (scalar(@$storage_list) > 1) {
1452 # only list storage IDs to avoid large listings
1454 foreach my $storeid (@$storage_list) {
1455 # Hack: simply return 2 artificial values, so that
1456 # completions does not finish
1457 push @$res, "$storeid:volname", "$storeid:...";
1464 foreach my $storeid (@$storage_list) {
1465 my $vollist = PVE
::Storage
::volume_list
($cfg, $storeid);
1467 foreach my $item (@$vollist) {
1468 push @$res, $item->{volid
};