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