]> git.proxmox.com Git - pve-container.git/commitdiff
implement lxc restart migration
authorDominik Csapak <d.csapak@proxmox.com>
Fri, 2 Dec 2016 10:42:50 +0000 (11:42 +0100)
committerDietmar Maurer <dietmar@proxmox.com>
Fri, 2 Dec 2016 11:06:46 +0000 (12:06 +0100)
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@proxmox.com>
src/PVE/LXC/Migrate.pm

index 73af6d4bf9134fd3b1b181e77b01e1d45dd67468..6b9687b8904735ce8e8e6fc60999b2a0bb24aa19 100644 (file)
@@ -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;