]>
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 $cmd = [@{$self->{rem_ssh
}}, '-L', "$lport:localhost:$rport",
82 my $tunnel = $self->fork_command_pipe($cmd);
84 my $reader = $tunnel->{reader
};
88 PVE
::Tools
::run_with_timeout
(60, sub { $helo = <$reader>; });
89 die "no reply\n" if !$helo;
90 die "no quorum on target node\n" if $helo =~ m/^no quorum$/;
91 die "got strange reply from mtunnel ('$helo')\n"
92 if $helo !~ m/^tunnel online$/;
97 $self->finish_command_pipe($tunnel);
98 die "can't open migration tunnel - $err";
104 my ($self, $tunnel) = @_;
106 my $writer = $tunnel->{writer
};
109 PVE
::Tools
::run_with_timeout
(30, sub {
110 print $writer "quit\n";
116 $self->finish_command_pipe($tunnel, 30);
122 my ($self, $vmid, $code, @param) = @_;
124 return PVE
::QemuServer
::lock_config
($vmid, $code, @param);
128 my ($self, $vmid) = @_;
130 my $online = $self->{opts
}->{online
};
132 $self->{storecfg
} = PVE
::Storage
::config
();
135 my $conf = $self->{vmconf
} = PVE
::QemuServer
::load_config
($vmid);
137 PVE
::QemuServer
::check_lock
($conf);
140 if (my $pid = PVE
::QemuServer
::check_running
($vmid)) {
141 die "cant migrate running VM without --online\n" if !$online;
143 $self->{forcemachine
} = PVE
::QemuServer
::get_current_qemu_machine
($vmid);
146 if (my $loc_res = PVE
::QemuServer
::check_local_resources
($conf, 1)) {
147 if ($self->{running
} || !$self->{opts
}->{force
}) {
148 die "can't migrate VM which uses local devices\n";
150 $self->log('info', "migrating VM which uses local devices");
155 my $vollist = PVE
::QemuServer
::get_vm_volumes
($conf);
156 PVE
::Storage
::activate_volumes
($self->{storecfg
}, $vollist);
158 # fixme: check if storage is available on both nodes
160 # test ssh connection
161 my $cmd = [ @{$self->{rem_ssh
}}, '/bin/true' ];
162 eval { $self->cmd_quiet($cmd); };
163 die "Can't connect to destination address using public key\n" if $@;
169 my ($self, $vmid) = @_;
171 $self->log('info', "copying disk images");
173 my $conf = $self->{vmconf
};
175 $self->{volumes
} = [];
186 my @sids = PVE
::Storage
::storage_ids
($self->{storecfg
});
187 foreach my $storeid (@sids) {
188 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $storeid);
189 next if $scfg->{shared
};
190 next if !PVE
::Storage
::storage_check_enabled
($self->{storecfg
}, $storeid, undef, 1);
192 # get list from PVE::Storage (for unused volumes)
193 my $dl = PVE
::Storage
::vdisk_list
($self->{storecfg
}, $storeid, $vmid);
194 PVE
::Storage
::foreach_volid
($dl, sub {
195 my ($volid, $sid, $volname) = @_;
197 # check if storage is available on target node
198 PVE
::Storage
::storage_check_node
($self->{storecfg
}, $sid, $self->{node
});
200 $volhash->{$volid} = 1;
201 $sharedvm = 0; # there is a non-shared disk
205 # and add used, owned/non-shared disks (just to be sure we have all)
207 PVE
::QemuServer
::foreach_volid
($conf, sub {
208 my ($volid, $is_cdrom) = @_;
212 die "cant migrate local file/device '$volid'\n" if $volid =~ m
|^/|;
215 die "cant migrate local cdrom drive\n" if $volid eq 'cdrom';
216 return if $volid eq 'none';
217 $cdromhash->{$volid} = 1;
220 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
222 # check if storage is available on both nodes
223 my $scfg = PVE
::Storage
::storage_check_node
($self->{storecfg
}, $sid);
224 PVE
::Storage
::storage_check_node
($self->{storecfg
}, $sid, $self->{node
});
226 return if $scfg->{shared
};
228 die "can't migrate local cdrom '$volid'\n" if $cdromhash->{$volid};
232 my ($path, $owner) = PVE
::Storage
::path
($self->{storecfg
}, $volid);
234 die "can't migrate volume '$volid' - owned by other VM (owner = VM $owner)\n"
235 if !$owner || ($owner != $self->{vmid
});
237 $volhash->{$volid} = 1;
240 if ($self->{running
} && !$sharedvm) {
241 die "can't do online migration - VM uses local disks\n";
244 # do some checks first
245 foreach my $volid (keys %$volhash) {
246 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
247 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $sid);
249 die "can't migrate '$volid' - storagy type '$scfg->{type}' not supported\n"
250 if $scfg->{type
} ne 'dir';
252 # if file, check if a backing file exist
253 if (($scfg->{type
} eq 'dir') && (!$sharedvm)) {
254 my (undef, undef, undef, $parent) = PVE
::Storage
::volume_size_info
($self->{storecfg
}, $volid, 1);
255 die "can't migrate '$volid' as it's a clone of '$parent'" if $parent;
259 foreach my $volid (keys %$volhash) {
260 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
261 push @{$self->{volumes
}}, $volid;
262 PVE
::Storage
::storage_migrate
($self->{storecfg
}, $volid, $self->{nodeip
}, $sid);
265 die "Failed to sync data - $@" if $@;
269 my ($self, $vmid) = @_;
271 $self->log('info', "starting migration of VM $vmid to node '$self->{node}' ($self->{nodeip})");
273 my $conf = $self->{vmconf
};
275 # set migrate lock in config file
276 $conf->{lock} = 'migrate';
277 PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1);
279 sync_disks
($self, $vmid);
284 my ($self, $vmid, $err) = @_;
286 $self->log('info', "aborting phase 1 - cleanup resources");
288 my $conf = $self->{vmconf
};
289 delete $conf->{lock};
290 eval { PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1) };
292 $self->log('err', $err);
295 if ($self->{volumes
}) {
296 foreach my $volid (@{$self->{volumes
}}) {
297 $self->log('err', "found stale volume copy '$volid' on node '$self->{node}'");
298 # fixme: try to remove ?
304 my ($self, $vmid) = @_;
306 my $conf = $self->{vmconf
};
308 $self->log('info', "starting VM $vmid on remote node '$self->{node}'");
312 my $nodename = PVE
::INotify
::nodename
();
314 ## start on remote node
315 my $cmd = [@{$self->{rem_ssh
}}];
318 if (PVE
::QemuServer
::vga_conf_has_spice
($conf->{vga
})) {
319 my $res = PVE
::QemuServer
::vm_mon_cmd
($vmid, 'query-spice');
320 $spice_ticket = $res->{ticket
};
323 push @$cmd , 'qm', 'start', $vmid, '--stateuri', 'tcp', '--skiplock', '--migratedfrom', $nodename;
325 if ($self->{forcemachine
}) {
326 push @$cmd, '--machine', $self->{forcemachine
};
331 # Note: We try to keep $spice_ticket secret (do not pass via command line parameter)
332 # instead we pipe it through STDIN
333 PVE
::Tools
::run_command
($cmd, input
=> $spice_ticket, outfunc
=> sub {
336 if ($line =~ m/^migration listens on port (\d+)$/) {
338 }elsif ($line =~ m/^spice listens on port (\d+)$/) {
339 $spice_port = int($1);
343 $self->log('info', $line);
346 die "unable to detect remote migration port\n" if !$rport;
348 $self->log('info', "starting migration tunnel");
350 ## create tunnel to remote port
351 my $lport = PVE
::Tools
::next_migrate_port
();
352 $self->{tunnel
} = $self->fork_tunnel($self->{nodeip
}, $lport, $rport);
354 $self->log('info', "starting online/live migration on port $lport");
360 my $defaults = PVE
::QemuServer
::load_defaults
();
362 # always set migrate speed (overwrite kvm default of 32m)
363 # we set a very hight default of 8192m which is basically unlimited
364 my $migrate_speed = $defaults->{migrate_speed
} || 8192;
365 $migrate_speed = $conf->{migrate_speed
} || $migrate_speed;
366 $migrate_speed = $migrate_speed * 1048576;
367 $self->log('info', "migrate_set_speed: $migrate_speed");
369 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate_set_speed", value
=> int($migrate_speed));
371 $self->log('info', "migrate_set_speed error: $@") if $@;
373 my $migrate_downtime = $defaults->{migrate_downtime
};
374 $migrate_downtime = $conf->{migrate_downtime
} if defined($conf->{migrate_downtime
});
375 if (defined($migrate_downtime)) {
376 $self->log('info', "migrate_set_downtime: $migrate_downtime");
378 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate_set_downtime", value
=> int($migrate_downtime*100)/100);
380 $self->log('info', "migrate_set_downtime error: $@") if $@;
383 my $capabilities = {};
384 $capabilities->{capability
} = "xbzrle";
385 $capabilities->{state} = JSON
::false
;
388 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate-set-capabilities", capabilities
=> [$capabilities]);
391 #set cachesize 10% of the total memory
392 my $cachesize = int($conf->{memory
}*1048576/10);
394 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate-set-cache-size", value
=> $cachesize);
397 if (PVE
::QemuServer
::vga_conf_has_spice
($conf->{vga
})) {
398 my $rpcenv = PVE
::RPCEnvironment
::get
();
399 my $authuser = $rpcenv->get_user();
401 my (undef, $proxyticket) = PVE
::AccessControl
::assemble_spice_ticket
($authuser, $vmid, $self->{node
});
403 my $filename = "/etc/pve/nodes/$self->{node}/pve-ssl.pem";
404 my $subject = PVE
::QemuServer
::read_x509_subject_spice
($filename);
406 $self->log('info', "spice client_migrate_info");
409 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "client_migrate_info", protocol
=> 'spice',
410 hostname
=> $proxyticket, 'tls-port' => $spice_port,
411 'cert-subject' => $subject);
413 $self->log('info', "client_migrate_info error: $@") if $@;
418 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate", uri
=> "tcp:localhost:$lport");
423 my $usleep = 2000000;
427 my $downtimecounter = 0;
430 my $avglstat = $lstat/$i if $lstat;
435 $stat = PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "query-migrate");
439 warn "query migrate failed: $err\n";
440 if ($err_count <= 5) {
444 die "too many query migrate failures - aborting\n";
446 if ($stat->{status
} =~ m/^(active|completed|failed|cancelled)$/im) {
449 if ($stat->{status
} eq 'completed') {
450 my $delay = time() - $start;
452 my $mbps = sprintf "%.2f", $conf->{memory
}/$delay;
453 my $downtime = $stat->{downtime
} || 0;
454 $self->log('info', "migration speed: $mbps MB/s - downtime $downtime ms");
458 if ($stat->{status
} eq 'failed' || $stat->{status
} eq 'cancelled') {
462 if ($stat->{status
} ne 'active') {
463 $self->log('info', "migration status: $stat->{status}");
467 if ($stat->{ram
}->{transferred
} ne $lstat) {
468 my $trans = $stat->{ram
}->{transferred
} || 0;
469 my $rem = $stat->{ram
}->{remaining
} || 0;
470 my $total = $stat->{ram
}->{total
} || 0;
471 my $xbzrlecachesize = $stat->{"xbzrle-cache"}->{"cache-size"} || 0;
472 my $xbzrlebytes = $stat->{"xbzrle-cache"}->{"bytes"} || 0;
473 my $xbzrlepages = $stat->{"xbzrle-cache"}->{"pages"} || 0;
474 my $xbzrlecachemiss = $stat->{"xbzrle-cache"}->{"cache-miss"} || 0;
475 my $xbzrleoverflow = $stat->{"xbzrle-cache"}->{"overflow"} || 0;
476 #reduce sleep if remainig memory if lower than the everage transfert
477 $usleep = 300000 if $avglstat && $rem < $avglstat;
479 $self->log('info', "migration status: $stat->{status} (transferred ${trans}, " .
480 "remaining ${rem}), total ${total})");
482 #$self->log('info', "migration xbzrle cachesize: ${xbzrlecachesize} transferred ${xbzrlebytes} pages ${xbzrlepages} cachemiss ${xbzrlecachemiss} overflow ${xbzrleoverflow}");
483 if (($lastrem && $rem > $lastrem ) || ($rem == 0)) {
488 if ($downtimecounter > 5) {
489 $downtimecounter = 0;
490 $migrate_downtime *= 2;
491 $self->log('info', "migrate_set_downtime: $migrate_downtime");
493 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate_set_downtime", value
=> int($migrate_downtime*100)/100);
495 $self->log('info', "migrate_set_downtime error: $@") if $@;
501 $lstat = $stat->{ram
}->{transferred
};
505 die "unable to parse migration status '$stat->{status}' - aborting\n";
511 my ($self, $vmid, $err) = @_;
513 return if !$self->{errors
};
514 $self->{phase2errors
} = 1;
516 $self->log('info', "aborting phase 2 - cleanup resources");
518 $self->log('info', "migrate_cancel");
520 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate_cancel");
522 $self->log('info', "migrate_cancel error: $@") if $@;
524 my $conf = $self->{vmconf
};
525 delete $conf->{lock};
526 eval { PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1) };
528 $self->log('err', $err);
531 # cleanup ressources on target host
532 my $nodename = PVE
::INotify
::nodename
();
534 my $cmd = [@{$self->{rem_ssh
}}, 'qm', 'stop', $vmid, '--skiplock', '--migratedfrom', $nodename];
535 eval{ PVE
::Tools
::run_command
($cmd, outfunc
=> sub {}, errfunc
=> sub {}) };
537 $self->log('err', $err);
543 my ($self, $vmid) = @_;
545 my $volids = $self->{volumes
};
546 return if $self->{phase2errors
};
548 # destroy local copies
549 foreach my $volid (@$volids) {
550 eval { PVE
::Storage
::vdisk_free
($self->{storecfg
}, $volid); };
552 $self->log('err', "removing local copy of '$volid' failed - $err");
554 last if $err =~ /^interrupted by signal$/;
560 my ($self, $vmid, $err) = @_;
562 my $conf = $self->{vmconf
};
563 return if $self->{phase2errors
};
565 # move config to remote node
566 my $conffile = PVE
::QemuServer
::config_file
($vmid);
567 my $newconffile = PVE
::QemuServer
::config_file
($vmid, $self->{node
});
569 die "Failed to move config to node '$self->{node}' - rename failed: $!\n"
570 if !rename($conffile, $newconffile);
572 # now that config file is move, we can resume vm on target if livemigrate
573 if ($self->{tunnel
}) {
574 my $cmd = [@{$self->{rem_ssh
}}, 'qm', 'resume', $vmid, '--skiplock'];
575 eval{ PVE
::Tools
::run_command
($cmd, outfunc
=> sub {},
578 $self->log('err', $line);
582 $self->log('err', $err);
588 if (PVE
::QemuServer
::vga_conf_has_spice
($conf->{vga
})) {
589 $self->log('info', "Waiting for spice server migration");
591 my $res = PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, 'query-spice');
592 last if int($res->{'migrated'}) == 1;
599 # always stop local VM
600 eval { PVE
::QemuServer
::vm_stop
($self->{storecfg
}, $vmid, 1, 1); };
602 $self->log('err', "stopping vm failed - $err");
606 if ($self->{tunnel
}) {
607 eval { finish_tunnel
($self, $self->{tunnel
}); };
609 $self->log('err', $err);
614 # always deactivate volumes - avoid lvm LVs to be active on several nodes
616 my $vollist = PVE
::QemuServer
::get_vm_volumes
($conf);
617 PVE
::Storage
::deactivate_volumes
($self->{storecfg
}, $vollist);
620 $self->log('err', $err);
625 my $cmd = [ @{$self->{rem_ssh
}}, 'qm', 'unlock', $vmid ];
626 $self->cmd_logerr($cmd, errmsg
=> "failed to clear migrate lock");
630 my ($self, $vmid) = @_;