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