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

Dominik Csapak d.csapak at proxmox.com
Thu Dec 1 17:19:29 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>
---
 src/PVE/LXC/Migrate.pm | 38 +++++++++++++++++++++++++++++++-------
 1 file changed, 31 insertions(+), 7 deletions(-)

diff --git a/src/PVE/LXC/Migrate.pm b/src/PVE/LXC/Migrate.pm
index 73af6d4..137a3ee 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();
 
@@ -31,12 +32,10 @@ sub prepare {
 
     PVE::LXC::Config->check_lock($conf);
 
-    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;
-	$running = 1;
+    my $running = PVE::LXC::check_running($vmid);
+    if ($running) {
+	die "lxc live migration is currently not implemented\n" if $online;
+	die "can't migrate running container without --restart\n" if !$restart;
     }
 
     my $force = $self->{opts}->{force} // 0;
@@ -78,8 +77,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 +93,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 +338,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->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