]> git.proxmox.com Git - qemu-server.git/blobdiff - PVE/QemuMigrate.pm
migrate: unlink unix socket before starting migration
[qemu-server.git] / PVE / QemuMigrate.pm
index 6c734cac7b4562ee6dad096e99c7482f6e11cdaa..baf736421c453bca79a8954a0068eb29f8a1c1bc 100644 (file)
@@ -5,6 +5,7 @@ use warnings;
 use PVE::AbstractMigrate;
 use IO::File;
 use IPC::Open2;
+use POSIX qw( WNOHANG );
 use PVE::INotify;
 use PVE::Tools;
 use PVE::Cluster;
@@ -44,17 +45,28 @@ sub fork_command_pipe {
 sub finish_command_pipe {
     my ($self, $cmdpipe, $timeout) = @_;
 
+    my $cpid = $cmdpipe->{pid};
+    return if !defined($cpid);
+
     my $writer = $cmdpipe->{writer};
     my $reader = $cmdpipe->{reader};
 
     $writer->close();
     $reader->close();
 
-    my $cpid = $cmdpipe->{pid};
+    my $collect_child_process = sub {
+       my $res = waitpid($cpid, WNOHANG);
+       if (defined($res) && ($res == $cpid)) {
+           delete $cmdpipe->{cpid};
+           return 1;
+       } else {
+           return 0;
+       }
+     };
 
     if ($timeout) {
        for (my $i = 0; $i < $timeout; $i++) {
-           return if !PVE::ProcFSTools::check_process_running($cpid);
+           return if &$collect_child_process();
            sleep(1);
        }
     }
@@ -64,21 +76,24 @@ sub finish_command_pipe {
 
     # wait again
     for (my $i = 0; $i < 10; $i++) {
-       return if !PVE::ProcFSTools::check_process_running($cpid);
+       return if &$collect_child_process();
        sleep(1);
     }
 
     $self->log('info', "ssh tunnel still running - terminating now with SIGKILL\n");
     kill 9, $cpid;
     sleep 1;
+
+    $self->log('err', "ssh tunnel child process (PID $cpid) couldn't be collected\n")
+       if !&$collect_child_process();
 }
 
 sub fork_tunnel {
-    my ($self, $nodeip, $lport, $rport) = @_;
+    my ($self, $tunnel_addr) = @_;
 
-    my @localtunnelinfo = $lport ? ('-L' , "$lport:localhost:$rport" ) : ();
+    my @localtunnelinfo = ('-L' , $tunnel_addr );
 
-    my $cmd = [@{$self->{rem_ssh}}, @localtunnelinfo, 'qm', 'mtunnel' ];
+    my $cmd = [@{$self->{rem_ssh}}, '-o ExitOnForwardFailure=yes', @localtunnelinfo, 'qm', 'mtunnel' ];
 
     my $tunnel = $self->fork_command_pipe($cmd);
 
@@ -116,6 +131,16 @@ sub finish_tunnel {
 
     $self->finish_command_pipe($tunnel, 30);
 
+    if ($tunnel->{sock_addr}) {
+       # ssh does not clean up on local host
+       my $cmd = ['rm', '-f', $tunnel->{sock_addr}]; #
+       PVE::Tools::run_command($cmd);
+
+       # .. and just to be sure check on remote side
+       unshift @{$cmd}, @{$self->{rem_ssh}};
+       PVE::Tools::run_command($cmd);
+    }
+
     die $err if $err;
 }
 
@@ -132,7 +157,7 @@ sub prepare {
 
     $self->{storecfg} = PVE::Storage::config();
 
-    # test is VM exist
+    # test if VM exists
     my $conf = $self->{vmconf} = PVE::QemuConfig->load_config($vmid);
 
     PVE::QemuConfig->check_lock($conf);
@@ -154,11 +179,29 @@ sub prepare {
        }
     }
 
-    # activate volumes
     my $vollist = PVE::QemuServer::get_vm_volumes($conf);
-    PVE::Storage::activate_volumes($self->{storecfg}, $vollist);
 
-    # fixme: check if storage is available on both nodes
+    my $need_activate = [];
+    foreach my $volid (@$vollist) {
+       my ($sid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
+
+       # check if storage is available on both nodes
+       my $scfg = PVE::Storage::storage_check_node($self->{storecfg}, $sid);
+       PVE::Storage::storage_check_node($self->{storecfg}, $sid, $self->{node});
+
+       if ($scfg->{shared}) {
+           # PVE::Storage::activate_storage checks this for non-shared storages
+           my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
+           warn "Used shared storage '$sid' is not online on source node!\n"
+               if !$plugin->check_connection($sid, $scfg);
+       } else {
+           # only activate if not shared
+           push @$need_activate, $volid;
+       }
+    }
+
+    # activate volumes
+    PVE::Storage::activate_volumes($self->{storecfg}, $need_activate);
 
     # test ssh connection
     my $cmd = [ @{$self->{rem_ssh}}, '/bin/true' ];
@@ -212,7 +255,7 @@ sub sync_disks {
 
            return if !$volid;
 
-           die "cant migrate local file/device '$volid'\n" if $volid =~ m|^/|;
+           die "can't migrate local file/device '$volid'\n" if $volid =~ m|^/|;
 
            if ($is_cdrom) {
                die "cant migrate local cdrom drive\n" if $volid eq 'cdrom';
@@ -312,6 +355,7 @@ sub phase2 {
 
     my $raddr;
     my $rport;
+    my $ruri; # the whole migration dst. URI (protocol:address[:port])
     my $nodename = PVE::INotify::nodename();
 
     ## start on remote node
@@ -323,7 +367,20 @@ sub phase2 {
        $spice_ticket = $res->{ticket};
     }
 
-    push @$cmd , 'qm', 'start', $vmid, '--stateuri', 'tcp', '--skiplock', '--migratedfrom', $nodename;
+    push @$cmd , 'qm', 'start', $vmid, '--skiplock', '--migratedfrom', $nodename;
+
+    # we use TCP only for unsecure migrations as TCP ssh forward tunnels often
+    # did appeared to late (they are hard, if not impossible, to check for)
+    # secure migration use UNIX sockets now, this *breaks* compatibilty when trying
+    # to migrate from new to old but *not* from old to new.
+    my $datacenterconf = PVE::Cluster::cfs_read_file('datacenter.cfg');
+    my $secure_migration = ($datacenterconf->{migration_unsecure}) ? 0 : 1;
+
+    if (!$secure_migration) {
+       push @$cmd, '--stateuri', 'tcp';
+    } else {
+       push @$cmd, '--stateuri', 'unix';
+    }
 
     if ($self->{forcemachine}) {
        push @$cmd, '--machine', $self->{forcemachine};
@@ -339,10 +396,17 @@ sub phase2 {
        if ($line =~ m/^migration listens on tcp:(localhost|[\d\.]+|\[[\d\.:a-fA-F]+\]):(\d+)$/) {
            $raddr = $1;
            $rport = int($2);
+           $ruri = "tcp:$raddr:$rport";
+       }
+       elsif ($line =~ m!^migration listens on unix:(/run/qemu-server/(\d+)\.migrate)$!) {
+           $raddr = $1;
+           die "Destination UNIX sockets VMID does not match source VMID" if $vmid ne $2;
+           $ruri = "unix:$raddr";
        }
        elsif ($line =~ m/^migration listens on port (\d+)$/) {
            $raddr = "localhost";
            $rport = int($1);
+           $ruri = "tcp:$raddr:$rport";
        }
         elsif ($line =~ m/^spice listens on port (\d+)$/) {
            $spice_port = int($1);
@@ -354,14 +418,40 @@ sub phase2 {
 
     die "unable to detect remote migration address\n" if !$raddr;
 
-    ## create tunnel to remote port
-    $self->log('info', "starting ssh migration tunnel");
-    my $pfamily = PVE::Tools::get_host_address_family($nodename);
-    my $lport = ($raddr eq "localhost") ? PVE::Tools::next_migrate_port($pfamily) : undef;
-    $self->{tunnel} = $self->fork_tunnel($self->{nodeip}, $lport, $rport);
+    if ($secure_migration) {
+       $self->log('info', "start remote tunnel");
+
+       if ($ruri =~ /^unix:/) {
+           unlink $raddr;
+           $self->{tunnel} = $self->fork_tunnel("$raddr:$raddr");
+           $self->{tunnel}->{sock_addr} = $raddr;
+
+           my $unix_socket_try = 0; # wait for the socket to become ready
+           while (! -S $raddr) {
+               $unix_socket_try++;
+               if ($unix_socket_try > 100) {
+                   $self->{errors} = 1;
+                   $self->finish_tunnel($self->{tunnel});
+                   die "Timeout, migration socket $ruri did not get ready";
+               }
+
+               usleep(50000);
+           }
+
+       } elsif ($ruri =~ /^tcp:/) {
+           # for backwards compatibility with older qemu-server versions
+           my $pfamily = PVE::Tools::get_host_address_family($nodename);
+           my $lport = PVE::Tools::next_migrate_port($pfamily);
+
+           $self->{tunnel} = $self->fork_tunnel("$lport:localhost:$rport");
+
+       } else {
+           die "unsupported protocol in migration URI: $ruri\n";
+       }
+    }
 
     my $start = time();
-    $self->log('info', "starting online/live migration on $raddr:$rport");
+    $self->log('info', "starting online/live migration on $ruri");
     $self->{livemigration} = 1;
 
     # load_defaults
@@ -388,6 +478,7 @@ sub phase2 {
        $self->log('info', "migrate_set_downtime error: $@") if $@;
     }
 
+    $self->log('info', "set migration_caps");
     eval {
        PVE::QemuServer::set_migration_caps($vmid);
     };
@@ -395,10 +486,12 @@ sub phase2 {
 
     #set cachesize 10% of the total memory
     my $cachesize = int($conf->{memory}*1048576/10);
+    $self->log('info', "set cachesize: $cachesize");
     eval {
-       PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate-set-cache-size", value => $cachesize);
+       PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate-set-cache-size", value => int($cachesize));
     };
-       
+    $self->log('info', "migrate-set-cache-size error: $@") if $@;
+
     if (PVE::QemuServer::vga_conf_has_spice($conf->{vga})) {
        my $rpcenv = PVE::RPCEnvironment::get();
        my $authuser = $rpcenv->get_user();
@@ -419,11 +512,12 @@ sub phase2 {
 
     }
 
+    $self->log('info', "start migrate command to $ruri");
     eval {
-        PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate", uri => "tcp:$raddr:$rport");
+        PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate", uri => $ruri);
     };
     my $merr = $@;
-    $self->log('info', "migrate uri => tcp:$raddr:$rport failed: $merr") if $merr;
+    $self->log('info', "migrate uri => $ruri failed: $merr") if $merr;
 
     my $lstat = 0;
     my $usleep = 2000000;
@@ -443,6 +537,7 @@ sub phase2 {
        if (my $err = $@) {
            $err_count++;
            warn "query migrate failed: $err\n";
+           $self->log('info', "query migrate failed: $err");
            if ($err_count <= 5) {
                usleep(1000000);
                next;
@@ -450,12 +545,12 @@ sub phase2 {
            die "too many query migrate failures - aborting\n";
        }
 
-        if ($stat->{status} =~ m/^(setup)$/im) {
+        if (defined($stat->{status}) && $stat->{status} =~ m/^(setup)$/im) {
             sleep(1);
             next;
         }
 
-       if ($stat->{status} =~ m/^(active|completed|failed|cancelled)$/im) {
+       if (defined($stat->{status}) && $stat->{status} =~ m/^(active|completed|failed|cancelled)$/im) {
            $merr = undef;
            $err_count = 0;
            if ($stat->{status} eq 'completed') {
@@ -468,6 +563,7 @@ sub phase2 {
            }
 
            if ($stat->{status} eq 'failed' || $stat->{status} eq 'cancelled') {
+               $self->log('info', "migration status error: $stat->{status}");
                die "aborting\n"
            }
 
@@ -520,8 +616,10 @@ sub phase2 {
            die "unable to parse migration status '$stat->{status}' - aborting\n";
        }
     }
-    #to be sure tat the tunnel is closed 
-    if ($self->{tunnel}) {
+
+    # just to be sure that the tunnel gets closed on successful migration, on error
+    # phase2_cleanup closes it *after* stopping the remote waiting VM
+    if (!$self->{errors} && $self->{tunnel}) {
        eval { finish_tunnel($self, $self->{tunnel});  };
        if (my $err = $@) {
            $self->log('err', $err);