X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=PVE%2FQemuMigrate.pm;h=36f6fda54bfdadfa4baf267fc056da31612192e8;hb=d652f7b1ab64bef3972b70d4ae14e1e05e07db5e;hp=5da62eb53db6682b13ba9f1a95a3f81cc2e55f52;hpb=386c6ba7f5dc7de2a6e48d0520cda72f4b2a8bf3;p=qemu-server.git diff --git a/PVE/QemuMigrate.pm b/PVE/QemuMigrate.pm index 5da62eb..36f6fda 100644 --- a/PVE/QemuMigrate.pm +++ b/PVE/QemuMigrate.pm @@ -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; @@ -12,6 +13,9 @@ use PVE::Storage; use PVE::QemuServer; use Time::HiRes qw( usleep ); use PVE::RPCEnvironment; +use PVE::ReplicationConfig; +use PVE::ReplicationState; +use JSON; use base qw(PVE::AbstractMigrate); @@ -44,17 +48,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 +79,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 = defined($tunnel_addr) ? ('-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,13 +134,23 @@ 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; } sub lock_vm { my ($self, $vmid, $code, @param) = @_; - return PVE::QemuServer::lock_config($vmid, $code, @param); + return PVE::QemuConfig->lock_config($vmid, $code, @param); } sub prepare { @@ -132,14 +160,14 @@ sub prepare { $self->{storecfg} = PVE::Storage::config(); - # test is VM exist - my $conf = $self->{vmconf} = PVE::QemuServer::load_config($vmid); + # test if VM exists + my $conf = $self->{vmconf} = PVE::QemuConfig->load_config($vmid); - PVE::QemuServer::check_lock($conf); + PVE::QemuConfig->check_lock($conf); my $running = 0; if (my $pid = PVE::QemuServer::check_running($vmid)) { - die "cant migrate running VM without --online\n" if !$online; + die "can't migrate running VM without --online\n" if !$online; $running = $pid; $self->{forcemachine} = PVE::QemuServer::qemu_machine_pxe($vmid, $conf); @@ -154,11 +182,31 @@ 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 $targetsid = $self->{opts}->{targetstorage} ? $self->{opts}->{targetstorage} : $sid; + + my $scfg = PVE::Storage::storage_check_node($self->{storecfg}, $sid); + PVE::Storage::storage_check_node($self->{storecfg}, $targetsid, $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' ]; @@ -171,103 +219,218 @@ sub prepare { sub sync_disks { my ($self, $vmid) = @_; - $self->log('info', "copying disk images"); - my $conf = $self->{vmconf}; + # local volumes which have been copied $self->{volumes} = []; my $res = []; eval { - my $volhash = {}; - my $cdromhash = {}; + # found local volumes and their origin + my $local_volumes = {}; + my $local_volumes_errors = {}; + my $other_errors = []; + my $abort = 0; my $sharedvm = 1; + my $log_error = sub { + my ($msg, $volid) = @_; + + if (defined($volid)) { + $local_volumes_errors->{$volid} = $msg; + } else { + push @$other_errors, $msg; + } + $abort = 1; + }; + my @sids = PVE::Storage::storage_ids($self->{storecfg}); - foreach my $storeid (@sids) { + foreach my $storeid (@sids) { my $scfg = PVE::Storage::storage_config($self->{storecfg}, $storeid); - next if $scfg->{shared}; + next if $scfg->{shared}; next if !PVE::Storage::storage_check_enabled($self->{storecfg}, $storeid, undef, 1); - # get list from PVE::Storage (for unused volumes) - my $dl = PVE::Storage::vdisk_list($self->{storecfg}, $storeid, $vmid); - PVE::Storage::foreach_volid($dl, sub { - my ($volid, $sid, $volname) = @_; + # get list from PVE::Storage (for unused volumes) + my $dl = PVE::Storage::vdisk_list($self->{storecfg}, $storeid, $vmid); - # check if storage is available on target node - PVE::Storage::storage_check_node($self->{storecfg}, $sid, $self->{node}); + next if @{$dl->{$storeid}} == 0; - $volhash->{$volid} = 1; - $sharedvm = 0; # there is a non-shared disk - }); - } + my $targetsid = $self->{opts}->{targetstorage} ? $self->{opts}->{targetstorage} : $storeid; - # and add used, owned/non-shared disks (just to be sure we have all) + # check if storage is available on target node + PVE::Storage::storage_check_node($self->{storecfg}, $targetsid, $self->{node}); + $sharedvm = 0; # there is a non-shared disk - PVE::QemuServer::foreach_volid($conf, sub { - my ($volid, $is_cdrom) = @_; + PVE::Storage::foreach_volid($dl, sub { + my ($volid, $sid, $volname) = @_; + + $local_volumes->{$volid} = 'storage'; + }); + } - return if !$volid; + my $test_volid = sub { + my ($volid, $attr) = @_; - die "cant migrate local file/device '$volid'\n" if $volid =~ m|^/|; + if ($volid =~ m|^/|) { + $local_volumes->{$volid} = 'config'; + die "local file/device\n"; + } - if ($is_cdrom) { - die "cant migrate local cdrom drive\n" if $volid eq 'cdrom'; + my $snaprefs = $attr->{referenced_in_snapshot}; + + if ($attr->{cdrom}) { + if ($volid eq 'cdrom') { + my $msg = "can't migrate local cdrom drive"; + if (defined($snaprefs) && !$attr->{referenced_in_config}) { + my $snapnames = join(', ', sort keys %$snaprefs); + $msg .= " (referenced in snapshot - $snapnames)"; + } + &$log_error("$msg\n"); + return; + } return if $volid eq 'none'; - $cdromhash->{$volid} = 1; } my ($sid, $volname) = PVE::Storage::parse_volume_id($volid); + 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}; - die "can't migrate local cdrom '$volid'\n" if $cdromhash->{$volid}; - $sharedvm = 0; + $local_volumes->{$volid} = $attr->{referenced_in_config} ? 'config' : 'snapshot'; + + die "local cdrom image\n" if $attr->{cdrom}; + my ($path, $owner) = PVE::Storage::path($self->{storecfg}, $volid); - die "can't migrate volume '$volid' - owned by other VM (owner = VM $owner)\n" + die "owned by other VM (owner = VM $owner)\n" if !$owner || ($owner != $self->{vmid}); - $volhash->{$volid} = 1; - }); + 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"; + } + } + + die "referenced by linked clone(s)\n" + if PVE::Storage::volume_is_base_and_used($self->{storecfg}, $volid); + }; + + 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') { + $self->log('info', "found local disk '$vol' (via storage)\n"); + } elsif ($local_volumes->{$vol} 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') { + $self->log('info', "found local disk '$vol' (referenced by snapshot(s))\n"); + } else { + $self->log('info', "found local disk '$vol'\n"); + } + } + + foreach my $vol (sort keys %$local_volumes_errors) { + $self->log('warn', "can't migrate local disk '$vol': $local_volumes_errors->{$vol}"); + } + foreach my $err (@$other_errors) { + $self->log('warn', "$err"); + } + + if ($self->{running} && !$sharedvm && !$self->{opts}->{targetstorage}) { + $self->{opts}->{targetstorage} = 1; #use same sid for remote local + } - if ($self->{running} && !$sharedvm) { - die "can't do online migration - VM uses local disks\n"; + if ($abort) { + die "can't migrate VM - check log\n"; } - # do some checks first - foreach my $volid (keys %$volhash) { + # additional checks for local storage + foreach my $volid (keys %$local_volumes) { my ($sid, $volname) = PVE::Storage::parse_volume_id($volid); my $scfg = PVE::Storage::storage_config($self->{storecfg}, $sid); + my $migratable = ($scfg->{type} eq 'dir') || ($scfg->{type} eq 'zfspool') || + ($scfg->{type} eq 'lvmthin') || ($scfg->{type} eq 'lvm'); + die "can't migrate '$volid' - storage type '$scfg->{type}' not supported\n" - if (!($scfg->{type} eq 'dir' || $scfg->{type} eq 'zfspool') && (!$sharedvm)); + if !$migratable; - # if file, check if a backing file exist - if (!($scfg->{type} eq 'dir' || $scfg->{type} eq 'zfspool') && (!$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'" if $parent; + # image is a linked clone on local storage, se we can't migrate. + if (my $basename = (PVE::Storage::parse_volname($self->{storecfg}, $volid))[3]) { + die "can't migrate '$volid' as it's a clone of '$basename'"; } } - foreach my $volid (keys %$volhash) { + 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); + } + + foreach my $volid (keys %$local_volumes) { my ($sid, $volname) = PVE::Storage::parse_volume_id($volid); - push @{$self->{volumes}}, $volid; - PVE::Storage::storage_migrate($self->{storecfg}, $volid, $self->{nodeip}, $sid); + if ($self->{running} && $self->{opts}->{targetstorage} && $local_volumes->{$volid} 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'; + PVE::Storage::storage_migrate($self->{storecfg}, $volid, $self->{ssh_info}, $sid, + undef, undef, undef, undef, $insecure); + } } }; die "Failed to sync data - $@" if $@; } +sub cleanup_remotedisks { + my ($self) = @_; + + foreach my $target_drive (keys %{$self->{target_drive}}) { + + my $drive = PVE::QemuServer::parse_drive($target_drive, $self->{target_drive}->{$target_drive}->{volid}); + my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file}); + + my $cmd = [@{$self->{rem_ssh}}, 'pvesm', 'free', "$storeid:$volname"]; + + eval{ PVE::Tools::run_command($cmd, outfunc => sub {}, errfunc => sub {}) }; + if (my $err = $@) { + $self->log('err', $err); + $self->{errors} = 1; + } + } +} + sub phase1 { my ($self, $vmid) = @_; @@ -277,7 +440,7 @@ sub phase1 { # set migrate lock in config file $conf->{lock} = 'migrate'; - PVE::QemuServer::update_config_nolock($vmid, $conf, 1); + PVE::QemuConfig->write_config($vmid, $conf); sync_disks($self, $vmid); @@ -290,7 +453,7 @@ sub phase1_cleanup { my $conf = $self->{vmconf}; delete $conf->{lock}; - eval { PVE::QemuServer::update_config_nolock($vmid, $conf, 1) }; + eval { PVE::QemuConfig->write_config($vmid, $conf) }; if (my $err = $@) { $self->log('err', $err); } @@ -312,6 +475,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,12 +487,29 @@ sub phase2 { $spice_ticket = $res->{ticket}; } - push @$cmd , 'qm', 'start', $vmid, '--stateuri', 'tcp', '--skiplock', '--migratedfrom', $nodename; + push @$cmd , 'qm', 'start', $vmid, '--skiplock', '--migratedfrom', $nodename; + + my $migration_type = $self->{opts}->{migration_type}; + + push @$cmd, '--migration_type', $migration_type; + + push @$cmd, '--migration_network', $self->{opts}->{migration_network} + if $self->{opts}->{migration_network}; + + if ($migration_type eq 'insecure') { + push @$cmd, '--stateuri', 'tcp'; + } else { + push @$cmd, '--stateuri', 'unix'; + } if ($self->{forcemachine}) { push @$cmd, '--machine', $self->{forcemachine}; } + if ($self->{opts}->{targetstorage}) { + push @$cmd, '--targetstorage', $self->{opts}->{targetstorage}; + } + my $spice_port; # Note: We try to keep $spice_ticket secret (do not pass via command line parameter) @@ -339,14 +520,31 @@ 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); } + elsif ($line =~ m/^storage migration listens on nbd:(localhost|[\d\.]+|\[[\d\.:a-fA-F]+\]):(\d+):exportname=(\S+) volume:(\S+)$/) { + my $volid = $4; + my $nbd_uri = "nbd:$1:$2:exportname=$3"; + my $targetdrive = $3; + $targetdrive =~ s/drive-//g; + + $self->{target_drive}->{$targetdrive}->{volid} = $volid; + $self->{target_drive}->{$targetdrive}->{nbd_uri} = $nbd_uri; + + } }, errfunc => sub { my $line = shift; $self->log('info', $line); @@ -354,14 +552,59 @@ 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 ($migration_type eq 'secure') { + $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:/) { + my $tunnel_addr; + if ($raddr eq "localhost") { + # 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); + $tunnel_addr = "$lport:localhost:$rport"; + } + + $self->{tunnel} = $self->fork_tunnel($tunnel_addr); + + } else { + die "unsupported protocol in migration URI: $ruri\n"; + } + } my $start = time(); - $self->log('info', "starting online/live migration on $raddr:$rport"); + + 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 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}}){ + my $nbd_uri = $self->{target_drive}->{$drive}->{nbd_uri}; + $self->log('info', "$drive: start migration to to $nbd_uri"); + PVE::QemuServer::qemu_drive_mirror($vmid, $drive, $nbd_uri, $vmid, undef, $self->{storage_migration_jobs}, 1); + } + } + + $self->log('info', "starting online/live migration on $ruri"); $self->{livemigration} = 1; # load_defaults @@ -388,6 +631,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 +639,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 +665,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 +690,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 +698,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 +716,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 +769,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); @@ -546,12 +797,25 @@ sub phase2_cleanup { my $conf = $self->{vmconf}; delete $conf->{lock}; - eval { PVE::QemuServer::update_config_nolock($vmid, $conf, 1) }; + eval { PVE::QemuConfig->write_config($vmid, $conf) }; if (my $err = $@) { $self->log('err', $err); } # cleanup ressources on target host + if ($self->{storage_migration}) { + + eval { PVE::QemuServer::qemu_blockjobs_cancel($vmid, $self->{storage_migration_jobs}) }; + if (my $err = $@) { + $self->log('err', $err); + } + + eval { PVE::QemuMigrate::cleanup_remotedisks($self) }; + if (my $err = $@) { + $self->log('err', $err); + } + } + my $nodename = PVE::INotify::nodename(); my $cmd = [@{$self->{rem_ssh}}, 'qm', 'stop', $vmid, '--skiplock', '--migratedfrom', $nodename]; @@ -593,15 +857,52 @@ sub phase3_cleanup { my $conf = $self->{vmconf}; return if $self->{phase2errors}; + 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); + PVE::QemuConfig->write_config($vmid, $conf); + } + } + } + + # transfer replication state before move config + $self->transfer_replication_state(); + # move config to remote node - my $conffile = PVE::QemuServer::config_file($vmid); - my $newconffile = PVE::QemuServer::config_file($vmid, $self->{node}); + my $conffile = PVE::QemuConfig->config_file($vmid); + my $newconffile = PVE::QemuConfig->config_file($vmid, $self->{node}); die "Failed to move config to node '$self->{node}' - rename failed: $!\n" if !rename($conffile, $newconffile); + $self->switch_replication_job_target(); + if ($self->{livemigration}) { - # now that config file is move, we can resume vm on target if livemigrate + 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]; + + 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 my $cmd = [@{$self->{rem_ssh}}, 'qm', 'resume', $vmid, '--skiplock', '--nocheck']; eval{ PVE::Tools::run_command($cmd, outfunc => sub {}, errfunc => sub { @@ -616,7 +917,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"); @@ -647,6 +947,21 @@ sub phase3_cleanup { $self->{errors} = 1; } + if($self->{storage_migration}) { + # destroy local copies + my $volids = $self->{online_local_volumes}; + + foreach my $volid (@$volids) { + eval { PVE::Storage::vdisk_free($self->{storecfg}, $volid); }; + if (my $err = $@) { + $self->log('err', "removing local copy of '$volid' failed - $err"); + $self->{errors} = 1; + last if $err =~ /^interrupted by signal$/; + } + } + + } + # clear migrate lock my $cmd = [ @{$self->{rem_ssh}}, 'qm', 'unlock', $vmid ]; $self->cmd_logerr($cmd, errmsg => "failed to clear migrate lock");