]>
Commit | Line | Data |
---|---|---|
3ea94c60 | 1 | package PVE::QemuMigrate; |
1ef75254 | 2 | |
1e3baf05 | 3 | use strict; |
3ea94c60 | 4 | use warnings; |
16e903f2 | 5 | use PVE::AbstractMigrate; |
3ea94c60 | 6 | use IO::File; |
1e3baf05 | 7 | use IPC::Open2; |
61b04c6d | 8 | use POSIX qw( WNOHANG ); |
3ea94c60 | 9 | use PVE::INotify; |
f9a971e0 | 10 | use PVE::Tools; |
3ea94c60 | 11 | use PVE::Cluster; |
1e3baf05 | 12 | use PVE::Storage; |
63d02c70 | 13 | use PVE::ReplicationTools; |
3ea94c60 | 14 | use PVE::QemuServer; |
e52bd94c | 15 | use Time::HiRes qw( usleep ); |
95a4b4a9 | 16 | use PVE::RPCEnvironment; |
1e3baf05 | 17 | |
16e903f2 | 18 | use base qw(PVE::AbstractMigrate); |
1e3baf05 | 19 | |
1ef75254 | 20 | sub fork_command_pipe { |
46a84fd4 | 21 | my ($self, $cmd) = @_; |
19672434 | 22 | |
1ef75254 DM |
23 | my $reader = IO::File->new(); |
24 | my $writer = IO::File->new(); | |
25 | ||
26 | my $orig_pid = $$; | |
27 | ||
28 | my $cpid; | |
29 | ||
30 | eval { $cpid = open2($reader, $writer, @$cmd); }; | |
31 | ||
32 | my $err = $@; | |
33 | ||
34 | # catch exec errors | |
35 | if ($orig_pid != $$) { | |
46a84fd4 | 36 | $self->log('err', "can't fork command pipe\n"); |
19672434 DM |
37 | POSIX::_exit(1); |
38 | kill('KILL', $$); | |
1ef75254 DM |
39 | } |
40 | ||
41 | die $err if $err; | |
42 | ||
43 | return { writer => $writer, reader => $reader, pid => $cpid }; | |
44 | } | |
45 | ||
19672434 | 46 | sub finish_command_pipe { |
97439670 | 47 | my ($self, $cmdpipe, $timeout) = @_; |
1ef75254 | 48 | |
61b04c6d TL |
49 | my $cpid = $cmdpipe->{pid}; |
50 | return if !defined($cpid); | |
51 | ||
1ef75254 DM |
52 | my $writer = $cmdpipe->{writer}; |
53 | my $reader = $cmdpipe->{reader}; | |
54 | ||
55 | $writer->close(); | |
56 | $reader->close(); | |
57 | ||
61b04c6d TL |
58 | my $collect_child_process = sub { |
59 | my $res = waitpid($cpid, WNOHANG); | |
60 | if (defined($res) && ($res == $cpid)) { | |
61 | delete $cmdpipe->{cpid}; | |
62 | return 1; | |
63 | } else { | |
64 | return 0; | |
65 | } | |
66 | }; | |
1ef75254 | 67 | |
97439670 DM |
68 | if ($timeout) { |
69 | for (my $i = 0; $i < $timeout; $i++) { | |
61b04c6d | 70 | return if &$collect_child_process(); |
97439670 DM |
71 | sleep(1); |
72 | } | |
73 | } | |
74 | ||
75 | $self->log('info', "ssh tunnel still running - terminating now with SIGTERM\n"); | |
76 | kill(15, $cpid); | |
1ef75254 | 77 | |
97439670 DM |
78 | # wait again |
79 | for (my $i = 0; $i < 10; $i++) { | |
61b04c6d | 80 | return if &$collect_child_process(); |
97439670 DM |
81 | sleep(1); |
82 | } | |
83 | ||
84 | $self->log('info', "ssh tunnel still running - terminating now with SIGKILL\n"); | |
85 | kill 9, $cpid; | |
86 | sleep 1; | |
61b04c6d TL |
87 | |
88 | $self->log('err', "ssh tunnel child process (PID $cpid) couldn't be collected\n") | |
89 | if !&$collect_child_process(); | |
1ef75254 DM |
90 | } |
91 | ||
1e3baf05 | 92 | sub fork_tunnel { |
1c9d54bf | 93 | my ($self, $tunnel_addr) = @_; |
1e3baf05 | 94 | |
e858e9d2 | 95 | my @localtunnelinfo = defined($tunnel_addr) ? ('-L' , $tunnel_addr ) : (); |
5bc1e039 | 96 | |
1c9d54bf | 97 | my $cmd = [@{$self->{rem_ssh}}, '-o ExitOnForwardFailure=yes', @localtunnelinfo, 'qm', 'mtunnel' ]; |
19672434 | 98 | |
46a84fd4 | 99 | my $tunnel = $self->fork_command_pipe($cmd); |
1e3baf05 DM |
100 | |
101 | my $reader = $tunnel->{reader}; | |
102 | ||
103 | my $helo; | |
19672434 | 104 | eval { |
17eed025 | 105 | PVE::Tools::run_with_timeout(60, sub { $helo = <$reader>; }); |
1e3baf05 | 106 | die "no reply\n" if !$helo; |
1ef75254 | 107 | die "no quorum on target node\n" if $helo =~ m/^no quorum$/; |
19672434 | 108 | die "got strange reply from mtunnel ('$helo')\n" |
1e3baf05 DM |
109 | if $helo !~ m/^tunnel online$/; |
110 | }; | |
111 | my $err = $@; | |
112 | ||
113 | if ($err) { | |
46a84fd4 | 114 | $self->finish_command_pipe($tunnel); |
1e3baf05 DM |
115 | die "can't open migration tunnel - $err"; |
116 | } | |
117 | return $tunnel; | |
118 | } | |
119 | ||
19672434 | 120 | sub finish_tunnel { |
16e903f2 | 121 | my ($self, $tunnel) = @_; |
1e3baf05 DM |
122 | |
123 | my $writer = $tunnel->{writer}; | |
124 | ||
19672434 | 125 | eval { |
17eed025 | 126 | PVE::Tools::run_with_timeout(30, sub { |
1e3baf05 DM |
127 | print $writer "quit\n"; |
128 | $writer->flush(); | |
19672434 | 129 | }); |
1e3baf05 DM |
130 | }; |
131 | my $err = $@; | |
19672434 | 132 | |
97439670 | 133 | $self->finish_command_pipe($tunnel, 30); |
19672434 | 134 | |
1c9d54bf TL |
135 | if ($tunnel->{sock_addr}) { |
136 | # ssh does not clean up on local host | |
137 | my $cmd = ['rm', '-f', $tunnel->{sock_addr}]; # | |
138 | PVE::Tools::run_command($cmd); | |
139 | ||
140 | # .. and just to be sure check on remote side | |
141 | unshift @{$cmd}, @{$self->{rem_ssh}}; | |
142 | PVE::Tools::run_command($cmd); | |
143 | } | |
144 | ||
1e3baf05 DM |
145 | die $err if $err; |
146 | } | |
147 | ||
16e903f2 DM |
148 | sub lock_vm { |
149 | my ($self, $vmid, $code, @param) = @_; | |
f5eb281a | 150 | |
ffda963f | 151 | return PVE::QemuConfig->lock_config($vmid, $code, @param); |
16e903f2 | 152 | } |
ff1a2432 | 153 | |
16e903f2 DM |
154 | sub prepare { |
155 | my ($self, $vmid) = @_; | |
ff1a2432 | 156 | |
16e903f2 | 157 | my $online = $self->{opts}->{online}; |
3ea94c60 | 158 | |
16e903f2 | 159 | $self->{storecfg} = PVE::Storage::config(); |
3ea94c60 | 160 | |
e1fc368d | 161 | # test if VM exists |
ffda963f | 162 | my $conf = $self->{vmconf} = PVE::QemuConfig->load_config($vmid); |
3ea94c60 | 163 | |
ffda963f | 164 | PVE::QemuConfig->check_lock($conf); |
3ea94c60 | 165 | |
16e903f2 DM |
166 | my $running = 0; |
167 | if (my $pid = PVE::QemuServer::check_running($vmid)) { | |
b6adff33 | 168 | die "can't migrate running VM without --online\n" if !$online; |
16e903f2 | 169 | $running = $pid; |
42dbd2ee AD |
170 | |
171 | $self->{forcemachine} = PVE::QemuServer::qemu_machine_pxe($vmid, $conf); | |
7bac824e | 172 | |
3ea94c60 DM |
173 | } |
174 | ||
16e903f2 DM |
175 | if (my $loc_res = PVE::QemuServer::check_local_resources($conf, 1)) { |
176 | if ($self->{running} || !$self->{opts}->{force}) { | |
177 | die "can't migrate VM which uses local devices\n"; | |
178 | } else { | |
179 | $self->log('info', "migrating VM which uses local devices"); | |
180 | } | |
3ea94c60 DM |
181 | } |
182 | ||
ff1a2432 | 183 | my $vollist = PVE::QemuServer::get_vm_volumes($conf); |
16e903f2 | 184 | |
73f5ee92 | 185 | my $need_activate = []; |
29701766 FG |
186 | foreach my $volid (@$vollist) { |
187 | my ($sid, $volname) = PVE::Storage::parse_volume_id($volid, 1); | |
188 | ||
189 | # check if storage is available on both nodes | |
b74cad8a AD |
190 | my $targetsid = $self->{opts}->{targetstorage} ? $self->{opts}->{targetstorage} : $sid; |
191 | ||
29701766 | 192 | my $scfg = PVE::Storage::storage_check_node($self->{storecfg}, $sid); |
b74cad8a | 193 | PVE::Storage::storage_check_node($self->{storecfg}, $targetsid, $self->{node}); |
73f5ee92 FG |
194 | |
195 | if ($scfg->{shared}) { | |
196 | # PVE::Storage::activate_storage checks this for non-shared storages | |
197 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
198 | warn "Used shared storage '$sid' is not online on source node!\n" | |
199 | if !$plugin->check_connection($sid, $scfg); | |
200 | } else { | |
201 | # only activate if not shared | |
202 | push @$need_activate, $volid; | |
203 | } | |
29701766 | 204 | } |
3ea94c60 | 205 | |
73f5ee92 FG |
206 | # activate volumes |
207 | PVE::Storage::activate_volumes($self->{storecfg}, $need_activate); | |
208 | ||
3ea94c60 | 209 | # test ssh connection |
16e903f2 DM |
210 | my $cmd = [ @{$self->{rem_ssh}}, '/bin/true' ]; |
211 | eval { $self->cmd_quiet($cmd); }; | |
3ea94c60 | 212 | die "Can't connect to destination address using public key\n" if $@; |
ff1a2432 | 213 | |
16e903f2 | 214 | return $running; |
3ea94c60 DM |
215 | } |
216 | ||
217 | sub sync_disks { | |
16e903f2 DM |
218 | my ($self, $vmid) = @_; |
219 | ||
16e903f2 DM |
220 | my $conf = $self->{vmconf}; |
221 | ||
dabf2473 | 222 | # local volumes which have been copied |
16e903f2 | 223 | $self->{volumes} = []; |
3ea94c60 DM |
224 | |
225 | my $res = []; | |
226 | ||
227 | eval { | |
228 | ||
dabf2473 FG |
229 | # found local volumes and their origin |
230 | my $local_volumes = {}; | |
5bf7f0f1 FG |
231 | my $local_volumes_errors = {}; |
232 | my $other_errors = []; | |
233 | my $abort = 0; | |
3ea94c60 | 234 | |
a06c7f7e DM |
235 | my $sharedvm = 1; |
236 | ||
5bf7f0f1 FG |
237 | my $log_error = sub { |
238 | my ($msg, $volid) = @_; | |
239 | ||
240 | if (defined($volid)) { | |
241 | $local_volumes_errors->{$volid} = $msg; | |
242 | } else { | |
243 | push @$other_errors, $msg; | |
244 | } | |
245 | $abort = 1; | |
246 | }; | |
247 | ||
522c8f97 | 248 | my @sids = PVE::Storage::storage_ids($self->{storecfg}); |
86638cc2 | 249 | foreach my $storeid (@sids) { |
522c8f97 | 250 | my $scfg = PVE::Storage::storage_config($self->{storecfg}, $storeid); |
86638cc2 | 251 | next if $scfg->{shared}; |
373ea579 DM |
252 | next if !PVE::Storage::storage_check_enabled($self->{storecfg}, $storeid, undef, 1); |
253 | ||
86638cc2 FG |
254 | # get list from PVE::Storage (for unused volumes) |
255 | my $dl = PVE::Storage::vdisk_list($self->{storecfg}, $storeid, $vmid); | |
89719f98 FG |
256 | |
257 | next if @{$dl->{$storeid}} == 0; | |
258 | ||
d80ad67f AD |
259 | my $targetsid = $self->{opts}->{targetstorage} ? $self->{opts}->{targetstorage} : $storeid; |
260 | ||
86638cc2 | 261 | # check if storage is available on target node |
d80ad67f | 262 | PVE::Storage::storage_check_node($self->{storecfg}, $targetsid, $self->{node}); |
89719f98 FG |
263 | $sharedvm = 0; # there is a non-shared disk |
264 | ||
86638cc2 FG |
265 | PVE::Storage::foreach_volid($dl, sub { |
266 | my ($volid, $sid, $volname) = @_; | |
80b2cbd1 | 267 | |
dabf2473 | 268 | $local_volumes->{$volid} = 'storage'; |
86638cc2 FG |
269 | }); |
270 | } | |
3ea94c60 | 271 | |
3629c19d | 272 | my $test_volid = sub { |
b6adff33 | 273 | my ($volid, $is_cdrom, $snapname) = @_; |
3ea94c60 | 274 | |
3ea94c60 DM |
275 | return if !$volid; |
276 | ||
5bf7f0f1 FG |
277 | if ($volid =~ m|^/|) { |
278 | $local_volumes->{$volid} = 'config'; | |
279 | die "local file/device\n"; | |
280 | } | |
3ea94c60 | 281 | |
d5769dc2 | 282 | if ($is_cdrom) { |
5bf7f0f1 FG |
283 | if ($volid eq 'cdrom') { |
284 | my $msg = "can't migrate local cdrom drive"; | |
285 | $msg .= " (referenced in snapshot '$snapname')" | |
286 | if defined($snapname); | |
287 | ||
288 | &$log_error("$msg\n"); | |
289 | return; | |
290 | } | |
3ea94c60 | 291 | return if $volid eq 'none'; |
3ea94c60 DM |
292 | } |
293 | ||
294 | my ($sid, $volname) = PVE::Storage::parse_volume_id($volid); | |
295 | ||
b74cad8a | 296 | my $targetsid = $self->{opts}->{targetstorage} ? $self->{opts}->{targetstorage} : $sid; |
16e903f2 DM |
297 | # check if storage is available on both nodes |
298 | my $scfg = PVE::Storage::storage_check_node($self->{storecfg}, $sid); | |
d80ad67f | 299 | PVE::Storage::storage_check_node($self->{storecfg}, $targetsid, $self->{node}); |
3ea94c60 DM |
300 | |
301 | return if $scfg->{shared}; | |
302 | ||
3ea94c60 DM |
303 | $sharedvm = 0; |
304 | ||
dabf2473 | 305 | $local_volumes->{$volid} = defined($snapname) ? 'snapshot' : 'config'; |
d62fcf74 | 306 | |
5bf7f0f1 | 307 | die "local cdrom image\n" if $is_cdrom; |
3629c19d | 308 | |
16e903f2 | 309 | my ($path, $owner) = PVE::Storage::path($self->{storecfg}, $volid); |
3ea94c60 | 310 | |
5bf7f0f1 | 311 | die "owned by other VM (owner = VM $owner)\n" |
16e903f2 | 312 | if !$owner || ($owner != $self->{vmid}); |
3ea94c60 | 313 | |
3629c19d DM |
314 | if (defined($snapname)) { |
315 | # we cannot migrate shapshots on local storage | |
316 | # exceptions: 'zfspool' or 'qcow2' files (on directory storage) | |
317 | ||
318 | my $format = PVE::QemuServer::qemu_img_format($scfg, $volname); | |
b74cad8a | 319 | die "online storage migration not possible if snapshot exists\n" if $self->{running}; |
b3205b15 | 320 | if (!($scfg->{type} eq 'zfspool' || $format eq 'qcow2')) { |
5bf7f0f1 | 321 | die "non-migratable snapshot exists\n"; |
3629c19d | 322 | } |
3629c19d | 323 | } |
3a7bc9e2 FG |
324 | |
325 | die "referenced by linked clone(s)\n" | |
326 | if PVE::Storage::volume_is_base_and_used($self->{storecfg}, $volid); | |
3629c19d DM |
327 | }; |
328 | ||
98d80cb6 FG |
329 | my $test_drive = sub { |
330 | my ($ds, $drive, $snapname) = @_; | |
331 | ||
5bf7f0f1 FG |
332 | eval { |
333 | &$test_volid($drive->{file}, PVE::QemuServer::drive_is_cdrom($drive), $snapname); | |
334 | }; | |
335 | ||
336 | &$log_error($@, $drive->{file}) if $@; | |
98d80cb6 FG |
337 | }; |
338 | ||
b6adff33 | 339 | foreach my $snapname (keys %{$conf->{snapshots}}) { |
5bf7f0f1 FG |
340 | eval { |
341 | &$test_volid($conf->{snapshots}->{$snapname}->{'vmstate'}, 0, undef) | |
342 | if defined($conf->{snapshots}->{$snapname}->{'vmstate'}); | |
343 | }; | |
344 | &$log_error($@, $conf->{snapshots}->{$snapname}->{'vmstate'}) if $@; | |
345 | ||
98d80cb6 | 346 | PVE::QemuServer::foreach_drive($conf->{snapshots}->{$snapname}, $test_drive, $snapname); |
3629c19d | 347 | } |
4abdd867 | 348 | PVE::QemuServer::foreach_drive($conf, $test_drive); |
3ea94c60 | 349 | |
dabf2473 FG |
350 | foreach my $vol (sort keys %$local_volumes) { |
351 | if ($local_volumes->{$vol} eq 'storage') { | |
d62fcf74 | 352 | $self->log('info', "found local disk '$vol' (via storage)\n"); |
dabf2473 | 353 | } elsif ($local_volumes->{$vol} eq 'config') { |
56af7146 | 354 | die "can't live migrate attached local disks without with-local-disks option\n" if $self->{running} && !$self->{opts}->{"with-local-disks"}; |
d62fcf74 | 355 | $self->log('info', "found local disk '$vol' (in current VM config)\n"); |
dabf2473 | 356 | } elsif ($local_volumes->{$vol} eq 'snapshot') { |
d62fcf74 FG |
357 | $self->log('info', "found local disk '$vol' (referenced by snapshot(s))\n"); |
358 | } else { | |
359 | $self->log('info', "found local disk '$vol'\n"); | |
360 | } | |
361 | } | |
362 | ||
5bf7f0f1 FG |
363 | foreach my $vol (sort keys %$local_volumes_errors) { |
364 | $self->log('warn', "can't migrate local disk '$vol': $local_volumes_errors->{$vol}"); | |
365 | } | |
366 | foreach my $err (@$other_errors) { | |
367 | $self->log('warn', "$err"); | |
368 | } | |
369 | ||
b74cad8a AD |
370 | if ($self->{running} && !$sharedvm && !$self->{opts}->{targetstorage}) { |
371 | $self->{opts}->{targetstorage} = 1; #use same sid for remote local | |
3ea94c60 DM |
372 | } |
373 | ||
5bf7f0f1 FG |
374 | if ($abort) { |
375 | die "can't migrate VM - check log\n"; | |
376 | } | |
377 | ||
c4d2d6c1 | 378 | # additional checks for local storage |
dabf2473 | 379 | foreach my $volid (keys %$local_volumes) { |
3ea94c60 | 380 | my ($sid, $volname) = PVE::Storage::parse_volume_id($volid); |
16e903f2 | 381 | my $scfg = PVE::Storage::storage_config($self->{storecfg}, $sid); |
3ea94c60 | 382 | |
c4d2d6c1 WL |
383 | my $migratable = ($scfg->{type} eq 'dir') || ($scfg->{type} eq 'zfspool') || |
384 | ($scfg->{type} eq 'lvmthin') || ($scfg->{type} eq 'lvm'); | |
385 | ||
37a6dc78 | 386 | die "can't migrate '$volid' - storage type '$scfg->{type}' not supported\n" |
c4d2d6c1 | 387 | if !$migratable; |
d5604092 | 388 | |
c4d2d6c1 WL |
389 | # image is a linked clone on local storage, se we can't migrate. |
390 | if (my $basename = (PVE::Storage::parse_volname($self->{storecfg}, $volid))[3]) { | |
391 | die "can't migrate '$volid' as it's a clone of '$basename'"; | |
d5604092 | 392 | } |
3ea94c60 DM |
393 | } |
394 | ||
b74cad8a AD |
395 | $self->log('info', "copying disk images"); |
396 | ||
dabf2473 | 397 | foreach my $volid (keys %$local_volumes) { |
3ea94c60 | 398 | my ($sid, $volname) = PVE::Storage::parse_volume_id($volid); |
b74cad8a AD |
399 | if ($self->{running} && $self->{opts}->{targetstorage} && $local_volumes->{$volid} eq 'config') { |
400 | push @{$self->{online_local_volumes}}, $volid; | |
401 | } else { | |
402 | push @{$self->{volumes}}, $volid; | |
403 | PVE::Storage::storage_migrate($self->{storecfg}, $volid, $self->{nodeip}, $sid); | |
404 | } | |
3ea94c60 DM |
405 | } |
406 | }; | |
407 | die "Failed to sync data - $@" if $@; | |
408 | } | |
409 | ||
b74cad8a AD |
410 | sub cleanup_remotedisks { |
411 | my ($self) = @_; | |
412 | ||
413 | foreach my $target_drive (keys %{$self->{target_drive}}) { | |
414 | ||
415 | my $drive = PVE::QemuServer::parse_drive($target_drive, $self->{target_drive}->{$target_drive}->{volid}); | |
416 | my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file}); | |
417 | ||
418 | my $cmd = [@{$self->{rem_ssh}}, 'pvesm', 'free', "$storeid:$volname"]; | |
419 | ||
420 | eval{ PVE::Tools::run_command($cmd, outfunc => sub {}, errfunc => sub {}) }; | |
421 | if (my $err = $@) { | |
422 | $self->log('err', $err); | |
423 | $self->{errors} = 1; | |
424 | } | |
425 | } | |
426 | } | |
427 | ||
1e3baf05 | 428 | sub phase1 { |
16e903f2 | 429 | my ($self, $vmid) = @_; |
1e3baf05 | 430 | |
16e903f2 | 431 | $self->log('info', "starting migration of VM $vmid to node '$self->{node}' ($self->{nodeip})"); |
1e3baf05 | 432 | |
16e903f2 | 433 | my $conf = $self->{vmconf}; |
1e3baf05 DM |
434 | |
435 | # set migrate lock in config file | |
1858638f | 436 | $conf->{lock} = 'migrate'; |
ffda963f | 437 | PVE::QemuConfig->write_config($vmid, $conf); |
1e3baf05 | 438 | |
16e903f2 | 439 | sync_disks($self, $vmid); |
1ef75254 | 440 | |
63d02c70 WL |
441 | # set new replica_target if we migrate to replica target. |
442 | if ($conf->{replica}) { | |
443 | $self->log('info', "change replica target to Node: $self->{opts}->{node}"); | |
444 | if ($conf->{replica_target} eq $self->{node}) { | |
445 | $conf->{replica_target} = $self->{opts}->{node}; | |
446 | } | |
447 | ||
448 | PVE::ReplicationTools::job_remove($vmid); | |
449 | PVE::QemuConfig->write_config($vmid, $conf); | |
450 | } | |
1e3baf05 DM |
451 | }; |
452 | ||
16e903f2 DM |
453 | sub phase1_cleanup { |
454 | my ($self, $vmid, $err) = @_; | |
455 | ||
456 | $self->log('info', "aborting phase 1 - cleanup resources"); | |
457 | ||
1858638f DM |
458 | my $conf = $self->{vmconf}; |
459 | delete $conf->{lock}; | |
ffda963f | 460 | eval { PVE::QemuConfig->write_config($vmid, $conf) }; |
16e903f2 DM |
461 | if (my $err = $@) { |
462 | $self->log('err', $err); | |
463 | } | |
f5eb281a | 464 | |
16e903f2 DM |
465 | if ($self->{volumes}) { |
466 | foreach my $volid (@{$self->{volumes}}) { | |
467 | $self->log('err', "found stale volume copy '$volid' on node '$self->{node}'"); | |
468 | # fixme: try to remove ? | |
469 | } | |
470 | } | |
471 | } | |
472 | ||
1e3baf05 | 473 | sub phase2 { |
16e903f2 | 474 | my ($self, $vmid) = @_; |
1e3baf05 | 475 | |
16e903f2 DM |
476 | my $conf = $self->{vmconf}; |
477 | ||
46a84fd4 | 478 | $self->log('info', "starting VM $vmid on remote node '$self->{node}'"); |
1e3baf05 | 479 | |
5bc1e039 | 480 | my $raddr; |
1e3baf05 | 481 | my $rport; |
1c9d54bf | 482 | my $ruri; # the whole migration dst. URI (protocol:address[:port]) |
7e8dcf2c AD |
483 | my $nodename = PVE::INotify::nodename(); |
484 | ||
19672434 | 485 | ## start on remote node |
95a4b4a9 AD |
486 | my $cmd = [@{$self->{rem_ssh}}]; |
487 | ||
7c14dcae | 488 | my $spice_ticket; |
86b8228b | 489 | if (PVE::QemuServer::vga_conf_has_spice($conf->{vga})) { |
95a4b4a9 | 490 | my $res = PVE::QemuServer::vm_mon_cmd($vmid, 'query-spice'); |
7c14dcae | 491 | $spice_ticket = $res->{ticket}; |
95a4b4a9 AD |
492 | } |
493 | ||
1c9d54bf TL |
494 | push @$cmd , 'qm', 'start', $vmid, '--skiplock', '--migratedfrom', $nodename; |
495 | ||
496 | # we use TCP only for unsecure migrations as TCP ssh forward tunnels often | |
497 | # did appeared to late (they are hard, if not impossible, to check for) | |
498 | # secure migration use UNIX sockets now, this *breaks* compatibilty when trying | |
499 | # to migrate from new to old but *not* from old to new. | |
500 | my $datacenterconf = PVE::Cluster::cfs_read_file('datacenter.cfg'); | |
1c9d54bf | 501 | |
2de2d6f7 TL |
502 | my $migration_type = 'secure'; |
503 | if (defined($self->{opts}->{migration_type})) { | |
504 | $migration_type = $self->{opts}->{migration_type}; | |
505 | } elsif (defined($datacenterconf->{migration}->{type})) { | |
506 | $migration_type = $datacenterconf->{migration}->{type}; | |
507 | } | |
508 | ||
509 | push @$cmd, '--migration_type', $migration_type; | |
510 | ||
511 | push @$cmd, '--migration_network', $self->{opts}->{migration_network} | |
512 | if $self->{opts}->{migration_network}; | |
513 | ||
514 | if ($migration_type eq 'insecure') { | |
1c9d54bf TL |
515 | push @$cmd, '--stateuri', 'tcp'; |
516 | } else { | |
517 | push @$cmd, '--stateuri', 'unix'; | |
518 | } | |
95a4b4a9 | 519 | |
42668529 DM |
520 | if ($self->{forcemachine}) { |
521 | push @$cmd, '--machine', $self->{forcemachine}; | |
522 | } | |
523 | ||
b74cad8a AD |
524 | if ($self->{opts}->{targetstorage}) { |
525 | push @$cmd, '--targetstorage', $self->{opts}->{targetstorage}; | |
526 | } | |
527 | ||
86b8228b DM |
528 | my $spice_port; |
529 | ||
7c14dcae DM |
530 | # Note: We try to keep $spice_ticket secret (do not pass via command line parameter) |
531 | # instead we pipe it through STDIN | |
532 | PVE::Tools::run_command($cmd, input => $spice_ticket, outfunc => sub { | |
1e3baf05 DM |
533 | my $line = shift; |
534 | ||
407e0b8b | 535 | if ($line =~ m/^migration listens on tcp:(localhost|[\d\.]+|\[[\d\.:a-fA-F]+\]):(\d+)$/) { |
5bc1e039 SP |
536 | $raddr = $1; |
537 | $rport = int($2); | |
1c9d54bf TL |
538 | $ruri = "tcp:$raddr:$rport"; |
539 | } | |
540 | elsif ($line =~ m!^migration listens on unix:(/run/qemu-server/(\d+)\.migrate)$!) { | |
541 | $raddr = $1; | |
542 | die "Destination UNIX sockets VMID does not match source VMID" if $vmid ne $2; | |
543 | $ruri = "unix:$raddr"; | |
5bc1e039 SP |
544 | } |
545 | elsif ($line =~ m/^migration listens on port (\d+)$/) { | |
546 | $raddr = "localhost"; | |
86b8228b | 547 | $rport = int($1); |
1c9d54bf | 548 | $ruri = "tcp:$raddr:$rport"; |
5bc1e039 SP |
549 | } |
550 | elsif ($line =~ m/^spice listens on port (\d+)$/) { | |
86b8228b | 551 | $spice_port = int($1); |
1e3baf05 | 552 | } |
b74cad8a AD |
553 | elsif ($line =~ m/^storage migration listens on nbd:(localhost|[\d\.]+|\[[\d\.:a-fA-F]+\]):(\d+):exportname=(\S+) volume:(\S+)$/) { |
554 | my $volid = $4; | |
555 | my $nbd_uri = "nbd:$1:$2:exportname=$3"; | |
556 | my $targetdrive = $3; | |
557 | $targetdrive =~ s/drive-//g; | |
558 | ||
559 | $self->{target_drive}->{$targetdrive}->{volid} = $volid; | |
560 | $self->{target_drive}->{$targetdrive}->{nbd_uri} = $nbd_uri; | |
561 | ||
562 | } | |
ab399b7c AD |
563 | }, errfunc => sub { |
564 | my $line = shift; | |
565 | $self->log('info', $line); | |
566 | }); | |
1e3baf05 | 567 | |
5bc1e039 | 568 | die "unable to detect remote migration address\n" if !$raddr; |
1ef75254 | 569 | |
2de2d6f7 | 570 | if ($migration_type eq 'secure') { |
1c9d54bf TL |
571 | $self->log('info', "start remote tunnel"); |
572 | ||
573 | if ($ruri =~ /^unix:/) { | |
54323eed | 574 | unlink $raddr; |
1c9d54bf TL |
575 | $self->{tunnel} = $self->fork_tunnel("$raddr:$raddr"); |
576 | $self->{tunnel}->{sock_addr} = $raddr; | |
577 | ||
578 | my $unix_socket_try = 0; # wait for the socket to become ready | |
579 | while (! -S $raddr) { | |
580 | $unix_socket_try++; | |
581 | if ($unix_socket_try > 100) { | |
582 | $self->{errors} = 1; | |
583 | $self->finish_tunnel($self->{tunnel}); | |
584 | die "Timeout, migration socket $ruri did not get ready"; | |
585 | } | |
586 | ||
587 | usleep(50000); | |
588 | } | |
589 | ||
590 | } elsif ($ruri =~ /^tcp:/) { | |
e858e9d2 TL |
591 | my $tunnel_addr; |
592 | if ($raddr eq "localhost") { | |
593 | # for backwards compatibility with older qemu-server versions | |
594 | my $pfamily = PVE::Tools::get_host_address_family($nodename); | |
595 | my $lport = PVE::Tools::next_migrate_port($pfamily); | |
596 | $tunnel_addr = "$lport:localhost:$rport"; | |
597 | } | |
1c9d54bf | 598 | |
e858e9d2 | 599 | $self->{tunnel} = $self->fork_tunnel($tunnel_addr); |
1c9d54bf TL |
600 | |
601 | } else { | |
602 | die "unsupported protocol in migration URI: $ruri\n"; | |
603 | } | |
604 | } | |
1e3baf05 | 605 | |
1e3baf05 | 606 | my $start = time(); |
b74cad8a | 607 | |
8b54f4b8 | 608 | if ($self->{opts}->{targetstorage} && defined($self->{online_local_volumes})) { |
b74cad8a AD |
609 | $self->{storage_migration} = 1; |
610 | $self->{storage_migration_jobs} = {}; | |
611 | $self->log('info', "starting storage migration"); | |
612 | ||
bd2d5fe6 | 613 | die "The number of local disks does not match between the source and the destination.\n" |
3b4cf0f0 | 614 | if (scalar(keys %{$self->{target_drive}}) != scalar @{$self->{online_local_volumes}}); |
b74cad8a | 615 | foreach my $drive (keys %{$self->{target_drive}}){ |
3b4cf0f0 WB |
616 | my $nbd_uri = $self->{target_drive}->{$drive}->{nbd_uri}; |
617 | $self->log('info', "$drive: start migration to to $nbd_uri"); | |
618 | PVE::QemuServer::qemu_drive_mirror($vmid, $drive, $nbd_uri, $vmid, undef, $self->{storage_migration_jobs}, 1); | |
b74cad8a AD |
619 | } |
620 | } | |
621 | ||
1c9d54bf | 622 | $self->log('info', "starting online/live migration on $ruri"); |
5bc1e039 | 623 | $self->{livemigration} = 1; |
e18b0b99 | 624 | |
3beb415b AD |
625 | # load_defaults |
626 | my $defaults = PVE::QemuServer::load_defaults(); | |
627 | ||
628 | # always set migrate speed (overwrite kvm default of 32m) | |
629 | # we set a very hight default of 8192m which is basically unlimited | |
630 | my $migrate_speed = $defaults->{migrate_speed} || 8192; | |
631 | $migrate_speed = $conf->{migrate_speed} || $migrate_speed; | |
632 | $migrate_speed = $migrate_speed * 1048576; | |
633 | $self->log('info', "migrate_set_speed: $migrate_speed"); | |
634 | eval { | |
635 | PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate_set_speed", value => int($migrate_speed)); | |
636 | }; | |
637 | $self->log('info', "migrate_set_speed error: $@") if $@; | |
638 | ||
639 | my $migrate_downtime = $defaults->{migrate_downtime}; | |
640 | $migrate_downtime = $conf->{migrate_downtime} if defined($conf->{migrate_downtime}); | |
641 | if (defined($migrate_downtime)) { | |
642 | $self->log('info', "migrate_set_downtime: $migrate_downtime"); | |
643 | eval { | |
865ef132 | 644 | PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate_set_downtime", value => int($migrate_downtime*100)/100); |
3beb415b AD |
645 | }; |
646 | $self->log('info', "migrate_set_downtime error: $@") if $@; | |
647 | } | |
648 | ||
f34d1466 | 649 | $self->log('info', "set migration_caps"); |
e18b0b99 | 650 | eval { |
a89fded1 | 651 | PVE::QemuServer::set_migration_caps($vmid); |
e18b0b99 | 652 | }; |
a89fded1 | 653 | warn $@ if $@; |
e18b0b99 AD |
654 | |
655 | #set cachesize 10% of the total memory | |
656 | my $cachesize = int($conf->{memory}*1048576/10); | |
f34d1466 | 657 | $self->log('info', "set cachesize: $cachesize"); |
e18b0b99 | 658 | eval { |
f34d1466 | 659 | PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate-set-cache-size", value => int($cachesize)); |
e18b0b99 | 660 | }; |
f34d1466 TL |
661 | $self->log('info', "migrate-set-cache-size error: $@") if $@; |
662 | ||
86b8228b | 663 | if (PVE::QemuServer::vga_conf_has_spice($conf->{vga})) { |
95a4b4a9 AD |
664 | my $rpcenv = PVE::RPCEnvironment::get(); |
665 | my $authuser = $rpcenv->get_user(); | |
666 | ||
86b8228b | 667 | my (undef, $proxyticket) = PVE::AccessControl::assemble_spice_ticket($authuser, $vmid, $self->{node}); |
95a4b4a9 | 668 | |
86b8228b | 669 | my $filename = "/etc/pve/nodes/$self->{node}/pve-ssl.pem"; |
dd25eecf | 670 | my $subject = PVE::AccessControl::read_x509_subject_spice($filename); |
95a4b4a9 AD |
671 | |
672 | $self->log('info', "spice client_migrate_info"); | |
673 | ||
674 | eval { | |
86b8228b DM |
675 | PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "client_migrate_info", protocol => 'spice', |
676 | hostname => $proxyticket, 'tls-port' => $spice_port, | |
677 | 'cert-subject' => $subject); | |
95a4b4a9 AD |
678 | }; |
679 | $self->log('info', "client_migrate_info error: $@") if $@; | |
680 | ||
681 | } | |
682 | ||
f34d1466 | 683 | $self->log('info', "start migrate command to $ruri"); |
5a7835f5 | 684 | eval { |
1c9d54bf | 685 | PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate", uri => $ruri); |
5a7835f5 AD |
686 | }; |
687 | my $merr = $@; | |
1c9d54bf | 688 | $self->log('info', "migrate uri => $ruri failed: $merr") if $merr; |
1e3baf05 | 689 | |
a05b47a8 | 690 | my $lstat = 0; |
e52bd94c AD |
691 | my $usleep = 2000000; |
692 | my $i = 0; | |
b0b756c1 | 693 | my $err_count = 0; |
865ef132 SP |
694 | my $lastrem = undef; |
695 | my $downtimecounter = 0; | |
1e3baf05 | 696 | while (1) { |
e52bd94c AD |
697 | $i++; |
698 | my $avglstat = $lstat/$i if $lstat; | |
699 | ||
b0b756c1 DM |
700 | usleep($usleep); |
701 | my $stat; | |
702 | eval { | |
703 | $stat = PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "query-migrate"); | |
704 | }; | |
705 | if (my $err = $@) { | |
706 | $err_count++; | |
707 | warn "query migrate failed: $err\n"; | |
f34d1466 | 708 | $self->log('info', "query migrate failed: $err"); |
b0b756c1 DM |
709 | if ($err_count <= 5) { |
710 | usleep(1000000); | |
711 | next; | |
712 | } | |
713 | die "too many query migrate failures - aborting\n"; | |
714 | } | |
985a5f48 | 715 | |
f34d1466 | 716 | if (defined($stat->{status}) && $stat->{status} =~ m/^(setup)$/im) { |
985a5f48 AD |
717 | sleep(1); |
718 | next; | |
719 | } | |
720 | ||
f34d1466 | 721 | if (defined($stat->{status}) && $stat->{status} =~ m/^(active|completed|failed|cancelled)$/im) { |
d68afb26 | 722 | $merr = undef; |
b0b756c1 | 723 | $err_count = 0; |
5a7835f5 | 724 | if ($stat->{status} eq 'completed') { |
1e3baf05 DM |
725 | my $delay = time() - $start; |
726 | if ($delay > 0) { | |
727 | my $mbps = sprintf "%.2f", $conf->{memory}/$delay; | |
135007c0 AD |
728 | my $downtime = $stat->{downtime} || 0; |
729 | $self->log('info', "migration speed: $mbps MB/s - downtime $downtime ms"); | |
1e3baf05 DM |
730 | } |
731 | } | |
f5eb281a | 732 | |
5a7835f5 | 733 | if ($stat->{status} eq 'failed' || $stat->{status} eq 'cancelled') { |
f34d1466 | 734 | $self->log('info', "migration status error: $stat->{status}"); |
1e3baf05 DM |
735 | die "aborting\n" |
736 | } | |
737 | ||
a05b47a8 DM |
738 | if ($stat->{status} ne 'active') { |
739 | $self->log('info', "migration status: $stat->{status}"); | |
740 | last; | |
741 | } | |
742 | ||
743 | if ($stat->{ram}->{transferred} ne $lstat) { | |
744 | my $trans = $stat->{ram}->{transferred} || 0; | |
745 | my $rem = $stat->{ram}->{remaining} || 0; | |
746 | my $total = $stat->{ram}->{total} || 0; | |
e18b0b99 AD |
747 | my $xbzrlecachesize = $stat->{"xbzrle-cache"}->{"cache-size"} || 0; |
748 | my $xbzrlebytes = $stat->{"xbzrle-cache"}->{"bytes"} || 0; | |
749 | my $xbzrlepages = $stat->{"xbzrle-cache"}->{"pages"} || 0; | |
750 | my $xbzrlecachemiss = $stat->{"xbzrle-cache"}->{"cache-miss"} || 0; | |
751 | my $xbzrleoverflow = $stat->{"xbzrle-cache"}->{"overflow"} || 0; | |
e52bd94c | 752 | #reduce sleep if remainig memory if lower than the everage transfert |
94235c59 | 753 | $usleep = 300000 if $avglstat && $rem < $avglstat; |
a05b47a8 DM |
754 | |
755 | $self->log('info', "migration status: $stat->{status} (transferred ${trans}, " . | |
0302101c | 756 | "remaining ${rem}), total ${total})"); |
e18b0b99 | 757 | |
2e787b18 SP |
758 | if (${xbzrlecachesize}) { |
759 | $self->log('info', "migration xbzrle cachesize: ${xbzrlecachesize} transferred ${xbzrlebytes} pages ${xbzrlepages} cachemiss ${xbzrlecachemiss} overflow ${xbzrleoverflow}"); | |
760 | } | |
761 | ||
865ef132 SP |
762 | if (($lastrem && $rem > $lastrem ) || ($rem == 0)) { |
763 | $downtimecounter++; | |
764 | } | |
765 | $lastrem = $rem; | |
766 | ||
767 | if ($downtimecounter > 5) { | |
768 | $downtimecounter = 0; | |
769 | $migrate_downtime *= 2; | |
770 | $self->log('info', "migrate_set_downtime: $migrate_downtime"); | |
771 | eval { | |
772 | PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate_set_downtime", value => int($migrate_downtime*100)/100); | |
773 | }; | |
774 | $self->log('info', "migrate_set_downtime error: $@") if $@; | |
775 | } | |
776 | ||
a05b47a8 DM |
777 | } |
778 | ||
865ef132 | 779 | |
a05b47a8 | 780 | $lstat = $stat->{ram}->{transferred}; |
e52bd94c | 781 | |
1e3baf05 | 782 | } else { |
d68afb26 | 783 | die $merr if $merr; |
5a7835f5 | 784 | die "unable to parse migration status '$stat->{status}' - aborting\n"; |
1e3baf05 | 785 | } |
a05b47a8 | 786 | } |
92437b8d TL |
787 | |
788 | # just to be sure that the tunnel gets closed on successful migration, on error | |
789 | # phase2_cleanup closes it *after* stopping the remote waiting VM | |
790 | if (!$self->{errors} && $self->{tunnel}) { | |
386c6ba7 WL |
791 | eval { finish_tunnel($self, $self->{tunnel}); }; |
792 | if (my $err = $@) { | |
793 | $self->log('err', $err); | |
794 | $self->{errors} = 1; | |
795 | } | |
796 | } | |
1e3baf05 | 797 | } |
16e903f2 | 798 | |
c04b5b04 AD |
799 | sub phase2_cleanup { |
800 | my ($self, $vmid, $err) = @_; | |
801 | ||
af30308f DM |
802 | return if !$self->{errors}; |
803 | $self->{phase2errors} = 1; | |
804 | ||
c04b5b04 AD |
805 | $self->log('info', "aborting phase 2 - cleanup resources"); |
806 | ||
19168b91 SP |
807 | $self->log('info', "migrate_cancel"); |
808 | eval { | |
809 | PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate_cancel"); | |
810 | }; | |
811 | $self->log('info', "migrate_cancel error: $@") if $@; | |
812 | ||
c04b5b04 AD |
813 | my $conf = $self->{vmconf}; |
814 | delete $conf->{lock}; | |
ffda963f | 815 | eval { PVE::QemuConfig->write_config($vmid, $conf) }; |
c04b5b04 AD |
816 | if (my $err = $@) { |
817 | $self->log('err', $err); | |
818 | } | |
819 | ||
af30308f | 820 | # cleanup ressources on target host |
3b4cf0f0 | 821 | if ($self->{storage_migration}) { |
b74cad8a AD |
822 | |
823 | eval { PVE::QemuServer::qemu_blockjobs_cancel($vmid, $self->{storage_migration_jobs}) }; | |
824 | if (my $err = $@) { | |
825 | $self->log('err', $err); | |
826 | } | |
827 | ||
828 | eval { PVE::QemuMigrate::cleanup_remotedisks($self) }; | |
829 | if (my $err = $@) { | |
830 | $self->log('err', $err); | |
831 | } | |
832 | } | |
833 | ||
af30308f DM |
834 | my $nodename = PVE::INotify::nodename(); |
835 | ||
836 | my $cmd = [@{$self->{rem_ssh}}, 'qm', 'stop', $vmid, '--skiplock', '--migratedfrom', $nodename]; | |
837 | eval{ PVE::Tools::run_command($cmd, outfunc => sub {}, errfunc => sub {}) }; | |
838 | if (my $err = $@) { | |
839 | $self->log('err', $err); | |
840 | $self->{errors} = 1; | |
841 | } | |
386c6ba7 WL |
842 | |
843 | if ($self->{tunnel}) { | |
844 | eval { finish_tunnel($self, $self->{tunnel}); }; | |
845 | if (my $err = $@) { | |
846 | $self->log('err', $err); | |
847 | $self->{errors} = 1; | |
848 | } | |
849 | } | |
c04b5b04 AD |
850 | } |
851 | ||
16e903f2 DM |
852 | sub phase3 { |
853 | my ($self, $vmid) = @_; | |
f5eb281a | 854 | |
16e903f2 | 855 | my $volids = $self->{volumes}; |
af30308f | 856 | return if $self->{phase2errors}; |
16e903f2 | 857 | |
63d02c70 WL |
858 | my $synced_volumes = PVE::ReplicationTools::get_syncable_guestdisks($self->{vmconf}, 'qemu') |
859 | if $self->{vmconf}->{replica}; | |
860 | ||
861 | ||
16e903f2 DM |
862 | # destroy local copies |
863 | foreach my $volid (@$volids) { | |
63d02c70 | 864 | # do not destroy if new target is local_host |
6e8044dc WB |
865 | next if $self->{vmconf}->{replica} && |
866 | defined($synced_volumes->{$volid}) && | |
867 | $self->{vmconf}->{replica_target} eq $self->{opts}->{node}; | |
868 | ||
869 | eval { PVE::Storage::vdisk_free($self->{storecfg}, $volid); }; | |
870 | if (my $err = $@) { | |
871 | $self->log('err', "removing local copy of '$volid' failed - $err"); | |
872 | $self->{errors} = 1; | |
873 | last if $err =~ /^interrupted by signal$/; | |
16e903f2 DM |
874 | } |
875 | } | |
16e903f2 DM |
876 | } |
877 | ||
878 | sub phase3_cleanup { | |
879 | my ($self, $vmid, $err) = @_; | |
880 | ||
881 | my $conf = $self->{vmconf}; | |
af30308f | 882 | return if $self->{phase2errors}; |
16e903f2 | 883 | |
b74cad8a | 884 | if ($self->{storage_migration}) { |
3b4cf0f0 WB |
885 | # finish block-job |
886 | eval { PVE::QemuServer::qemu_drive_mirror_monitor($vmid, undef, $self->{storage_migration_jobs}); }; | |
b74cad8a AD |
887 | |
888 | if (my $err = $@) { | |
889 | eval { PVE::QemuServer::qemu_blockjobs_cancel($vmid, $self->{storage_migration_jobs}) }; | |
890 | eval { PVE::QemuMigrate::cleanup_remotedisks($self) }; | |
891 | die "Failed to completed storage migration\n"; | |
892 | } else { | |
b74cad8a AD |
893 | foreach my $target_drive (keys %{$self->{target_drive}}) { |
894 | my $drive = PVE::QemuServer::parse_drive($target_drive, $self->{target_drive}->{$target_drive}->{volid}); | |
895 | $conf->{$target_drive} = PVE::QemuServer::print_drive($vmid, $drive); | |
896 | PVE::QemuConfig->write_config($vmid, $conf); | |
897 | } | |
898 | } | |
899 | } | |
900 | ||
b8d20802 | 901 | # move config to remote node |
ffda963f FG |
902 | my $conffile = PVE::QemuConfig->config_file($vmid); |
903 | my $newconffile = PVE::QemuConfig->config_file($vmid, $self->{node}); | |
b8d20802 AD |
904 | |
905 | die "Failed to move config to node '$self->{node}' - rename failed: $!\n" | |
906 | if !rename($conffile, $newconffile); | |
907 | ||
5bc1e039 | 908 | if ($self->{livemigration}) { |
504105c6 | 909 | if ($self->{storage_migration}) { |
28412ae4 FG |
910 | # remove drives referencing the nbd server from source |
911 | # otherwise vm_stop might hang later on | |
912 | foreach my $drive (keys %{$self->{target_drive}}){ | |
913 | PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "device_del", id => $drive); | |
914 | } | |
504105c6 FG |
915 | # stop nbd server on remote vm - requirement for resume since 2.9 |
916 | my $cmd = [@{$self->{rem_ssh}}, 'qm', 'nbdstop', $vmid]; | |
917 | ||
918 | eval{ PVE::Tools::run_command($cmd, outfunc => sub {}, errfunc => sub {}) }; | |
919 | if (my $err = $@) { | |
920 | $self->log('err', $err); | |
921 | $self->{errors} = 1; | |
922 | } | |
923 | } | |
877e2ea7 | 924 | # config moved and nbd server stopped - now we can resume vm on target |
289e0b85 | 925 | my $cmd = [@{$self->{rem_ssh}}, 'qm', 'resume', $vmid, '--skiplock', '--nocheck']; |
d5f315fd AD |
926 | eval{ PVE::Tools::run_command($cmd, outfunc => sub {}, |
927 | errfunc => sub { | |
928 | my $line = shift; | |
929 | $self->log('err', $line); | |
930 | }); | |
931 | }; | |
f5eb281a | 932 | if (my $err = $@) { |
b67900f1 AD |
933 | $self->log('err', $err); |
934 | $self->{errors} = 1; | |
935 | } | |
936 | } | |
937 | ||
fd8469f7 | 938 | eval { |
fd8469f7 AD |
939 | my $timer = 0; |
940 | if (PVE::QemuServer::vga_conf_has_spice($conf->{vga}) && $self->{running}) { | |
941 | $self->log('info', "Waiting for spice server migration"); | |
942 | while (1) { | |
943 | my $res = PVE::QemuServer::vm_mon_cmd_nocheck($vmid, 'query-spice'); | |
944 | last if int($res->{'migrated'}) == 1; | |
945 | last if $timer > 50; | |
946 | $timer ++; | |
947 | usleep(200000); | |
948 | } | |
949 | } | |
950 | }; | |
95a4b4a9 | 951 | |
16e903f2 DM |
952 | # always stop local VM |
953 | eval { PVE::QemuServer::vm_stop($self->{storecfg}, $vmid, 1, 1); }; | |
954 | if (my $err = $@) { | |
955 | $self->log('err', "stopping vm failed - $err"); | |
956 | $self->{errors} = 1; | |
957 | } | |
958 | ||
959 | # always deactivate volumes - avoid lvm LVs to be active on several nodes | |
960 | eval { | |
961 | my $vollist = PVE::QemuServer::get_vm_volumes($conf); | |
962 | PVE::Storage::deactivate_volumes($self->{storecfg}, $vollist); | |
963 | }; | |
964 | if (my $err = $@) { | |
965 | $self->log('err', $err); | |
966 | $self->{errors} = 1; | |
967 | } | |
968 | ||
b74cad8a AD |
969 | if($self->{storage_migration}) { |
970 | # destroy local copies | |
971 | my $volids = $self->{online_local_volumes}; | |
972 | ||
973 | foreach my $volid (@$volids) { | |
974 | eval { PVE::Storage::vdisk_free($self->{storecfg}, $volid); }; | |
975 | if (my $err = $@) { | |
976 | $self->log('err', "removing local copy of '$volid' failed - $err"); | |
977 | $self->{errors} = 1; | |
978 | last if $err =~ /^interrupted by signal$/; | |
979 | } | |
980 | } | |
981 | ||
b74cad8a AD |
982 | } |
983 | ||
16e903f2 DM |
984 | # clear migrate lock |
985 | my $cmd = [ @{$self->{rem_ssh}}, 'qm', 'unlock', $vmid ]; | |
986 | $self->cmd_logerr($cmd, errmsg => "failed to clear migrate lock"); | |
63d02c70 WL |
987 | |
988 | if ($self->{vmconf}->{replica}) { | |
989 | my $cmd = [ @{$self->{rem_ssh}}, 'qm', 'set', $vmid, '--replica']; | |
990 | $self->cmd_logerr($cmd, errmsg => "failed to activate replica"); | |
991 | } | |
16e903f2 DM |
992 | } |
993 | ||
994 | sub final_cleanup { | |
995 | my ($self, $vmid) = @_; | |
996 | ||
997 | # nothing to do | |
998 | } | |
999 | ||
1000 | 1; |