1 package PVE
::QemuMigrate
;
5 use PVE
::AbstractMigrate
;
8 use POSIX
qw( WNOHANG );
14 use Time
::HiRes
qw( usleep );
15 use PVE
::RPCEnvironment
;
16 use PVE
::ReplicationConfig
;
17 use PVE
::ReplicationState
;
20 use base
qw(PVE::AbstractMigrate);
22 sub fork_command_pipe
{
23 my ($self, $cmd) = @_;
25 my $reader = IO
::File-
>new();
26 my $writer = IO
::File-
>new();
32 eval { $cpid = open2
($reader, $writer, @$cmd); };
37 if ($orig_pid != $$) {
38 $self->log('err', "can't fork command pipe\n");
45 return { writer
=> $writer, reader
=> $reader, pid
=> $cpid };
48 sub finish_command_pipe
{
49 my ($self, $cmdpipe, $timeout) = @_;
51 my $cpid = $cmdpipe->{pid
};
52 return if !defined($cpid);
54 my $writer = $cmdpipe->{writer
};
55 my $reader = $cmdpipe->{reader
};
60 my $collect_child_process = sub {
61 my $res = waitpid($cpid, WNOHANG
);
62 if (defined($res) && ($res == $cpid)) {
63 delete $cmdpipe->{cpid
};
71 for (my $i = 0; $i < $timeout; $i++) {
72 return if &$collect_child_process();
77 $self->log('info', "ssh tunnel still running - terminating now with SIGTERM\n");
81 for (my $i = 0; $i < 10; $i++) {
82 return if &$collect_child_process();
86 $self->log('info', "ssh tunnel still running - terminating now with SIGKILL\n");
90 $self->log('err', "ssh tunnel child process (PID $cpid) couldn't be collected\n")
91 if !&$collect_child_process();
95 my ($self, $tunnel_addr) = @_;
97 my @localtunnelinfo = defined($tunnel_addr) ?
('-L' , $tunnel_addr ) : ();
99 my $cmd = [@{$self->{rem_ssh
}}, '-o ExitOnForwardFailure=yes', @localtunnelinfo, 'qm', 'mtunnel' ];
101 my $tunnel = $self->fork_command_pipe($cmd);
103 my $reader = $tunnel->{reader
};
107 PVE
::Tools
::run_with_timeout
(60, sub { $helo = <$reader>; });
108 die "no reply\n" if !$helo;
109 die "no quorum on target node\n" if $helo =~ m/^no quorum$/;
110 die "got strange reply from mtunnel ('$helo')\n"
111 if $helo !~ m/^tunnel online$/;
116 $self->finish_command_pipe($tunnel);
117 die "can't open migration tunnel - $err";
123 my ($self, $tunnel) = @_;
125 my $writer = $tunnel->{writer
};
128 PVE
::Tools
::run_with_timeout
(30, sub {
129 print $writer "quit\n";
135 $self->finish_command_pipe($tunnel, 30);
137 if ($tunnel->{sock_addr
}) {
138 # ssh does not clean up on local host
139 my $cmd = ['rm', '-f', $tunnel->{sock_addr
}]; #
140 PVE
::Tools
::run_command
($cmd);
142 # .. and just to be sure check on remote side
143 unshift @{$cmd}, @{$self->{rem_ssh
}};
144 PVE
::Tools
::run_command
($cmd);
151 my ($self, $vmid, $code, @param) = @_;
153 return PVE
::QemuConfig-
>lock_config($vmid, $code, @param);
157 my ($self, $vmid) = @_;
159 my $online = $self->{opts
}->{online
};
161 $self->{storecfg
} = PVE
::Storage
::config
();
164 my $conf = $self->{vmconf
} = PVE
::QemuConfig-
>load_config($vmid);
166 PVE
::QemuConfig-
>check_lock($conf);
169 if (my $pid = PVE
::QemuServer
::check_running
($vmid)) {
170 die "can't migrate running VM without --online\n" if !$online;
173 $self->{forcemachine
} = PVE
::QemuServer
::qemu_machine_pxe
($vmid, $conf);
177 if (my $loc_res = PVE
::QemuServer
::check_local_resources
($conf, 1)) {
178 if ($self->{running
} || !$self->{opts
}->{force
}) {
179 die "can't migrate VM which uses local devices\n";
181 $self->log('info', "migrating VM which uses local devices");
185 my $vollist = PVE
::QemuServer
::get_vm_volumes
($conf);
187 my $need_activate = [];
188 foreach my $volid (@$vollist) {
189 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
191 # check if storage is available on both nodes
192 my $targetsid = $self->{opts
}->{targetstorage
} ?
$self->{opts
}->{targetstorage
} : $sid;
194 my $scfg = PVE
::Storage
::storage_check_node
($self->{storecfg
}, $sid);
195 PVE
::Storage
::storage_check_node
($self->{storecfg
}, $targetsid, $self->{node
});
197 if ($scfg->{shared
}) {
198 # PVE::Storage::activate_storage checks this for non-shared storages
199 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
200 warn "Used shared storage '$sid' is not online on source node!\n"
201 if !$plugin->check_connection($sid, $scfg);
203 # only activate if not shared
204 push @$need_activate, $volid;
209 PVE
::Storage
::activate_volumes
($self->{storecfg
}, $need_activate);
211 # test ssh connection
212 my $cmd = [ @{$self->{rem_ssh
}}, '/bin/true' ];
213 eval { $self->cmd_quiet($cmd); };
214 die "Can't connect to destination address using public key\n" if $@;
220 my ($self, $vmid) = @_;
222 my $conf = $self->{vmconf
};
224 # local volumes which have been copied
225 $self->{volumes
} = [];
231 # found local volumes and their origin
232 my $local_volumes = {};
233 my $local_volumes_errors = {};
234 my $other_errors = [];
239 my $log_error = sub {
240 my ($msg, $volid) = @_;
242 if (defined($volid)) {
243 $local_volumes_errors->{$volid} = $msg;
245 push @$other_errors, $msg;
250 my @sids = PVE
::Storage
::storage_ids
($self->{storecfg
});
251 foreach my $storeid (@sids) {
252 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $storeid);
253 next if $scfg->{shared
};
254 next if !PVE
::Storage
::storage_check_enabled
($self->{storecfg
}, $storeid, undef, 1);
256 # get list from PVE::Storage (for unused volumes)
257 my $dl = PVE
::Storage
::vdisk_list
($self->{storecfg
}, $storeid, $vmid);
259 next if @{$dl->{$storeid}} == 0;
261 my $targetsid = $self->{opts
}->{targetstorage
} ?
$self->{opts
}->{targetstorage
} : $storeid;
263 # check if storage is available on target node
264 PVE
::Storage
::storage_check_node
($self->{storecfg
}, $targetsid, $self->{node
});
265 $sharedvm = 0; # there is a non-shared disk
267 PVE
::Storage
::foreach_volid
($dl, sub {
268 my ($volid, $sid, $volname) = @_;
270 $local_volumes->{$volid} = 'storage';
274 my $test_volid = sub {
275 my ($volid, $attr) = @_;
277 if ($volid =~ m
|^/|) {
278 $local_volumes->{$volid} = 'config';
279 die "local file/device\n";
282 my $snaprefs = $attr->{referenced_in_snapshot
};
284 if ($attr->{cdrom
}) {
285 if ($volid eq 'cdrom') {
286 my $msg = "can't migrate local cdrom drive";
287 if (defined($snaprefs) && !$attr->{referenced_in_config
}) {
288 my $snapnames = join(', ', sort keys %$snaprefs);
289 $msg .= " (referenced in snapshot - $snapnames)";
291 &$log_error("$msg\n");
294 return if $volid eq 'none';
297 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
299 my $targetsid = $self->{opts
}->{targetstorage
} ?
$self->{opts
}->{targetstorage
} : $sid;
300 # check if storage is available on both nodes
301 my $scfg = PVE
::Storage
::storage_check_node
($self->{storecfg
}, $sid);
302 PVE
::Storage
::storage_check_node
($self->{storecfg
}, $targetsid, $self->{node
});
304 return if $scfg->{shared
};
308 $local_volumes->{$volid} = $attr->{referenced_in_config
} ?
'config' : 'snapshot';
310 die "local cdrom image\n" if $attr->{cdrom
};
312 my ($path, $owner) = PVE
::Storage
::path
($self->{storecfg
}, $volid);
314 die "owned by other VM (owner = VM $owner)\n"
315 if !$owner || ($owner != $self->{vmid
});
317 if (defined($snaprefs)) {
318 # we cannot migrate shapshots on local storage
319 # exceptions: 'zfspool' or 'qcow2' files (on directory storage)
321 my $format = PVE
::QemuServer
::qemu_img_format
($scfg, $volname);
322 die "online storage migration not possible if snapshot exists\n" if $self->{running
};
323 if (!($scfg->{type
} eq 'zfspool' || $format eq 'qcow2')) {
324 die "non-migratable snapshot exists\n";
328 die "referenced by linked clone(s)\n"
329 if PVE
::Storage
::volume_is_base_and_used
($self->{storecfg
}, $volid);
332 PVE
::QemuServer
::foreach_volid
($conf, sub {
333 my ($volid, $attr) = @_;
334 eval { $test_volid->($volid, $attr); };
336 &$log_error($err, $volid);
340 foreach my $vol (sort keys %$local_volumes) {
341 if ($local_volumes->{$vol} eq 'storage') {
342 $self->log('info', "found local disk '$vol' (via storage)\n");
343 } elsif ($local_volumes->{$vol} eq 'config') {
344 &$log_error("can't live migrate attached local disks without with-local-disks option\n", $vol)
345 if $self->{running
} && !$self->{opts
}->{"with-local-disks"};
346 $self->log('info', "found local disk '$vol' (in current VM config)\n");
347 } elsif ($local_volumes->{$vol} eq 'snapshot') {
348 $self->log('info', "found local disk '$vol' (referenced by snapshot(s))\n");
350 $self->log('info', "found local disk '$vol'\n");
354 foreach my $vol (sort keys %$local_volumes_errors) {
355 $self->log('warn', "can't migrate local disk '$vol': $local_volumes_errors->{$vol}");
357 foreach my $err (@$other_errors) {
358 $self->log('warn', "$err");
361 if ($self->{running
} && !$sharedvm && !$self->{opts
}->{targetstorage
}) {
362 $self->{opts
}->{targetstorage
} = 1; #use same sid for remote local
366 die "can't migrate VM - check log\n";
369 # additional checks for local storage
370 foreach my $volid (keys %$local_volumes) {
371 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
372 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $sid);
374 my $migratable = ($scfg->{type
} eq 'dir') || ($scfg->{type
} eq 'zfspool') ||
375 ($scfg->{type
} eq 'lvmthin') || ($scfg->{type
} eq 'lvm');
377 die "can't migrate '$volid' - storage type '$scfg->{type}' not supported\n"
380 # image is a linked clone on local storage, se we can't migrate.
381 if (my $basename = (PVE
::Storage
::parse_volname
($self->{storecfg
}, $volid))[3]) {
382 die "can't migrate '$volid' as it's a clone of '$basename'";
388 $self->log('info', "copying disk images");
390 my $rep_cfg = PVE
::ReplicationConfig-
>new();
392 if (my $jobcfg = $rep_cfg->find_local_replication_job($vmid, $self->{node
})) {
393 die "can't live migrate VM with replicated volumes\n" if $self->{running
};
394 my $start_time = time();
395 my $logfunc = sub { my ($msg) = @_; $self->log('info', $msg); };
396 $rep_volumes = PVE
::Replication
::run_replication
(
397 'PVE::QemuConfig', $jobcfg, $start_time, $start_time, $logfunc);
400 foreach my $volid (keys %$local_volumes) {
401 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
402 if ($self->{running
} && $self->{opts
}->{targetstorage
} && $local_volumes->{$volid} eq 'config') {
403 push @{$self->{online_local_volumes
}}, $volid;
405 next if $rep_volumes->{$volid};
406 push @{$self->{volumes
}}, $volid;
407 my $insecure = $self->{opts
}->{migration_type
} eq 'insecure';
408 PVE
::Storage
::storage_migrate
($self->{storecfg
}, $volid, $self->{ssh_info
}, $sid,
409 undef, undef, undef, undef, $insecure);
413 die "Failed to sync data - $@" if $@;
416 sub cleanup_remotedisks
{
419 foreach my $target_drive (keys %{$self->{target_drive
}}) {
421 my $drive = PVE
::QemuServer
::parse_drive
($target_drive, $self->{target_drive
}->{$target_drive}->{volid
});
422 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($drive->{file
});
424 my $cmd = [@{$self->{rem_ssh
}}, 'pvesm', 'free', "$storeid:$volname"];
426 eval{ PVE
::Tools
::run_command
($cmd, outfunc
=> sub {}, errfunc
=> sub {}) };
428 $self->log('err', $err);
435 my ($self, $vmid) = @_;
437 $self->log('info', "starting migration of VM $vmid to node '$self->{node}' ($self->{nodeip})");
439 my $conf = $self->{vmconf
};
441 # set migrate lock in config file
442 $conf->{lock} = 'migrate';
443 PVE
::QemuConfig-
>write_config($vmid, $conf);
445 sync_disks
($self, $vmid);
450 my ($self, $vmid, $err) = @_;
452 $self->log('info', "aborting phase 1 - cleanup resources");
454 my $conf = $self->{vmconf
};
455 delete $conf->{lock};
456 eval { PVE
::QemuConfig-
>write_config($vmid, $conf) };
458 $self->log('err', $err);
461 if ($self->{volumes
}) {
462 foreach my $volid (@{$self->{volumes
}}) {
463 $self->log('err', "found stale volume copy '$volid' on node '$self->{node}'");
464 # fixme: try to remove ?
470 my ($self, $vmid) = @_;
472 my $conf = $self->{vmconf
};
474 $self->log('info', "starting VM $vmid on remote node '$self->{node}'");
478 my $ruri; # the whole migration dst. URI (protocol:address[:port])
479 my $nodename = PVE
::INotify
::nodename
();
481 ## start on remote node
482 my $cmd = [@{$self->{rem_ssh
}}];
485 if (PVE
::QemuServer
::vga_conf_has_spice
($conf->{vga
})) {
486 my $res = PVE
::QemuServer
::vm_mon_cmd
($vmid, 'query-spice');
487 $spice_ticket = $res->{ticket
};
490 push @$cmd , 'qm', 'start', $vmid, '--skiplock', '--migratedfrom', $nodename;
492 my $migration_type = $self->{opts
}->{migration_type
};
494 push @$cmd, '--migration_type', $migration_type;
496 push @$cmd, '--migration_network', $self->{opts
}->{migration_network
}
497 if $self->{opts
}->{migration_network
};
499 if ($migration_type eq 'insecure') {
500 push @$cmd, '--stateuri', 'tcp';
502 push @$cmd, '--stateuri', 'unix';
505 if ($self->{forcemachine
}) {
506 push @$cmd, '--machine', $self->{forcemachine
};
509 if ($self->{opts
}->{targetstorage
}) {
510 push @$cmd, '--targetstorage', $self->{opts
}->{targetstorage
};
515 # Note: We try to keep $spice_ticket secret (do not pass via command line parameter)
516 # instead we pipe it through STDIN
517 PVE
::Tools
::run_command
($cmd, input
=> $spice_ticket, outfunc
=> sub {
520 if ($line =~ m/^migration listens on tcp:(localhost|[\d\.]+|\[[\d\.:a-fA-F]+\]):(\d+)$/) {
523 $ruri = "tcp:$raddr:$rport";
525 elsif ($line =~ m!^migration listens on unix:(/run/qemu-server/(\d+)\.migrate)$!) {
527 die "Destination UNIX sockets VMID does not match source VMID" if $vmid ne $2;
528 $ruri = "unix:$raddr";
530 elsif ($line =~ m/^migration listens on port (\d+)$/) {
531 $raddr = "localhost";
533 $ruri = "tcp:$raddr:$rport";
535 elsif ($line =~ m/^spice listens on port (\d+)$/) {
536 $spice_port = int($1);
538 elsif ($line =~ m/^storage migration listens on nbd:(localhost|[\d\.]+|\[[\d\.:a-fA-F]+\]):(\d+):exportname=(\S+) volume:(\S+)$/) {
540 my $nbd_uri = "nbd:$1:$2:exportname=$3";
541 my $targetdrive = $3;
542 $targetdrive =~ s/drive-//g;
544 $self->{target_drive
}->{$targetdrive}->{volid
} = $volid;
545 $self->{target_drive
}->{$targetdrive}->{nbd_uri
} = $nbd_uri;
550 $self->log('info', $line);
553 die "unable to detect remote migration address\n" if !$raddr;
555 if ($migration_type eq 'secure') {
556 $self->log('info', "start remote tunnel");
558 if ($ruri =~ /^unix:/) {
560 $self->{tunnel
} = $self->fork_tunnel("$raddr:$raddr");
561 $self->{tunnel
}->{sock_addr
} = $raddr;
563 my $unix_socket_try = 0; # wait for the socket to become ready
564 while (! -S
$raddr) {
566 if ($unix_socket_try > 100) {
568 $self->finish_tunnel($self->{tunnel
});
569 die "Timeout, migration socket $ruri did not get ready";
575 } elsif ($ruri =~ /^tcp:/) {
577 if ($raddr eq "localhost") {
578 # for backwards compatibility with older qemu-server versions
579 my $pfamily = PVE
::Tools
::get_host_address_family
($nodename);
580 my $lport = PVE
::Tools
::next_migrate_port
($pfamily);
581 $tunnel_addr = "$lport:localhost:$rport";
584 $self->{tunnel
} = $self->fork_tunnel($tunnel_addr);
587 die "unsupported protocol in migration URI: $ruri\n";
593 if ($self->{opts
}->{targetstorage
} && defined($self->{online_local_volumes
})) {
594 $self->{storage_migration
} = 1;
595 $self->{storage_migration_jobs
} = {};
596 $self->log('info', "starting storage migration");
598 die "The number of local disks does not match between the source and the destination.\n"
599 if (scalar(keys %{$self->{target_drive
}}) != scalar @{$self->{online_local_volumes
}});
600 foreach my $drive (keys %{$self->{target_drive
}}){
601 my $nbd_uri = $self->{target_drive
}->{$drive}->{nbd_uri
};
602 $self->log('info', "$drive: start migration to to $nbd_uri");
603 PVE
::QemuServer
::qemu_drive_mirror
($vmid, $drive, $nbd_uri, $vmid, undef, $self->{storage_migration_jobs
}, 1);
607 $self->log('info', "starting online/live migration on $ruri");
608 $self->{livemigration
} = 1;
611 my $defaults = PVE
::QemuServer
::load_defaults
();
613 # always set migrate speed (overwrite kvm default of 32m)
614 # we set a very hight default of 8192m which is basically unlimited
615 my $migrate_speed = $defaults->{migrate_speed
} || 8192;
616 $migrate_speed = $conf->{migrate_speed
} || $migrate_speed;
617 $migrate_speed = $migrate_speed * 1048576;
618 $self->log('info', "migrate_set_speed: $migrate_speed");
620 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate_set_speed", value
=> int($migrate_speed));
622 $self->log('info', "migrate_set_speed error: $@") if $@;
624 my $migrate_downtime = $defaults->{migrate_downtime
};
625 $migrate_downtime = $conf->{migrate_downtime
} if defined($conf->{migrate_downtime
});
626 if (defined($migrate_downtime)) {
627 $self->log('info', "migrate_set_downtime: $migrate_downtime");
629 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate_set_downtime", value
=> int($migrate_downtime*100)/100);
631 $self->log('info', "migrate_set_downtime error: $@") if $@;
634 $self->log('info', "set migration_caps");
636 PVE
::QemuServer
::set_migration_caps
($vmid);
640 #set cachesize 10% of the total memory
641 my $cachesize = int($conf->{memory
}*1048576/10);
642 $self->log('info', "set cachesize: $cachesize");
644 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate-set-cache-size", value
=> int($cachesize));
646 $self->log('info', "migrate-set-cache-size error: $@") if $@;
648 if (PVE
::QemuServer
::vga_conf_has_spice
($conf->{vga
})) {
649 my $rpcenv = PVE
::RPCEnvironment
::get
();
650 my $authuser = $rpcenv->get_user();
652 my (undef, $proxyticket) = PVE
::AccessControl
::assemble_spice_ticket
($authuser, $vmid, $self->{node
});
654 my $filename = "/etc/pve/nodes/$self->{node}/pve-ssl.pem";
655 my $subject = PVE
::AccessControl
::read_x509_subject_spice
($filename);
657 $self->log('info', "spice client_migrate_info");
660 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "client_migrate_info", protocol
=> 'spice',
661 hostname
=> $proxyticket, 'tls-port' => $spice_port,
662 'cert-subject' => $subject);
664 $self->log('info', "client_migrate_info error: $@") if $@;
668 $self->log('info', "start migrate command to $ruri");
670 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate", uri
=> $ruri);
673 $self->log('info', "migrate uri => $ruri failed: $merr") if $merr;
676 my $usleep = 2000000;
680 my $downtimecounter = 0;
683 my $avglstat = $lstat/$i if $lstat;
688 $stat = PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "query-migrate");
692 warn "query migrate failed: $err\n";
693 $self->log('info', "query migrate failed: $err");
694 if ($err_count <= 5) {
698 die "too many query migrate failures - aborting\n";
701 if (defined($stat->{status
}) && $stat->{status
} =~ m/^(setup)$/im) {
706 if (defined($stat->{status
}) && $stat->{status
} =~ m/^(active|completed|failed|cancelled)$/im) {
709 if ($stat->{status
} eq 'completed') {
710 my $delay = time() - $start;
712 my $mbps = sprintf "%.2f", $conf->{memory
}/$delay;
713 my $downtime = $stat->{downtime
} || 0;
714 $self->log('info', "migration speed: $mbps MB/s - downtime $downtime ms");
718 if ($stat->{status
} eq 'failed' || $stat->{status
} eq 'cancelled') {
719 $self->log('info', "migration status error: $stat->{status}");
723 if ($stat->{status
} ne 'active') {
724 $self->log('info', "migration status: $stat->{status}");
728 if ($stat->{ram
}->{transferred
} ne $lstat) {
729 my $trans = $stat->{ram
}->{transferred
} || 0;
730 my $rem = $stat->{ram
}->{remaining
} || 0;
731 my $total = $stat->{ram
}->{total
} || 0;
732 my $xbzrlecachesize = $stat->{"xbzrle-cache"}->{"cache-size"} || 0;
733 my $xbzrlebytes = $stat->{"xbzrle-cache"}->{"bytes"} || 0;
734 my $xbzrlepages = $stat->{"xbzrle-cache"}->{"pages"} || 0;
735 my $xbzrlecachemiss = $stat->{"xbzrle-cache"}->{"cache-miss"} || 0;
736 my $xbzrleoverflow = $stat->{"xbzrle-cache"}->{"overflow"} || 0;
737 #reduce sleep if remainig memory if lower than the everage transfert
738 $usleep = 300000 if $avglstat && $rem < $avglstat;
740 $self->log('info', "migration status: $stat->{status} (transferred ${trans}, " .
741 "remaining ${rem}), total ${total})");
743 if (${xbzrlecachesize
}) {
744 $self->log('info', "migration xbzrle cachesize: ${xbzrlecachesize} transferred ${xbzrlebytes} pages ${xbzrlepages} cachemiss ${xbzrlecachemiss} overflow ${xbzrleoverflow}");
747 if (($lastrem && $rem > $lastrem ) || ($rem == 0)) {
752 if ($downtimecounter > 5) {
753 $downtimecounter = 0;
754 $migrate_downtime *= 2;
755 $self->log('info', "migrate_set_downtime: $migrate_downtime");
757 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate_set_downtime", value
=> int($migrate_downtime*100)/100);
759 $self->log('info', "migrate_set_downtime error: $@") if $@;
765 $lstat = $stat->{ram
}->{transferred
};
769 die "unable to parse migration status '$stat->{status}' - aborting\n";
773 # just to be sure that the tunnel gets closed on successful migration, on error
774 # phase2_cleanup closes it *after* stopping the remote waiting VM
775 if (!$self->{errors
} && $self->{tunnel
}) {
776 eval { finish_tunnel
($self, $self->{tunnel
}); };
778 $self->log('err', $err);
785 my ($self, $vmid, $err) = @_;
787 return if !$self->{errors
};
788 $self->{phase2errors
} = 1;
790 $self->log('info', "aborting phase 2 - cleanup resources");
792 $self->log('info', "migrate_cancel");
794 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate_cancel");
796 $self->log('info', "migrate_cancel error: $@") if $@;
798 my $conf = $self->{vmconf
};
799 delete $conf->{lock};
800 eval { PVE
::QemuConfig-
>write_config($vmid, $conf) };
802 $self->log('err', $err);
805 # cleanup ressources on target host
806 if ($self->{storage_migration
}) {
808 eval { PVE
::QemuServer
::qemu_blockjobs_cancel
($vmid, $self->{storage_migration_jobs
}) };
810 $self->log('err', $err);
813 eval { PVE
::QemuMigrate
::cleanup_remotedisks
($self) };
815 $self->log('err', $err);
819 my $nodename = PVE
::INotify
::nodename
();
821 my $cmd = [@{$self->{rem_ssh
}}, 'qm', 'stop', $vmid, '--skiplock', '--migratedfrom', $nodename];
822 eval{ PVE
::Tools
::run_command
($cmd, outfunc
=> sub {}, errfunc
=> sub {}) };
824 $self->log('err', $err);
828 if ($self->{tunnel
}) {
829 eval { finish_tunnel
($self, $self->{tunnel
}); };
831 $self->log('err', $err);
838 my ($self, $vmid) = @_;
840 my $volids = $self->{volumes
};
841 return if $self->{phase2errors
};
843 # destroy local copies
844 foreach my $volid (@$volids) {
845 eval { PVE
::Storage
::vdisk_free
($self->{storecfg
}, $volid); };
847 $self->log('err', "removing local copy of '$volid' failed - $err");
849 last if $err =~ /^interrupted by signal$/;
855 # transfer replication state for vmid to migration target node.
856 my $transfer_replication_state = sub {
857 my ($self, $vmid) = @_;
859 my $stateobj = PVE
::ReplicationState
::read_state
();
861 if (defined($stateobj->{$vmid})) {
862 # This have to be quoted when it run it over ssh.
863 my $state = PVE
::Tools
::shellquote
(encode_json
($stateobj->{$vmid}));
865 my $cmd = [ @{$self->{rem_ssh
}}, 'pvesr', 'set-state', $vmid, $state];
870 # switch replication job target
871 my $switch_replication_job_target = sub {
872 my ($self, $vmid) = @_;
873 my $transfer_job = sub {
874 my $rep_cfg = PVE
::ReplicationConfig-
>new();
875 my $jobcfg = $rep_cfg->find_local_replication_job($vmid, $self->{node
});
879 $jobcfg->{target
} = PVE
::INotify
::nodename
();
884 PVE
::ReplicationConfig
::lock($transfer_job);
888 my ($self, $vmid, $err) = @_;
890 my $conf = $self->{vmconf
};
891 return if $self->{phase2errors
};
893 if ($self->{storage_migration
}) {
895 eval { PVE
::QemuServer
::qemu_drive_mirror_monitor
($vmid, undef, $self->{storage_migration_jobs
}); };
898 eval { PVE
::QemuServer
::qemu_blockjobs_cancel
($vmid, $self->{storage_migration_jobs
}) };
899 eval { PVE
::QemuMigrate
::cleanup_remotedisks
($self) };
900 die "Failed to completed storage migration\n";
902 foreach my $target_drive (keys %{$self->{target_drive
}}) {
903 my $drive = PVE
::QemuServer
::parse_drive
($target_drive, $self->{target_drive
}->{$target_drive}->{volid
});
904 $conf->{$target_drive} = PVE
::QemuServer
::print_drive
($vmid, $drive);
905 PVE
::QemuConfig-
>write_config($vmid, $conf);
910 # transfer replication state before move config
911 eval { $transfer_replication_state->($self, $vmid); };
913 $self->log('err', "transfer replication state failed - $err");
917 # move config to remote node
918 my $conffile = PVE
::QemuConfig-
>config_file($vmid);
919 my $newconffile = PVE
::QemuConfig-
>config_file($vmid, $self->{node
});
921 die "Failed to move config to node '$self->{node}' - rename failed: $!\n"
922 if !rename($conffile, $newconffile);
924 eval { $switch_replication_job_target->($self, $vmid); };
926 $self->log('err', "switch replication job target failed - $err");
930 if ($self->{livemigration
}) {
931 if ($self->{storage_migration
}) {
932 # remove drives referencing the nbd server from source
933 # otherwise vm_stop might hang later on
934 foreach my $drive (keys %{$self->{target_drive
}}){
935 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "device_del", id
=> $drive);
937 # stop nbd server on remote vm - requirement for resume since 2.9
938 my $cmd = [@{$self->{rem_ssh
}}, 'qm', 'nbdstop', $vmid];
940 eval{ PVE
::Tools
::run_command
($cmd, outfunc
=> sub {}, errfunc
=> sub {}) };
942 $self->log('err', $err);
946 # config moved and nbd server stopped - now we can resume vm on target
947 my $cmd = [@{$self->{rem_ssh
}}, 'qm', 'resume', $vmid, '--skiplock', '--nocheck'];
948 eval{ PVE
::Tools
::run_command
($cmd, outfunc
=> sub {},
951 $self->log('err', $line);
955 $self->log('err', $err);
962 if (PVE
::QemuServer
::vga_conf_has_spice
($conf->{vga
}) && $self->{running
}) {
963 $self->log('info', "Waiting for spice server migration");
965 my $res = PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, 'query-spice');
966 last if int($res->{'migrated'}) == 1;
974 # always stop local VM
975 eval { PVE
::QemuServer
::vm_stop
($self->{storecfg
}, $vmid, 1, 1); };
977 $self->log('err', "stopping vm failed - $err");
981 # always deactivate volumes - avoid lvm LVs to be active on several nodes
983 my $vollist = PVE
::QemuServer
::get_vm_volumes
($conf);
984 PVE
::Storage
::deactivate_volumes
($self->{storecfg
}, $vollist);
987 $self->log('err', $err);
991 if($self->{storage_migration
}) {
992 # destroy local copies
993 my $volids = $self->{online_local_volumes
};
995 foreach my $volid (@$volids) {
996 eval { PVE
::Storage
::vdisk_free
($self->{storecfg
}, $volid); };
998 $self->log('err', "removing local copy of '$volid' failed - $err");
1000 last if $err =~ /^interrupted by signal$/;
1006 # clear migrate lock
1007 my $cmd = [ @{$self->{rem_ssh
}}, 'qm', 'unlock', $vmid ];
1008 $self->cmd_logerr($cmd, errmsg
=> "failed to clear migrate lock");
1012 my ($self, $vmid) = @_;