]>
git.proxmox.com Git - qemu-server.git/blob - PVE/QemuMigrate.pm
1 package PVE
::QemuMigrate
;
5 use PVE
::AbstractMigrate
;
13 use Time
::HiRes
qw( usleep );
14 use PVE
::RPCEnvironment
;
16 use base
qw(PVE::AbstractMigrate);
18 sub fork_command_pipe
{
19 my ($self, $cmd) = @_;
21 my $reader = IO
::File-
>new();
22 my $writer = IO
::File-
>new();
28 eval { $cpid = open2
($reader, $writer, @$cmd); };
33 if ($orig_pid != $$) {
34 $self->log('err', "can't fork command pipe\n");
41 return { writer
=> $writer, reader
=> $reader, pid
=> $cpid };
44 sub finish_command_pipe
{
45 my ($self, $cmdpipe, $timeout) = @_;
47 my $writer = $cmdpipe->{writer
};
48 my $reader = $cmdpipe->{reader
};
53 my $cpid = $cmdpipe->{pid
};
56 for (my $i = 0; $i < $timeout; $i++) {
57 return if !PVE
::ProcFSTools
::check_process_running
($cpid);
62 $self->log('info', "ssh tunnel still running - terminating now with SIGTERM\n");
66 for (my $i = 0; $i < 10; $i++) {
67 return if !PVE
::ProcFSTools
::check_process_running
($cpid);
71 $self->log('info', "ssh tunnel still running - terminating now with SIGKILL\n");
77 my ($self, $nodeip, $lport, $rport) = @_;
79 my @localtunnelinfo = (defined $lport) ?
qw(-L $lport:localhost:$rport) : ();
81 my $cmd = [@{$self->{rem_ssh
}}, @localtunnelinfo,
84 my $tunnel = $self->fork_command_pipe($cmd);
86 my $reader = $tunnel->{reader
};
90 PVE
::Tools
::run_with_timeout
(60, sub { $helo = <$reader>; });
91 die "no reply\n" if !$helo;
92 die "no quorum on target node\n" if $helo =~ m/^no quorum$/;
93 die "got strange reply from mtunnel ('$helo')\n"
94 if $helo !~ m/^tunnel online$/;
99 $self->finish_command_pipe($tunnel);
100 die "can't open migration tunnel - $err";
106 my ($self, $tunnel) = @_;
108 my $writer = $tunnel->{writer
};
111 PVE
::Tools
::run_with_timeout
(30, sub {
112 print $writer "quit\n";
118 $self->finish_command_pipe($tunnel, 30);
124 my ($self, $vmid, $code, @param) = @_;
126 return PVE
::QemuServer
::lock_config
($vmid, $code, @param);
130 my ($self, $vmid) = @_;
132 my $online = $self->{opts
}->{online
};
134 $self->{storecfg
} = PVE
::Storage
::config
();
137 my $conf = $self->{vmconf
} = PVE
::QemuServer
::load_config
($vmid);
139 PVE
::QemuServer
::check_lock
($conf);
142 if (my $pid = PVE
::QemuServer
::check_running
($vmid)) {
143 die "cant migrate running VM without --online\n" if !$online;
145 $self->{forcemachine
} = PVE
::QemuServer
::get_current_qemu_machine
($vmid);
148 if (my $loc_res = PVE
::QemuServer
::check_local_resources
($conf, 1)) {
149 if ($self->{running
} || !$self->{opts
}->{force
}) {
150 die "can't migrate VM which uses local devices\n";
152 $self->log('info', "migrating VM which uses local devices");
157 my $vollist = PVE
::QemuServer
::get_vm_volumes
($conf);
158 PVE
::Storage
::activate_volumes
($self->{storecfg
}, $vollist);
160 # fixme: check if storage is available on both nodes
162 # test ssh connection
163 my $cmd = [ @{$self->{rem_ssh
}}, '/bin/true' ];
164 eval { $self->cmd_quiet($cmd); };
165 die "Can't connect to destination address using public key\n" if $@;
171 my ($self, $vmid) = @_;
173 $self->log('info', "copying disk images");
175 my $conf = $self->{vmconf
};
177 $self->{volumes
} = [];
188 my @sids = PVE
::Storage
::storage_ids
($self->{storecfg
});
189 foreach my $storeid (@sids) {
190 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $storeid);
191 next if $scfg->{shared
};
192 next if !PVE
::Storage
::storage_check_enabled
($self->{storecfg
}, $storeid, undef, 1);
194 # get list from PVE::Storage (for unused volumes)
195 my $dl = PVE
::Storage
::vdisk_list
($self->{storecfg
}, $storeid, $vmid);
196 PVE
::Storage
::foreach_volid
($dl, sub {
197 my ($volid, $sid, $volname) = @_;
199 # check if storage is available on target node
200 PVE
::Storage
::storage_check_node
($self->{storecfg
}, $sid, $self->{node
});
202 $volhash->{$volid} = 1;
203 $sharedvm = 0; # there is a non-shared disk
207 # and add used, owned/non-shared disks (just to be sure we have all)
209 PVE
::QemuServer
::foreach_volid
($conf, sub {
210 my ($volid, $is_cdrom) = @_;
214 die "cant migrate local file/device '$volid'\n" if $volid =~ m
|^/|;
217 die "cant migrate local cdrom drive\n" if $volid eq 'cdrom';
218 return if $volid eq 'none';
219 $cdromhash->{$volid} = 1;
222 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
224 # check if storage is available on both nodes
225 my $scfg = PVE
::Storage
::storage_check_node
($self->{storecfg
}, $sid);
226 PVE
::Storage
::storage_check_node
($self->{storecfg
}, $sid, $self->{node
});
228 return if $scfg->{shared
};
230 die "can't migrate local cdrom '$volid'\n" if $cdromhash->{$volid};
234 my ($path, $owner) = PVE
::Storage
::path
($self->{storecfg
}, $volid);
236 die "can't migrate volume '$volid' - owned by other VM (owner = VM $owner)\n"
237 if !$owner || ($owner != $self->{vmid
});
239 $volhash->{$volid} = 1;
242 if ($self->{running
} && !$sharedvm) {
243 die "can't do online migration - VM uses local disks\n";
246 # do some checks first
247 foreach my $volid (keys %$volhash) {
248 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
249 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $sid);
251 die "can't migrate '$volid' - storagy type '$scfg->{type}' not supported\n"
252 if $scfg->{type
} ne 'dir';
254 # if file, check if a backing file exist
255 if (($scfg->{type
} eq 'dir') && (!$sharedvm)) {
256 my (undef, undef, undef, $parent) = PVE
::Storage
::volume_size_info
($self->{storecfg
}, $volid, 1);
257 die "can't migrate '$volid' as it's a clone of '$parent'" if $parent;
261 foreach my $volid (keys %$volhash) {
262 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
263 push @{$self->{volumes
}}, $volid;
264 PVE
::Storage
::storage_migrate
($self->{storecfg
}, $volid, $self->{nodeip
}, $sid);
267 die "Failed to sync data - $@" if $@;
271 my ($self, $vmid) = @_;
273 $self->log('info', "starting migration of VM $vmid to node '$self->{node}' ($self->{nodeip})");
275 my $conf = $self->{vmconf
};
277 # set migrate lock in config file
278 $conf->{lock} = 'migrate';
279 PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1);
281 sync_disks
($self, $vmid);
286 my ($self, $vmid, $err) = @_;
288 $self->log('info', "aborting phase 1 - cleanup resources");
290 my $conf = $self->{vmconf
};
291 delete $conf->{lock};
292 eval { PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1) };
294 $self->log('err', $err);
297 if ($self->{volumes
}) {
298 foreach my $volid (@{$self->{volumes
}}) {
299 $self->log('err', "found stale volume copy '$volid' on node '$self->{node}'");
300 # fixme: try to remove ?
306 my ($self, $vmid) = @_;
308 my $conf = $self->{vmconf
};
310 $self->log('info', "starting VM $vmid on remote node '$self->{node}'");
314 my $nodename = PVE
::INotify
::nodename
();
316 ## start on remote node
317 my $cmd = [@{$self->{rem_ssh
}}];
320 if (PVE
::QemuServer
::vga_conf_has_spice
($conf->{vga
})) {
321 my $res = PVE
::QemuServer
::vm_mon_cmd
($vmid, 'query-spice');
322 $spice_ticket = $res->{ticket
};
325 push @$cmd , 'qm', 'start', $vmid, '--stateuri', 'tcp', '--skiplock', '--migratedfrom', $nodename;
327 if ($self->{forcemachine
}) {
328 push @$cmd, '--machine', $self->{forcemachine
};
333 # Note: We try to keep $spice_ticket secret (do not pass via command line parameter)
334 # instead we pipe it through STDIN
335 PVE
::Tools
::run_command
($cmd, input
=> $spice_ticket, outfunc
=> sub {
338 if ($line =~ m/^migration listens on tcp:([\d\.]+|localhost):(\d+)$/) {
342 elsif ($line =~ m/^migration listens on port (\d+)$/) {
343 $raddr = "localhost";
346 elsif ($line =~ m/^spice listens on port (\d+)$/) {
347 $spice_port = int($1);
351 $self->log('info', $line);
354 die "unable to detect remote migration address\n" if !$raddr;
356 ## create tunnel to remote port
357 $self->log('info', "starting ssh migration tunnel");
358 my $lport = ($raddr eq "localhost") ? PVE
::Tools
::next_migrate_port
() : undef;
359 $self->{tunnel
} = $self->fork_tunnel($self->{nodeip
}, $lport, $rport);
362 $self->log('info', "starting online/live migration on $raddr:$rport");
363 $self->{livemigration
} = 1;
366 my $defaults = PVE
::QemuServer
::load_defaults
();
368 # always set migrate speed (overwrite kvm default of 32m)
369 # we set a very hight default of 8192m which is basically unlimited
370 my $migrate_speed = $defaults->{migrate_speed
} || 8192;
371 $migrate_speed = $conf->{migrate_speed
} || $migrate_speed;
372 $migrate_speed = $migrate_speed * 1048576;
373 $self->log('info', "migrate_set_speed: $migrate_speed");
375 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate_set_speed", value
=> int($migrate_speed));
377 $self->log('info', "migrate_set_speed error: $@") if $@;
379 my $migrate_downtime = $defaults->{migrate_downtime
};
380 $migrate_downtime = $conf->{migrate_downtime
} if defined($conf->{migrate_downtime
});
381 if (defined($migrate_downtime)) {
382 $self->log('info', "migrate_set_downtime: $migrate_downtime");
384 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate_set_downtime", value
=> int($migrate_downtime*100)/100);
386 $self->log('info', "migrate_set_downtime error: $@") if $@;
389 my $capabilities = {};
390 $capabilities->{capability
} = "xbzrle";
391 $capabilities->{state} = JSON
::false
;
394 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate-set-capabilities", capabilities
=> [$capabilities]);
397 #set cachesize 10% of the total memory
398 my $cachesize = int($conf->{memory
}*1048576/10);
400 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate-set-cache-size", value
=> $cachesize);
403 if (PVE
::QemuServer
::vga_conf_has_spice
($conf->{vga
})) {
404 my $rpcenv = PVE
::RPCEnvironment
::get
();
405 my $authuser = $rpcenv->get_user();
407 my (undef, $proxyticket) = PVE
::AccessControl
::assemble_spice_ticket
($authuser, $vmid, $self->{node
});
409 my $filename = "/etc/pve/nodes/$self->{node}/pve-ssl.pem";
410 my $subject = PVE
::QemuServer
::read_x509_subject_spice
($filename);
412 $self->log('info', "spice client_migrate_info");
415 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "client_migrate_info", protocol
=> 'spice',
416 hostname
=> $proxyticket, 'tls-port' => $spice_port,
417 'cert-subject' => $subject);
419 $self->log('info', "client_migrate_info error: $@") if $@;
424 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate", uri
=> "tcp:$raddr:$rport");
427 $self->log('info', "migrate uri => tcp:$raddr:$rport failed: $merr") if $merr;
430 my $usleep = 2000000;
434 my $downtimecounter = 0;
437 my $avglstat = $lstat/$i if $lstat;
442 $stat = PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "query-migrate");
446 warn "query migrate failed: $err\n";
447 if ($err_count <= 5) {
451 die "too many query migrate failures - aborting\n";
453 if ($stat->{status
} =~ m/^(active|completed|failed|cancelled)$/im) {
456 if ($stat->{status
} eq 'completed') {
457 my $delay = time() - $start;
459 my $mbps = sprintf "%.2f", $conf->{memory
}/$delay;
460 my $downtime = $stat->{downtime
} || 0;
461 $self->log('info', "migration speed: $mbps MB/s - downtime $downtime ms");
465 if ($stat->{status
} eq 'failed' || $stat->{status
} eq 'cancelled') {
469 if ($stat->{status
} ne 'active') {
470 $self->log('info', "migration status: $stat->{status}");
474 if ($stat->{ram
}->{transferred
} ne $lstat) {
475 my $trans = $stat->{ram
}->{transferred
} || 0;
476 my $rem = $stat->{ram
}->{remaining
} || 0;
477 my $total = $stat->{ram
}->{total
} || 0;
478 my $xbzrlecachesize = $stat->{"xbzrle-cache"}->{"cache-size"} || 0;
479 my $xbzrlebytes = $stat->{"xbzrle-cache"}->{"bytes"} || 0;
480 my $xbzrlepages = $stat->{"xbzrle-cache"}->{"pages"} || 0;
481 my $xbzrlecachemiss = $stat->{"xbzrle-cache"}->{"cache-miss"} || 0;
482 my $xbzrleoverflow = $stat->{"xbzrle-cache"}->{"overflow"} || 0;
483 #reduce sleep if remainig memory if lower than the everage transfert
484 $usleep = 300000 if $avglstat && $rem < $avglstat;
486 $self->log('info', "migration status: $stat->{status} (transferred ${trans}, " .
487 "remaining ${rem}), total ${total})");
489 #$self->log('info', "migration xbzrle cachesize: ${xbzrlecachesize} transferred ${xbzrlebytes} pages ${xbzrlepages} cachemiss ${xbzrlecachemiss} overflow ${xbzrleoverflow}");
490 if (($lastrem && $rem > $lastrem ) || ($rem == 0)) {
495 if ($downtimecounter > 5) {
496 $downtimecounter = 0;
497 $migrate_downtime *= 2;
498 $self->log('info', "migrate_set_downtime: $migrate_downtime");
500 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate_set_downtime", value
=> int($migrate_downtime*100)/100);
502 $self->log('info', "migrate_set_downtime error: $@") if $@;
508 $lstat = $stat->{ram
}->{transferred
};
512 die "unable to parse migration status '$stat->{status}' - aborting\n";
518 my ($self, $vmid, $err) = @_;
520 return if !$self->{errors
};
521 $self->{phase2errors
} = 1;
523 $self->log('info', "aborting phase 2 - cleanup resources");
525 $self->log('info', "migrate_cancel");
527 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate_cancel");
529 $self->log('info', "migrate_cancel error: $@") if $@;
531 my $conf = $self->{vmconf
};
532 delete $conf->{lock};
533 eval { PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1) };
535 $self->log('err', $err);
538 # cleanup ressources on target host
539 my $nodename = PVE
::INotify
::nodename
();
541 my $cmd = [@{$self->{rem_ssh
}}, 'qm', 'stop', $vmid, '--skiplock', '--migratedfrom', $nodename];
542 eval{ PVE
::Tools
::run_command
($cmd, outfunc
=> sub {}, errfunc
=> sub {}) };
544 $self->log('err', $err);
550 my ($self, $vmid) = @_;
552 my $volids = $self->{volumes
};
553 return if $self->{phase2errors
};
555 # destroy local copies
556 foreach my $volid (@$volids) {
557 eval { PVE
::Storage
::vdisk_free
($self->{storecfg
}, $volid); };
559 $self->log('err', "removing local copy of '$volid' failed - $err");
561 last if $err =~ /^interrupted by signal$/;
567 my ($self, $vmid, $err) = @_;
569 my $conf = $self->{vmconf
};
570 return if $self->{phase2errors
};
572 # move config to remote node
573 my $conffile = PVE
::QemuServer
::config_file
($vmid);
574 my $newconffile = PVE
::QemuServer
::config_file
($vmid, $self->{node
});
576 die "Failed to move config to node '$self->{node}' - rename failed: $!\n"
577 if !rename($conffile, $newconffile);
579 if ($self->{livemigration
}) {
580 # now that config file is move, we can resume vm on target if livemigrate
581 my $cmd = [@{$self->{rem_ssh
}}, 'qm', 'resume', $vmid, '--skiplock'];
582 eval{ PVE
::Tools
::run_command
($cmd, outfunc
=> sub {},
585 $self->log('err', $line);
589 $self->log('err', $err);
595 if (PVE
::QemuServer
::vga_conf_has_spice
($conf->{vga
})) {
596 $self->log('info', "Waiting for spice server migration");
598 my $res = PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, 'query-spice');
599 last if int($res->{'migrated'}) == 1;
606 # always stop local VM
607 eval { PVE
::QemuServer
::vm_stop
($self->{storecfg
}, $vmid, 1, 1); };
609 $self->log('err', "stopping vm failed - $err");
613 if ($self->{tunnel
}) {
614 eval { finish_tunnel
($self, $self->{tunnel
}); };
616 $self->log('err', $err);
621 # always deactivate volumes - avoid lvm LVs to be active on several nodes
623 my $vollist = PVE
::QemuServer
::get_vm_volumes
($conf);
624 PVE
::Storage
::deactivate_volumes
($self->{storecfg
}, $vollist);
627 $self->log('err', $err);
632 my $cmd = [ @{$self->{rem_ssh
}}, 'qm', 'unlock', $vmid ];
633 $self->cmd_logerr($cmd, errmsg
=> "failed to clear migrate lock");
637 my ($self, $vmid) = @_;