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