X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=PVE%2FQemuMigrate.pm;h=e9e9075ab4e3373cfa8505e08d6d1d1b494b8274;hb=d6140280f7ebfbb58efa4c47661b1b216ca01528;hp=e099a42b8c90f6069dd6fd6c88ede2024cc5d343;hpb=b74cad8ae315c0b2db864cabbff52c27d3704396;p=qemu-server.git diff --git a/PVE/QemuMigrate.pm b/PVE/QemuMigrate.pm index e099a42..e9e9075 100644 --- a/PVE/QemuMigrate.pm +++ b/PVE/QemuMigrate.pm @@ -13,6 +13,9 @@ use PVE::Storage; use PVE::QemuServer; use Time::HiRes qw( usleep ); use PVE::RPCEnvironment; +use PVE::ReplicationConfig; +use PVE::ReplicationState; +use PVE::Replication; use base qw(PVE::AbstractMigrate); @@ -88,20 +91,62 @@ sub finish_command_pipe { 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" @@ -109,6 +154,16 @@ sub fork_tunnel { }; 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"; @@ -119,14 +174,7 @@ sub fork_tunnel { 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); @@ -221,8 +269,6 @@ sub sync_disks { # local volumes which have been copied $self->{volumes} = []; - my $res = []; - eval { # found local volumes and their origin @@ -255,33 +301,37 @@ sub sync_disks { next if @{$dl->{$storeid}} == 0; + my $targetsid = $self->{opts}->{targetstorage} ? $self->{opts}->{targetstorage} : $storeid; + # check if storage is available on target node - PVE::Storage::storage_check_node($self->{storecfg}, $storeid, $self->{node}); + PVE::Storage::storage_check_node($self->{storecfg}, $targetsid, $self->{node}); $sharedvm = 0; # there is a non-shared disk PVE::Storage::foreach_volid($dl, sub { my ($volid, $sid, $volname) = @_; - $local_volumes->{$volid} = 'storage'; + $local_volumes->{$volid}->{ref} = 'storage'; }); } my $test_volid = sub { - my ($volid, $is_cdrom, $snapname) = @_; - - return if !$volid; + my ($volid, $attr) = @_; if ($volid =~ m|^/|) { - $local_volumes->{$volid} = 'config'; + return if $attr->{shared}; + $local_volumes->{$volid}->{ref} = 'config'; die "local file/device\n"; } - if ($is_cdrom) { + my $snaprefs = $attr->{referenced_in_snapshot}; + + if ($attr->{cdrom}) { if ($volid eq 'cdrom') { my $msg = "can't migrate local cdrom drive"; - $msg .= " (referenced in snapshot '$snapname')" - if defined($snapname); - + if (defined($snaprefs) && !$attr->{referenced_in_config}) { + my $snapnames = join(', ', sort keys %$snaprefs); + $msg .= " (referenced in snapshot - $snapnames)"; + } &$log_error("$msg\n"); return; } @@ -293,26 +343,27 @@ sub sync_disks { my $targetsid = $self->{opts}->{targetstorage} ? $self->{opts}->{targetstorage} : $sid; # 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}); + PVE::Storage::storage_check_node($self->{storecfg}, $targetsid, $self->{node}); return if $scfg->{shared}; $sharedvm = 0; - $local_volumes->{$volid} = defined($snapname) ? 'snapshot' : 'config'; + $local_volumes->{$volid}->{ref} = $attr->{referenced_in_config} ? 'config' : 'snapshot'; - die "local cdrom image\n" if $is_cdrom; + die "local cdrom image\n" if $attr->{cdrom}; my ($path, $owner) = PVE::Storage::path($self->{storecfg}, $volid); die "owned by other VM (owner = VM $owner)\n" if !$owner || ($owner != $self->{vmid}); - if (defined($snapname)) { + 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"; @@ -323,33 +374,23 @@ sub sync_disks { if PVE::Storage::volume_is_base_and_used($self->{storecfg}, $volid); }; - my $test_drive = sub { - my ($ds, $drive, $snapname) = @_; - - eval { - &$test_volid($drive->{file}, PVE::QemuServer::drive_is_cdrom($drive), $snapname); - }; - - &$log_error($@, $drive->{file}) if $@; - }; - - foreach my $snapname (keys %{$conf->{snapshots}}) { - eval { - &$test_volid($conf->{snapshots}->{$snapname}->{'vmstate'}, 0, undef) - if defined($conf->{snapshots}->{$snapname}->{'vmstate'}); - }; - &$log_error($@, $conf->{snapshots}->{$snapname}->{'vmstate'}) if $@; - - PVE::QemuServer::foreach_drive($conf->{snapshots}->{$snapname}, $test_drive, $snapname); - } - PVE::QemuServer::foreach_drive($conf, $test_drive); + PVE::QemuServer::foreach_volid($conf, sub { + my ($volid, $attr) = @_; + eval { $test_volid->($volid, $attr); }; + if (my $err = $@) { + &$log_error($err, $volid); + } + }); 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"); @@ -388,15 +429,32 @@ sub sync_disks { } } + my $rep_volumes; + $self->log('info', "copying disk images"); + my $rep_cfg = PVE::ReplicationConfig->new(); + + if (my $jobcfg = $rep_cfg->find_local_replication_job($vmid, $self->{node})) { + die "can't live migrate VM with replicated volumes\n" if $self->{running}; + my $start_time = time(); + 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; - PVE::Storage::storage_migrate($self->{storecfg}, $volid, $self->{nodeip}, $sid); + 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, $with_snapshots); } } }; @@ -479,18 +537,7 @@ sub phase2 { 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 $migration_type = 'secure'; - if (defined($self->{opts}->{migration_type})) { - $migration_type = $self->{opts}->{migration_type}; - } elsif (defined($datacenterconf->{migration}->{type})) { - $migration_type = $datacenterconf->{migration}->{type}; - } + my $migration_type = $self->{opts}->{migration_type}; push @$cmd, '--migration_type', $migration_type; @@ -553,8 +600,9 @@ sub phase2 { 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; @@ -587,19 +635,24 @@ sub phase2 { } 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 ($self->{opts}->{targetstorage}) { + if ($self->{opts}->{targetstorage} && defined($self->{online_local_volumes})) { $self->{storage_migration} = 1; $self->{storage_migration_jobs} = {}; $self->log('info', "starting storage migration"); - die "the number of destination local disk is not equal to number of source local disk" if (scalar(keys %{$self->{target_drive}}) != scalar @{$self->{online_local_volumes}}); + die "The number of local disks does not match between the source and the destination.\n" + if (scalar(keys %{$self->{target_drive}}) != scalar @{$self->{online_local_volumes}}); foreach my $drive (keys %{$self->{target_drive}}){ - $self->log('info', "$drive: start migration to to $self->{target_drive}->{$drive}->{nbd_uri}"); - PVE::QemuServer::qemu_drive_mirror($vmid, $drive, $self->{target_drive}->{$drive}->{nbd_uri}, $vmid, undef, $self->{storage_migration_jobs}, 1); + my $nbd_uri = $self->{target_drive}->{$drive}->{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); } } @@ -636,8 +689,11 @@ sub phase2 { }; 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)); @@ -672,7 +728,7 @@ sub phase2 { $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; @@ -708,7 +764,7 @@ sub phase2 { 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"); } @@ -733,8 +789,8 @@ sub phase2 { 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})"); @@ -768,16 +824,6 @@ sub phase2 { 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 { @@ -802,7 +848,7 @@ sub phase2_cleanup { } # cleanup ressources on target host - if ( $self->{storage_migration} ) { + if ($self->{storage_migration}) { eval { PVE::QemuServer::qemu_blockjobs_cancel($vmid, $self->{storage_migration_jobs}) }; if (my $err = $@) { @@ -856,16 +902,17 @@ sub phase3_cleanup { my $conf = $self->{vmconf}; return if $self->{phase2errors}; - if ($self->{storage_migration}) { + my $tunnel = $self->{tunnel}; - eval { PVE::QemuServer::qemu_drive_mirror_monitor($vmid, undef, $self->{storage_migration_jobs}); }; #finish block-job + if ($self->{storage_migration}) { + # finish block-job + eval { PVE::QemuServer::qemu_drive_mirror_monitor($vmid, undef, $self->{storage_migration_jobs}); }; if (my $err = $@) { eval { PVE::QemuServer::qemu_blockjobs_cancel($vmid, $self->{storage_migration_jobs}) }; eval { PVE::QemuMigrate::cleanup_remotedisks($self) }; die "Failed to completed storage migration\n"; } else { - foreach my $target_drive (keys %{$self->{target_drive}}) { my $drive = PVE::QemuServer::parse_drive($target_drive, $self->{target_drive}->{$target_drive}->{volid}); $conf->{$target_drive} = PVE::QemuServer::print_drive($vmid, $drive); @@ -874,6 +921,9 @@ sub phase3_cleanup { } } + # transfer replication state before move config + $self->transfer_replication_state() if $self->{replicated_volumes}; + # move config to remote node my $conffile = PVE::QemuConfig->config_file($vmid); my $newconffile = PVE::QemuConfig->config_file($vmid, $self->{node}); @@ -881,15 +931,51 @@ sub phase3_cleanup { die "Failed to move config to node '$self->{node}' - rename failed: $!\n" if !rename($conffile, $newconffile); + $self->switch_replication_job_target() if $self->{replicated_volumes}; + 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', '--nocheck']; - eval{ PVE::Tools::run_command($cmd, outfunc => sub {}, - errfunc => sub { - my $line = shift; - $self->log('err', $line); - }); - }; + if ($self->{storage_migration}) { + # stop nbd server on remote vm - requirement for resume since 2.9 + my $cmd = [@{$self->{rem_ssh}}, 'qm', 'nbdstop', $vmid]; + + eval{ PVE::Tools::run_command($cmd, outfunc => sub {}, errfunc => sub {}) }; + if (my $err = $@) { + $self->log('err', $err); + $self->{errors} = 1; + } + } + + # config moved and nbd server stopped - now we can resume vm on target + 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; + } + } + + if ($self->{storage_migration} && PVE::QemuServer::parse_guest_agent($conf)->{fstrim_cloned_disks} && $self->{running}) { + my $cmd = [@{$self->{rem_ssh}}, 'qm', 'guest', 'cmd', $vmid, 'fstrim']; + eval{ PVE::Tools::run_command($cmd, outfunc => sub {}, errfunc => sub {}) }; + } + } + + # 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; @@ -897,7 +983,6 @@ sub phase3_cleanup { } eval { - my $timer = 0; if (PVE::QemuServer::vga_conf_has_spice($conf->{vga}) && $self->{running}) { $self->log('info', "Waiting for spice server migration"); @@ -941,14 +1026,6 @@ sub phase3_cleanup { } } - #stop nbd server to remote vm - my $cmd = [@{$self->{rem_ssh}}, 'qm', 'nbdstop', $vmid]; - - eval{ PVE::Tools::run_command($cmd, outfunc => sub {}, errfunc => sub {}) }; - if (my $err = $@) { - $self->log('err', $err); - $self->{errors} = 1; - } } # clear migrate lock @@ -962,4 +1039,9 @@ sub final_cleanup { # nothing to do } +sub round_powerof2 { + return 1 if $_[0] < 2; + return 2 << int(log($_[0]-1)/log(2)); +} + 1;