if !&$collect_child_process();
}
+sub read_tunnel {
+ my ($self, $tunnel, $timeout) = @_;
+
+ $timeout = 60 if !defined($timeout);
+
+ my $reader = $tunnel->{reader};
+
+ my $output;
+ eval {
+ PVE::Tools::run_with_timeout($timeout, sub { $output = <$reader>; });
+ };
+ die "reading from tunnel failed: $@\n" if $@;
+
+ chomp $output;
+
+ return $output;
+}
+
+sub write_tunnel {
+ my ($self, $tunnel, $timeout, $command) = @_;
+
+ $timeout = 60 if !defined($timeout);
+
+ my $writer = $tunnel->{writer};
+
+ eval {
+ PVE::Tools::run_with_timeout($timeout, sub {
+ print $writer "$command\n";
+ $writer->flush();
+ });
+ };
+ die "writing to tunnel failed: $@\n" if $@;
+
+ if ($tunnel->{version} && $tunnel->{version} >= 1) {
+ my $res = eval { $self->read_tunnel($tunnel, 10); };
+ die "no reply to command '$command': $@\n" if $@;
+
+ if ($res eq 'OK') {
+ return;
+ } else {
+ die "tunnel replied '$res' to command '$command'\n";
+ }
+ }
+}
+
sub fork_tunnel {
my ($self, $tunnel_addr) = @_;
my @localtunnelinfo = defined($tunnel_addr) ? ('-L' , $tunnel_addr ) : ();
- my $cmd = [@{$self->{rem_ssh}}, '-o ExitOnForwardFailure=yes', @localtunnelinfo, 'qm', 'mtunnel' ];
+ my $cmd = [@{$self->{rem_ssh}}, '-o ExitOnForwardFailure=yes', @localtunnelinfo, '/usr/sbin/qm', 'mtunnel' ];
my $tunnel = $self->fork_command_pipe($cmd);
- my $reader = $tunnel->{reader};
-
- my $helo;
eval {
- PVE::Tools::run_with_timeout(60, sub { $helo = <$reader>; });
+ my $helo = $self->read_tunnel($tunnel, 60);
die "no reply\n" if !$helo;
die "no quorum on target node\n" if $helo =~ m/^no quorum$/;
die "got strange reply from mtunnel ('$helo')\n"
};
my $err = $@;
+ eval {
+ my $ver = $self->read_tunnel($tunnel, 10);
+ if ($ver =~ /^ver (\d+)$/) {
+ $tunnel->{version} = $1;
+ $self->log('info', "ssh tunnel $ver\n");
+ } else {
+ $err = "received invalid tunnel version string '$ver'\n" if !$err;
+ }
+ };
+
if ($err) {
$self->finish_command_pipe($tunnel);
die "can't open migration tunnel - $err";
sub finish_tunnel {
my ($self, $tunnel) = @_;
- my $writer = $tunnel->{writer};
-
- eval {
- PVE::Tools::run_with_timeout(30, sub {
- print $writer "quit\n";
- $writer->flush();
- });
- };
+ eval { $self->write_tunnel($tunnel, 30, 'quit'); };
my $err = $@;
$self->finish_command_pipe($tunnel, 30);
# local volumes which have been copied
$self->{volumes} = [];
- my $res = [];
-
eval {
# found local volumes and their origin
PVE::Storage::foreach_volid($dl, sub {
my ($volid, $sid, $volname) = @_;
- $local_volumes->{$volid} = 'storage';
+ $local_volumes->{$volid}->{ref} = 'storage';
});
}
my ($volid, $attr) = @_;
if ($volid =~ m|^/|) {
- $local_volumes->{$volid} = 'config';
+ return if $attr->{shared};
+ $local_volumes->{$volid}->{ref} = 'config';
die "local file/device\n";
}
$sharedvm = 0;
- $local_volumes->{$volid} = $attr->{referenced_in_config} ? 'config' : 'snapshot';
+ $local_volumes->{$volid}->{ref} = $attr->{referenced_in_config} ? 'config' : 'snapshot';
die "local cdrom image\n" if $attr->{cdrom};
die "owned by other VM (owner = VM $owner)\n"
if !$owner || ($owner != $self->{vmid});
+ my $format = PVE::QemuServer::qemu_img_format($scfg, $volname);
+ $local_volumes->{$volid}->{snapshots} = defined($snaprefs) || ($format =~ /^(?:qcow2|vmdk)$/);
if (defined($snaprefs)) {
# we cannot migrate shapshots on local storage
# exceptions: 'zfspool' or 'qcow2' files (on directory storage)
- my $format = PVE::QemuServer::qemu_img_format($scfg, $volname);
die "online storage migration not possible if snapshot exists\n" if $self->{running};
if (!($scfg->{type} eq 'zfspool' || $format eq 'qcow2')) {
die "non-migratable snapshot exists\n";
});
foreach my $vol (sort keys %$local_volumes) {
- if ($local_volumes->{$vol} eq 'storage') {
+ my $ref = $local_volumes->{$vol}->{ref};
+ if ($ref eq 'storage') {
$self->log('info', "found local disk '$vol' (via storage)\n");
- } elsif ($local_volumes->{$vol} eq 'config') {
+ } elsif ($ref eq 'config') {
&$log_error("can't live migrate attached local disks without with-local-disks option\n", $vol)
if $self->{running} && !$self->{opts}->{"with-local-disks"};
$self->log('info', "found local disk '$vol' (in current VM config)\n");
- } elsif ($local_volumes->{$vol} eq 'snapshot') {
+ } elsif ($ref eq 'snapshot') {
$self->log('info', "found local disk '$vol' (referenced by snapshot(s))\n");
} else {
$self->log('info', "found local disk '$vol'\n");
my $logfunc = sub { my ($msg) = @_; $self->log('info', $msg); };
$rep_volumes = PVE::Replication::run_replication(
'PVE::QemuConfig', $jobcfg, $start_time, $start_time, $logfunc);
+ $self->{replicated_volumes} = $rep_volumes;
}
foreach my $volid (keys %$local_volumes) {
my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
- if ($self->{running} && $self->{opts}->{targetstorage} && $local_volumes->{$volid} eq 'config') {
+ if ($self->{running} && $self->{opts}->{targetstorage} && $local_volumes->{$volid}->{ref} eq 'config') {
push @{$self->{online_local_volumes}}, $volid;
} else {
next if $rep_volumes->{$volid};
push @{$self->{volumes}}, $volid;
my $insecure = $self->{opts}->{migration_type} eq 'insecure';
+ my $with_snapshots = $local_volumes->{$volid}->{snapshots};
PVE::Storage::storage_migrate($self->{storecfg}, $volid, $self->{ssh_info}, $sid,
- undef, undef, undef, undef, $insecure);
+ undef, undef, undef, undef, $insecure, $with_snapshots);
}
}
};
die "unable to detect remote migration address\n" if !$raddr;
+ $self->log('info', "start remote tunnel");
+
if ($migration_type eq 'secure') {
- $self->log('info', "start remote tunnel");
if ($ruri =~ /^unix:/) {
unlink $raddr;
} else {
die "unsupported protocol in migration URI: $ruri\n";
}
+ } else {
+ #fork tunnel for insecure migration, to send faster commands like resume
+ $self->{tunnel} = $self->fork_tunnel();
}
my $start = time();
if (scalar(keys %{$self->{target_drive}}) != scalar @{$self->{online_local_volumes}});
foreach my $drive (keys %{$self->{target_drive}}){
my $nbd_uri = $self->{target_drive}->{$drive}->{nbd_uri};
- $self->log('info', "$drive: start migration to to $nbd_uri");
+ $self->log('info', "$drive: start migration to $nbd_uri");
PVE::QemuServer::qemu_drive_mirror($vmid, $drive, $nbd_uri, $vmid, undef, $self->{storage_migration_jobs}, 1);
}
}
};
warn $@ if $@;
- #set cachesize 10% of the total memory
- my $cachesize = int($conf->{memory}*1048576/10);
+ # set cachesize to 10% of the total memory
+ my $memory = $conf->{memory} || $defaults->{memory};
+ my $cachesize = int($memory * 1048576 / 10);
+ $cachesize = round_powerof2($cachesize);
+
$self->log('info', "set cachesize: $cachesize");
eval {
PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate-set-cache-size", value => int($cachesize));
$self->log('info', "migrate uri => $ruri failed: $merr") if $merr;
my $lstat = 0;
- my $usleep = 2000000;
+ my $usleep = 1000000;
my $i = 0;
my $err_count = 0;
my $lastrem = undef;
if ($stat->{status} eq 'completed') {
my $delay = time() - $start;
if ($delay > 0) {
- my $mbps = sprintf "%.2f", $conf->{memory}/$delay;
+ my $mbps = sprintf "%.2f", $memory / $delay;
my $downtime = $stat->{downtime} || 0;
$self->log('info', "migration speed: $mbps MB/s - downtime $downtime ms");
}
my $xbzrlepages = $stat->{"xbzrle-cache"}->{"pages"} || 0;
my $xbzrlecachemiss = $stat->{"xbzrle-cache"}->{"cache-miss"} || 0;
my $xbzrleoverflow = $stat->{"xbzrle-cache"}->{"overflow"} || 0;
- #reduce sleep if remainig memory if lower than the everage transfert
- $usleep = 300000 if $avglstat && $rem < $avglstat;
+ # reduce sleep if remainig memory is lower than the average transfer speed
+ $usleep = 100000 if $avglstat && $rem < $avglstat;
$self->log('info', "migration status: $stat->{status} (transferred ${trans}, " .
"remaining ${rem}), total ${total})");
die "unable to parse migration status '$stat->{status}' - aborting\n";
}
}
-
- # 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);
- $self->{errors} = 1;
- }
- }
}
sub phase2_cleanup {
my $conf = $self->{vmconf};
return if $self->{phase2errors};
+ my $tunnel = $self->{tunnel};
+
if ($self->{storage_migration}) {
# finish block-job
eval { PVE::QemuServer::qemu_drive_mirror_monitor($vmid, undef, $self->{storage_migration_jobs}); };
}
# transfer replication state before move config
- $self->transfer_replication_state();
+ $self->transfer_replication_state() if $self->{replicated_volumes};
# move config to remote node
my $conffile = PVE::QemuConfig->config_file($vmid);
die "Failed to move config to node '$self->{node}' - rename failed: $!\n"
if !rename($conffile, $newconffile);
- $self->switch_replication_job_target();
+ $self->switch_replication_job_target() if $self->{replicated_volumes};
if ($self->{livemigration}) {
if ($self->{storage_migration}) {
- # remove drives referencing the nbd server from source
- # otherwise vm_stop might hang later on
- foreach my $drive (keys %{$self->{target_drive}}){
- PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "device_del", id => $drive);
- }
# stop nbd server on remote vm - requirement for resume since 2.9
my $cmd = [@{$self->{rem_ssh}}, 'qm', 'nbdstop', $vmid];
$self->{errors} = 1;
}
}
+
# config moved and nbd server stopped - now we can resume vm on target
- my $cmd = [@{$self->{rem_ssh}}, 'qm', 'resume', $vmid, '--skiplock', '--nocheck'];
- eval{ PVE::Tools::run_command($cmd, outfunc => sub {},
- errfunc => sub {
- my $line = shift;
- $self->log('err', $line);
- });
- };
+ if ($tunnel && $tunnel->{version} && $tunnel->{version} >= 1) {
+ eval {
+ $self->write_tunnel($tunnel, 30, "resume $vmid");
+ };
+ if (my $err = $@) {
+ $self->log('err', $err);
+ $self->{errors} = 1;
+ }
+ } else {
+ my $cmd = [@{$self->{rem_ssh}}, 'qm', 'resume', $vmid, '--skiplock', '--nocheck'];
+ my $logf = sub {
+ my $line = shift;
+ $self->log('err', $line);
+ };
+ eval { PVE::Tools::run_command($cmd, outfunc => sub {}, errfunc => $logf); };
+ if (my $err = $@) {
+ $self->log('err', $err);
+ $self->{errors} = 1;
+ }
+ }
+ }
+
+ # close tunnel on successful migration, on error phase2_cleanup closed it
+ if ($tunnel) {
+ eval { finish_tunnel($self, $tunnel); };
if (my $err = $@) {
$self->log('err', $err);
$self->{errors} = 1;
# nothing to do
}
+sub round_powerof2 {
+ return 1 if $_[0] < 2;
+ return 2 << int(log($_[0]-1)/log(2));
+}
+
1;