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