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