]> git.proxmox.com Git - qemu-server.git/blob - PVE/QemuMigrate.pm
add check for snapshots at migration
[qemu-server.git] / PVE / QemuMigrate.pm
1 package PVE::QemuMigrate;
2
3 use strict;
4 use warnings;
5 use PVE::AbstractMigrate;
6 use IO::File;
7 use IPC::Open2;
8 use POSIX qw( WNOHANG );
9 use PVE::INotify;
10 use PVE::Tools;
11 use PVE::Cluster;
12 use PVE::Storage;
13 use PVE::QemuServer;
14 use Time::HiRes qw( usleep );
15 use PVE::RPCEnvironment;
16
17 use base qw(PVE::AbstractMigrate);
18
19 sub fork_command_pipe {
20 my ($self, $cmd) = @_;
21
22 my $reader = IO::File->new();
23 my $writer = IO::File->new();
24
25 my $orig_pid = $$;
26
27 my $cpid;
28
29 eval { $cpid = open2($reader, $writer, @$cmd); };
30
31 my $err = $@;
32
33 # catch exec errors
34 if ($orig_pid != $$) {
35 $self->log('err', "can't fork command pipe\n");
36 POSIX::_exit(1);
37 kill('KILL', $$);
38 }
39
40 die $err if $err;
41
42 return { writer => $writer, reader => $reader, pid => $cpid };
43 }
44
45 sub finish_command_pipe {
46 my ($self, $cmdpipe, $timeout) = @_;
47
48 my $cpid = $cmdpipe->{pid};
49 return if !defined($cpid);
50
51 my $writer = $cmdpipe->{writer};
52 my $reader = $cmdpipe->{reader};
53
54 $writer->close();
55 $reader->close();
56
57 my $collect_child_process = sub {
58 my $res = waitpid($cpid, WNOHANG);
59 if (defined($res) && ($res == $cpid)) {
60 delete $cmdpipe->{cpid};
61 return 1;
62 } else {
63 return 0;
64 }
65 };
66
67 if ($timeout) {
68 for (my $i = 0; $i < $timeout; $i++) {
69 return if &$collect_child_process();
70 sleep(1);
71 }
72 }
73
74 $self->log('info', "ssh tunnel still running - terminating now with SIGTERM\n");
75 kill(15, $cpid);
76
77 # wait again
78 for (my $i = 0; $i < 10; $i++) {
79 return if &$collect_child_process();
80 sleep(1);
81 }
82
83 $self->log('info', "ssh tunnel still running - terminating now with SIGKILL\n");
84 kill 9, $cpid;
85 sleep 1;
86
87 $self->log('err', "ssh tunnel child process (PID $cpid) couldn't be collected\n")
88 if !&$collect_child_process();
89 }
90
91 sub fork_tunnel {
92 my ($self, $tunnel_addr) = @_;
93
94 my @localtunnelinfo = defined($tunnel_addr) ? ('-L' , $tunnel_addr ) : ();
95
96 my $cmd = [@{$self->{rem_ssh}}, '-o ExitOnForwardFailure=yes', @localtunnelinfo, 'qm', 'mtunnel' ];
97
98 my $tunnel = $self->fork_command_pipe($cmd);
99
100 my $reader = $tunnel->{reader};
101
102 my $helo;
103 eval {
104 PVE::Tools::run_with_timeout(60, sub { $helo = <$reader>; });
105 die "no reply\n" if !$helo;
106 die "no quorum on target node\n" if $helo =~ m/^no quorum$/;
107 die "got strange reply from mtunnel ('$helo')\n"
108 if $helo !~ m/^tunnel online$/;
109 };
110 my $err = $@;
111
112 if ($err) {
113 $self->finish_command_pipe($tunnel);
114 die "can't open migration tunnel - $err";
115 }
116 return $tunnel;
117 }
118
119 sub finish_tunnel {
120 my ($self, $tunnel) = @_;
121
122 my $writer = $tunnel->{writer};
123
124 eval {
125 PVE::Tools::run_with_timeout(30, sub {
126 print $writer "quit\n";
127 $writer->flush();
128 });
129 };
130 my $err = $@;
131
132 $self->finish_command_pipe($tunnel, 30);
133
134 if ($tunnel->{sock_addr}) {
135 # ssh does not clean up on local host
136 my $cmd = ['rm', '-f', $tunnel->{sock_addr}]; #
137 PVE::Tools::run_command($cmd);
138
139 # .. and just to be sure check on remote side
140 unshift @{$cmd}, @{$self->{rem_ssh}};
141 PVE::Tools::run_command($cmd);
142 }
143
144 die $err if $err;
145 }
146
147 sub lock_vm {
148 my ($self, $vmid, $code, @param) = @_;
149
150 return PVE::QemuConfig->lock_config($vmid, $code, @param);
151 }
152
153 sub prepare {
154 my ($self, $vmid) = @_;
155
156 my $online = $self->{opts}->{online};
157
158 $self->{storecfg} = PVE::Storage::config();
159
160 # test if VM exists
161 my $conf = $self->{vmconf} = PVE::QemuConfig->load_config($vmid);
162
163 PVE::QemuConfig->check_lock($conf);
164
165 my $running = 0;
166 if (my $pid = PVE::QemuServer::check_running($vmid)) {
167 die "cant migrate running VM without --online\n" if !$online;
168 $running = $pid;
169
170 $self->{forcemachine} = PVE::QemuServer::qemu_machine_pxe($vmid, $conf);
171
172 }
173
174 if (my $loc_res = PVE::QemuServer::check_local_resources($conf, 1)) {
175 if ($self->{running} || !$self->{opts}->{force}) {
176 die "can't migrate VM which uses local devices\n";
177 } else {
178 $self->log('info', "migrating VM which uses local devices");
179 }
180 }
181
182 my $vollist = PVE::QemuServer::get_vm_volumes($conf);
183
184 my $need_activate = [];
185 foreach my $volid (@$vollist) {
186 my ($sid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
187
188 # check if storage is available on both nodes
189 my $scfg = PVE::Storage::storage_check_node($self->{storecfg}, $sid);
190 PVE::Storage::storage_check_node($self->{storecfg}, $sid, $self->{node});
191
192 if ($scfg->{shared}) {
193 # PVE::Storage::activate_storage checks this for non-shared storages
194 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
195 warn "Used shared storage '$sid' is not online on source node!\n"
196 if !$plugin->check_connection($sid, $scfg);
197 } else {
198 # only activate if not shared
199 push @$need_activate, $volid;
200 }
201 }
202
203 # activate volumes
204 PVE::Storage::activate_volumes($self->{storecfg}, $need_activate);
205
206 # test ssh connection
207 my $cmd = [ @{$self->{rem_ssh}}, '/bin/true' ];
208 eval { $self->cmd_quiet($cmd); };
209 die "Can't connect to destination address using public key\n" if $@;
210
211 return $running;
212 }
213
214 sub sync_disks {
215 my ($self, $vmid) = @_;
216
217 $self->log('info', "copying disk images");
218
219 my $conf = $self->{vmconf};
220
221 $self->{volumes} = [];
222
223 my $res = [];
224
225 eval {
226
227 my $volhash = {};
228 my $cdromhash = {};
229
230 my $sharedvm = 1;
231
232 my @sids = PVE::Storage::storage_ids($self->{storecfg});
233 foreach my $storeid (@sids) {
234 my $scfg = PVE::Storage::storage_config($self->{storecfg}, $storeid);
235 next if $scfg->{shared};
236 next if !PVE::Storage::storage_check_enabled($self->{storecfg}, $storeid, undef, 1);
237
238 # get list from PVE::Storage (for unused volumes)
239 my $dl = PVE::Storage::vdisk_list($self->{storecfg}, $storeid, $vmid);
240 PVE::Storage::foreach_volid($dl, sub {
241 my ($volid, $sid, $volname) = @_;
242
243 # check if storage is available on target node
244 PVE::Storage::storage_check_node($self->{storecfg}, $sid, $self->{node});
245
246 $volhash->{$volid} = 1;
247 $sharedvm = 0; # there is a non-shared disk
248 });
249 }
250
251 my $snapname;
252
253 my $test_volid = sub {
254 my ($volid, $is_cdrom) = @_;
255
256 return if !$volid;
257
258 die "can't migrate local file/device '$volid'\n" if $volid =~ m|^/|;
259
260 if ($is_cdrom) {
261 die "cant migrate local cdrom drive\n" if $volid eq 'cdrom';
262 return if $volid eq 'none';
263 $cdromhash->{$volid} = 1;
264 }
265
266 my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
267
268 # check if storage is available on both nodes
269 my $scfg = PVE::Storage::storage_check_node($self->{storecfg}, $sid);
270 PVE::Storage::storage_check_node($self->{storecfg}, $sid, $self->{node});
271
272 return if $scfg->{shared};
273
274 $sharedvm = 0;
275
276 die "can't migrate local cdrom '$volid'\n" if $cdromhash->{$volid};
277
278 my ($path, $owner) = PVE::Storage::path($self->{storecfg}, $volid);
279
280 die "can't migrate volume '$volid' - owned by other VM (owner = VM $owner)\n"
281 if !$owner || ($owner != $self->{vmid});
282
283 if (defined($snapname)) {
284 # we cannot migrate shapshots on local storage
285 # exceptions: 'zfspool' or 'qcow2' files (on directory storage)
286
287 my $format = PVE::QemuServer::qemu_img_format($scfg, $volname);
288
289 if (($scfg->{type} eq 'zfspool') || ($format eq 'qcow2')) {
290 $volhash->{$volid} = 1;
291 return;
292 }
293
294 die "can't migrate snapshot of local volume '$volid'\n";
295
296 } else {
297 $volhash->{$volid} = 1;
298 }
299 };
300
301 PVE::QemuServer::foreach_volid($conf, $test_volid);
302 foreach $snapname (keys %{$conf->{snapshots}}) {
303 PVE::QemuServer::foreach_volid($conf->{snapshots}->{$snapname}, $test_volid);
304 }
305
306 if ($self->{running} && !$sharedvm) {
307 die "can't do online migration - VM uses local disks\n";
308 }
309
310 # additional checks for local storage
311 foreach my $volid (keys %$volhash) {
312 my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
313 my $scfg = PVE::Storage::storage_config($self->{storecfg}, $sid);
314
315 my $migratable = ($scfg->{type} eq 'dir') || ($scfg->{type} eq 'zfspool') ||
316 ($scfg->{type} eq 'lvmthin') || ($scfg->{type} eq 'lvm');
317
318 die "can't migrate '$volid' - storage type '$scfg->{type}' not supported\n"
319 if !$migratable;
320
321 # image is a linked clone on local storage, se we can't migrate.
322 if (my $basename = (PVE::Storage::parse_volname($self->{storecfg}, $volid))[3]) {
323 die "can't migrate '$volid' as it's a clone of '$basename'";
324 }
325 }
326
327 foreach my $volid (keys %$volhash) {
328 my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
329 push @{$self->{volumes}}, $volid;
330 PVE::Storage::storage_migrate($self->{storecfg}, $volid, $self->{nodeip}, $sid);
331 }
332 };
333 die "Failed to sync data - $@" if $@;
334 }
335
336 sub phase1 {
337 my ($self, $vmid) = @_;
338
339 $self->log('info', "starting migration of VM $vmid to node '$self->{node}' ($self->{nodeip})");
340
341 my $conf = $self->{vmconf};
342
343 # set migrate lock in config file
344 $conf->{lock} = 'migrate';
345 PVE::QemuConfig->write_config($vmid, $conf);
346
347 sync_disks($self, $vmid);
348
349 };
350
351 sub phase1_cleanup {
352 my ($self, $vmid, $err) = @_;
353
354 $self->log('info', "aborting phase 1 - cleanup resources");
355
356 my $conf = $self->{vmconf};
357 delete $conf->{lock};
358 eval { PVE::QemuConfig->write_config($vmid, $conf) };
359 if (my $err = $@) {
360 $self->log('err', $err);
361 }
362
363 if ($self->{volumes}) {
364 foreach my $volid (@{$self->{volumes}}) {
365 $self->log('err', "found stale volume copy '$volid' on node '$self->{node}'");
366 # fixme: try to remove ?
367 }
368 }
369 }
370
371 sub phase2 {
372 my ($self, $vmid) = @_;
373
374 my $conf = $self->{vmconf};
375
376 $self->log('info', "starting VM $vmid on remote node '$self->{node}'");
377
378 my $raddr;
379 my $rport;
380 my $ruri; # the whole migration dst. URI (protocol:address[:port])
381 my $nodename = PVE::INotify::nodename();
382
383 ## start on remote node
384 my $cmd = [@{$self->{rem_ssh}}];
385
386 my $spice_ticket;
387 if (PVE::QemuServer::vga_conf_has_spice($conf->{vga})) {
388 my $res = PVE::QemuServer::vm_mon_cmd($vmid, 'query-spice');
389 $spice_ticket = $res->{ticket};
390 }
391
392 push @$cmd , 'qm', 'start', $vmid, '--skiplock', '--migratedfrom', $nodename;
393
394 # we use TCP only for unsecure migrations as TCP ssh forward tunnels often
395 # did appeared to late (they are hard, if not impossible, to check for)
396 # secure migration use UNIX sockets now, this *breaks* compatibilty when trying
397 # to migrate from new to old but *not* from old to new.
398 my $datacenterconf = PVE::Cluster::cfs_read_file('datacenter.cfg');
399 my $secure_migration = ($datacenterconf->{migration_unsecure}) ? 0 : 1;
400
401 if (!$secure_migration) {
402 push @$cmd, '--stateuri', 'tcp';
403 } else {
404 push @$cmd, '--stateuri', 'unix';
405 }
406
407 if ($self->{forcemachine}) {
408 push @$cmd, '--machine', $self->{forcemachine};
409 }
410
411 my $spice_port;
412
413 # Note: We try to keep $spice_ticket secret (do not pass via command line parameter)
414 # instead we pipe it through STDIN
415 PVE::Tools::run_command($cmd, input => $spice_ticket, outfunc => sub {
416 my $line = shift;
417
418 if ($line =~ m/^migration listens on tcp:(localhost|[\d\.]+|\[[\d\.:a-fA-F]+\]):(\d+)$/) {
419 $raddr = $1;
420 $rport = int($2);
421 $ruri = "tcp:$raddr:$rport";
422 }
423 elsif ($line =~ m!^migration listens on unix:(/run/qemu-server/(\d+)\.migrate)$!) {
424 $raddr = $1;
425 die "Destination UNIX sockets VMID does not match source VMID" if $vmid ne $2;
426 $ruri = "unix:$raddr";
427 }
428 elsif ($line =~ m/^migration listens on port (\d+)$/) {
429 $raddr = "localhost";
430 $rport = int($1);
431 $ruri = "tcp:$raddr:$rport";
432 }
433 elsif ($line =~ m/^spice listens on port (\d+)$/) {
434 $spice_port = int($1);
435 }
436 }, errfunc => sub {
437 my $line = shift;
438 $self->log('info', $line);
439 });
440
441 die "unable to detect remote migration address\n" if !$raddr;
442
443 if ($secure_migration) {
444 $self->log('info', "start remote tunnel");
445
446 if ($ruri =~ /^unix:/) {
447 unlink $raddr;
448 $self->{tunnel} = $self->fork_tunnel("$raddr:$raddr");
449 $self->{tunnel}->{sock_addr} = $raddr;
450
451 my $unix_socket_try = 0; # wait for the socket to become ready
452 while (! -S $raddr) {
453 $unix_socket_try++;
454 if ($unix_socket_try > 100) {
455 $self->{errors} = 1;
456 $self->finish_tunnel($self->{tunnel});
457 die "Timeout, migration socket $ruri did not get ready";
458 }
459
460 usleep(50000);
461 }
462
463 } elsif ($ruri =~ /^tcp:/) {
464 my $tunnel_addr;
465 if ($raddr eq "localhost") {
466 # for backwards compatibility with older qemu-server versions
467 my $pfamily = PVE::Tools::get_host_address_family($nodename);
468 my $lport = PVE::Tools::next_migrate_port($pfamily);
469 $tunnel_addr = "$lport:localhost:$rport";
470 }
471
472 $self->{tunnel} = $self->fork_tunnel($tunnel_addr);
473
474 } else {
475 die "unsupported protocol in migration URI: $ruri\n";
476 }
477 }
478
479 my $start = time();
480 $self->log('info', "starting online/live migration on $ruri");
481 $self->{livemigration} = 1;
482
483 # load_defaults
484 my $defaults = PVE::QemuServer::load_defaults();
485
486 # always set migrate speed (overwrite kvm default of 32m)
487 # we set a very hight default of 8192m which is basically unlimited
488 my $migrate_speed = $defaults->{migrate_speed} || 8192;
489 $migrate_speed = $conf->{migrate_speed} || $migrate_speed;
490 $migrate_speed = $migrate_speed * 1048576;
491 $self->log('info', "migrate_set_speed: $migrate_speed");
492 eval {
493 PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate_set_speed", value => int($migrate_speed));
494 };
495 $self->log('info', "migrate_set_speed error: $@") if $@;
496
497 my $migrate_downtime = $defaults->{migrate_downtime};
498 $migrate_downtime = $conf->{migrate_downtime} if defined($conf->{migrate_downtime});
499 if (defined($migrate_downtime)) {
500 $self->log('info', "migrate_set_downtime: $migrate_downtime");
501 eval {
502 PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate_set_downtime", value => int($migrate_downtime*100)/100);
503 };
504 $self->log('info', "migrate_set_downtime error: $@") if $@;
505 }
506
507 $self->log('info', "set migration_caps");
508 eval {
509 PVE::QemuServer::set_migration_caps($vmid);
510 };
511 warn $@ if $@;
512
513 #set cachesize 10% of the total memory
514 my $cachesize = int($conf->{memory}*1048576/10);
515 $self->log('info', "set cachesize: $cachesize");
516 eval {
517 PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate-set-cache-size", value => int($cachesize));
518 };
519 $self->log('info', "migrate-set-cache-size error: $@") if $@;
520
521 if (PVE::QemuServer::vga_conf_has_spice($conf->{vga})) {
522 my $rpcenv = PVE::RPCEnvironment::get();
523 my $authuser = $rpcenv->get_user();
524
525 my (undef, $proxyticket) = PVE::AccessControl::assemble_spice_ticket($authuser, $vmid, $self->{node});
526
527 my $filename = "/etc/pve/nodes/$self->{node}/pve-ssl.pem";
528 my $subject = PVE::AccessControl::read_x509_subject_spice($filename);
529
530 $self->log('info', "spice client_migrate_info");
531
532 eval {
533 PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "client_migrate_info", protocol => 'spice',
534 hostname => $proxyticket, 'tls-port' => $spice_port,
535 'cert-subject' => $subject);
536 };
537 $self->log('info', "client_migrate_info error: $@") if $@;
538
539 }
540
541 $self->log('info', "start migrate command to $ruri");
542 eval {
543 PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate", uri => $ruri);
544 };
545 my $merr = $@;
546 $self->log('info', "migrate uri => $ruri failed: $merr") if $merr;
547
548 my $lstat = 0;
549 my $usleep = 2000000;
550 my $i = 0;
551 my $err_count = 0;
552 my $lastrem = undef;
553 my $downtimecounter = 0;
554 while (1) {
555 $i++;
556 my $avglstat = $lstat/$i if $lstat;
557
558 usleep($usleep);
559 my $stat;
560 eval {
561 $stat = PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "query-migrate");
562 };
563 if (my $err = $@) {
564 $err_count++;
565 warn "query migrate failed: $err\n";
566 $self->log('info', "query migrate failed: $err");
567 if ($err_count <= 5) {
568 usleep(1000000);
569 next;
570 }
571 die "too many query migrate failures - aborting\n";
572 }
573
574 if (defined($stat->{status}) && $stat->{status} =~ m/^(setup)$/im) {
575 sleep(1);
576 next;
577 }
578
579 if (defined($stat->{status}) && $stat->{status} =~ m/^(active|completed|failed|cancelled)$/im) {
580 $merr = undef;
581 $err_count = 0;
582 if ($stat->{status} eq 'completed') {
583 my $delay = time() - $start;
584 if ($delay > 0) {
585 my $mbps = sprintf "%.2f", $conf->{memory}/$delay;
586 my $downtime = $stat->{downtime} || 0;
587 $self->log('info', "migration speed: $mbps MB/s - downtime $downtime ms");
588 }
589 }
590
591 if ($stat->{status} eq 'failed' || $stat->{status} eq 'cancelled') {
592 $self->log('info', "migration status error: $stat->{status}");
593 die "aborting\n"
594 }
595
596 if ($stat->{status} ne 'active') {
597 $self->log('info', "migration status: $stat->{status}");
598 last;
599 }
600
601 if ($stat->{ram}->{transferred} ne $lstat) {
602 my $trans = $stat->{ram}->{transferred} || 0;
603 my $rem = $stat->{ram}->{remaining} || 0;
604 my $total = $stat->{ram}->{total} || 0;
605 my $xbzrlecachesize = $stat->{"xbzrle-cache"}->{"cache-size"} || 0;
606 my $xbzrlebytes = $stat->{"xbzrle-cache"}->{"bytes"} || 0;
607 my $xbzrlepages = $stat->{"xbzrle-cache"}->{"pages"} || 0;
608 my $xbzrlecachemiss = $stat->{"xbzrle-cache"}->{"cache-miss"} || 0;
609 my $xbzrleoverflow = $stat->{"xbzrle-cache"}->{"overflow"} || 0;
610 #reduce sleep if remainig memory if lower than the everage transfert
611 $usleep = 300000 if $avglstat && $rem < $avglstat;
612
613 $self->log('info', "migration status: $stat->{status} (transferred ${trans}, " .
614 "remaining ${rem}), total ${total})");
615
616 if (${xbzrlecachesize}) {
617 $self->log('info', "migration xbzrle cachesize: ${xbzrlecachesize} transferred ${xbzrlebytes} pages ${xbzrlepages} cachemiss ${xbzrlecachemiss} overflow ${xbzrleoverflow}");
618 }
619
620 if (($lastrem && $rem > $lastrem ) || ($rem == 0)) {
621 $downtimecounter++;
622 }
623 $lastrem = $rem;
624
625 if ($downtimecounter > 5) {
626 $downtimecounter = 0;
627 $migrate_downtime *= 2;
628 $self->log('info', "migrate_set_downtime: $migrate_downtime");
629 eval {
630 PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate_set_downtime", value => int($migrate_downtime*100)/100);
631 };
632 $self->log('info', "migrate_set_downtime error: $@") if $@;
633 }
634
635 }
636
637
638 $lstat = $stat->{ram}->{transferred};
639
640 } else {
641 die $merr if $merr;
642 die "unable to parse migration status '$stat->{status}' - aborting\n";
643 }
644 }
645
646 # just to be sure that the tunnel gets closed on successful migration, on error
647 # phase2_cleanup closes it *after* stopping the remote waiting VM
648 if (!$self->{errors} && $self->{tunnel}) {
649 eval { finish_tunnel($self, $self->{tunnel}); };
650 if (my $err = $@) {
651 $self->log('err', $err);
652 $self->{errors} = 1;
653 }
654 }
655 }
656
657 sub phase2_cleanup {
658 my ($self, $vmid, $err) = @_;
659
660 return if !$self->{errors};
661 $self->{phase2errors} = 1;
662
663 $self->log('info', "aborting phase 2 - cleanup resources");
664
665 $self->log('info', "migrate_cancel");
666 eval {
667 PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate_cancel");
668 };
669 $self->log('info', "migrate_cancel error: $@") if $@;
670
671 my $conf = $self->{vmconf};
672 delete $conf->{lock};
673 eval { PVE::QemuConfig->write_config($vmid, $conf) };
674 if (my $err = $@) {
675 $self->log('err', $err);
676 }
677
678 # cleanup ressources on target host
679 my $nodename = PVE::INotify::nodename();
680
681 my $cmd = [@{$self->{rem_ssh}}, 'qm', 'stop', $vmid, '--skiplock', '--migratedfrom', $nodename];
682 eval{ PVE::Tools::run_command($cmd, outfunc => sub {}, errfunc => sub {}) };
683 if (my $err = $@) {
684 $self->log('err', $err);
685 $self->{errors} = 1;
686 }
687
688 if ($self->{tunnel}) {
689 eval { finish_tunnel($self, $self->{tunnel}); };
690 if (my $err = $@) {
691 $self->log('err', $err);
692 $self->{errors} = 1;
693 }
694 }
695 }
696
697 sub phase3 {
698 my ($self, $vmid) = @_;
699
700 my $volids = $self->{volumes};
701 return if $self->{phase2errors};
702
703 # destroy local copies
704 foreach my $volid (@$volids) {
705 eval { PVE::Storage::vdisk_free($self->{storecfg}, $volid); };
706 if (my $err = $@) {
707 $self->log('err', "removing local copy of '$volid' failed - $err");
708 $self->{errors} = 1;
709 last if $err =~ /^interrupted by signal$/;
710 }
711 }
712 }
713
714 sub phase3_cleanup {
715 my ($self, $vmid, $err) = @_;
716
717 my $conf = $self->{vmconf};
718 return if $self->{phase2errors};
719
720 # move config to remote node
721 my $conffile = PVE::QemuConfig->config_file($vmid);
722 my $newconffile = PVE::QemuConfig->config_file($vmid, $self->{node});
723
724 die "Failed to move config to node '$self->{node}' - rename failed: $!\n"
725 if !rename($conffile, $newconffile);
726
727 if ($self->{livemigration}) {
728 # now that config file is move, we can resume vm on target if livemigrate
729 my $cmd = [@{$self->{rem_ssh}}, 'qm', 'resume', $vmid, '--skiplock', '--nocheck'];
730 eval{ PVE::Tools::run_command($cmd, outfunc => sub {},
731 errfunc => sub {
732 my $line = shift;
733 $self->log('err', $line);
734 });
735 };
736 if (my $err = $@) {
737 $self->log('err', $err);
738 $self->{errors} = 1;
739 }
740 }
741
742 eval {
743
744 my $timer = 0;
745 if (PVE::QemuServer::vga_conf_has_spice($conf->{vga}) && $self->{running}) {
746 $self->log('info', "Waiting for spice server migration");
747 while (1) {
748 my $res = PVE::QemuServer::vm_mon_cmd_nocheck($vmid, 'query-spice');
749 last if int($res->{'migrated'}) == 1;
750 last if $timer > 50;
751 $timer ++;
752 usleep(200000);
753 }
754 }
755 };
756
757 # always stop local VM
758 eval { PVE::QemuServer::vm_stop($self->{storecfg}, $vmid, 1, 1); };
759 if (my $err = $@) {
760 $self->log('err', "stopping vm failed - $err");
761 $self->{errors} = 1;
762 }
763
764 # always deactivate volumes - avoid lvm LVs to be active on several nodes
765 eval {
766 my $vollist = PVE::QemuServer::get_vm_volumes($conf);
767 PVE::Storage::deactivate_volumes($self->{storecfg}, $vollist);
768 };
769 if (my $err = $@) {
770 $self->log('err', $err);
771 $self->{errors} = 1;
772 }
773
774 # clear migrate lock
775 my $cmd = [ @{$self->{rem_ssh}}, 'qm', 'unlock', $vmid ];
776 $self->cmd_logerr($cmd, errmsg => "failed to clear migrate lock");
777 }
778
779 sub final_cleanup {
780 my ($self, $vmid) = @_;
781
782 # nothing to do
783 }
784
785 1;