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