use IO::File;
use IPC::Open2;
use PVE::INotify;
+use PVE::Tools;
use PVE::Cluster;
use PVE::Storage;
use PVE::QemuServer;
use Time::HiRes qw( usleep );
+use PVE::RPCEnvironment;
use base qw(PVE::AbstractMigrate);
sub fork_tunnel {
my ($self, $nodeip, $lport, $rport) = @_;
- my $cmd = [@{$self->{rem_ssh}}, '-L', "$lport:localhost:$rport",
- 'qm', 'mtunnel' ];
+ my @localtunnelinfo = $lport ? ('-L' , "$lport:localhost:$rport" ) : ();
+
+ my $cmd = [@{$self->{rem_ssh}}, @localtunnelinfo, 'qm', 'mtunnel' ];
my $tunnel = $self->fork_command_pipe($cmd);
if (my $pid = PVE::QemuServer::check_running($vmid)) {
die "cant migrate running VM without --online\n" if !$online;
$running = $pid;
+ $self->{forcemachine} = PVE::QemuServer::get_current_qemu_machine($vmid);
}
if (my $loc_res = PVE::QemuServer::check_local_resources($conf, 1)) {
die "can't migrate '$volid' - storagy type '$scfg->{type}' not supported\n"
if $scfg->{type} ne 'dir';
- #if file, check if a backing file exist
- if(($scfg->{type} eq 'dir') && (!$sharedvm)){
+ # if file, check if a backing file exist
+ if (($scfg->{type} eq 'dir') && (!$sharedvm)) {
my (undef, undef, undef, $parent) = PVE::Storage::volume_size_info($self->{storecfg}, $volid, 1);
- die "can't migrate '$volid' as it's a clone of '$parent'";
+ die "can't migrate '$volid' as it's a clone of '$parent'" if $parent;
}
}
$self->log('info', "starting VM $vmid on remote node '$self->{node}'");
+ my $raddr;
my $rport;
-
my $nodename = PVE::INotify::nodename();
## start on remote node
- my $cmd = [@{$self->{rem_ssh}}, 'qm', 'start',
- $vmid, '--stateuri', 'tcp', '--skiplock', '--migratedfrom', $nodename];
+ my $cmd = [@{$self->{rem_ssh}}];
+
+ my $spice_ticket;
+ if (PVE::QemuServer::vga_conf_has_spice($conf->{vga})) {
+ my $res = PVE::QemuServer::vm_mon_cmd($vmid, 'query-spice');
+ $spice_ticket = $res->{ticket};
+ }
+
+ push @$cmd , 'qm', 'start', $vmid, '--stateuri', 'tcp', '--skiplock', '--migratedfrom', $nodename;
+
+ if ($self->{forcemachine}) {
+ push @$cmd, '--machine', $self->{forcemachine};
+ }
+
+ my $spice_port;
- PVE::Tools::run_command($cmd, outfunc => sub {
+ # Note: We try to keep $spice_ticket secret (do not pass via command line parameter)
+ # instead we pipe it through STDIN
+ PVE::Tools::run_command($cmd, input => $spice_ticket, outfunc => sub {
my $line = shift;
- if ($line =~ m/^migration listens on port (\d+)$/) {
- $rport = $1;
+ if ($line =~ m/^migration listens on tcp:([\d\.]+|localhost):(\d+)$/) {
+ $raddr = $1;
+ $rport = int($2);
+ }
+ elsif ($line =~ m/^migration listens on port (\d+)$/) {
+ $raddr = "localhost";
+ $rport = int($1);
+ }
+ elsif ($line =~ m/^spice listens on port (\d+)$/) {
+ $spice_port = int($1);
}
}, errfunc => sub {
my $line = shift;
$self->log('info', $line);
});
- die "unable to detect remote migration port\n" if !$rport;
-
- $self->log('info', "starting migration tunnel");
+ die "unable to detect remote migration address\n" if !$raddr;
## create tunnel to remote port
- my $lport = PVE::QemuServer::next_migrate_port();
+ $self->log('info', "starting ssh migration tunnel");
+ my $lport = ($raddr eq "localhost") ? PVE::Tools::next_migrate_port() : undef;
$self->{tunnel} = $self->fork_tunnel($self->{nodeip}, $lport, $rport);
- $self->log('info', "starting online/live migration on port $lport");
- # start migration
-
my $start = time();
+ $self->log('info', "starting online/live migration on $raddr:$rport");
+ $self->{livemigration} = 1;
# load_defaults
my $defaults = PVE::QemuServer::load_defaults();
$self->log('info', "migrate_set_downtime error: $@") if $@;
}
- my $capabilities = {};
- $capabilities->{capability} = "xbzrle";
- $capabilities->{state} = JSON::false;
-
eval {
- PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate-set-capabilities", capabilities => [$capabilities]);
+ PVE::QemuServer::set_migration_caps($vmid);
};
+ warn $@ if $@;
#set cachesize 10% of the total memory
my $cachesize = int($conf->{memory}*1048576/10);
eval {
PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate-set-cache-size", value => $cachesize);
};
+
+ if (PVE::QemuServer::vga_conf_has_spice($conf->{vga})) {
+ my $rpcenv = PVE::RPCEnvironment::get();
+ my $authuser = $rpcenv->get_user();
+
+ my (undef, $proxyticket) = PVE::AccessControl::assemble_spice_ticket($authuser, $vmid, $self->{node});
+
+ my $filename = "/etc/pve/nodes/$self->{node}/pve-ssl.pem";
+ my $subject = PVE::AccessControl::read_x509_subject_spice($filename);
+
+ $self->log('info', "spice client_migrate_info");
+
+ eval {
+ PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "client_migrate_info", protocol => 'spice',
+ hostname => $proxyticket, 'tls-port' => $spice_port,
+ 'cert-subject' => $subject);
+ };
+ $self->log('info', "client_migrate_info error: $@") if $@;
+
+ }
eval {
- PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate", uri => "tcp:localhost:$lport");
+ PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate", uri => "tcp:$raddr:$rport");
};
my $merr = $@;
+ $self->log('info', "migrate uri => tcp:$raddr:$rport failed: $merr") if $merr;
my $lstat = 0;
my $usleep = 2000000;
$self->log('info', "migration status: $stat->{status} (transferred ${trans}, " .
"remaining ${rem}), total ${total})");
- #$self->log('info', "migration xbzrle cachesize: ${xbzrlecachesize} transferred ${xbzrlebytes} pages ${xbzrlepages} cachemiss ${xbzrlecachemiss} overflow ${xbzrleoverflow}");
+ if (${xbzrlecachesize}) {
+ $self->log('info', "migration xbzrle cachesize: ${xbzrlecachesize} transferred ${xbzrlebytes} pages ${xbzrlepages} cachemiss ${xbzrlecachemiss} overflow ${xbzrleoverflow}");
+ }
+
if (($lastrem && $rem > $lastrem ) || ($rem == 0)) {
$downtimecounter++;
}
die "Failed to move config to node '$self->{node}' - rename failed: $!\n"
if !rename($conffile, $newconffile);
- # now that config file is move, we can resume vm on target if livemigrate
- if ($self->{tunnel}) {
+ if ($self->{livemigration}) {
+ # now that config file is move, we can resume vm on target if livemigrate
my $cmd = [@{$self->{rem_ssh}}, 'qm', 'resume', $vmid, '--skiplock'];
- eval{ PVE::Tools::run_command($cmd, outfunc => sub {}, errfunc => sub {}) };
+ eval{ PVE::Tools::run_command($cmd, outfunc => sub {},
+ errfunc => sub {
+ my $line = shift;
+ $self->log('err', $line);
+ });
+ };
if (my $err = $@) {
$self->log('err', $err);
$self->{errors} = 1;
}
}
+ eval {
+
+ my $timer = 0;
+ if (PVE::QemuServer::vga_conf_has_spice($conf->{vga}) && $self->{running}) {
+ $self->log('info', "Waiting for spice server migration");
+ while (1) {
+ my $res = PVE::QemuServer::vm_mon_cmd_nocheck($vmid, 'query-spice');
+ last if int($res->{'migrated'}) == 1;
+ last if $timer > 50;
+ $timer ++;
+ usleep(200000);
+ }
+ }
+ };
+
# always stop local VM
eval { PVE::QemuServer::vm_stop($self->{storecfg}, $vmid, 1, 1); };
if (my $err = $@) {