15 use PVE
::Tools
qw(run_command file_read_firstline dir_glob_foreach $IPV6RE);
16 use PVE
::Cluster
qw(cfs_read_file cfs_write_file cfs_lock_file);
17 use PVE
::Exception
qw(raise_param_exc);
20 use PVE
::RPCEnvironment
;
22 use PVE
::Storage
::Plugin
;
23 use PVE
::Storage
::DirPlugin
;
24 use PVE
::Storage
::LVMPlugin
;
25 use PVE
::Storage
::LvmThinPlugin
;
26 use PVE
::Storage
::NFSPlugin
;
27 use PVE
::Storage
::ISCSIPlugin
;
28 use PVE
::Storage
::RBDPlugin
;
29 use PVE
::Storage
::SheepdogPlugin
;
30 use PVE
::Storage
::ISCSIDirectPlugin
;
31 use PVE
::Storage
::GlusterfsPlugin
;
32 use PVE
::Storage
::ZFSPoolPlugin
;
33 use PVE
::Storage
::ZFSPlugin
;
34 use PVE
::Storage
::DRBDPlugin
;
36 # Storage API version. Icrement it on changes in storage API interface.
37 use constant APIVER
=> 1;
39 # load standard plugins
40 PVE
::Storage
::DirPlugin-
>register();
41 PVE
::Storage
::LVMPlugin-
>register();
42 PVE
::Storage
::LvmThinPlugin-
>register();
43 PVE
::Storage
::NFSPlugin-
>register();
44 PVE
::Storage
::ISCSIPlugin-
>register();
45 PVE
::Storage
::RBDPlugin-
>register();
46 PVE
::Storage
::SheepdogPlugin-
>register();
47 PVE
::Storage
::ISCSIDirectPlugin-
>register();
48 PVE
::Storage
::GlusterfsPlugin-
>register();
49 PVE
::Storage
::ZFSPoolPlugin-
>register();
50 PVE
::Storage
::ZFSPlugin-
>register();
51 PVE
::Storage
::DRBDPlugin-
>register();
53 # load third-party plugins
54 if ( -d
'/usr/share/perl5/PVE/Storage/Custom' ) {
55 dir_glob_foreach
('/usr/share/perl5/PVE/Storage/Custom', '.*\.pm$', sub {
57 my $modname = 'PVE::Storage::Custom::' . $file;
58 $modname =~ s!\.pm$!!;
59 $file = 'PVE/Storage/Custom/' . $file;
66 # Check storage API version and that file is really storage plugin.
67 } elsif ($modname->isa('PVE::Storage::Plugin') && $modname->can('api') && $modname->api() == APIVER
) {
74 warn "Error loading storage plugin \"$modname\" because of API version mismatch
. Please
, update it
.\n"
79 # initialize all plugins
80 PVE::Storage::Plugin->init();
82 my $UDEVADM = '/sbin/udevadm';
84 # PVE::Storage utility functions
87 return cfs_read_file("storage
.cfg
");
93 cfs_write_file('storage.cfg', $cfg);
96 sub lock_storage_config {
97 my ($code, $errmsg) = @_;
99 cfs_lock_file("storage
.cfg
", undef, $code);
102 $errmsg ? die "$errmsg: $err" : die $err;
107 my ($cfg, $storeid, $noerr) = @_;
109 die "no storage ID specified
\n" if !$storeid;
111 my $scfg = $cfg->{ids}->{$storeid};
113 die "storage
'$storeid' does not exists\n" if (!$noerr && !$scfg);
118 sub storage_check_node {
119 my ($cfg, $storeid, $node, $noerr) = @_;
121 my $scfg = storage_config($cfg, $storeid);
123 if ($scfg->{nodes}) {
124 $node = PVE::INotify::nodename() if !$node || ($node eq 'localhost');
125 if (!$scfg->{nodes}->{$node}) {
126 die "storage
'$storeid' is not available on node
'$node'\n" if !$noerr;
134 sub storage_check_enabled {
135 my ($cfg, $storeid, $node, $noerr) = @_;
137 my $scfg = storage_config($cfg, $storeid);
139 if ($scfg->{disable}) {
140 die "storage
'$storeid' is disabled
\n" if !$noerr;
144 return storage_check_node($cfg, $storeid, $node, $noerr);
150 return keys %{$cfg->{ids}};
154 my ($filename, $timeout) = @_;
156 return PVE::Storage::Plugin::file_size_info($filename, $timeout);
159 sub volume_size_info {
160 my ($cfg, $volid, $timeout) = @_;
162 my ($storeid, $volname) = parse_volume_id($volid, 1);
164 my $scfg = storage_config($cfg, $storeid);
165 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
166 return $plugin->volume_size_info($scfg, $storeid, $volname, $timeout);
167 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
168 return file_size_info($volid, $timeout);
175 my ($cfg, $volid, $size, $running) = @_;
177 my ($storeid, $volname) = parse_volume_id($volid, 1);
179 my $scfg = storage_config($cfg, $storeid);
180 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
181 return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running);
182 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
183 die "resize file
/device
'$volid' is not possible
\n";
185 die "unable to parse volume ID
'$volid'\n";
189 sub volume_rollback_is_possible {
190 my ($cfg, $volid, $snap) = @_;
192 my ($storeid, $volname) = parse_volume_id($volid, 1);
194 my $scfg = storage_config($cfg, $storeid);
195 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
196 return $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
197 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
198 die "snapshot rollback file
/device
'$volid' is not possible
\n";
200 die "unable to parse volume ID
'$volid'\n";
204 sub volume_snapshot {
205 my ($cfg, $volid, $snap) = @_;
207 my ($storeid, $volname) = parse_volume_id($volid, 1);
209 my $scfg = storage_config($cfg, $storeid);
210 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
211 return $plugin->volume_snapshot($scfg, $storeid, $volname, $snap);
212 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
213 die "snapshot file
/device
'$volid' is not possible
\n";
215 die "unable to parse volume ID
'$volid'\n";
220 my ($cfg, $volid, $snap, $ip, $incremental_snap, $verbose, $limit,
223 my ($storeid, $volname) = parse_volume_id($volid, 1);
225 my $scfg = storage_config($cfg, $storeid);
226 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
227 return $plugin->volume_send($scfg, $storeid, $volname, $ip, $snap,
228 $incremental_snap, $verbose, $limit, $target_path);
230 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
231 die "send file
/device
'$volid' is not possible
\n";
233 die "unable to parse volume ID
'$volid'\n";
237 sub volume_snapshot_rollback {
238 my ($cfg, $volid, $snap) = @_;
240 my ($storeid, $volname) = parse_volume_id($volid, 1);
242 my $scfg = storage_config($cfg, $storeid);
243 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
244 $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap);
245 return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap);
246 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
247 die "snapshot rollback file
/device
'$volid' is not possible
\n";
249 die "unable to parse volume ID
'$volid'\n";
253 sub volume_snapshot_delete {
254 my ($cfg, $volid, $snap, $running) = @_;
256 my ($storeid, $volname) = parse_volume_id($volid, 1);
258 my $scfg = storage_config($cfg, $storeid);
259 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
260 return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running);
261 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
262 die "snapshot
delete file
/device
'$volid' is not possible
\n";
264 die "unable to parse volume ID
'$volid'\n";
268 sub volume_has_feature {
269 my ($cfg, $feature, $volid, $snap, $running) = @_;
271 my ($storeid, $volname) = parse_volume_id($volid, 1);
273 my $scfg = storage_config($cfg, $storeid);
274 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
275 return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running);
276 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
284 my ($cfg, $storeid, $vmid) = @_;
286 my $scfg = storage_config($cfg, $storeid);
287 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
289 my $path = $plugin->get_subdir($scfg, 'images');
291 return $vmid ? "$path/$vmid" : $path;
294 sub get_private_dir {
295 my ($cfg, $storeid, $vmid) = @_;
297 my $scfg = storage_config($cfg, $storeid);
298 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
300 my $path = $plugin->get_subdir($scfg, 'rootdir');
302 return $vmid ? "$path/$vmid" : $path;
306 my ($cfg, $storeid) = @_;
308 my $scfg = storage_config($cfg, $storeid);
309 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
311 return $plugin->get_subdir($scfg, 'iso');
315 my ($cfg, $storeid) = @_;
317 my $scfg = storage_config($cfg, $storeid);
318 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
320 return $plugin->get_subdir($scfg, 'vztmpl');
324 my ($cfg, $storeid) = @_;
326 my $scfg = storage_config($cfg, $storeid);
327 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
329 return $plugin->get_subdir($scfg, 'backup');
332 # library implementation
337 die "VMID
'$vmid' contains illegal characters
\n" if $vmid !~ m/^\d+$/;
342 # NOTE: basename and basevmid are always undef for LVM-thin, where the
343 # clone -> base reference is not encoded in the volume ID.
344 # see note in PVE::Storage::LvmThinPlugin for details.
346 my ($cfg, $volid) = @_;
348 my ($storeid, $volname) = parse_volume_id($volid);
350 my $scfg = storage_config($cfg, $storeid);
352 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
354 # returns ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format)
356 return $plugin->parse_volname($volname);
359 sub parse_volume_id {
360 my ($volid, $noerr) = @_;
362 return PVE::Storage::Plugin::parse_volume_id($volid, $noerr);
365 # test if we have read access to volid
366 sub check_volume_access {
367 my ($rpcenv, $user, $cfg, $vmid, $volid) = @_;
369 my ($sid, $volname) = parse_volume_id($volid, 1);
371 my ($vtype, undef, $ownervm) = parse_volname($cfg, $volid);
372 if ($vtype eq 'iso' || $vtype eq 'vztmpl') {
373 # we simply allow access
374 } elsif (defined($ownervm) && defined($vmid) && ($ownervm == $vmid)) {
375 # we are owner - allow access
376 } elsif ($vtype eq 'backup' && $ownervm) {
377 $rpcenv->check($user, "/storage/$sid", ['Datastore.AllocateSpace']);
378 $rpcenv->check($user, "/vms/$ownervm", ['VM.Backup']);
380 # allow if we are Datastore administrator
381 $rpcenv->check($user, "/storage/$sid", ['Datastore.Allocate']);
384 die "Only root can pass arbitrary filesystem paths
."
385 if $user ne 'root@pam';
391 my $volume_is_base_and_used__no_lock = sub {
392 my ($scfg, $storeid, $plugin, $volname) = @_;
394 my ($vtype, $name, $vmid, undef, undef, $isBase, undef) =
395 $plugin->parse_volname($volname);
398 my $vollist = $plugin->list_images($storeid, $scfg);
399 foreach my $info (@$vollist) {
400 my (undef, $tmpvolname) = parse_volume_id($info->{volid});
401 my $basename = undef;
402 my $basevmid = undef;
405 (undef, undef, undef, $basename, $basevmid) =
406 $plugin->parse_volname($tmpvolname);
409 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
417 # NOTE: this check does not work for LVM-thin, where the clone -> base
418 # reference is not encoded in the volume ID.
419 # see note in PVE::Storage::LvmThinPlugin for details.
420 sub volume_is_base_and_used {
421 my ($cfg, $volid) = @_;
423 my ($storeid, $volname) = parse_volume_id($volid);
424 my $scfg = storage_config($cfg, $storeid);
425 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
427 $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
428 return &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
432 # try to map a filesystem path to a volume identifier
433 sub path_to_volume_id {
434 my ($cfg, $path) = @_;
436 my $ids = $cfg->{ids};
438 my ($sid, $volname) = parse_volume_id($path, 1);
440 if (my $scfg = $ids->{$sid}) {
442 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
443 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
444 return ($vtype, $path);
450 # Note: abs_path() return undef if $path doesn not exist
451 # for example when nfs storage is not mounted
452 $path = abs_path($path) || $path;
454 foreach my $sid (keys %$ids) {
455 my $scfg = $ids->{$sid};
456 next if !$scfg->{path};
457 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
458 my $imagedir = $plugin->get_subdir($scfg, 'images');
459 my $isodir = $plugin->get_subdir($scfg, 'iso');
460 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
461 my $backupdir = $plugin->get_subdir($scfg, 'backup');
462 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
464 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
468 my $vollist = $plugin->list_images($sid, $scfg, $vmid);
469 foreach my $info (@$vollist) {
470 my ($storeid, $volname) = parse_volume_id($info->{volid});
471 my $volpath = $plugin->path($scfg, $volname, $storeid);
472 if ($volpath eq $path) {
473 return ('images', $info->{volid});
476 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
478 return ('iso', "$sid:iso
/$name");
479 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
481 return ('vztmpl', "$sid:vztmpl
/$name");
482 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
484 return ('rootdir', "$sid:rootdir
/$vmid");
485 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!) {
487 return ('iso', "$sid:backup
/$name");
491 # can't map path to volume id
496 my ($cfg, $volid, $snapname) = @_;
498 my ($storeid, $volname) = parse_volume_id($volid);
500 my $scfg = storage_config($cfg, $storeid);
502 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
503 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname);
504 return wantarray ? ($path, $owner, $vtype) : $path;
507 sub abs_filesystem_path {
508 my ($cfg, $volid) = @_;
511 if (PVE::Storage::parse_volume_id ($volid, 1)) {
512 PVE::Storage::activate_volumes($cfg, [ $volid ]);
513 $path = PVE::Storage::path($cfg, $volid);
516 my $abspath = abs_path($volid);
517 if ($abspath && $abspath =~ m|^(/.+)$|) {
518 $path = $1; # untaint any path
523 die "can
't find file '$volid'\n" if !($path && -f $path);
528 sub storage_migrate {
529 my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
531 my ($storeid, $volname) = parse_volume_id($volid);
532 $target_volname = $volname if !$target_volname;
534 my $scfg = storage_config($cfg, $storeid);
536 # no need to migrate shared content
537 return if $storeid eq $target_storeid && $scfg->{shared};
539 my $tcfg = storage_config($cfg, $target_storeid);
541 my $target_volid = "${target_storeid}:${target_volname}";
543 my $errstr = "unable to migrate '$volid' to '${target_volid
}' on host '$target_host'";
545 my $sshoptions = "-o 'BatchMode
=yes
'";
546 my $ssh = "/usr/bin/ssh $sshoptions";
548 local $ENV{RSYNC_RSH} = $ssh;
550 # only implemented for file system based storage
554 my $src_plugin = PVE::Storage::Plugin->lookup($scfg->{type});
555 my $dst_plugin = PVE::Storage::Plugin->lookup($tcfg->{type});
556 my $src = $src_plugin->path($scfg, $volname, $storeid);
557 my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid);
559 my $dirname = dirname($dst);
561 if ($tcfg->{shared}) { # we can do a local copy
563 run_command(['/bin/mkdir
', '-p
', $dirname]);
565 run_command(['/bin/cp', $src, $dst]);
568 run_command(['/usr/bin
/ssh
', "root\@${target_host}",
569 '/bin/mkdir
', '-p
', $dirname]);
571 # we use rsync with --sparse, so we can't
use --inplace
,
572 # so we remove file on the target if it already exists to
574 my ($size, $format) = PVE
::Storage
::Plugin
::file_size_info
($src);
575 if ($format && ($format eq 'raw') && $size) {
576 run_command
(['/usr/bin/ssh', "root\@${target_host}",
582 if ($format eq 'subvol') {
583 $cmd = ['/usr/bin/rsync', '--progress', '-X', '-A', '--numeric-ids',
584 '-aH', '--delete', '--no-whole-file', '--inplace',
585 '--one-file-system', "$src/", "[root\@${target_host}]:$dst"];
587 $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
588 $src, "[root\@${target_host}]:$dst"];
593 run_command
($cmd, outfunc
=> sub {
596 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
599 print "rsync status: $1\n";
609 die "$errstr - target type '$tcfg->{type}' not implemented\n";
612 } elsif ($scfg->{type
} eq 'zfspool') {
614 if ($tcfg->{type
} eq 'zfspool') {
616 die "$errstr - pool on target does not have the same name as on source!"
617 if $tcfg->{pool
} ne $scfg->{pool
};
619 my (undef, $volname) = parse_volname
($cfg, $volid);
621 my $zfspath = "$scfg->{pool}\/$volname";
623 my $snap = ['zfs', 'snapshot', "$zfspath\@__migration__"];
625 my $send = [['zfs', 'send', '-Rpv', "$zfspath\@__migration__"], ['ssh', "root\@$target_host",
626 'zfs', 'recv', $zfspath]];
628 my $destroy_target = ['ssh', "root\@$target_host", 'zfs', 'destroy', "$zfspath\@__migration__"];
634 warn "zfs send/receive failed, cleaning up snapshot(s)..\n" if $err;
635 eval { run_command
(['zfs', 'destroy', "$zfspath\@__migration__"]); };
636 warn "could not remove source snapshot: $@\n" if $@;
637 eval { run_command
($destroy_target); };
638 warn "could not remove target snapshot: $@\n" if $@;
642 die "$errstr - target type $tcfg->{type} is not valid\n";
645 } elsif ($scfg->{type
} eq 'lvmthin' || $scfg->{type
} eq 'lvm') {
647 if (($scfg->{type
} eq $tcfg->{type
}) &&
648 ($tcfg->{type
} eq 'lvmthin' || $tcfg->{type
} eq 'lvm')) {
650 my (undef, $volname, $vmid) = parse_volname
($cfg, $volid);
651 my $size = volume_size_info
($cfg, $volid, 5);
652 my $src = path
($cfg, $volid);
653 my $dst = path
($cfg, $target_volid);
655 run_command
(['/usr/bin/ssh', "root\@${target_host}",
656 'pvesm', 'alloc', $target_storeid, $vmid,
657 $target_volname, int($size/1024)]);
660 if ($tcfg->{type
} eq 'lvmthin') {
661 run_command
([["dd", "if=$src", "bs=4k"],["/usr/bin/ssh", "root\@${target_host}",
662 "dd", 'conv=sparse', "of=$dst", "bs=4k"]]);
664 run_command
([["dd", "if=$src", "bs=4k"],["/usr/bin/ssh", "root\@${target_host}",
665 "dd", "of=$dst", "bs=4k"]]);
669 run_command
(['/usr/bin/ssh', "root\@${target_host}",
670 'pvesm', 'free', $target_volid]);
674 die "$errstr - migrate from source type '$scfg->{type}' to '$tcfg->{type}' not implemented\n";
677 die "$errstr - source type '$scfg->{type}' not implemented\n";
682 my ($cfg, $volid, $vmid, $snap) = @_;
684 my ($storeid, $volname) = parse_volume_id
($volid);
686 my $scfg = storage_config
($cfg, $storeid);
688 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
690 activate_storage
($cfg, $storeid);
692 # lock shared storage
693 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
694 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
695 return "$storeid:$volname";
699 sub vdisk_create_base
{
700 my ($cfg, $volid) = @_;
702 my ($storeid, $volname) = parse_volume_id
($volid);
704 my $scfg = storage_config
($cfg, $storeid);
706 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
708 activate_storage
($cfg, $storeid);
710 # lock shared storage
711 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
712 my $volname = $plugin->create_base($storeid, $scfg, $volname);
713 return "$storeid:$volname";
718 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
720 die "no storage ID specified\n" if !$storeid;
722 PVE
::JSONSchema
::parse_storage_id
($storeid);
724 my $scfg = storage_config
($cfg, $storeid);
726 die "no VMID specified\n" if !$vmid;
728 $vmid = parse_vmid
($vmid);
730 my $defformat = PVE
::Storage
::Plugin
::default_format
($scfg);
732 $fmt = $defformat if !$fmt;
734 activate_storage
($cfg, $storeid);
736 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
738 # lock shared storage
739 return $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
740 my $old_umask = umask(umask|0037);
741 my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) };
745 return "$storeid:$volname";
750 my ($cfg, $volid) = @_;
752 my ($storeid, $volname) = parse_volume_id
($volid);
753 my $scfg = storage_config
($cfg, $storeid);
754 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
756 activate_storage
($cfg, $storeid);
760 # lock shared storage
761 $plugin->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
762 # LVM-thin allows deletion of still referenced base volumes!
763 die "base volume '$volname' is still in use by linked clones\n"
764 if &$volume_is_base_and_used__no_lock($scfg, $storeid, $plugin, $volname);
766 my (undef, undef, undef, undef, undef, $isBase, $format) =
767 $plugin->parse_volname($volname);
768 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format);
771 return if !$cleanup_worker;
773 my $rpcenv = PVE
::RPCEnvironment
::get
();
774 my $authuser = $rpcenv->get_user();
776 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
779 #list iso or openvz template ($tt = <iso|vztmpl|backup>)
781 my ($cfg, $storeid, $tt) = @_;
783 die "unknown template type '$tt'\n"
784 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
786 my $ids = $cfg->{ids
};
788 storage_check_enabled
($cfg, $storeid) if ($storeid);
794 foreach my $sid (keys %$ids) {
795 next if $storeid && $storeid ne $sid;
797 my $scfg = $ids->{$sid};
798 my $type = $scfg->{type
};
800 next if !storage_check_enabled
($cfg, $sid, undef, 1);
802 next if $tt eq 'iso' && !$scfg->{content
}->{iso
};
803 next if $tt eq 'vztmpl' && !$scfg->{content
}->{vztmpl
};
804 next if $tt eq 'backup' && !$scfg->{content
}->{backup
};
806 activate_storage
($cfg, $sid);
809 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
811 my $path = $plugin->get_subdir($scfg, $tt);
813 foreach my $fn (<$path/*>) {
818 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
820 $info = { volid
=> "$sid:iso/$1", format
=> 'iso' };
822 } elsif ($tt eq 'vztmpl') {
823 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
825 $info = { volid
=> "$sid:vztmpl/$1", format
=> "t$2" };
827 } elsif ($tt eq 'backup') {
828 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
830 $info = { volid
=> "$sid:backup/$1", format
=> $2 };
833 $info->{size
} = -s
$fn;
835 push @{$res->{$sid}}, $info;
840 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
848 my ($cfg, $storeid, $vmid, $vollist) = @_;
850 my $ids = $cfg->{ids
};
852 storage_check_enabled
($cfg, $storeid) if ($storeid);
856 # prepare/activate/refresh all storages
858 my $storage_list = [];
860 foreach my $volid (@$vollist) {
861 my ($sid, undef) = parse_volume_id
($volid);
862 next if !defined($ids->{$sid});
863 next if !storage_check_enabled
($cfg, $sid, undef, 1);
864 push @$storage_list, $sid;
867 foreach my $sid (keys %$ids) {
868 next if $storeid && $storeid ne $sid;
869 next if !storage_check_enabled
($cfg, $sid, undef, 1);
870 push @$storage_list, $sid;
876 activate_storage_list
($cfg, $storage_list, $cache);
878 foreach my $sid (keys %$ids) {
879 next if $storeid && $storeid ne $sid;
880 next if !storage_check_enabled
($cfg, $sid, undef, 1);
882 my $scfg = $ids->{$sid};
883 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
884 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
885 @{$res->{$sid}} = sort {lc($a->{volid
}) cmp lc ($b->{volid
}) } @{$res->{$sid}} if $res->{$sid};
892 my ($cfg, $storeid, $vmid, $content) = @_;
894 my @ctypes = qw(images vztmpl iso backup);
896 my $cts = $content ?
[ $content ] : [ @ctypes ];
898 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
901 foreach my $ct (@$cts) {
903 if ($ct eq 'images') {
904 $data = vdisk_list
($cfg, $storeid, $vmid);
905 } elsif ($ct eq 'iso' && !defined($vmid)) {
906 $data = template_list
($cfg, $storeid, 'iso');
907 } elsif ($ct eq 'vztmpl'&& !defined($vmid)) {
908 $data = template_list
($cfg, $storeid, 'vztmpl');
909 } elsif ($ct eq 'backup') {
910 $data = template_list
($cfg, $storeid, 'backup');
911 foreach my $item (@{$data->{$storeid}}) {
912 if (defined($vmid)) {
913 @{$data->{$storeid}} = grep { $_->{volid
} =~ m/\S+-$vmid-\S+/ } @{$data->{$storeid}};
918 next if !$data || !$data->{$storeid};
920 foreach my $item (@{$data->{$storeid}}) {
921 $item->{content
} = $ct;
931 my $filename = "/sys/kernel/uevent_seqnum";
934 if (my $fh = IO
::File-
>new($filename, "r")) {
936 if ($line =~ m/^(\d+)$/) {
944 sub activate_storage
{
945 my ($cfg, $storeid, $cache) = @_;
947 $cache = {} if !$cache;
949 my $scfg = storage_check_enabled
($cfg, $storeid);
951 return if $cache->{activated
}->{$storeid};
953 $cache->{uevent_seqnum
} = uevent_seqnum
() if !$cache->{uevent_seqnum
};
955 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
958 my ($baseid, undef) = parse_volume_id
($scfg->{base
});
959 activate_storage
($cfg, $baseid, $cache);
962 if (!$plugin->check_connection($storeid, $scfg)) {
963 die "storage '$storeid' is not online\n";
966 $plugin->activate_storage($storeid, $scfg, $cache);
968 my $newseq = uevent_seqnum
();
970 # only call udevsettle if there are events
971 if ($newseq > $cache->{uevent_seqnum
}) {
973 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
974 $cache->{uevent_seqnum
} = $newseq;
977 $cache->{activated
}->{$storeid} = 1;
980 sub activate_storage_list
{
981 my ($cfg, $storeid_list, $cache) = @_;
983 $cache = {} if !$cache;
985 foreach my $storeid (@$storeid_list) {
986 activate_storage
($cfg, $storeid, $cache);
990 sub deactivate_storage
{
991 my ($cfg, $storeid) = @_;
993 my $scfg = storage_config
($cfg, $storeid);
994 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
997 $plugin->deactivate_storage($storeid, $scfg, $cache);
1000 sub activate_volumes
{
1001 my ($cfg, $vollist, $snapname) = @_;
1003 return if !($vollist && scalar(@$vollist));
1005 my $storagehash = {};
1006 foreach my $volid (@$vollist) {
1007 my ($storeid, undef) = parse_volume_id
($volid);
1008 $storagehash->{$storeid} = 1;
1013 activate_storage_list
($cfg, [keys %$storagehash], $cache);
1015 foreach my $volid (@$vollist) {
1016 my ($storeid, $volname) = parse_volume_id
($volid);
1017 my $scfg = storage_config
($cfg, $storeid);
1018 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1019 $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache);
1023 sub deactivate_volumes
{
1024 my ($cfg, $vollist, $snapname) = @_;
1026 return if !($vollist && scalar(@$vollist));
1031 foreach my $volid (@$vollist) {
1032 my ($storeid, $volname) = parse_volume_id
($volid);
1034 my $scfg = storage_config
($cfg, $storeid);
1035 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1038 $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache);
1042 push @errlist, $volid;
1046 die "volume deactivation failed: " . join(' ', @errlist)
1047 if scalar(@errlist);
1051 my ($cfg, $content) = @_;
1053 my $ids = $cfg->{ids
};
1057 my @ctypes = PVE
::Tools
::split_list
($content);
1060 foreach my $storeid (keys %$ids) {
1062 next if !storage_check_enabled
($cfg, $storeid, undef, 1);
1064 if (defined($content)) {
1066 foreach my $ctype (@ctypes) {
1067 if ($ids->{$storeid}->{content
}->{$ctype}) {
1072 next if !$want_ctype;
1075 my $type = $ids->{$storeid}->{type
};
1077 $info->{$storeid} = {
1082 shared
=> $ids->{$storeid}->{shared
} ?
1 : 0,
1083 content
=> PVE
::Storage
::Plugin
::content_hash_to_string
($ids->{$storeid}->{content
}),
1087 push @$slist, $storeid;
1092 foreach my $storeid (keys %$ids) {
1093 my $scfg = $ids->{$storeid};
1094 next if !$info->{$storeid};
1096 eval { activate_storage
($cfg, $storeid, $cache); };
1102 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
1103 my ($total, $avail, $used, $active);
1104 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
1107 $info->{$storeid}->{total
} = int($total);
1108 $info->{$storeid}->{avail
} = int($avail);
1109 $info->{$storeid}->{used
} = int($used);
1110 $info->{$storeid}->{active
} = $active;
1119 my ($packed_ip, $family);
1121 my @res = PVE
::Tools
::getaddrinfo_all
($server);
1122 $family = $res[0]->{family
};
1123 $packed_ip = (PVE
::Tools
::unpack_sockaddr_in46
($res[0]->{addr
}))[2];
1125 if (defined $packed_ip) {
1126 return Socket
::inet_ntop
($family, $packed_ip);
1132 my ($server_in) = @_;
1135 if (!($server = resolv_server
($server_in))) {
1136 die "unable to resolve address for server '${server_in}'\n";
1139 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
1142 run_command
($cmd, outfunc
=> sub {
1145 # note: howto handle white spaces in export path??
1146 if ($line =~ m!^(/\S+)\s+(.+)$!) {
1156 my $cmd = ['zfs', 'list', '-t', 'filesystem', '-H', '-o', 'name,avail,used'];
1159 run_command
($cmd, outfunc
=> sub {
1162 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
1163 my ($pool, $size_str, $used_str) = ($1, $2, $3);
1164 my $size = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($size_str);
1165 my $used = PVE
::Storage
::ZFSPoolPlugin
::zfs_parse_size
($used_str);
1166 # ignore subvolumes generated by our ZFSPoolPlugin
1167 return if $pool =~ m!/subvol-\d+-[^/]+$!;
1168 return if $pool =~ m!/basevol-\d+-[^/]+$!;
1169 push @$res, { pool
=> $pool, size
=> $size, free
=> $size-$used };
1177 my ($portal, $noerr) = @_;
1179 my ($server, $port) = PVE
::Tools
::parse_host_and_port
($portal);
1181 if (my $ip = resolv_server
($server)) {
1183 $server = "[$server]" if $server =~ /^$IPV6RE$/;
1184 return $port ?
"$server:$port" : $server;
1187 return undef if $noerr;
1189 raise_param_exc
({ portal
=> "unable to resolve portal address '$portal'" });
1192 # idea is from usbutils package (/usr/bin/usb-devices) script
1193 sub __scan_usb_device
{
1194 my ($res, $devpath, $parent, $level) = @_;
1196 return if ! -d
$devpath;
1197 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
1198 my $port = $level ?
int($1 - 1) : 0;
1200 my $busnum = int(file_read_firstline
("$devpath/busnum"));
1201 my $devnum = int(file_read_firstline
("$devpath/devnum"));
1208 speed
=> file_read_firstline
("$devpath/speed"),
1209 class => hex(file_read_firstline
("$devpath/bDeviceClass")),
1210 vendid
=> file_read_firstline
("$devpath/idVendor"),
1211 prodid
=> file_read_firstline
("$devpath/idProduct"),
1215 my $usbpath = $devpath;
1216 $usbpath =~ s
|^.*/\d
+\
-||;
1217 $d->{usbpath
} = $usbpath;
1220 my $product = file_read_firstline
("$devpath/product");
1221 $d->{product
} = $product if $product;
1223 my $manu = file_read_firstline
("$devpath/manufacturer");
1224 $d->{manufacturer
} = $manu if $manu;
1226 my $serial => file_read_firstline
("$devpath/serial");
1227 $d->{serial
} = $serial if $serial;
1231 foreach my $subdev (<$devpath/$busnum-*>) {
1232 next if $subdev !~ m
|/$busnum-[0-9]+(\
.[0-9]+)*$|;
1233 __scan_usb_device
($res, $subdev, $devnum, $level + 1);
1242 foreach my $device (</sys/bus
/usb
/devices
/usb
*>) {
1243 __scan_usb_device
($devlist, $device, 0, 0);
1250 my ($portal_in) = @_;
1253 if (!($portal = resolv_portal
($portal_in))) {
1254 die "unable to parse/resolve portal address '${portal_in}'\n";
1257 return PVE
::Storage
::ISCSIPlugin
::iscsi_discovery
($portal);
1260 sub storage_default_format
{
1261 my ($cfg, $storeid) = @_;
1263 my $scfg = storage_config
($cfg, $storeid);
1265 return PVE
::Storage
::Plugin
::default_format
($scfg);
1268 sub vgroup_is_used
{
1269 my ($cfg, $vgname) = @_;
1271 foreach my $storeid (keys %{$cfg->{ids
}}) {
1272 my $scfg = storage_config
($cfg, $storeid);
1273 if ($scfg->{type
} eq 'lvm' && $scfg->{vgname
} eq $vgname) {
1281 sub target_is_used
{
1282 my ($cfg, $target) = @_;
1284 foreach my $storeid (keys %{$cfg->{ids
}}) {
1285 my $scfg = storage_config
($cfg, $storeid);
1286 if ($scfg->{type
} eq 'iscsi' && $scfg->{target
} eq $target) {
1294 sub volume_is_used
{
1295 my ($cfg, $volid) = @_;
1297 foreach my $storeid (keys %{$cfg->{ids
}}) {
1298 my $scfg = storage_config
($cfg, $storeid);
1299 if ($scfg->{base
} && $scfg->{base
} eq $volid) {
1307 sub storage_is_used
{
1308 my ($cfg, $storeid) = @_;
1310 foreach my $sid (keys %{$cfg->{ids
}}) {
1311 my $scfg = storage_config
($cfg, $sid);
1312 next if !$scfg->{base
};
1313 my ($st) = parse_volume_id
($scfg->{base
});
1314 return 1 if $st && $st eq $storeid;
1321 my ($list, $func) = @_;
1325 foreach my $sid (keys %$list) {
1326 foreach my $info (@{$list->{$sid}}) {
1327 my $volid = $info->{volid
};
1328 my ($sid1, $volname) = parse_volume_id
($volid, 1);
1329 if ($sid1 && $sid1 eq $sid) {
1330 &$func ($volid, $sid, $info);
1332 warn "detected strange volid '$volid' in volume list for '$sid'\n";
1338 sub extract_vzdump_config_tar
{
1339 my ($archive, $conf_re) = @_;
1341 die "ERROR: file '$archive' does not exist\n" if ! -f
$archive;
1343 my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) ||
1344 die "unable to open file '$archive'\n";
1347 while (defined($file = <$fh>)) {
1348 if ($file =~ $conf_re) {
1349 $file = $1; # untaint
1358 die "ERROR: archive contains no configuration file\n" if !$file;
1364 $raw .= "$output\n";
1367 PVE
::Tools
::run_command
(['tar', '-xpOf', $archive, $file, '--occurrence'], outfunc
=> $out);
1369 return wantarray ?
($raw, $file) : $raw;
1372 sub extract_vzdump_config_vma
{
1373 my ($archive, $comp) = @_;
1379 $raw .= "$output\n";
1385 if ($comp eq 'gz') {
1386 $uncomp = ["zcat", $archive];
1387 } elsif ($comp eq 'lzo') {
1388 $uncomp = ["lzop", "-d", "-c", $archive];
1390 die "unknown compression method '$comp'\n";
1392 $cmd = [$uncomp, ["vma", "config", "-"]];
1394 # in some cases, lzop/zcat exits with 1 when its stdout pipe is
1395 # closed early by vma, detect this and ignore the exit code later
1400 if ($output =~ m/lzop: Broken pipe: <stdout>/ || $output =~ m/gzip: stdout: Broken pipe/) {
1402 } elsif (!defined ($errstring) && $output !~ m/^\s*$/) {
1403 $errstring = "Failed to extract config from VMA archive: $output\n";
1407 # in other cases, the pipeline will exit with exit code 141
1408 # because of the broken pipe, handle / ignore this as well
1411 $rc = PVE
::Tools
::run_command
($cmd, outfunc
=> $out, errfunc
=> $err, noerr
=> 1);
1415 # use exit code if no stderr output and not just broken pipe
1416 if (!$errstring && !$broken_pipe && $rc != 0 && $rc != 141) {
1417 die "$rerr\n" if $rerr;
1418 die "config extraction failed with exit code $rc\n";
1420 die "$errstring\n" if $errstring;
1422 # simple case without compression and weird piping behaviour
1423 PVE
::Tools
::run_command
(["vma", "config", $archive], outfunc
=> $out);
1426 return wantarray ?
($raw, undef) : $raw;
1429 sub extract_vzdump_config
{
1430 my ($cfg, $volid) = @_;
1432 my $archive = abs_filesystem_path
($cfg, $volid);
1434 if ($volid =~ /vzdump-(lxc|openvz)-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|(tar(\.(gz|lzo))?))$/) {
1435 return extract_vzdump_config_tar
($archive, qr!^(\./etc/vzdump/(pct|vps)\.conf)$!);
1436 } elsif ($volid =~ /vzdump-qemu-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|((tar|vma)(\.(gz|lzo))?))$/) {
1444 $comp = $11 if defined($11);
1447 if ($format eq 'tar') {
1448 return extract_vzdump_config_tar
($archive, qr!\(\./qemu-server\.conf\)!);
1450 return extract_vzdump_config_vma
($archive, $comp);
1453 die "cannot determine backup guest type for backup archive '$volid'\n";
1457 # bash completion helper
1459 sub complete_storage
{
1460 my ($cmdname, $pname, $cvalue) = @_;
1462 my $cfg = PVE
::Storage
::config
();
1464 return $cmdname eq 'add' ?
[] : [ PVE
::Storage
::storage_ids
($cfg) ];
1467 sub complete_storage_enabled
{
1468 my ($cmdname, $pname, $cvalue) = @_;
1472 my $cfg = PVE
::Storage
::config
();
1473 foreach my $sid (keys %{$cfg->{ids
}}) {
1474 next if !storage_check_enabled
($cfg, $sid, undef, 1);
1480 sub complete_content_type
{
1481 my ($cmdname, $pname, $cvalue) = @_;
1483 return [qw(rootdir images vztmpl iso backup)];
1486 sub complete_volume
{
1487 my ($cmdname, $pname, $cvalue) = @_;
1491 my $storage_list = complete_storage_enabled
();
1493 if ($cvalue =~ m/^([^:]+):/) {
1494 $storage_list = [ $1 ];
1496 if (scalar(@$storage_list) > 1) {
1497 # only list storage IDs to avoid large listings
1499 foreach my $storeid (@$storage_list) {
1500 # Hack: simply return 2 artificial values, so that
1501 # completions does not finish
1502 push @$res, "$storeid:volname", "$storeid:...";
1509 foreach my $storeid (@$storage_list) {
1510 my $vollist = PVE
::Storage
::volume_list
($cfg, $storeid);
1512 foreach my $item (@$vollist) {
1513 push @$res, $item->{volid
};