]> git.proxmox.com Git - qemu-server.git/blob - PVE/QemuMigrate.pm
collect and log origin of found local volumes
[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 "can't 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
229 my $sharedvm = 1;
230
231 my @sids = PVE::Storage::storage_ids($self->{storecfg});
232 foreach my $storeid (@sids) {
233 my $scfg = PVE::Storage::storage_config($self->{storecfg}, $storeid);
234 next if $scfg->{shared};
235 next if !PVE::Storage::storage_check_enabled($self->{storecfg}, $storeid, undef, 1);
236
237 # get list from PVE::Storage (for unused volumes)
238 my $dl = PVE::Storage::vdisk_list($self->{storecfg}, $storeid, $vmid);
239
240 next if @{$dl->{$storeid}} == 0;
241
242 # check if storage is available on target node
243 PVE::Storage::storage_check_node($self->{storecfg}, $storeid, $self->{node});
244 $sharedvm = 0; # there is a non-shared disk
245
246 PVE::Storage::foreach_volid($dl, sub {
247 my ($volid, $sid, $volname) = @_;
248
249 $volhash->{$volid} = 'storage';
250 });
251 }
252
253 my $test_volid = sub {
254 my ($volid, $is_cdrom, $snapname) = @_;
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 "can't migrate local cdrom drive\n" if $volid eq 'cdrom';
262 return if $volid eq 'none';
263 }
264
265 my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
266
267 # check if storage is available on both nodes
268 my $scfg = PVE::Storage::storage_check_node($self->{storecfg}, $sid);
269 PVE::Storage::storage_check_node($self->{storecfg}, $sid, $self->{node});
270
271 return if $scfg->{shared};
272
273 $sharedvm = 0;
274
275 $volhash->{$volid} = defined($snapname) ? 'snapshot' : 'config';
276
277 die "can't migrate local cdrom '$volid'\n" if $is_cdrom;
278
279 my ($path, $owner) = PVE::Storage::path($self->{storecfg}, $volid);
280
281 die "can't migrate volume '$volid' - owned by other VM (owner = VM $owner)\n"
282 if !$owner || ($owner != $self->{vmid});
283
284 if (defined($snapname)) {
285 # we cannot migrate shapshots on local storage
286 # exceptions: 'zfspool' or 'qcow2' files (on directory storage)
287
288 my $format = PVE::QemuServer::qemu_img_format($scfg, $volname);
289
290 if (($scfg->{type} eq 'zfspool') || ($format eq 'qcow2')) {
291 return;
292 }
293
294 die "can't migrate snapshot of local volume '$volid'\n";
295
296 }
297 };
298
299 my $test_drive = sub {
300 my ($ds, $drive, $snapname) = @_;
301
302 &$test_volid($drive->{file}, PVE::QemuServer::drive_is_cdrom($drive), $snapname);
303 };
304
305 PVE::QemuServer::foreach_drive($conf, $test_drive);
306 foreach my $snapname (keys %{$conf->{snapshots}}) {
307 &$test_volid($conf->{snapshots}->{$snapname}->{'vmstate'}, 0, undef)
308 if defined($conf->{snapshots}->{$snapname}->{'vmstate'});
309 PVE::QemuServer::foreach_drive($conf->{snapshots}->{$snapname}, $test_drive, $snapname);
310 }
311
312 foreach my $vol (sort keys %$volhash) {
313 if ($volhash->{$vol} eq 'storage') {
314 $self->log('info', "found local disk '$vol' (via storage)\n");
315 } elsif ($volhash->{$vol} eq 'config') {
316 $self->log('info', "found local disk '$vol' (in current VM config)\n");
317 } elsif ($volhash->{$vol} eq 'snapshot') {
318 $self->log('info', "found local disk '$vol' (referenced by snapshot(s))\n");
319 } else {
320 $self->log('info', "found local disk '$vol'\n");
321 }
322 }
323
324 if ($self->{running} && !$sharedvm) {
325 die "can't do online migration - VM uses local disks\n";
326 }
327
328 # additional checks for local storage
329 foreach my $volid (keys %$volhash) {
330 my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
331 my $scfg = PVE::Storage::storage_config($self->{storecfg}, $sid);
332
333 my $migratable = ($scfg->{type} eq 'dir') || ($scfg->{type} eq 'zfspool') ||
334 ($scfg->{type} eq 'lvmthin') || ($scfg->{type} eq 'lvm');
335
336 die "can't migrate '$volid' - storage type '$scfg->{type}' not supported\n"
337 if !$migratable;
338
339 # image is a linked clone on local storage, se we can't migrate.
340 if (my $basename = (PVE::Storage::parse_volname($self->{storecfg}, $volid))[3]) {
341 die "can't migrate '$volid' as it's a clone of '$basename'";
342 }
343 }
344
345 foreach my $volid (keys %$volhash) {
346 my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
347 push @{$self->{volumes}}, $volid;
348 PVE::Storage::storage_migrate($self->{storecfg}, $volid, $self->{nodeip}, $sid);
349 }
350 };
351 die "Failed to sync data - $@" if $@;
352 }
353
354 sub phase1 {
355 my ($self, $vmid) = @_;
356
357 $self->log('info', "starting migration of VM $vmid to node '$self->{node}' ($self->{nodeip})");
358
359 my $conf = $self->{vmconf};
360
361 # set migrate lock in config file
362 $conf->{lock} = 'migrate';
363 PVE::QemuConfig->write_config($vmid, $conf);
364
365 sync_disks($self, $vmid);
366
367 };
368
369 sub phase1_cleanup {
370 my ($self, $vmid, $err) = @_;
371
372 $self->log('info', "aborting phase 1 - cleanup resources");
373
374 my $conf = $self->{vmconf};
375 delete $conf->{lock};
376 eval { PVE::QemuConfig->write_config($vmid, $conf) };
377 if (my $err = $@) {
378 $self->log('err', $err);
379 }
380
381 if ($self->{volumes}) {
382 foreach my $volid (@{$self->{volumes}}) {
383 $self->log('err', "found stale volume copy '$volid' on node '$self->{node}'");
384 # fixme: try to remove ?
385 }
386 }
387 }
388
389 sub phase2 {
390 my ($self, $vmid) = @_;
391
392 my $conf = $self->{vmconf};
393
394 $self->log('info', "starting VM $vmid on remote node '$self->{node}'");
395
396 my $raddr;
397 my $rport;
398 my $ruri; # the whole migration dst. URI (protocol:address[:port])
399 my $nodename = PVE::INotify::nodename();
400
401 ## start on remote node
402 my $cmd = [@{$self->{rem_ssh}}];
403
404 my $spice_ticket;
405 if (PVE::QemuServer::vga_conf_has_spice($conf->{vga})) {
406 my $res = PVE::QemuServer::vm_mon_cmd($vmid, 'query-spice');
407 $spice_ticket = $res->{ticket};
408 }
409
410 push @$cmd , 'qm', 'start', $vmid, '--skiplock', '--migratedfrom', $nodename;
411
412 # we use TCP only for unsecure migrations as TCP ssh forward tunnels often
413 # did appeared to late (they are hard, if not impossible, to check for)
414 # secure migration use UNIX sockets now, this *breaks* compatibilty when trying
415 # to migrate from new to old but *not* from old to new.
416 my $datacenterconf = PVE::Cluster::cfs_read_file('datacenter.cfg');
417 my $secure_migration = ($datacenterconf->{migration_unsecure}) ? 0 : 1;
418
419 if (!$secure_migration) {
420 push @$cmd, '--stateuri', 'tcp';
421 } else {
422 push @$cmd, '--stateuri', 'unix';
423 }
424
425 if ($self->{forcemachine}) {
426 push @$cmd, '--machine', $self->{forcemachine};
427 }
428
429 my $spice_port;
430
431 # Note: We try to keep $spice_ticket secret (do not pass via command line parameter)
432 # instead we pipe it through STDIN
433 PVE::Tools::run_command($cmd, input => $spice_ticket, outfunc => sub {
434 my $line = shift;
435
436 if ($line =~ m/^migration listens on tcp:(localhost|[\d\.]+|\[[\d\.:a-fA-F]+\]):(\d+)$/) {
437 $raddr = $1;
438 $rport = int($2);
439 $ruri = "tcp:$raddr:$rport";
440 }
441 elsif ($line =~ m!^migration listens on unix:(/run/qemu-server/(\d+)\.migrate)$!) {
442 $raddr = $1;
443 die "Destination UNIX sockets VMID does not match source VMID" if $vmid ne $2;
444 $ruri = "unix:$raddr";
445 }
446 elsif ($line =~ m/^migration listens on port (\d+)$/) {
447 $raddr = "localhost";
448 $rport = int($1);
449 $ruri = "tcp:$raddr:$rport";
450 }
451 elsif ($line =~ m/^spice listens on port (\d+)$/) {
452 $spice_port = int($1);
453 }
454 }, errfunc => sub {
455 my $line = shift;
456 $self->log('info', $line);
457 });
458
459 die "unable to detect remote migration address\n" if !$raddr;
460
461 if ($secure_migration) {
462 $self->log('info', "start remote tunnel");
463
464 if ($ruri =~ /^unix:/) {
465 unlink $raddr;
466 $self->{tunnel} = $self->fork_tunnel("$raddr:$raddr");
467 $self->{tunnel}->{sock_addr} = $raddr;
468
469 my $unix_socket_try = 0; # wait for the socket to become ready
470 while (! -S $raddr) {
471 $unix_socket_try++;
472 if ($unix_socket_try > 100) {
473 $self->{errors} = 1;
474 $self->finish_tunnel($self->{tunnel});
475 die "Timeout, migration socket $ruri did not get ready";
476 }
477
478 usleep(50000);
479 }
480
481 } elsif ($ruri =~ /^tcp:/) {
482 my $tunnel_addr;
483 if ($raddr eq "localhost") {
484 # for backwards compatibility with older qemu-server versions
485 my $pfamily = PVE::Tools::get_host_address_family($nodename);
486 my $lport = PVE::Tools::next_migrate_port($pfamily);
487 $tunnel_addr = "$lport:localhost:$rport";
488 }
489
490 $self->{tunnel} = $self->fork_tunnel($tunnel_addr);
491
492 } else {
493 die "unsupported protocol in migration URI: $ruri\n";
494 }
495 }
496
497 my $start = time();
498 $self->log('info', "starting online/live migration on $ruri");
499 $self->{livemigration} = 1;
500
501 # load_defaults
502 my $defaults = PVE::QemuServer::load_defaults();
503
504 # always set migrate speed (overwrite kvm default of 32m)
505 # we set a very hight default of 8192m which is basically unlimited
506 my $migrate_speed = $defaults->{migrate_speed} || 8192;
507 $migrate_speed = $conf->{migrate_speed} || $migrate_speed;
508 $migrate_speed = $migrate_speed * 1048576;
509 $self->log('info', "migrate_set_speed: $migrate_speed");
510 eval {
511 PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate_set_speed", value => int($migrate_speed));
512 };
513 $self->log('info', "migrate_set_speed error: $@") if $@;
514
515 my $migrate_downtime = $defaults->{migrate_downtime};
516 $migrate_downtime = $conf->{migrate_downtime} if defined($conf->{migrate_downtime});
517 if (defined($migrate_downtime)) {
518 $self->log('info', "migrate_set_downtime: $migrate_downtime");
519 eval {
520 PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate_set_downtime", value => int($migrate_downtime*100)/100);
521 };
522 $self->log('info', "migrate_set_downtime error: $@") if $@;
523 }
524
525 $self->log('info', "set migration_caps");
526 eval {
527 PVE::QemuServer::set_migration_caps($vmid);
528 };
529 warn $@ if $@;
530
531 #set cachesize 10% of the total memory
532 my $cachesize = int($conf->{memory}*1048576/10);
533 $self->log('info', "set cachesize: $cachesize");
534 eval {
535 PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate-set-cache-size", value => int($cachesize));
536 };
537 $self->log('info', "migrate-set-cache-size error: $@") if $@;
538
539 if (PVE::QemuServer::vga_conf_has_spice($conf->{vga})) {
540 my $rpcenv = PVE::RPCEnvironment::get();
541 my $authuser = $rpcenv->get_user();
542
543 my (undef, $proxyticket) = PVE::AccessControl::assemble_spice_ticket($authuser, $vmid, $self->{node});
544
545 my $filename = "/etc/pve/nodes/$self->{node}/pve-ssl.pem";
546 my $subject = PVE::AccessControl::read_x509_subject_spice($filename);
547
548 $self->log('info', "spice client_migrate_info");
549
550 eval {
551 PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "client_migrate_info", protocol => 'spice',
552 hostname => $proxyticket, 'tls-port' => $spice_port,
553 'cert-subject' => $subject);
554 };
555 $self->log('info', "client_migrate_info error: $@") if $@;
556
557 }
558
559 $self->log('info', "start migrate command to $ruri");
560 eval {
561 PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate", uri => $ruri);
562 };
563 my $merr = $@;
564 $self->log('info', "migrate uri => $ruri failed: $merr") if $merr;
565
566 my $lstat = 0;
567 my $usleep = 2000000;
568 my $i = 0;
569 my $err_count = 0;
570 my $lastrem = undef;
571 my $downtimecounter = 0;
572 while (1) {
573 $i++;
574 my $avglstat = $lstat/$i if $lstat;
575
576 usleep($usleep);
577 my $stat;
578 eval {
579 $stat = PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "query-migrate");
580 };
581 if (my $err = $@) {
582 $err_count++;
583 warn "query migrate failed: $err\n";
584 $self->log('info', "query migrate failed: $err");
585 if ($err_count <= 5) {
586 usleep(1000000);
587 next;
588 }
589 die "too many query migrate failures - aborting\n";
590 }
591
592 if (defined($stat->{status}) && $stat->{status} =~ m/^(setup)$/im) {
593 sleep(1);
594 next;
595 }
596
597 if (defined($stat->{status}) && $stat->{status} =~ m/^(active|completed|failed|cancelled)$/im) {
598 $merr = undef;
599 $err_count = 0;
600 if ($stat->{status} eq 'completed') {
601 my $delay = time() - $start;
602 if ($delay > 0) {
603 my $mbps = sprintf "%.2f", $conf->{memory}/$delay;
604 my $downtime = $stat->{downtime} || 0;
605 $self->log('info', "migration speed: $mbps MB/s - downtime $downtime ms");
606 }
607 }
608
609 if ($stat->{status} eq 'failed' || $stat->{status} eq 'cancelled') {
610 $self->log('info', "migration status error: $stat->{status}");
611 die "aborting\n"
612 }
613
614 if ($stat->{status} ne 'active') {
615 $self->log('info', "migration status: $stat->{status}");
616 last;
617 }
618
619 if ($stat->{ram}->{transferred} ne $lstat) {
620 my $trans = $stat->{ram}->{transferred} || 0;
621 my $rem = $stat->{ram}->{remaining} || 0;
622 my $total = $stat->{ram}->{total} || 0;
623 my $xbzrlecachesize = $stat->{"xbzrle-cache"}->{"cache-size"} || 0;
624 my $xbzrlebytes = $stat->{"xbzrle-cache"}->{"bytes"} || 0;
625 my $xbzrlepages = $stat->{"xbzrle-cache"}->{"pages"} || 0;
626 my $xbzrlecachemiss = $stat->{"xbzrle-cache"}->{"cache-miss"} || 0;
627 my $xbzrleoverflow = $stat->{"xbzrle-cache"}->{"overflow"} || 0;
628 #reduce sleep if remainig memory if lower than the everage transfert
629 $usleep = 300000 if $avglstat && $rem < $avglstat;
630
631 $self->log('info', "migration status: $stat->{status} (transferred ${trans}, " .
632 "remaining ${rem}), total ${total})");
633
634 if (${xbzrlecachesize}) {
635 $self->log('info', "migration xbzrle cachesize: ${xbzrlecachesize} transferred ${xbzrlebytes} pages ${xbzrlepages} cachemiss ${xbzrlecachemiss} overflow ${xbzrleoverflow}");
636 }
637
638 if (($lastrem && $rem > $lastrem ) || ($rem == 0)) {
639 $downtimecounter++;
640 }
641 $lastrem = $rem;
642
643 if ($downtimecounter > 5) {
644 $downtimecounter = 0;
645 $migrate_downtime *= 2;
646 $self->log('info', "migrate_set_downtime: $migrate_downtime");
647 eval {
648 PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate_set_downtime", value => int($migrate_downtime*100)/100);
649 };
650 $self->log('info', "migrate_set_downtime error: $@") if $@;
651 }
652
653 }
654
655
656 $lstat = $stat->{ram}->{transferred};
657
658 } else {
659 die $merr if $merr;
660 die "unable to parse migration status '$stat->{status}' - aborting\n";
661 }
662 }
663
664 # just to be sure that the tunnel gets closed on successful migration, on error
665 # phase2_cleanup closes it *after* stopping the remote waiting VM
666 if (!$self->{errors} && $self->{tunnel}) {
667 eval { finish_tunnel($self, $self->{tunnel}); };
668 if (my $err = $@) {
669 $self->log('err', $err);
670 $self->{errors} = 1;
671 }
672 }
673 }
674
675 sub phase2_cleanup {
676 my ($self, $vmid, $err) = @_;
677
678 return if !$self->{errors};
679 $self->{phase2errors} = 1;
680
681 $self->log('info', "aborting phase 2 - cleanup resources");
682
683 $self->log('info', "migrate_cancel");
684 eval {
685 PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate_cancel");
686 };
687 $self->log('info', "migrate_cancel error: $@") if $@;
688
689 my $conf = $self->{vmconf};
690 delete $conf->{lock};
691 eval { PVE::QemuConfig->write_config($vmid, $conf) };
692 if (my $err = $@) {
693 $self->log('err', $err);
694 }
695
696 # cleanup ressources on target host
697 my $nodename = PVE::INotify::nodename();
698
699 my $cmd = [@{$self->{rem_ssh}}, 'qm', 'stop', $vmid, '--skiplock', '--migratedfrom', $nodename];
700 eval{ PVE::Tools::run_command($cmd, outfunc => sub {}, errfunc => sub {}) };
701 if (my $err = $@) {
702 $self->log('err', $err);
703 $self->{errors} = 1;
704 }
705
706 if ($self->{tunnel}) {
707 eval { finish_tunnel($self, $self->{tunnel}); };
708 if (my $err = $@) {
709 $self->log('err', $err);
710 $self->{errors} = 1;
711 }
712 }
713 }
714
715 sub phase3 {
716 my ($self, $vmid) = @_;
717
718 my $volids = $self->{volumes};
719 return if $self->{phase2errors};
720
721 # destroy local copies
722 foreach my $volid (@$volids) {
723 eval { PVE::Storage::vdisk_free($self->{storecfg}, $volid); };
724 if (my $err = $@) {
725 $self->log('err', "removing local copy of '$volid' failed - $err");
726 $self->{errors} = 1;
727 last if $err =~ /^interrupted by signal$/;
728 }
729 }
730 }
731
732 sub phase3_cleanup {
733 my ($self, $vmid, $err) = @_;
734
735 my $conf = $self->{vmconf};
736 return if $self->{phase2errors};
737
738 # move config to remote node
739 my $conffile = PVE::QemuConfig->config_file($vmid);
740 my $newconffile = PVE::QemuConfig->config_file($vmid, $self->{node});
741
742 die "Failed to move config to node '$self->{node}' - rename failed: $!\n"
743 if !rename($conffile, $newconffile);
744
745 if ($self->{livemigration}) {
746 # now that config file is move, we can resume vm on target if livemigrate
747 my $cmd = [@{$self->{rem_ssh}}, 'qm', 'resume', $vmid, '--skiplock', '--nocheck'];
748 eval{ PVE::Tools::run_command($cmd, outfunc => sub {},
749 errfunc => sub {
750 my $line = shift;
751 $self->log('err', $line);
752 });
753 };
754 if (my $err = $@) {
755 $self->log('err', $err);
756 $self->{errors} = 1;
757 }
758 }
759
760 eval {
761
762 my $timer = 0;
763 if (PVE::QemuServer::vga_conf_has_spice($conf->{vga}) && $self->{running}) {
764 $self->log('info', "Waiting for spice server migration");
765 while (1) {
766 my $res = PVE::QemuServer::vm_mon_cmd_nocheck($vmid, 'query-spice');
767 last if int($res->{'migrated'}) == 1;
768 last if $timer > 50;
769 $timer ++;
770 usleep(200000);
771 }
772 }
773 };
774
775 # always stop local VM
776 eval { PVE::QemuServer::vm_stop($self->{storecfg}, $vmid, 1, 1); };
777 if (my $err = $@) {
778 $self->log('err', "stopping vm failed - $err");
779 $self->{errors} = 1;
780 }
781
782 # always deactivate volumes - avoid lvm LVs to be active on several nodes
783 eval {
784 my $vollist = PVE::QemuServer::get_vm_volumes($conf);
785 PVE::Storage::deactivate_volumes($self->{storecfg}, $vollist);
786 };
787 if (my $err = $@) {
788 $self->log('err', $err);
789 $self->{errors} = 1;
790 }
791
792 # clear migrate lock
793 my $cmd = [ @{$self->{rem_ssh}}, 'qm', 'unlock', $vmid ];
794 $self->cmd_logerr($cmd, errmsg => "failed to clear migrate lock");
795 }
796
797 sub final_cleanup {
798 my ($self, $vmid) = @_;
799
800 # nothing to do
801 }
802
803 1;