]>
Commit | Line | Data |
---|---|---|
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 PVE::INotify; | |
9 | use PVE::Tools; | |
10 | use PVE::Cluster; | |
11 | use PVE::Storage; | |
12 | use PVE::QemuServer; | |
13 | use Time::HiRes qw( usleep ); | |
14 | use PVE::RPCEnvironment; | |
15 | ||
16 | use base qw(PVE::AbstractMigrate); | |
17 | ||
18 | sub fork_command_pipe { | |
19 | my ($self, $cmd) = @_; | |
20 | ||
21 | my $reader = IO::File->new(); | |
22 | my $writer = IO::File->new(); | |
23 | ||
24 | my $orig_pid = $$; | |
25 | ||
26 | my $cpid; | |
27 | ||
28 | eval { $cpid = open2($reader, $writer, @$cmd); }; | |
29 | ||
30 | my $err = $@; | |
31 | ||
32 | # catch exec errors | |
33 | if ($orig_pid != $$) { | |
34 | $self->log('err', "can't fork command pipe\n"); | |
35 | POSIX::_exit(1); | |
36 | kill('KILL', $$); | |
37 | } | |
38 | ||
39 | die $err if $err; | |
40 | ||
41 | return { writer => $writer, reader => $reader, pid => $cpid }; | |
42 | } | |
43 | ||
44 | sub finish_command_pipe { | |
45 | my ($self, $cmdpipe, $timeout) = @_; | |
46 | ||
47 | my $writer = $cmdpipe->{writer}; | |
48 | my $reader = $cmdpipe->{reader}; | |
49 | ||
50 | $writer->close(); | |
51 | $reader->close(); | |
52 | ||
53 | my $cpid = $cmdpipe->{pid}; | |
54 | ||
55 | if ($timeout) { | |
56 | for (my $i = 0; $i < $timeout; $i++) { | |
57 | return if !PVE::ProcFSTools::check_process_running($cpid); | |
58 | sleep(1); | |
59 | } | |
60 | } | |
61 | ||
62 | $self->log('info', "ssh tunnel still running - terminating now with SIGTERM\n"); | |
63 | kill(15, $cpid); | |
64 | ||
65 | # wait again | |
66 | for (my $i = 0; $i < 10; $i++) { | |
67 | return if !PVE::ProcFSTools::check_process_running($cpid); | |
68 | sleep(1); | |
69 | } | |
70 | ||
71 | $self->log('info', "ssh tunnel still running - terminating now with SIGKILL\n"); | |
72 | kill 9, $cpid; | |
73 | sleep 1; | |
74 | } | |
75 | ||
76 | sub fork_tunnel { | |
77 | my ($self, $nodeip, $lport, $rport) = @_; | |
78 | ||
79 | my @localtunnelinfo = $lport ? ('-L' , "$lport:localhost:$rport" ) : (); | |
80 | ||
81 | my $cmd = [@{$self->{rem_ssh}}, @localtunnelinfo, 'qm', 'mtunnel' ]; | |
82 | ||
83 | my $tunnel = $self->fork_command_pipe($cmd); | |
84 | ||
85 | my $reader = $tunnel->{reader}; | |
86 | ||
87 | my $helo; | |
88 | eval { | |
89 | PVE::Tools::run_with_timeout(60, sub { $helo = <$reader>; }); | |
90 | die "no reply\n" if !$helo; | |
91 | die "no quorum on target node\n" if $helo =~ m/^no quorum$/; | |
92 | die "got strange reply from mtunnel ('$helo')\n" | |
93 | if $helo !~ m/^tunnel online$/; | |
94 | }; | |
95 | my $err = $@; | |
96 | ||
97 | if ($err) { | |
98 | $self->finish_command_pipe($tunnel); | |
99 | die "can't open migration tunnel - $err"; | |
100 | } | |
101 | return $tunnel; | |
102 | } | |
103 | ||
104 | sub finish_tunnel { | |
105 | my ($self, $tunnel) = @_; | |
106 | ||
107 | my $writer = $tunnel->{writer}; | |
108 | ||
109 | eval { | |
110 | PVE::Tools::run_with_timeout(30, sub { | |
111 | print $writer "quit\n"; | |
112 | $writer->flush(); | |
113 | }); | |
114 | }; | |
115 | my $err = $@; | |
116 | ||
117 | $self->finish_command_pipe($tunnel, 30); | |
118 | ||
119 | die $err if $err; | |
120 | } | |
121 | ||
122 | sub lock_vm { | |
123 | my ($self, $vmid, $code, @param) = @_; | |
124 | ||
125 | return PVE::QemuServer::lock_config($vmid, $code, @param); | |
126 | } | |
127 | ||
128 | sub prepare { | |
129 | my ($self, $vmid) = @_; | |
130 | ||
131 | my $online = $self->{opts}->{online}; | |
132 | ||
133 | $self->{storecfg} = PVE::Storage::config(); | |
134 | ||
135 | # test is VM exist | |
136 | my $conf = $self->{vmconf} = PVE::QemuServer::load_config($vmid); | |
137 | ||
138 | PVE::QemuServer::check_lock($conf); | |
139 | ||
140 | my $running = 0; | |
141 | if (my $pid = PVE::QemuServer::check_running($vmid)) { | |
142 | die "cant migrate running VM without --online\n" if !$online; | |
143 | $running = $pid; | |
144 | $self->{forcemachine} = PVE::QemuServer::get_current_qemu_machine($vmid); | |
145 | } | |
146 | ||
147 | if (my $loc_res = PVE::QemuServer::check_local_resources($conf, 1)) { | |
148 | if ($self->{running} || !$self->{opts}->{force}) { | |
149 | die "can't migrate VM which uses local devices\n"; | |
150 | } else { | |
151 | $self->log('info', "migrating VM which uses local devices"); | |
152 | } | |
153 | } | |
154 | ||
155 | # activate volumes | |
156 | my $vollist = PVE::QemuServer::get_vm_volumes($conf); | |
157 | PVE::Storage::activate_volumes($self->{storecfg}, $vollist); | |
158 | ||
159 | # fixme: check if storage is available on both nodes | |
160 | ||
161 | # test ssh connection | |
162 | my $cmd = [ @{$self->{rem_ssh}}, '/bin/true' ]; | |
163 | eval { $self->cmd_quiet($cmd); }; | |
164 | die "Can't connect to destination address using public key\n" if $@; | |
165 | ||
166 | return $running; | |
167 | } | |
168 | ||
169 | sub sync_disks { | |
170 | my ($self, $vmid) = @_; | |
171 | ||
172 | $self->log('info', "copying disk images"); | |
173 | ||
174 | my $conf = $self->{vmconf}; | |
175 | ||
176 | $self->{volumes} = []; | |
177 | ||
178 | my $res = []; | |
179 | ||
180 | eval { | |
181 | ||
182 | my $volhash = {}; | |
183 | my $cdromhash = {}; | |
184 | ||
185 | my $sharedvm = 1; | |
186 | ||
187 | my @sids = PVE::Storage::storage_ids($self->{storecfg}); | |
188 | foreach my $storeid (@sids) { | |
189 | my $scfg = PVE::Storage::storage_config($self->{storecfg}, $storeid); | |
190 | next if $scfg->{shared}; | |
191 | next if !PVE::Storage::storage_check_enabled($self->{storecfg}, $storeid, undef, 1); | |
192 | ||
193 | # get list from PVE::Storage (for unused volumes) | |
194 | my $dl = PVE::Storage::vdisk_list($self->{storecfg}, $storeid, $vmid); | |
195 | PVE::Storage::foreach_volid($dl, sub { | |
196 | my ($volid, $sid, $volname) = @_; | |
197 | ||
198 | # check if storage is available on target node | |
199 | PVE::Storage::storage_check_node($self->{storecfg}, $sid, $self->{node}); | |
200 | ||
201 | $volhash->{$volid} = 1; | |
202 | $sharedvm = 0; # there is a non-shared disk | |
203 | }); | |
204 | } | |
205 | ||
206 | # and add used, owned/non-shared disks (just to be sure we have all) | |
207 | ||
208 | PVE::QemuServer::foreach_volid($conf, sub { | |
209 | my ($volid, $is_cdrom) = @_; | |
210 | ||
211 | return if !$volid; | |
212 | ||
213 | die "cant migrate local file/device '$volid'\n" if $volid =~ m|^/|; | |
214 | ||
215 | if ($is_cdrom) { | |
216 | die "cant migrate local cdrom drive\n" if $volid eq 'cdrom'; | |
217 | return if $volid eq 'none'; | |
218 | $cdromhash->{$volid} = 1; | |
219 | } | |
220 | ||
221 | my ($sid, $volname) = PVE::Storage::parse_volume_id($volid); | |
222 | ||
223 | # check if storage is available on both nodes | |
224 | my $scfg = PVE::Storage::storage_check_node($self->{storecfg}, $sid); | |
225 | PVE::Storage::storage_check_node($self->{storecfg}, $sid, $self->{node}); | |
226 | ||
227 | return if $scfg->{shared}; | |
228 | ||
229 | die "can't migrate local cdrom '$volid'\n" if $cdromhash->{$volid}; | |
230 | ||
231 | $sharedvm = 0; | |
232 | ||
233 | my ($path, $owner) = PVE::Storage::path($self->{storecfg}, $volid); | |
234 | ||
235 | die "can't migrate volume '$volid' - owned by other VM (owner = VM $owner)\n" | |
236 | if !$owner || ($owner != $self->{vmid}); | |
237 | ||
238 | $volhash->{$volid} = 1; | |
239 | }); | |
240 | ||
241 | if ($self->{running} && !$sharedvm) { | |
242 | die "can't do online migration - VM uses local disks\n"; | |
243 | } | |
244 | ||
245 | # do some checks first | |
246 | foreach my $volid (keys %$volhash) { | |
247 | my ($sid, $volname) = PVE::Storage::parse_volume_id($volid); | |
248 | my $scfg = PVE::Storage::storage_config($self->{storecfg}, $sid); | |
249 | ||
250 | die "can't migrate '$volid' - storagy type '$scfg->{type}' not supported\n" | |
251 | if $scfg->{type} ne 'dir'; | |
252 | ||
253 | # if file, check if a backing file exist | |
254 | if (($scfg->{type} eq 'dir') && (!$sharedvm)) { | |
255 | my (undef, undef, undef, $parent) = PVE::Storage::volume_size_info($self->{storecfg}, $volid, 1); | |
256 | die "can't migrate '$volid' as it's a clone of '$parent'" if $parent; | |
257 | } | |
258 | } | |
259 | ||
260 | foreach my $volid (keys %$volhash) { | |
261 | my ($sid, $volname) = PVE::Storage::parse_volume_id($volid); | |
262 | push @{$self->{volumes}}, $volid; | |
263 | PVE::Storage::storage_migrate($self->{storecfg}, $volid, $self->{nodeip}, $sid); | |
264 | } | |
265 | }; | |
266 | die "Failed to sync data - $@" if $@; | |
267 | } | |
268 | ||
269 | sub phase1 { | |
270 | my ($self, $vmid) = @_; | |
271 | ||
272 | $self->log('info', "starting migration of VM $vmid to node '$self->{node}' ($self->{nodeip})"); | |
273 | ||
274 | my $conf = $self->{vmconf}; | |
275 | ||
276 | # set migrate lock in config file | |
277 | $conf->{lock} = 'migrate'; | |
278 | PVE::QemuServer::update_config_nolock($vmid, $conf, 1); | |
279 | ||
280 | sync_disks($self, $vmid); | |
281 | ||
282 | }; | |
283 | ||
284 | sub phase1_cleanup { | |
285 | my ($self, $vmid, $err) = @_; | |
286 | ||
287 | $self->log('info', "aborting phase 1 - cleanup resources"); | |
288 | ||
289 | my $conf = $self->{vmconf}; | |
290 | delete $conf->{lock}; | |
291 | eval { PVE::QemuServer::update_config_nolock($vmid, $conf, 1) }; | |
292 | if (my $err = $@) { | |
293 | $self->log('err', $err); | |
294 | } | |
295 | ||
296 | if ($self->{volumes}) { | |
297 | foreach my $volid (@{$self->{volumes}}) { | |
298 | $self->log('err', "found stale volume copy '$volid' on node '$self->{node}'"); | |
299 | # fixme: try to remove ? | |
300 | } | |
301 | } | |
302 | } | |
303 | ||
304 | sub phase2 { | |
305 | my ($self, $vmid) = @_; | |
306 | ||
307 | my $conf = $self->{vmconf}; | |
308 | ||
309 | $self->log('info', "starting VM $vmid on remote node '$self->{node}'"); | |
310 | ||
311 | my $raddr; | |
312 | my $rport; | |
313 | my $nodename = PVE::INotify::nodename(); | |
314 | ||
315 | ## start on remote node | |
316 | my $cmd = [@{$self->{rem_ssh}}]; | |
317 | ||
318 | my $spice_ticket; | |
319 | if (PVE::QemuServer::vga_conf_has_spice($conf->{vga})) { | |
320 | my $res = PVE::QemuServer::vm_mon_cmd($vmid, 'query-spice'); | |
321 | $spice_ticket = $res->{ticket}; | |
322 | } | |
323 | ||
324 | push @$cmd , 'qm', 'start', $vmid, '--stateuri', 'tcp', '--skiplock', '--migratedfrom', $nodename; | |
325 | ||
326 | if ($self->{forcemachine}) { | |
327 | push @$cmd, '--machine', $self->{forcemachine}; | |
328 | } | |
329 | ||
330 | my $spice_port; | |
331 | ||
332 | # Note: We try to keep $spice_ticket secret (do not pass via command line parameter) | |
333 | # instead we pipe it through STDIN | |
334 | PVE::Tools::run_command($cmd, input => $spice_ticket, outfunc => sub { | |
335 | my $line = shift; | |
336 | ||
337 | if ($line =~ m/^migration listens on tcp:([\d\.]+|localhost):(\d+)$/) { | |
338 | $raddr = $1; | |
339 | $rport = int($2); | |
340 | } | |
341 | elsif ($line =~ m/^migration listens on port (\d+)$/) { | |
342 | $raddr = "localhost"; | |
343 | $rport = int($1); | |
344 | } | |
345 | elsif ($line =~ m/^spice listens on port (\d+)$/) { | |
346 | $spice_port = int($1); | |
347 | } | |
348 | }, errfunc => sub { | |
349 | my $line = shift; | |
350 | $self->log('info', $line); | |
351 | }); | |
352 | ||
353 | die "unable to detect remote migration address\n" if !$raddr; | |
354 | ||
355 | ## create tunnel to remote port | |
356 | $self->log('info', "starting ssh migration tunnel"); | |
357 | my $lport = ($raddr eq "localhost") ? PVE::Tools::next_migrate_port() : undef; | |
358 | $self->{tunnel} = $self->fork_tunnel($self->{nodeip}, $lport, $rport); | |
359 | ||
360 | my $start = time(); | |
361 | $self->log('info', "starting online/live migration on $raddr:$rport"); | |
362 | $self->{livemigration} = 1; | |
363 | ||
364 | # load_defaults | |
365 | my $defaults = PVE::QemuServer::load_defaults(); | |
366 | ||
367 | # always set migrate speed (overwrite kvm default of 32m) | |
368 | # we set a very hight default of 8192m which is basically unlimited | |
369 | my $migrate_speed = $defaults->{migrate_speed} || 8192; | |
370 | $migrate_speed = $conf->{migrate_speed} || $migrate_speed; | |
371 | $migrate_speed = $migrate_speed * 1048576; | |
372 | $self->log('info', "migrate_set_speed: $migrate_speed"); | |
373 | eval { | |
374 | PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate_set_speed", value => int($migrate_speed)); | |
375 | }; | |
376 | $self->log('info', "migrate_set_speed error: $@") if $@; | |
377 | ||
378 | my $migrate_downtime = $defaults->{migrate_downtime}; | |
379 | $migrate_downtime = $conf->{migrate_downtime} if defined($conf->{migrate_downtime}); | |
380 | if (defined($migrate_downtime)) { | |
381 | $self->log('info', "migrate_set_downtime: $migrate_downtime"); | |
382 | eval { | |
383 | PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate_set_downtime", value => int($migrate_downtime*100)/100); | |
384 | }; | |
385 | $self->log('info', "migrate_set_downtime error: $@") if $@; | |
386 | } | |
387 | ||
388 | my $capabilities = {}; | |
389 | $capabilities->{capability} = "xbzrle"; | |
390 | $capabilities->{state} = JSON::false; | |
391 | ||
392 | eval { | |
393 | PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate-set-capabilities", capabilities => [$capabilities]); | |
394 | }; | |
395 | ||
396 | #set cachesize 10% of the total memory | |
397 | my $cachesize = int($conf->{memory}*1048576/10); | |
398 | eval { | |
399 | PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate-set-cache-size", value => $cachesize); | |
400 | }; | |
401 | ||
402 | if (PVE::QemuServer::vga_conf_has_spice($conf->{vga})) { | |
403 | my $rpcenv = PVE::RPCEnvironment::get(); | |
404 | my $authuser = $rpcenv->get_user(); | |
405 | ||
406 | my (undef, $proxyticket) = PVE::AccessControl::assemble_spice_ticket($authuser, $vmid, $self->{node}); | |
407 | ||
408 | my $filename = "/etc/pve/nodes/$self->{node}/pve-ssl.pem"; | |
409 | my $subject = PVE::QemuServer::read_x509_subject_spice($filename); | |
410 | ||
411 | $self->log('info', "spice client_migrate_info"); | |
412 | ||
413 | eval { | |
414 | PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "client_migrate_info", protocol => 'spice', | |
415 | hostname => $proxyticket, 'tls-port' => $spice_port, | |
416 | 'cert-subject' => $subject); | |
417 | }; | |
418 | $self->log('info', "client_migrate_info error: $@") if $@; | |
419 | ||
420 | } | |
421 | ||
422 | eval { | |
423 | PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate", uri => "tcp:$raddr:$rport"); | |
424 | }; | |
425 | my $merr = $@; | |
426 | $self->log('info', "migrate uri => tcp:$raddr:$rport failed: $merr") if $merr; | |
427 | ||
428 | my $lstat = 0; | |
429 | my $usleep = 2000000; | |
430 | my $i = 0; | |
431 | my $err_count = 0; | |
432 | my $lastrem = undef; | |
433 | my $downtimecounter = 0; | |
434 | while (1) { | |
435 | $i++; | |
436 | my $avglstat = $lstat/$i if $lstat; | |
437 | ||
438 | usleep($usleep); | |
439 | my $stat; | |
440 | eval { | |
441 | $stat = PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "query-migrate"); | |
442 | }; | |
443 | if (my $err = $@) { | |
444 | $err_count++; | |
445 | warn "query migrate failed: $err\n"; | |
446 | if ($err_count <= 5) { | |
447 | usleep(1000000); | |
448 | next; | |
449 | } | |
450 | die "too many query migrate failures - aborting\n"; | |
451 | } | |
452 | if ($stat->{status} =~ m/^(active|completed|failed|cancelled)$/im) { | |
453 | $merr = undef; | |
454 | $err_count = 0; | |
455 | if ($stat->{status} eq 'completed') { | |
456 | my $delay = time() - $start; | |
457 | if ($delay > 0) { | |
458 | my $mbps = sprintf "%.2f", $conf->{memory}/$delay; | |
459 | my $downtime = $stat->{downtime} || 0; | |
460 | $self->log('info', "migration speed: $mbps MB/s - downtime $downtime ms"); | |
461 | } | |
462 | } | |
463 | ||
464 | if ($stat->{status} eq 'failed' || $stat->{status} eq 'cancelled') { | |
465 | die "aborting\n" | |
466 | } | |
467 | ||
468 | if ($stat->{status} ne 'active') { | |
469 | $self->log('info', "migration status: $stat->{status}"); | |
470 | last; | |
471 | } | |
472 | ||
473 | if ($stat->{ram}->{transferred} ne $lstat) { | |
474 | my $trans = $stat->{ram}->{transferred} || 0; | |
475 | my $rem = $stat->{ram}->{remaining} || 0; | |
476 | my $total = $stat->{ram}->{total} || 0; | |
477 | my $xbzrlecachesize = $stat->{"xbzrle-cache"}->{"cache-size"} || 0; | |
478 | my $xbzrlebytes = $stat->{"xbzrle-cache"}->{"bytes"} || 0; | |
479 | my $xbzrlepages = $stat->{"xbzrle-cache"}->{"pages"} || 0; | |
480 | my $xbzrlecachemiss = $stat->{"xbzrle-cache"}->{"cache-miss"} || 0; | |
481 | my $xbzrleoverflow = $stat->{"xbzrle-cache"}->{"overflow"} || 0; | |
482 | #reduce sleep if remainig memory if lower than the everage transfert | |
483 | $usleep = 300000 if $avglstat && $rem < $avglstat; | |
484 | ||
485 | $self->log('info', "migration status: $stat->{status} (transferred ${trans}, " . | |
486 | "remaining ${rem}), total ${total})"); | |
487 | ||
488 | #$self->log('info', "migration xbzrle cachesize: ${xbzrlecachesize} transferred ${xbzrlebytes} pages ${xbzrlepages} cachemiss ${xbzrlecachemiss} overflow ${xbzrleoverflow}"); | |
489 | if (($lastrem && $rem > $lastrem ) || ($rem == 0)) { | |
490 | $downtimecounter++; | |
491 | } | |
492 | $lastrem = $rem; | |
493 | ||
494 | if ($downtimecounter > 5) { | |
495 | $downtimecounter = 0; | |
496 | $migrate_downtime *= 2; | |
497 | $self->log('info', "migrate_set_downtime: $migrate_downtime"); | |
498 | eval { | |
499 | PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate_set_downtime", value => int($migrate_downtime*100)/100); | |
500 | }; | |
501 | $self->log('info', "migrate_set_downtime error: $@") if $@; | |
502 | } | |
503 | ||
504 | } | |
505 | ||
506 | ||
507 | $lstat = $stat->{ram}->{transferred}; | |
508 | ||
509 | } else { | |
510 | die $merr if $merr; | |
511 | die "unable to parse migration status '$stat->{status}' - aborting\n"; | |
512 | } | |
513 | } | |
514 | } | |
515 | ||
516 | sub phase2_cleanup { | |
517 | my ($self, $vmid, $err) = @_; | |
518 | ||
519 | return if !$self->{errors}; | |
520 | $self->{phase2errors} = 1; | |
521 | ||
522 | $self->log('info', "aborting phase 2 - cleanup resources"); | |
523 | ||
524 | $self->log('info', "migrate_cancel"); | |
525 | eval { | |
526 | PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate_cancel"); | |
527 | }; | |
528 | $self->log('info', "migrate_cancel error: $@") if $@; | |
529 | ||
530 | my $conf = $self->{vmconf}; | |
531 | delete $conf->{lock}; | |
532 | eval { PVE::QemuServer::update_config_nolock($vmid, $conf, 1) }; | |
533 | if (my $err = $@) { | |
534 | $self->log('err', $err); | |
535 | } | |
536 | ||
537 | # cleanup ressources on target host | |
538 | my $nodename = PVE::INotify::nodename(); | |
539 | ||
540 | my $cmd = [@{$self->{rem_ssh}}, 'qm', 'stop', $vmid, '--skiplock', '--migratedfrom', $nodename]; | |
541 | eval{ PVE::Tools::run_command($cmd, outfunc => sub {}, errfunc => sub {}) }; | |
542 | if (my $err = $@) { | |
543 | $self->log('err', $err); | |
544 | $self->{errors} = 1; | |
545 | } | |
546 | } | |
547 | ||
548 | sub phase3 { | |
549 | my ($self, $vmid) = @_; | |
550 | ||
551 | my $volids = $self->{volumes}; | |
552 | return if $self->{phase2errors}; | |
553 | ||
554 | # destroy local copies | |
555 | foreach my $volid (@$volids) { | |
556 | eval { PVE::Storage::vdisk_free($self->{storecfg}, $volid); }; | |
557 | if (my $err = $@) { | |
558 | $self->log('err', "removing local copy of '$volid' failed - $err"); | |
559 | $self->{errors} = 1; | |
560 | last if $err =~ /^interrupted by signal$/; | |
561 | } | |
562 | } | |
563 | } | |
564 | ||
565 | sub phase3_cleanup { | |
566 | my ($self, $vmid, $err) = @_; | |
567 | ||
568 | my $conf = $self->{vmconf}; | |
569 | return if $self->{phase2errors}; | |
570 | ||
571 | # move config to remote node | |
572 | my $conffile = PVE::QemuServer::config_file($vmid); | |
573 | my $newconffile = PVE::QemuServer::config_file($vmid, $self->{node}); | |
574 | ||
575 | die "Failed to move config to node '$self->{node}' - rename failed: $!\n" | |
576 | if !rename($conffile, $newconffile); | |
577 | ||
578 | if ($self->{livemigration}) { | |
579 | # now that config file is move, we can resume vm on target if livemigrate | |
580 | my $cmd = [@{$self->{rem_ssh}}, 'qm', 'resume', $vmid, '--skiplock']; | |
581 | eval{ PVE::Tools::run_command($cmd, outfunc => sub {}, | |
582 | errfunc => sub { | |
583 | my $line = shift; | |
584 | $self->log('err', $line); | |
585 | }); | |
586 | }; | |
587 | if (my $err = $@) { | |
588 | $self->log('err', $err); | |
589 | $self->{errors} = 1; | |
590 | } | |
591 | } | |
592 | ||
593 | eval { | |
594 | ||
595 | my $timer = 0; | |
596 | if (PVE::QemuServer::vga_conf_has_spice($conf->{vga}) && $self->{running}) { | |
597 | $self->log('info', "Waiting for spice server migration"); | |
598 | while (1) { | |
599 | my $res = PVE::QemuServer::vm_mon_cmd_nocheck($vmid, 'query-spice'); | |
600 | last if int($res->{'migrated'}) == 1; | |
601 | last if $timer > 50; | |
602 | $timer ++; | |
603 | usleep(200000); | |
604 | } | |
605 | } | |
606 | }; | |
607 | ||
608 | # always stop local VM | |
609 | eval { PVE::QemuServer::vm_stop($self->{storecfg}, $vmid, 1, 1); }; | |
610 | if (my $err = $@) { | |
611 | $self->log('err', "stopping vm failed - $err"); | |
612 | $self->{errors} = 1; | |
613 | } | |
614 | ||
615 | if ($self->{tunnel}) { | |
616 | eval { finish_tunnel($self, $self->{tunnel}); }; | |
617 | if (my $err = $@) { | |
618 | $self->log('err', $err); | |
619 | $self->{errors} = 1; | |
620 | } | |
621 | } | |
622 | ||
623 | # always deactivate volumes - avoid lvm LVs to be active on several nodes | |
624 | eval { | |
625 | my $vollist = PVE::QemuServer::get_vm_volumes($conf); | |
626 | PVE::Storage::deactivate_volumes($self->{storecfg}, $vollist); | |
627 | }; | |
628 | if (my $err = $@) { | |
629 | $self->log('err', $err); | |
630 | $self->{errors} = 1; | |
631 | } | |
632 | ||
633 | # clear migrate lock | |
634 | my $cmd = [ @{$self->{rem_ssh}}, 'qm', 'unlock', $vmid ]; | |
635 | $self->cmd_logerr($cmd, errmsg => "failed to clear migrate lock"); | |
636 | } | |
637 | ||
638 | sub final_cleanup { | |
639 | my ($self, $vmid) = @_; | |
640 | ||
641 | # nothing to do | |
642 | } | |
643 | ||
644 | 1; |