]>
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
}}];
317 if (PVE
::QemuServer
::vga_conf_has_spice
($conf->{vga
})) {
318 my $res = PVE
::QemuServer
::vm_mon_cmd
($vmid, 'query-spice');
319 push @$cmd, 'SPICETICKET='.$res->{ticket
} if $res->{ticket
};
322 push @$cmd , 'qm', 'start', $vmid, '--stateuri', 'tcp', '--skiplock', '--migratedfrom', $nodename;
325 if ($self->{forcemachine
}) {
326 push @$cmd, '--machine', $self->{forcemachine
};
331 PVE
::Tools
::run_command
($cmd, outfunc
=> sub {
334 if ($line =~ m/^migration listens on port (\d+)$/) {
336 }elsif ($line =~ m/^spice listens on port (\d+)$/) {
337 $spice_port = int($1);
341 $self->log('info', $line);
344 die "unable to detect remote migration port\n" if !$rport;
346 $self->log('info', "starting migration tunnel");
348 ## create tunnel to remote port
349 my $lport = PVE
::Tools
::next_migrate_port
();
350 $self->{tunnel
} = $self->fork_tunnel($self->{nodeip
}, $lport, $rport);
352 $self->log('info', "starting online/live migration on port $lport");
358 my $defaults = PVE
::QemuServer
::load_defaults
();
360 # always set migrate speed (overwrite kvm default of 32m)
361 # we set a very hight default of 8192m which is basically unlimited
362 my $migrate_speed = $defaults->{migrate_speed
} || 8192;
363 $migrate_speed = $conf->{migrate_speed
} || $migrate_speed;
364 $migrate_speed = $migrate_speed * 1048576;
365 $self->log('info', "migrate_set_speed: $migrate_speed");
367 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate_set_speed", value
=> int($migrate_speed));
369 $self->log('info', "migrate_set_speed error: $@") if $@;
371 my $migrate_downtime = $defaults->{migrate_downtime
};
372 $migrate_downtime = $conf->{migrate_downtime
} if defined($conf->{migrate_downtime
});
373 if (defined($migrate_downtime)) {
374 $self->log('info', "migrate_set_downtime: $migrate_downtime");
376 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate_set_downtime", value
=> int($migrate_downtime*100)/100);
378 $self->log('info', "migrate_set_downtime error: $@") if $@;
381 my $capabilities = {};
382 $capabilities->{capability
} = "xbzrle";
383 $capabilities->{state} = JSON
::false
;
386 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate-set-capabilities", capabilities
=> [$capabilities]);
389 #set cachesize 10% of the total memory
390 my $cachesize = int($conf->{memory
}*1048576/10);
392 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate-set-cache-size", value
=> $cachesize);
395 if (PVE
::QemuServer
::vga_conf_has_spice
($conf->{vga
})) {
396 my $rpcenv = PVE
::RPCEnvironment
::get
();
397 my $authuser = $rpcenv->get_user();
399 my (undef, $proxyticket) = PVE
::AccessControl
::assemble_spice_ticket
($authuser, $vmid, $self->{node
});
401 my $filename = "/etc/pve/nodes/$self->{node}/pve-ssl.pem";
402 my $subject = PVE
::QemuServer
::read_x509_subject_spice
($filename);
404 $self->log('info', "spice client_migrate_info");
407 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "client_migrate_info", protocol
=> 'spice',
408 hostname
=> $proxyticket, 'tls-port' => $spice_port,
409 'cert-subject' => $subject);
411 $self->log('info', "client_migrate_info error: $@") if $@;
416 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate", uri
=> "tcp:localhost:$lport");
421 my $usleep = 2000000;
425 my $downtimecounter = 0;
428 my $avglstat = $lstat/$i if $lstat;
433 $stat = PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "query-migrate");
437 warn "query migrate failed: $err\n";
438 if ($err_count <= 5) {
442 die "too many query migrate failures - aborting\n";
444 if ($stat->{status
} =~ m/^(active|completed|failed|cancelled)$/im) {
447 if ($stat->{status
} eq 'completed') {
448 my $delay = time() - $start;
450 my $mbps = sprintf "%.2f", $conf->{memory
}/$delay;
451 my $downtime = $stat->{downtime
} || 0;
452 $self->log('info', "migration speed: $mbps MB/s - downtime $downtime ms");
456 if ($stat->{status
} eq 'failed' || $stat->{status
} eq 'cancelled') {
460 if ($stat->{status
} ne 'active') {
461 $self->log('info', "migration status: $stat->{status}");
465 if ($stat->{ram
}->{transferred
} ne $lstat) {
466 my $trans = $stat->{ram
}->{transferred
} || 0;
467 my $rem = $stat->{ram
}->{remaining
} || 0;
468 my $total = $stat->{ram
}->{total
} || 0;
469 my $xbzrlecachesize = $stat->{"xbzrle-cache"}->{"cache-size"} || 0;
470 my $xbzrlebytes = $stat->{"xbzrle-cache"}->{"bytes"} || 0;
471 my $xbzrlepages = $stat->{"xbzrle-cache"}->{"pages"} || 0;
472 my $xbzrlecachemiss = $stat->{"xbzrle-cache"}->{"cache-miss"} || 0;
473 my $xbzrleoverflow = $stat->{"xbzrle-cache"}->{"overflow"} || 0;
474 #reduce sleep if remainig memory if lower than the everage transfert
475 $usleep = 300000 if $avglstat && $rem < $avglstat;
477 $self->log('info', "migration status: $stat->{status} (transferred ${trans}, " .
478 "remaining ${rem}), total ${total})");
480 #$self->log('info', "migration xbzrle cachesize: ${xbzrlecachesize} transferred ${xbzrlebytes} pages ${xbzrlepages} cachemiss ${xbzrlecachemiss} overflow ${xbzrleoverflow}");
481 if (($lastrem && $rem > $lastrem ) || ($rem == 0)) {
486 if ($downtimecounter > 5) {
487 $downtimecounter = 0;
488 $migrate_downtime *= 2;
489 $self->log('info', "migrate_set_downtime: $migrate_downtime");
491 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate_set_downtime", value
=> int($migrate_downtime*100)/100);
493 $self->log('info', "migrate_set_downtime error: $@") if $@;
499 $lstat = $stat->{ram
}->{transferred
};
503 die "unable to parse migration status '$stat->{status}' - aborting\n";
509 my ($self, $vmid, $err) = @_;
511 return if !$self->{errors
};
512 $self->{phase2errors
} = 1;
514 $self->log('info', "aborting phase 2 - cleanup resources");
516 $self->log('info', "migrate_cancel");
518 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate_cancel");
520 $self->log('info', "migrate_cancel error: $@") if $@;
522 my $conf = $self->{vmconf
};
523 delete $conf->{lock};
524 eval { PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1) };
526 $self->log('err', $err);
529 # cleanup ressources on target host
530 my $nodename = PVE
::INotify
::nodename
();
532 my $cmd = [@{$self->{rem_ssh
}}, 'qm', 'stop', $vmid, '--skiplock', '--migratedfrom', $nodename];
533 eval{ PVE
::Tools
::run_command
($cmd, outfunc
=> sub {}, errfunc
=> sub {}) };
535 $self->log('err', $err);
541 my ($self, $vmid) = @_;
543 my $volids = $self->{volumes
};
544 return if $self->{phase2errors
};
546 # destroy local copies
547 foreach my $volid (@$volids) {
548 eval { PVE
::Storage
::vdisk_free
($self->{storecfg
}, $volid); };
550 $self->log('err', "removing local copy of '$volid' failed - $err");
552 last if $err =~ /^interrupted by signal$/;
558 my ($self, $vmid, $err) = @_;
560 my $conf = $self->{vmconf
};
561 return if $self->{phase2errors
};
563 # move config to remote node
564 my $conffile = PVE
::QemuServer
::config_file
($vmid);
565 my $newconffile = PVE
::QemuServer
::config_file
($vmid, $self->{node
});
567 die "Failed to move config to node '$self->{node}' - rename failed: $!\n"
568 if !rename($conffile, $newconffile);
570 # now that config file is move, we can resume vm on target if livemigrate
571 if ($self->{tunnel
}) {
572 my $cmd = [@{$self->{rem_ssh
}}, 'qm', 'resume', $vmid, '--skiplock'];
573 eval{ PVE
::Tools
::run_command
($cmd, outfunc
=> sub {},
576 $self->log('err', $line);
580 $self->log('err', $err);
586 if (PVE
::QemuServer
::vga_conf_has_spice
($conf->{vga
})) {
587 $self->log('info', "Waiting for spice server migration");
589 my $res = PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, 'query-spice');
590 last if int($res->{'migrated'}) == 1;
597 # always stop local VM
598 eval { PVE
::QemuServer
::vm_stop
($self->{storecfg
}, $vmid, 1, 1); };
600 $self->log('err', "stopping vm failed - $err");
604 if ($self->{tunnel
}) {
605 eval { finish_tunnel
($self, $self->{tunnel
}); };
607 $self->log('err', $err);
612 # always deactivate volumes - avoid lvm LVs to be active on several nodes
614 my $vollist = PVE
::QemuServer
::get_vm_volumes
($conf);
615 PVE
::Storage
::deactivate_volumes
($self->{storecfg
}, $vollist);
618 $self->log('err', $err);
623 my $cmd = [ @{$self->{rem_ssh
}}, 'qm', 'unlock', $vmid ];
624 $self->cmd_logerr($cmd, errmsg
=> "failed to clear migrate lock");
628 my ($self, $vmid) = @_;