[pve-devel] [PATCH container v2 1/2] implement lxc restart migration

Dominik Csapak d.csapak at proxmox.com
Fri Dec 2 11:42:50 CET 2016


this checks for the 'restart' parameter and if given, shuts down the
container with a optionally defined timeout (default 180s), and
continues the migration like an offline one.

after finishing, we start the container on the target node

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
changes to v1:
* do not start ct if it was stopped before restart migration
 src/PVE/LXC/Migrate.pm | 34 ++++++++++++++++++++++++++++++----
 1 file changed, 30 insertions(+), 4 deletions(-)

diff --git a/src/PVE/LXC/Migrate.pm b/src/PVE/LXC/Migrate.pm
index 73af6d4..6b9687b 100644
--- a/src/PVE/LXC/Migrate.pm
+++ b/src/PVE/LXC/Migrate.pm
@@ -23,6 +23,7 @@ sub prepare {
     my ($self, $vmid) = @_;
 
     my $online = $self->{opts}->{online};
+    my $restart= $self->{opts}->{restart};
 
     $self->{storecfg} = PVE::Storage::config();
 
@@ -33,11 +34,11 @@ sub prepare {
 
     my $running = 0;
     if (PVE::LXC::check_running($vmid)) {
-	die "lxc live migration is currently not implemented\n";
-
-	die "can't migrate running container without --online\n" if !$online;
+	die "lxc live migration is currently not implemented\n" if $online;
+	die "running container can only be migrated in restart mode" if !$restart;
 	$running = 1;
     }
+    $self->{was_running} = $running;
 
     my $force = $self->{opts}->{force} // 0;
     my $need_activate = [];
@@ -78,8 +79,9 @@ sub prepare {
 	    # only activate if not shared
 	    push @$need_activate, $volid;
 
+	    # unless in restart mode because we shut the container down
 	    die "unable to migrate local mount point '$volid' while CT is running"
-		if $running;
+		if $running && !$restart;
 	}
 
     });
@@ -93,6 +95,22 @@ sub prepare {
     eval { $self->cmd_quiet($cmd); };
     die "Can't connect to destination address using public key\n" if $@;
 
+    # in restart mode, we shutdown the container before migrating
+    if ($restart && $running) {
+	my $timeout = $self->{opts}->{timeout} // 180;
+
+	$self->log('info', "shutdown CT $vmid\n");
+
+	my $cmd = ['lxc-stop', '-n', $vmid, '--timeout', $timeout];
+	$self->cmd($cmd, timeout => $timeout + 5);
+
+	# make sure container is stopped
+	$cmd = ['lxc-wait',  '-n', $vmid, '-t', 5, '-s', 'STOPPED'];
+	$self->cmd($cmd);
+
+	$running = 0;
+    }
+
     return $running;
 }
 
@@ -322,6 +340,14 @@ sub final_cleanup {
 	my $cmd = [ @{$self->{rem_ssh}}, 'pct', 'unlock', $vmid ];
 	$self->cmd_logerr($cmd, errmsg => "failed to clear migrate lock");	
     }
+
+    # in restart mode, we start the container on the target node
+    # after migration
+    if ($self->{opts}->{restart} && $self->{was_running}) {
+	$self->log('info', "start container on target node");
+	my $cmd = [ @{$self->{rem_ssh}}, 'pct', 'start', $vmid];
+	$self->cmd($cmd);
+    }
 }
 
 1;
-- 
2.1.4





More information about the pve-devel mailing list