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__"];
590 warn "zfs send/receive failed, cleaning up snapshot(s)..\n" if $err;
591 eval { run_command
(['zfs', 'destroy', "$zfspath\@__migration__"]); };
592 warn "could not remove source snapshot: $@\n" if $@;
593 eval { run_command
($destroy_target); };
594 warn "could not remove target snapshot: $@\n" if $@;
598 die "$errstr - target type $tcfg->{type} is not valid\n";
601 } elsif ($scfg->{type
} eq 'lvmthin' || $scfg->{type
} eq 'lvm') {
603 if (($scfg->{type
} eq $tcfg->{type
}) &&
604 ($tcfg->{type
} eq 'lvmthin' || $tcfg->{type
} eq 'lvm')) {
606 my (undef, $volname, $vmid) = parse_volname
($cfg, $volid);
607 my $size = volume_size_info
($cfg, $volid, 5);
608 my $src = path
($cfg, $volid);
609 my $dst = path
($cfg, $target_volid);
611 run_command
(['/usr/bin/ssh', "root\@${target_host}",
612 'pvesm', 'alloc', $target_storeid, $vmid,
613 $target_volname, int($size/1024)]);
616 if ($tcfg->{type
} eq 'lvmthin') {
617 run_command
([["dd", "if=$src", "bs=4k"],["/usr/bin/ssh", "root\@${target_host}",
618 "dd", 'conv=sparse', "of=$dst", "bs=4k"]]);
620 run_command
([["dd", "if=$src", "bs=4k"],["/usr/bin/ssh", "root\@${target_host}",
621 "dd", "of=$dst", "bs=4k"]]);
625 run_command
(['/usr/bin/ssh', "root\@${target_host}",
626 'pvesm', 'free', $target_volid]);
630 die "$errstr - migrate from source type '$scfg->{type}' to '$tcfg->{type}' not implemented\n";
633 die "$errstr - source type '$scfg->{type}' not implemented\n";
638 my ($cfg, $volid, $vmid, $snap) = @_;
640 my ($storeid, $volname) = parse_volume_id
($volid);
642 my $scfg = storage_config
($cfg, $storeid);
644 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
646 activate_storage
($cfg, $storeid);
648 # lock shared storage
649 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
650 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
651 return "$storeid:$volname";
655 sub vdisk_create_base
{
656 my ($cfg, $volid) = @_;
658 my ($storeid, $volname) = parse_volume_id
($volid);
660 my $scfg = storage_config
($cfg, $storeid);
662 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
664 activate_storage
($cfg, $storeid);
666 # lock shared storage
667 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
668 my $volname = $plugin->create_base($storeid, $scfg, $volname);
669 return "$storeid:$volname";
674 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
676 die "no storage ID specified\n" if !$storeid;
678 PVE
::JSONSchema
::parse_storage_id
($storeid);
680 my $scfg = storage_config
($cfg, $storeid);
682 die "no VMID specified\n" if !$vmid;
684 $vmid = parse_vmid
($vmid);
686 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
688 $fmt = $defformat if !$fmt;
690 activate_storage
($cfg, $storeid);
692 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
694 # lock shared storage
695 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
696 my $old_umask = umask(umask|0037);
697 my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) };
701 return "$storeid:$volname";
706 my ($cfg, $volid) = @_;
708 my ($storeid, $volname) = parse_volume_id
($volid);
709 my $scfg = storage_config
($cfg, $storeid);
710 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
712 activate_storage
($cfg, $storeid);
716 # lock shared storage
717 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
718 # LVM-thin allows deletion of still referenced base volumes!
719 die "base volume '$volname' is still in use by linked clones\n"
720 if &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
722 my (undef, undef, undef, undef, undef, $isBase, $format) =
723 $plugin->parse_volname($volname);
724 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format);
727 return if !$cleanup_worker;
729 my $rpcenv = PVE
::RPCEnvironment
::get
();
730 my $authuser = $rpcenv->get_user();
732 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
735 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
737 my ($cfg, $storeid, $tt) = @_;
739 die "unknown template type '$tt'\n"
740 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
742 my $ids = $cfg->{ids
};
744 storage_check_enabled
($cfg, $storeid) if ($storeid);
750 foreach my $sid (keys %$ids) {
751 next if $storeid && $storeid ne $sid;
753 my $scfg = $ids->{$sid};
754 my $type = $scfg->{type
};
756 next if !storage_check_enabled
($cfg, $sid, undef, 1);
758 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
759 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
760 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
762 activate_storage
($cfg, $sid);
765 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
767 my $path = $plugin->get_subdir($scfg, $tt);
769 foreach my $fn (<$path/*>) {
774 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
776 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
778 } elsif ($tt eq 'vztmpl') {
779 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
781 $info = { volid
=> "$sid:vztmpl/$1", format
=> "t$2" };
783 } elsif ($tt eq 'backup') {
784 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
786 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
789 $info->{size
} = -s
$fn;
791 push @{$res->{$sid}}, $info;
796 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
804 my ($cfg, $storeid, $vmid, $vollist) = @_;
806 my $ids = $cfg->{ids
};
808 storage_check_enabled
($cfg, $storeid) if ($storeid);
812 # prepare/activate/refresh all storages
814 my $storage_list = [];
816 foreach my $volid (@$vollist) {
817 my ($sid, undef) = parse_volume_id
($volid);
818 next if !defined($ids->{$sid});
819 next if !storage_check_enabled
($cfg, $sid, undef, 1);
820 push @$storage_list, $sid;
823 foreach my $sid (keys %$ids) {
824 next if $storeid && $storeid ne $sid;
825 next if !storage_check_enabled
($cfg, $sid, undef, 1);
826 push @$storage_list, $sid;
832 activate_storage_list
($cfg, $storage_list, $cache);
834 foreach my $sid (keys %$ids) {
835 next if $storeid && $storeid ne $sid;
836 next if !storage_check_enabled
($cfg, $sid, undef, 1);
838 my $scfg = $ids->{$sid};
839 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
840 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
841 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
848 my ($cfg, $storeid, $vmid, $content) = @_;
850 my @ctypes = qw(images vztmpl iso backup);
852 my $cts = $content ?
[ $content ] : [ @ctypes ];
854 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
857 foreach my $ct (@$cts) {
859 if ($ct eq 'images') {
860 $data = vdisk_list
($cfg, $storeid, $vmid);
861 } elsif ($ct eq 'iso' && !defined($vmid)) {
862 $data = template_list
($cfg, $storeid, 'iso');
863 } elsif ($ct eq 'vztmpl'&& !defined($vmid)) {
864 $data = template_list
($cfg, $storeid, 'vztmpl');
865 } elsif ($ct eq 'backup') {
866 $data = template_list
($cfg, $storeid, 'backup');
867 foreach my $item (@{$data->{$storeid}}) {
868 if (defined($vmid)) {
869 @{$data->{$storeid}} = grep { $_->{volid
} =~ m/\S+-$vmid-\S+/ } @{$data->{$storeid}};
874 next if !$data || !$data->{$storeid};
876 foreach my $item (@{$data->{$storeid}}) {
877 $item->{content
} = $ct;
887 my $filename = "/sys/kernel/uevent_seqnum";
890 if (my $fh = IO
::File-
>new($filename, "r")) {
892 if ($line =~ m/^(\d+)$/) {
900 sub activate_storage
{
901 my ($cfg, $storeid, $cache) = @_;
903 $cache = {} if !$cache;
905 my $scfg = storage_check_enabled
($cfg, $storeid);
907 return if $cache->{activated
}->{$storeid};
909 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
911 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
914 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
915 activate_storage
($cfg, $baseid, $cache);
918 if (!$plugin->check_connection($storeid, $scfg)) {
919 die "storage '$storeid' is not online\n";
922 $plugin->activate_storage($storeid, $scfg, $cache);
924 my $newseq = uevent_seqnum
();
926 # only call udevsettle if there are events
927 if ($newseq > $cache->{uevent_seqnum
}) {
929 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
930 $cache->{uevent_seqnum
} = $newseq;
933 $cache->{activated
}->{$storeid} = 1;
936 sub activate_storage_list
{
937 my ($cfg, $storeid_list, $cache) = @_;
939 $cache = {} if !$cache;
941 foreach my $storeid (@$storeid_list) {
942 activate_storage
($cfg, $storeid, $cache);
946 sub deactivate_storage
{
947 my ($cfg, $storeid) = @_;
949 my $scfg = storage_config
($cfg, $storeid);
950 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
953 $plugin->deactivate_storage($storeid, $scfg, $cache);
956 sub activate_volumes
{
957 my ($cfg, $vollist, $snapname) = @_;
959 return if !($vollist && scalar(@$vollist));
961 my $storagehash = {};
962 foreach my $volid (@$vollist) {
963 my ($storeid, undef) = parse_volume_id
($volid);
964 $storagehash->{$storeid} = 1;
969 activate_storage_list
($cfg, [keys %$storagehash], $cache);
971 foreach my $volid (@$vollist) {
972 my ($storeid, $volname) = parse_volume_id
($volid);
973 my $scfg = storage_config
($cfg, $storeid);
974 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
975 $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache);
979 sub deactivate_volumes
{
980 my ($cfg, $vollist, $snapname) = @_;
982 return if !($vollist && scalar(@$vollist));
987 foreach my $volid (@$vollist) {
988 my ($storeid, $volname) = parse_volume_id
($volid);
990 my $scfg = storage_config
($cfg, $storeid);
991 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
994 $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache);
998 push @errlist, $volid;
1002 die "volume deactivation failed: " . join(' ', @errlist)
1003 if scalar(@errlist);
1007 my ($cfg, $content) = @_;
1009 my $ids = $cfg->{ids
};
1013 my @ctypes = PVE
::Tools
::split_list
($content);
1016 foreach my $storeid (keys %$ids) {
1018 next if !storage_check_enabled
($cfg, $storeid, undef, 1);
1020 if (defined($content)) {
1022 foreach my $ctype (@ctypes) {
1023 if ($ids->{$storeid}->{content
}->{$ctype}) {
1028 next if !$want_ctype;
1031 my $type = $ids->{$storeid}->{type
};
1033 $info->{$storeid} = {
1038 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
1039 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
1043 push @$slist, $storeid;
1048 foreach my $storeid (keys %$ids) {
1049 my $scfg = $ids->{$storeid};
1050 next if !$info->{$storeid};
1052 eval { activate_storage
($cfg, $storeid, $cache); };
1058 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1059 my ($total, $avail, $used, $active);
1060 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
1063 $info->{$storeid}->{total
} = int($total);
1064 $info->{$storeid}->{avail
} = int($avail);
1065 $info->{$storeid}->{used
} = int($used);
1066 $info->{$storeid}->{active
} = $active;
1075 my ($packed_ip, $family);
1077 my @res = PVE
::Tools
::getaddrinfo_all
($server);
1078 $family = $res[0]->{family
};
1079 $packed_ip = (PVE
::Tools
::unpack_sockaddr_in46
($res[0]->{addr
}))[2];
1081 if (defined $packed_ip) {
1082 return Socket
::inet_ntop
($family, $packed_ip);
1088 my ($server_in) = @_;
1091 if (!($server = resolv_server
($server_in))) {
1092 die "unable to resolve address for server '${server_in}'\n";
1095 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
1098 run_command
($cmd, outfunc
=> sub {
1101 # note: howto handle white spaces in export path??
1102 if ($line =~ m!^(/\S+)\s+(.+)$!) {
1112 my $cmd = ['zfs', 'list', '-t', 'filesystem', '-H', '-o', 'name,avail,used'];
1115 run_command
($cmd, outfunc
=> sub {
1118 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
1119 my ($pool, $size_str, $used_str) = ($1, $2, $3);
1120 my $size = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($size_str);
1121 my $used = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($used_str);
1122 # ignore subvolumes generated by our ZFSPoolPlugin
1123 return if $pool =~ m!/subvol-\d+-[^/]+$!;
1124 return if $pool =~ m!/basevol-\d+-[^/]+$!;
1125 push @$res, { pool
=> $pool, size
=> $size, free
=> $size-$used };
1133 my ($portal, $noerr) = @_;
1135 my ($server, $port) = PVE
::Tools
::parse_host_and_port
($portal);
1137 if (my $ip = resolv_server
($server)) {
1139 $server = "[$server]" if $server =~ /^$IPV6RE$/;
1140 return $port ?
"$server:$port" : $server;
1143 return undef if $noerr;
1145 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
1148 # idea is from usbutils package (/usr/bin/usb-devices) script
1149 sub __scan_usb_device
{
1150 my ($res, $devpath, $parent, $level) = @_;
1152 return if ! -d
$devpath;
1153 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
1154 my $port = $level ?
int($1 - 1) : 0;
1156 my $busnum = int(file_read_firstline
("$devpath/busnum"));
1157 my $devnum = int(file_read_firstline
("$devpath/devnum"));
1164 speed
=> file_read_firstline
("$devpath/speed"),
1165 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
1166 vendid
=> file_read_firstline
("$devpath/idVendor"),
1167 prodid
=> file_read_firstline
("$devpath/idProduct"),
1171 my $usbpath = $devpath;
1172 $usbpath =~ s
|^.*/\d
+\
-||;
1173 $d->{usbpath
} = $usbpath;
1176 my $product = file_read_firstline
("$devpath/product");
1177 $d->{product
} = $product if $product;
1179 my $manu = file_read_firstline
("$devpath/manufacturer");
1180 $d->{manufacturer
} = $manu if $manu;
1182 my $serial => file_read_firstline
("$devpath/serial");
1183 $d->{serial
} = $serial if $serial;
1187 foreach my $subdev (<$devpath/$busnum-*>) {
1188 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
1189 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
1198 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
1199 __scan_usb_device
($devlist, $device, 0, 0);
1206 my ($portal_in) = @_;
1209 if (!($portal = resolv_portal
($portal_in))) {
1210 die "unable to parse/resolve portal address '${portal_in}'\n";
1213 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
1216 sub storage_default_format
{
1217 my ($cfg, $storeid) = @_;
1219 my $scfg = storage_config
($cfg, $storeid);
1221 return PVE
::Storage
::Plugin
::default_format
($scfg);
1224 sub vgroup_is_used
{
1225 my ($cfg, $vgname) = @_;
1227 foreach my $storeid (keys %{$cfg->{ids
}}) {
1228 my $scfg = storage_config
($cfg, $storeid);
1229 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
1237 sub target_is_used
{
1238 my ($cfg, $target) = @_;
1240 foreach my $storeid (keys %{$cfg->{ids
}}) {
1241 my $scfg = storage_config
($cfg, $storeid);
1242 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
1250 sub volume_is_used
{
1251 my ($cfg, $volid) = @_;
1253 foreach my $storeid (keys %{$cfg->{ids
}}) {
1254 my $scfg = storage_config
($cfg, $storeid);
1255 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
1263 sub storage_is_used
{
1264 my ($cfg, $storeid) = @_;
1266 foreach my $sid (keys %{$cfg->{ids
}}) {
1267 my $scfg = storage_config
($cfg, $sid);
1268 next if !$scfg->{base
};
1269 my ($st) = parse_volume_id
($scfg->{base
});
1270 return 1 if $st && $st eq $storeid;
1277 my ($list, $func) = @_;
1281 foreach my $sid (keys %$list) {
1282 foreach my $info (@{$list->{$sid}}) {
1283 my $volid = $info->{volid
};
1284 my ($sid1, $volname) = parse_volume_id
($volid, 1);
1285 if ($sid1 && $sid1 eq $sid) {
1286 &$func ($volid, $sid, $info);
1288 warn "detected strange volid '$volid' in volume list for '$sid'\n";
1294 sub extract_vzdump_config_tar
{
1295 my ($archive, $conf_re) = @_;
1297 die "ERROR: file '$archive' does not exist\n" if ! -f
$archive;
1299 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
1300 die "unable to open file '$archive'\n";
1303 while (defined($file = <$fh>)) {
1304 if ($file =~ m!$conf_re!) {
1305 $file = $1; # untaint
1314 die "ERROR: archive contains no configuration file\n" if !$file;
1320 $raw .= "$output\n";
1323 PVE
::Tools
::run_command
(['tar', '-xpOf', $archive, $file, '--occurrence'], outfunc
=> $out);
1325 return wantarray ?
($raw, $file) : $raw;
1328 sub extract_vzdump_config_vma
{
1329 my ($archive, $comp) = @_;
1335 $raw .= "$output\n";
1341 if ($comp eq 'gz') {
1342 $uncomp = ["zcat", $archive];
1343 } elsif ($comp eq 'lzo') {
1344 $uncomp = ["lzop", "-d", "-c", $archive];
1346 die "unknown compression method '$comp'\n";
1348 $cmd = [$uncomp, ["vma", "config", "-"]];
1350 # in some cases, lzop/zcat exits with 1 when its stdout pipe is
1351 # closed early by vma, detect this and ignore the exit code later
1356 if ($output =~ m/lzop: Broken pipe: <stdout>/ || $output =~ m/gzip: stdout: Broken pipe/) {
1358 } elsif (!defined ($errstring) && $output !~ m/^\s*$/) {
1359 $errstring = "Failed to extract config from VMA archive: $output\n";
1363 # in other cases, the pipeline will exit with exit code 141
1364 # because of the broken pipe, handle / ignore this as well
1367 $rc = PVE
::Tools
::run_command
($cmd, outfunc
=> $out, errfunc
=> $err, noerr
=> 1);
1371 # use exit code if no stderr output and not just broken pipe
1372 if (!$errstring && !$broken_pipe && $rc > 0 && $rc != 141) {
1373 die "$rerr\n" if $rerr;
1374 die "config extraction failed with exit code $rc\n";
1376 die "$errstring\n" if $errstring;
1378 # simple case without compression and weird piping behaviour
1379 PVE
::Tools
::run_command
(["vma", "config", $archive], outfunc
=> $out);
1382 return wantarray ?
($raw, undef) : $raw;
1385 sub extract_vzdump_config
{
1386 my ($cfg, $volid) = @_;
1388 my $archive = abs_filesystem_path
($cfg, $volid);
1390 if ($volid =~ /\/vzdump-
(lxc
|openvz
)-\d
+-(\d
{4})_
(\d
{2})_
(\d
{2})-(\d
{2})_
(\d
{2})_
(\d
{2})\
.(tgz
|(tar
(\
.(gz
|lzo
))?
))$/) {
1391 return extract_vzdump_config_tar
($archive,'^(\./etc/vzdump/(pct|vps)\.conf)$');
1392 } elsif ($volid =~ /\/vzdump-qemu-\d
+-(\d
{4})_
(\d
{2})_
(\d
{2})-(\d
{2})_
(\d
{2})_
(\d
{2})\
.(tgz
|((tar
|vma
)(\
.(gz
|lzo
))?
))$/) {
1400 $comp = $11 if defined($11);
1403 if ($format eq 'tar') {
1404 return extract_vzdump_config_tar
($archive, qr!\(\./qemu-server\.conf\)!);
1406 return extract_vzdump_config_vma
($archive, $comp);
1409 die "cannot determine backup guest type for backup archive '$volid'\n";
1413 # bash completion helper
1415 sub complete_storage
{
1416 my ($cmdname, $pname, $cvalue) = @_;
1418 my $cfg = PVE
::Storage
::config
();
1420 return $cmdname eq 'add' ?
[] : [ PVE
::Storage
::storage_ids
($cfg) ];
1423 sub complete_storage_enabled
{
1424 my ($cmdname, $pname, $cvalue) = @_;
1428 my $cfg = PVE
::Storage
::config
();
1429 foreach my $sid (keys %{$cfg->{ids
}}) {
1430 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1436 sub complete_content_type
{
1437 my ($cmdname, $pname, $cvalue) = @_;
1439 return [qw(rootdir images vztmpl iso backup)];
1442 sub complete_volume
{
1443 my ($cmdname, $pname, $cvalue) = @_;
1447 my $storage_list = complete_storage_enabled
();
1449 if ($cvalue =~ m/^([^:]+):/) {
1450 $storage_list = [ $1 ];
1452 if (scalar(@$storage_list) > 1) {
1453 # only list storage IDs to avoid large listings
1455 foreach my $storeid (@$storage_list) {
1456 # Hack: simply return 2 artificial values, so that
1457 # completions does not finish
1458 push @$res, "$storeid:volname", "$storeid:...";
1465 foreach my $storeid (@$storage_list) {
1466 my $vollist = PVE
::Storage
::volume_list
($cfg, $storeid);
1468 foreach my $item (@$vollist) {
1469 push @$res, $item->{volid
};