[pve-devel] [PATCH v2 storage 15/28] storage_migrate: return volume ID of migrated volume

Fabian Ebner f.ebner at proxmox.com
Mon Feb 24 13:44:05 CET 2020


In preparation to introduce an allow_rename parameter.

Signed-off-by: Fabian Ebner <f.ebner at proxmox.com>
---
 PVE/CLI/pvesm.pm |  2 +-
 PVE/Storage.pm   | 41 ++++++++++++++++++++++++++++++++---------
 2 files changed, 33 insertions(+), 10 deletions(-)

diff --git a/PVE/CLI/pvesm.pm b/PVE/CLI/pvesm.pm
index 7b83ec8..c55cdd0 100755
--- a/PVE/CLI/pvesm.pm
+++ b/PVE/CLI/pvesm.pm
@@ -779,7 +779,7 @@ our $cmddef = {
     export => [ __PACKAGE__, 'export', ['volume', 'format', 'filename']],
     import => [ __PACKAGE__, 'import', ['volume', 'format', 'filename'], {}, sub  {
 	my $volid = shift;
-	print "successfully imported '$volid'\n";
+	print PVE::Storage::volume_imported_message($volid);
     }],
 };
 
diff --git a/PVE/Storage.pm b/PVE/Storage.pm
index 38341f6..cb07066 100755
--- a/PVE/Storage.pm
+++ b/PVE/Storage.pm
@@ -571,7 +571,7 @@ sub storage_migrate {
     my $scfg = storage_config($cfg, $storeid);
 
     # no need to migrate shared content
-    return if $storeid eq $target_storeid && $scfg->{shared};
+    return $volid if $storeid eq $target_storeid && $scfg->{shared};
 
     my $tcfg = storage_config($cfg, $target_storeid);
 
@@ -620,6 +620,19 @@ sub storage_migrate {
 	push @$recv, '-base', $base_snapshot;
     }
 
+    my $new_volid;
+    my $pattern = volume_imported_message(undef, 1);
+    my $match_volid_and_log = sub {
+	my $line = shift;
+
+	$new_volid = $1 if ($line =~ m!$pattern!);
+
+	if ($logfunc) {
+	    chomp($line);
+	    $logfunc->($line);
+	}
+    };
+
     volume_snapshot($cfg, $volid, $snapshot) if $migration_snapshot;
     eval {
 	if ($insecure) {
@@ -637,13 +650,8 @@ sub storage_migrate {
 	    shutdown($socket, 1);
 
 	    # wait for the remote process to finish
-	    if ($logfunc) {
-		while (my $line = <$info>) {
-		    chomp($line);
-		    $logfunc->("[$target_sshinfo->{name}] $line");
-		}
-	    } else {
-		1 while <$info>;
+	    while (my $line = <$info>) {
+		$match_volid_and_log->("[$target_sshinfo->{name}] $line");
 	    }
 
 	    # now close the socket
@@ -653,8 +661,11 @@ sub storage_migrate {
 		die "import failed: exit code ".($?>>8)."\n";
 	    }
 	} else {
-	    run_command([$send, @cstream, $recv], logfunc => $logfunc);
+	    run_command([$send, @cstream, $recv], logfunc => $match_volid_and_log);
 	}
+
+	die "unable to get ID of the migrated volume\n"
+	    if !defined($new_volid);
     };
     my $err = $@;
     warn "send/receive failed, cleaning up snapshot(s)..\n" if $err;
@@ -663,6 +674,8 @@ sub storage_migrate {
 	warn "could not remove source snapshot: $@\n" if $@;
     }
     die $err if $err;
+
+    return $new_volid;
 }
 
 sub vdisk_clone {
@@ -1461,6 +1474,16 @@ sub volume_transfer_formats {
     return @common;
 }
 
+sub volume_imported_message {
+    my ($volid, $want_pattern) = @_;
+
+    if ($want_pattern) {
+	return "successfully imported '([^']*)'\$";
+    } else {
+	return "successfully imported '$volid'\n";
+    }
+}
+
 # bash completion helper
 
 sub complete_storage {
-- 
2.20.1





More information about the pve-devel mailing list