[pve-devel] [PATCH storage 1/2] Add flag $map so container images can be mapped

Alwin Antreich a.antreich at proxmox.com
Fri Aug 17 15:44:26 CEST 2018


This allows methods to request a mapped device/path explicitly,
regardles of the storage option, eg. krbd option in the RBDplugin.

Signed-off-by: Alwin Antreich <a.antreich at proxmox.com>
---
 PVE/CLI/pvesm.pm      |  6 ++---
 PVE/Storage.pm        | 63 +++++++++++++++++++++++++--------------------------
 PVE/Storage/Plugin.pm | 36 ++++++++++++++---------------
 3 files changed, 52 insertions(+), 53 deletions(-)

diff --git a/PVE/CLI/pvesm.pm b/PVE/CLI/pvesm.pm
index d95b5f5..2bb31bd 100755
--- a/PVE/CLI/pvesm.pm
+++ b/PVE/CLI/pvesm.pm
@@ -238,7 +238,7 @@ __PACKAGE__->register_method ({
 	eval {
 	    my $cfg = PVE::Storage::config();
 	    PVE::Storage::volume_export($cfg, $outfh, $param->{volume}, $param->{format},
-		$param->{snapshot}, $param->{base}, $param->{'with-snapshots'});
+		$param->{snapshot}, $param->{base}, $param->{'with-snapshots'}, 1);
 	};
 	my $err = $@;
 	if ($filename ne '-') {
@@ -314,8 +314,8 @@ __PACKAGE__->register_method ({
 	my $volume = $param->{volume};
 	my $delete = $param->{'delete-snapshot'};
 	PVE::Storage::volume_import($cfg, $infh, $volume, $param->{format},
-	    $param->{base}, $param->{'with-snapshots'});
-	PVE::Storage::volume_snapshot_delete($cfg, $volume, $delete)
+	    $param->{base}, $param->{'with-snapshots'}, 1);
+	PVE::Storage::volume_snapshot_delete($cfg, $volume, $delete, undef, 1)
 	    if defined($delete);
 	return;
     }
diff --git a/PVE/Storage.pm b/PVE/Storage.pm
index f9732fe..771054d 100755
--- a/PVE/Storage.pm
+++ b/PVE/Storage.pm
@@ -2,7 +2,6 @@ package PVE::Storage;
 
 use strict;
 use warnings;
-use Data::Dumper;
 
 use POSIX;
 use IO::Select;
@@ -188,13 +187,13 @@ sub volume_size_info {
 }
 
 sub volume_resize {
-    my ($cfg, $volid, $size, $running) = @_;
+    my ($cfg, $volid, $size, $running, $map) = @_;
 
     my ($storeid, $volname) = parse_volume_id($volid, 1);
     if ($storeid) {
         my $scfg = storage_config($cfg, $storeid);
         my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
-        return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running);
+        return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running, $map);
     } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
         die "resize file/device '$volid' is not possible\n";
     } else {
@@ -249,13 +248,13 @@ sub volume_snapshot_rollback {
 }
 
 sub volume_snapshot_delete {
-    my ($cfg, $volid, $snap, $running) = @_;
+    my ($cfg, $volid, $snap, $running, $map) = @_;
 
     my ($storeid, $volname) = parse_volume_id($volid, 1);
     if ($storeid) {
         my $scfg = storage_config($cfg, $storeid);
         my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
-        return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running);
+        return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running, $map);
     } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
         die "snapshot delete file/device '$volid' is not possible\n";
     } else {
@@ -445,7 +444,7 @@ sub volume_is_base_and_used {
 
 # try to map a filesystem path to a volume identifier
 sub path_to_volume_id {
-    my ($cfg, $path) = @_;
+    my ($cfg, $path, $map) = @_;
 
     my $ids = $cfg->{ids};
 
@@ -482,7 +481,7 @@ sub path_to_volume_id {
 	    my $vollist = $plugin->list_images($sid, $scfg, $vmid);
 	    foreach my $info (@$vollist) {
 		my ($storeid, $volname) = parse_volume_id($info->{volid});
-		my $volpath = $plugin->path($scfg, $volname, $storeid);
+		my $volpath = $plugin->path($scfg, $volname, $storeid, $map);
 		if ($volpath eq $path) {
 		    return ('images', $info->{volid});
 		}
@@ -507,14 +506,14 @@ sub path_to_volume_id {
 }
 
 sub path {
-    my ($cfg, $volid, $snapname) = @_;
+    my ($cfg, $volid, $snapname, $map) = @_;
 
     my ($storeid, $volname) = parse_volume_id($volid);
 
     my $scfg = storage_config($cfg, $storeid);
 
     my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
-    my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname);
+    my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname, $map);
     return wantarray ? ($path, $owner, $vtype) : $path;
 }
 
@@ -524,7 +523,7 @@ sub abs_filesystem_path {
     my $path;
     if (PVE::Storage::parse_volume_id ($volid, 1)) {
 	PVE::Storage::activate_volumes($cfg, [ $volid ]);
-	$path = PVE::Storage::path($cfg, $volid);
+	$path = PVE::Storage::path($cfg, $volid, undef);
     } else {
 	if (-f $volid) {
 	    my $abspath = abs_path($volid);
@@ -540,7 +539,7 @@ sub abs_filesystem_path {
 }
 
 sub storage_migrate {
-    my ($cfg, $volid, $target_sshinfo, $target_storeid, $target_volname, $base_snapshot, $snapshot, $ratelimit_bps, $insecure, $with_snapshots, $logfunc) = @_;
+    my ($cfg, $volid, $target_sshinfo, $target_storeid, $target_volname, $base_snapshot, $snapshot, $ratelimit_bps, $insecure, $with_snapshots, $logfunc, $map) = @_;
 
     my ($storeid, $volname) = parse_volume_id($volid);
     $target_volname = $volname if !$target_volname;
@@ -572,7 +571,7 @@ sub storage_migrate {
 	}
     }
 
-    my @formats = volume_transfer_formats($cfg, $volid, $volid, $snapshot, $base_snapshot, $with_snapshots);
+    my @formats = volume_transfer_formats($cfg, $volid, $volid, $snapshot, $base_snapshot, $with_snapshots, $map);
     die "cannot migrate from storage type '$scfg->{type}' to '$tcfg->{type}'\n" if !@formats;
     my $format = $formats[0];
 
@@ -629,14 +628,14 @@ sub storage_migrate {
     my $err = $@;
     warn "send/receive failed, cleaning up snapshot(s)..\n" if $err;
     if ($migration_snapshot) {
-	eval { volume_snapshot_delete($cfg, $volid, $snapshot, 0) };
+	eval { volume_snapshot_delete($cfg, $volid, $snapshot, 0, $map) };
 	warn "could not remove source snapshot: $@\n" if $@;
     }
     die $err if $err;
 }
 
 sub vdisk_clone {
-    my ($cfg, $volid, $vmid, $snap) = @_;
+    my ($cfg, $volid, $vmid, $snap, $map) = @_;
 
     my ($storeid, $volname) = parse_volume_id($volid);
 
@@ -648,7 +647,7 @@ sub vdisk_clone {
 
     # lock shared storage
     return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
-	my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
+	my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap, $map);
 	return "$storeid:$volname";
     });
 }
@@ -672,7 +671,7 @@ sub vdisk_create_base {
 }
 
 sub vdisk_alloc {
-    my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
+    my ($cfg, $storeid, $vmid, $fmt, $name, $size, $map) = @_;
 
     die "no storage ID specified\n" if !$storeid;
 
@@ -695,7 +694,7 @@ sub vdisk_alloc {
     # lock shared storage
     return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
 	my $old_umask = umask(umask|0037);
-	my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) };
+	my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size, $map) };
 	my $err = $@;
 	umask $old_umask;
 	die $err if $err;
@@ -704,7 +703,7 @@ sub vdisk_alloc {
 }
 
 sub vdisk_free {
-    my ($cfg, $volid) = @_;
+    my ($cfg, $volid, $map) = @_;
 
     my ($storeid, $volname) = parse_volume_id($volid);
     my $scfg = storage_config($cfg, $storeid);
@@ -722,7 +721,7 @@ sub vdisk_free {
 
 	my (undef, undef, undef, undef, undef, $isBase, $format) =
 	    $plugin->parse_volname($volname);
-	$cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format);
+	$cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format, $map);
     });
 
     return if !$cleanup_worker;
@@ -955,7 +954,7 @@ sub deactivate_storage {
 }
 
 sub activate_volumes {
-    my ($cfg, $vollist, $snapname) = @_;
+    my ($cfg, $vollist, $snapname, $map) = @_;
 
     return if !($vollist && scalar(@$vollist));
 
@@ -973,12 +972,12 @@ sub activate_volumes {
 	my ($storeid, $volname) = parse_volume_id($volid);
 	my $scfg = storage_config($cfg, $storeid);
 	my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
-	$plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache);
+	$plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache, $map);
     }
 }
 
 sub deactivate_volumes {
-    my ($cfg, $vollist, $snapname) = @_;
+    my ($cfg, $vollist, $snapname, $map) = @_;
 
     return if !($vollist && scalar(@$vollist));
 
@@ -992,7 +991,7 @@ sub deactivate_volumes {
 	my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
 
 	eval {
-	    $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache);
+	    $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache, $map);
 	};
 	if (my $err = $@) {
 	    warn $err;
@@ -1456,29 +1455,29 @@ sub extract_vzdump_config {
 }
 
 sub volume_export {
-    my ($cfg, $fh, $volid, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
+    my ($cfg, $fh, $volid, $format, $snapshot, $base_snapshot, $with_snapshots, $map) = @_;
 
     my ($storeid, $volname) = parse_volume_id($volid, 1);
     die "cannot export volume '$volid'\n" if !$storeid;
     my $scfg = storage_config($cfg, $storeid);
     my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
     return $plugin->volume_export($scfg, $storeid, $fh, $volname, $format,
-                                  $snapshot, $base_snapshot, $with_snapshots);
+                                  $snapshot, $base_snapshot, $with_snapshots, $map);
 }
 
 sub volume_import {
-    my ($cfg, $fh, $volid, $format, $base_snapshot, $with_snapshots) = @_;
+    my ($cfg, $fh, $volid, $format, $base_snapshot, $with_snapshots, $map) = @_;
 
     my ($storeid, $volname) = parse_volume_id($volid, 1);
     die "cannot import into volume '$volid'\n" if !$storeid;
     my $scfg = storage_config($cfg, $storeid);
     my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
     return $plugin->volume_import($scfg, $storeid, $fh, $volname, $format,
-                                  $base_snapshot, $with_snapshots);
+                                  $base_snapshot, $with_snapshots, $map);
 }
 
 sub volume_export_formats {
-    my ($cfg, $volid, $snapshot, $base_snapshot, $with_snapshots) = @_;
+    my ($cfg, $volid, $snapshot, $base_snapshot, $with_snapshots, $map) = @_;
 
     my ($storeid, $volname) = parse_volume_id($volid, 1);
     return if !$storeid;
@@ -1486,7 +1485,7 @@ sub volume_export_formats {
     my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
     return $plugin->volume_export_formats($scfg, $storeid, $volname,
                                           $snapshot, $base_snapshot,
-                                          $with_snapshots);
+                                          $with_snapshots, $map);
 }
 
 sub volume_import_formats {
@@ -1501,9 +1500,9 @@ sub volume_import_formats {
 }
 
 sub volume_transfer_formats {
-    my ($cfg, $src_volid, $dst_volid, $snapshot, $base_snapshot, $with_snapshots) = @_;
-    my @export_formats = volume_export_formats($cfg, $src_volid, $snapshot, $base_snapshot, $with_snapshots);
-    my @import_formats = volume_import_formats($cfg, $dst_volid, $base_snapshot, $with_snapshots);
+    my ($cfg, $src_volid, $dst_volid, $snapshot, $base_snapshot, $with_snapshots, $map) = @_;
+    my @export_formats = volume_export_formats($cfg, $src_volid, $snapshot, $base_snapshot, $with_snapshots, $map);
+    my @import_formats = volume_import_formats($cfg, $dst_volid, $base_snapshot, $with_snapshots, $map);
     my %import_hash = map { $_ => 1 } @import_formats;
     my @common = grep { $import_hash{$_} } @export_formats;
     return @common;
diff --git a/PVE/Storage/Plugin.pm b/PVE/Storage/Plugin.pm
index 7db3a95..fef71b5 100644
--- a/PVE/Storage/Plugin.pm
+++ b/PVE/Storage/Plugin.pm
@@ -473,7 +473,7 @@ sub filesystem_path {
 }
 
 sub path {
-    my ($class, $scfg, $volname, $storeid, $snapname) = @_;
+    my ($class, $scfg, $volname, $storeid, $snapname, $map) = @_;
 
     return $class->filesystem_path($scfg, $volname, $snapname);
 }
@@ -544,7 +544,7 @@ my $find_free_diskname = sub {
 };
 
 sub clone_image {
-    my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
+    my ($class, $scfg, $storeid, $volname, $vmid, $snap, $map) = @_;
 
     # this only works for file based storage types
     die "storage definintion has no path\n" if !$scfg->{path};
@@ -590,7 +590,7 @@ sub clone_image {
 }
 
 sub alloc_image {
-    my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
+    my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size, $map) = @_;
 
     my $imagedir = $class->get_subdir($scfg, 'images');
     $imagedir .= "/$vmid";
@@ -631,7 +631,7 @@ sub alloc_image {
 }
 
 sub free_image {
-    my ($class, $storeid, $scfg, $volname, $isBase, $format) = @_;
+    my ($class, $storeid, $scfg, $volname, $isBase, $format, $map) = @_;
 
     my $path = $class->filesystem_path($scfg, $volname);
 
@@ -704,7 +704,7 @@ sub volume_size_info {
 }
 
 sub volume_resize {
-    my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
+    my ($class, $scfg, $storeid, $volname, $size, $running, $map) = @_;
 
     die "can't resize this image format\n" if $volname !~ m/\.(raw|qcow2)$/;
 
@@ -756,7 +756,7 @@ sub volume_snapshot_rollback {
 }
 
 sub volume_snapshot_delete {
-    my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
+    my ($class, $scfg, $storeid, $volname, $snap, $running, $map) = @_;
 
     die "can't delete snapshot for this image format\n" if $volname !~ m/\.(qcow2|qed)$/;
 
@@ -764,7 +764,7 @@ sub volume_snapshot_delete {
 
     my $path = $class->filesystem_path($scfg, $volname);
 
-    $class->deactivate_volume($storeid, $scfg, $volname, $snap, {});
+    $class->deactivate_volume($storeid, $scfg, $volname, $snap, {}, $map);
 
     my $cmd = ['/usr/bin/qemu-img', 'snapshot','-d', $snap, $path];
 
@@ -913,7 +913,7 @@ sub deactivate_storage {
 }
 
 sub activate_volume {
-    my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
+    my ($class, $storeid, $scfg, $volname, $snapname, $cache, $map) = @_;
 
     my $path = $class->filesystem_path($scfg, $volname, $snapname);
 
@@ -926,7 +926,7 @@ sub activate_volume {
 }
 
 sub deactivate_volume {
-    my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
+    my ($class, $storeid, $scfg, $volname, $snapname, $cache, $map) = @_;
 
     # do nothing by default
 }
@@ -982,9 +982,9 @@ sub read_common_header($) {
 
 # Export a volume into a file handle as a stream of desired format.
 sub volume_export {
-    my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
+    my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots, $map) = @_;
     if ($scfg->{path} && !defined($snapshot) && !defined($base_snapshot)) {
-	my $file = $class->path($scfg, $volname, $storeid)
+	my $file = $class->path($scfg, $volname, $storeid, undef, undef, $map)
 	    or goto unsupported;
 	my ($size, $file_format) = file_size_info($file);
 
@@ -1017,9 +1017,9 @@ sub volume_export {
 }
 
 sub volume_export_formats {
-    my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
+    my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots, $map) = @_;
     if ($scfg->{path} && !defined($snapshot) && !defined($base_snapshot)) {
-	my $file = $class->path($scfg, $volname, $storeid)
+	my $file = $class->path($scfg, $volname, $storeid, undef, undef, $map)
 	    or return;
 	my ($size, $format) = file_size_info($file);
 
@@ -1035,7 +1035,7 @@ sub volume_export_formats {
 
 # Import data from a stream, creating a new or replacing or adding to an existing volume.
 sub volume_import {
-    my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_;
+    my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots, $map) = @_;
 
     die "volume import format '$format' not available for $class\n"
 	if $format !~ /^(raw|tar|qcow2|vmdk)\+size$/;
@@ -1056,20 +1056,20 @@ sub volume_import {
 
     # Check for an existing file first since interrupting alloc_image doesn't
     # free it.
-    my $file = $class->path($scfg, $volname, $storeid);
+    my $file = $class->path($scfg, $volname, $storeid, undef, undef, $map);
     die "file '$file' already exists\n" if -e $file;
 
     my ($size) = read_common_header($fh);
     $size = int($size/1024);
 
     eval {
-	my $allocname = $class->alloc_image($storeid, $scfg, $vmid, $file_format, $name, $size);
+	my $allocname = $class->alloc_image($storeid, $scfg, $vmid, $file_format, $name, $size, $map);
 	if ($allocname ne $volname) {
 	    my $oldname = $volname;
 	    $volname = $allocname; # Let the cleanup code know what to free
 	    die "internal error: unexpected allocated name: '$allocname' != '$oldname'\n";
 	}
-	my $file = $class->path($scfg, $volname, $storeid)
+	my $file = $class->path($scfg, $volname, $storeid, undef, undef, $map)
 	    or die "internal error: failed to get path to newly allocated volume $volname\n";
 	if ($data_format eq 'raw' || $data_format eq 'qcow2' || $data_format eq 'vmdk') {
 	    run_command(['dd', "of=$file", 'conv=sparse', 'bs=64k'],
@@ -1082,7 +1082,7 @@ sub volume_import {
 	}
     };
     if (my $err = $@) {
-	eval { $class->free_image($storeid, $scfg, $volname, 0, $file_format) };
+	eval { $class->free_image($storeid, $scfg, $volname, 0, $file_format, $map) };
 	warn $@ if $@;
 	die $err;
     }
-- 
2.11.0





More information about the pve-devel mailing list