]>
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 = $lport ?
('-L' , "$lport:localhost:$rport" ) : ();
81 my $cmd = [@{$self->{rem_ssh
}}, @localtunnelinfo, 'qm', 'mtunnel' ];
83 my $tunnel = $self->fork_command_pipe($cmd);
85 my $reader = $tunnel->{reader
};
89 PVE
::Tools
::run_with_timeout
(60, sub { $helo = <$reader>; });
90 die "no reply\n" if !$helo;
91 die "no quorum on target node\n" if $helo =~ m/^no quorum$/;
92 die "got strange reply from mtunnel ('$helo')\n"
93 if $helo !~ m/^tunnel online$/;
98 $self->finish_command_pipe($tunnel);
99 die "can't open migration tunnel - $err";
105 my ($self, $tunnel) = @_;
107 my $writer = $tunnel->{writer
};
110 PVE
::Tools
::run_with_timeout
(30, sub {
111 print $writer "quit\n";
117 $self->finish_command_pipe($tunnel, 30);
123 my ($self, $vmid, $code, @param) = @_;
125 return PVE
::QemuServer
::lock_config
($vmid, $code, @param);
129 my ($self, $vmid) = @_;
131 my $online = $self->{opts
}->{online
};
133 $self->{storecfg
} = PVE
::Storage
::config
();
136 my $conf = $self->{vmconf
} = PVE
::QemuServer
::load_config
($vmid);
138 PVE
::QemuServer
::check_lock
($conf);
141 if (my $pid = PVE
::QemuServer
::check_running
($vmid)) {
142 die "cant migrate running VM without --online\n" if !$online;
144 $self->{forcemachine
} = PVE
::QemuServer
::get_current_qemu_machine
($vmid);
147 if (my $loc_res = PVE
::QemuServer
::check_local_resources
($conf, 1)) {
148 if ($self->{running
} || !$self->{opts
}->{force
}) {
149 die "can't migrate VM which uses local devices\n";
151 $self->log('info', "migrating VM which uses local devices");
156 my $vollist = PVE
::QemuServer
::get_vm_volumes
($conf);
157 PVE
::Storage
::activate_volumes
($self->{storecfg
}, $vollist);
159 # fixme: check if storage is available on both nodes
161 # test ssh connection
162 my $cmd = [ @{$self->{rem_ssh
}}, '/bin/true' ];
163 eval { $self->cmd_quiet($cmd); };
164 die "Can't connect to destination address using public key\n" if $@;
170 my ($self, $vmid) = @_;
172 $self->log('info', "copying disk images");
174 my $conf = $self->{vmconf
};
176 $self->{volumes
} = [];
187 my @sids = PVE
::Storage
::storage_ids
($self->{storecfg
});
188 foreach my $storeid (@sids) {
189 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $storeid);
190 next if $scfg->{shared
};
191 next if !PVE
::Storage
::storage_check_enabled
($self->{storecfg
}, $storeid, undef, 1);
193 # get list from PVE::Storage (for unused volumes)
194 my $dl = PVE
::Storage
::vdisk_list
($self->{storecfg
}, $storeid, $vmid);
195 PVE
::Storage
::foreach_volid
($dl, sub {
196 my ($volid, $sid, $volname) = @_;
198 # check if storage is available on target node
199 PVE
::Storage
::storage_check_node
($self->{storecfg
}, $sid, $self->{node
});
201 $volhash->{$volid} = 1;
202 $sharedvm = 0; # there is a non-shared disk
206 # and add used, owned/non-shared disks (just to be sure we have all)
208 PVE
::QemuServer
::foreach_volid
($conf, sub {
209 my ($volid, $is_cdrom) = @_;
213 die "cant migrate local file/device '$volid'\n" if $volid =~ m
|^/|;
216 die "cant migrate local cdrom drive\n" if $volid eq 'cdrom';
217 return if $volid eq 'none';
218 $cdromhash->{$volid} = 1;
221 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
223 # check if storage is available on both nodes
224 my $scfg = PVE
::Storage
::storage_check_node
($self->{storecfg
}, $sid);
225 PVE
::Storage
::storage_check_node
($self->{storecfg
}, $sid, $self->{node
});
227 return if $scfg->{shared
};
229 die "can't migrate local cdrom '$volid'\n" if $cdromhash->{$volid};
233 my ($path, $owner) = PVE
::Storage
::path
($self->{storecfg
}, $volid);
235 die "can't migrate volume '$volid' - owned by other VM (owner = VM $owner)\n"
236 if !$owner || ($owner != $self->{vmid
});
238 $volhash->{$volid} = 1;
241 if ($self->{running
} && !$sharedvm) {
242 die "can't do online migration - VM uses local disks\n";
245 # do some checks first
246 foreach my $volid (keys %$volhash) {
247 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
248 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $sid);
250 die "can't migrate '$volid' - storagy type '$scfg->{type}' not supported\n"
251 if $scfg->{type
} ne 'dir';
253 # if file, check if a backing file exist
254 if (($scfg->{type
} eq 'dir') && (!$sharedvm)) {
255 my (undef, undef, undef, $parent) = PVE
::Storage
::volume_size_info
($self->{storecfg
}, $volid, 1);
256 die "can't migrate '$volid' as it's a clone of '$parent'" if $parent;
260 foreach my $volid (keys %$volhash) {
261 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
262 push @{$self->{volumes
}}, $volid;
263 PVE
::Storage
::storage_migrate
($self->{storecfg
}, $volid, $self->{nodeip
}, $sid);
266 die "Failed to sync data - $@" if $@;
270 my ($self, $vmid) = @_;
272 $self->log('info', "starting migration of VM $vmid to node '$self->{node}' ($self->{nodeip})");
274 my $conf = $self->{vmconf
};
276 # set migrate lock in config file
277 $conf->{lock} = 'migrate';
278 PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1);
280 sync_disks
($self, $vmid);
285 my ($self, $vmid, $err) = @_;
287 $self->log('info', "aborting phase 1 - cleanup resources");
289 my $conf = $self->{vmconf
};
290 delete $conf->{lock};
291 eval { PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1) };
293 $self->log('err', $err);
296 if ($self->{volumes
}) {
297 foreach my $volid (@{$self->{volumes
}}) {
298 $self->log('err', "found stale volume copy '$volid' on node '$self->{node}'");
299 # fixme: try to remove ?
305 my ($self, $vmid) = @_;
307 my $conf = $self->{vmconf
};
309 $self->log('info', "starting VM $vmid on remote node '$self->{node}'");
313 my $nodename = PVE
::INotify
::nodename
();
315 ## start on remote node
316 my $cmd = [@{$self->{rem_ssh
}}];
319 if (PVE
::QemuServer
::vga_conf_has_spice
($conf->{vga
})) {
320 my $res = PVE
::QemuServer
::vm_mon_cmd
($vmid, 'query-spice');
321 $spice_ticket = $res->{ticket
};
324 push @$cmd , 'qm', 'start', $vmid, '--stateuri', 'tcp', '--skiplock', '--migratedfrom', $nodename;
326 if ($self->{forcemachine
}) {
327 push @$cmd, '--machine', $self->{forcemachine
};
332 # Note: We try to keep $spice_ticket secret (do not pass via command line parameter)
333 # instead we pipe it through STDIN
334 PVE
::Tools
::run_command
($cmd, input
=> $spice_ticket, outfunc
=> sub {
337 if ($line =~ m/^migration listens on tcp:([\d\.]+|localhost):(\d+)$/) {
341 elsif ($line =~ m/^migration listens on port (\d+)$/) {
342 $raddr = "localhost";
345 elsif ($line =~ m/^spice listens on port (\d+)$/) {
346 $spice_port = int($1);
350 $self->log('info', $line);
353 die "unable to detect remote migration address\n" if !$raddr;
355 ## create tunnel to remote port
356 $self->log('info', "starting ssh migration tunnel");
357 my $lport = ($raddr eq "localhost") ? PVE
::Tools
::next_migrate_port
() : undef;
358 $self->{tunnel
} = $self->fork_tunnel($self->{nodeip
}, $lport, $rport);
361 $self->log('info', "starting online/live migration on $raddr:$rport");
362 $self->{livemigration
} = 1;
365 my $defaults = PVE
::QemuServer
::load_defaults
();
367 # always set migrate speed (overwrite kvm default of 32m)
368 # we set a very hight default of 8192m which is basically unlimited
369 my $migrate_speed = $defaults->{migrate_speed
} || 8192;
370 $migrate_speed = $conf->{migrate_speed
} || $migrate_speed;
371 $migrate_speed = $migrate_speed * 1048576;
372 $self->log('info', "migrate_set_speed: $migrate_speed");
374 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate_set_speed", value
=> int($migrate_speed));
376 $self->log('info', "migrate_set_speed error: $@") if $@;
378 my $migrate_downtime = $defaults->{migrate_downtime
};
379 $migrate_downtime = $conf->{migrate_downtime
} if defined($conf->{migrate_downtime
});
380 if (defined($migrate_downtime)) {
381 $self->log('info', "migrate_set_downtime: $migrate_downtime");
383 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate_set_downtime", value
=> int($migrate_downtime*100)/100);
385 $self->log('info', "migrate_set_downtime error: $@") if $@;
389 PVE
::QemuServer
::set_migration_caps
($vmid);
393 #set cachesize 10% of the total memory
394 my $cachesize = int($conf->{memory
}*1048576/10);
396 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate-set-cache-size", value
=> $cachesize);
399 if (PVE
::QemuServer
::vga_conf_has_spice
($conf->{vga
})) {
400 my $rpcenv = PVE
::RPCEnvironment
::get
();
401 my $authuser = $rpcenv->get_user();
403 my (undef, $proxyticket) = PVE
::AccessControl
::assemble_spice_ticket
($authuser, $vmid, $self->{node
});
405 my $filename = "/etc/pve/nodes/$self->{node}/pve-ssl.pem";
406 my $subject = PVE
::AccessControl
::read_x509_subject_spice
($filename);
408 $self->log('info', "spice client_migrate_info");
411 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "client_migrate_info", protocol
=> 'spice',
412 hostname
=> $proxyticket, 'tls-port' => $spice_port,
413 'cert-subject' => $subject);
415 $self->log('info', "client_migrate_info error: $@") if $@;
420 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate", uri
=> "tcp:$raddr:$rport");
423 $self->log('info', "migrate uri => tcp:$raddr:$rport failed: $merr") if $merr;
426 my $usleep = 2000000;
430 my $downtimecounter = 0;
433 my $avglstat = $lstat/$i if $lstat;
438 $stat = PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "query-migrate");
442 warn "query migrate failed: $err\n";
443 if ($err_count <= 5) {
447 die "too many query migrate failures - aborting\n";
450 if ($stat->{status
} =~ m/^(setup)$/im) {
455 if ($stat->{status
} =~ m/^(active|completed|failed|cancelled)$/im) {
458 if ($stat->{status
} eq 'completed') {
459 my $delay = time() - $start;
461 my $mbps = sprintf "%.2f", $conf->{memory
}/$delay;
462 my $downtime = $stat->{downtime
} || 0;
463 $self->log('info', "migration speed: $mbps MB/s - downtime $downtime ms");
467 if ($stat->{status
} eq 'failed' || $stat->{status
} eq 'cancelled') {
471 if ($stat->{status
} ne 'active') {
472 $self->log('info', "migration status: $stat->{status}");
476 if ($stat->{ram
}->{transferred
} ne $lstat) {
477 my $trans = $stat->{ram
}->{transferred
} || 0;
478 my $rem = $stat->{ram
}->{remaining
} || 0;
479 my $total = $stat->{ram
}->{total
} || 0;
480 my $xbzrlecachesize = $stat->{"xbzrle-cache"}->{"cache-size"} || 0;
481 my $xbzrlebytes = $stat->{"xbzrle-cache"}->{"bytes"} || 0;
482 my $xbzrlepages = $stat->{"xbzrle-cache"}->{"pages"} || 0;
483 my $xbzrlecachemiss = $stat->{"xbzrle-cache"}->{"cache-miss"} || 0;
484 my $xbzrleoverflow = $stat->{"xbzrle-cache"}->{"overflow"} || 0;
485 #reduce sleep if remainig memory if lower than the everage transfert
486 $usleep = 300000 if $avglstat && $rem < $avglstat;
488 $self->log('info', "migration status: $stat->{status} (transferred ${trans}, " .
489 "remaining ${rem}), total ${total})");
491 if (${xbzrlecachesize
}) {
492 $self->log('info', "migration xbzrle cachesize: ${xbzrlecachesize} transferred ${xbzrlebytes} pages ${xbzrlepages} cachemiss ${xbzrlecachemiss} overflow ${xbzrleoverflow}");
495 if (($lastrem && $rem > $lastrem ) || ($rem == 0)) {
500 if ($downtimecounter > 5) {
501 $downtimecounter = 0;
502 $migrate_downtime *= 2;
503 $self->log('info', "migrate_set_downtime: $migrate_downtime");
505 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate_set_downtime", value
=> int($migrate_downtime*100)/100);
507 $self->log('info', "migrate_set_downtime error: $@") if $@;
513 $lstat = $stat->{ram
}->{transferred
};
517 die "unable to parse migration status '$stat->{status}' - aborting\n";
523 my ($self, $vmid, $err) = @_;
525 return if !$self->{errors
};
526 $self->{phase2errors
} = 1;
528 $self->log('info', "aborting phase 2 - cleanup resources");
530 $self->log('info', "migrate_cancel");
532 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate_cancel");
534 $self->log('info', "migrate_cancel error: $@") if $@;
536 my $conf = $self->{vmconf
};
537 delete $conf->{lock};
538 eval { PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1) };
540 $self->log('err', $err);
543 # cleanup ressources on target host
544 my $nodename = PVE
::INotify
::nodename
();
546 my $cmd = [@{$self->{rem_ssh
}}, 'qm', 'stop', $vmid, '--skiplock', '--migratedfrom', $nodename];
547 eval{ PVE
::Tools
::run_command
($cmd, outfunc
=> sub {}, errfunc
=> sub {}) };
549 $self->log('err', $err);
555 my ($self, $vmid) = @_;
557 my $volids = $self->{volumes
};
558 return if $self->{phase2errors
};
560 # destroy local copies
561 foreach my $volid (@$volids) {
562 eval { PVE
::Storage
::vdisk_free
($self->{storecfg
}, $volid); };
564 $self->log('err', "removing local copy of '$volid' failed - $err");
566 last if $err =~ /^interrupted by signal$/;
572 my ($self, $vmid, $err) = @_;
574 my $conf = $self->{vmconf
};
575 return if $self->{phase2errors
};
577 # move config to remote node
578 my $conffile = PVE
::QemuServer
::config_file
($vmid);
579 my $newconffile = PVE
::QemuServer
::config_file
($vmid, $self->{node
});
581 die "Failed to move config to node '$self->{node}' - rename failed: $!\n"
582 if !rename($conffile, $newconffile);
584 if ($self->{livemigration
}) {
585 # now that config file is move, we can resume vm on target if livemigrate
586 my $cmd = [@{$self->{rem_ssh
}}, 'qm', 'resume', $vmid, '--skiplock'];
587 eval{ PVE
::Tools
::run_command
($cmd, outfunc
=> sub {},
590 $self->log('err', $line);
594 $self->log('err', $err);
602 if (PVE
::QemuServer
::vga_conf_has_spice
($conf->{vga
}) && $self->{running
}) {
603 $self->log('info', "Waiting for spice server migration");
605 my $res = PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, 'query-spice');
606 last if int($res->{'migrated'}) == 1;
614 # always stop local VM
615 eval { PVE
::QemuServer
::vm_stop
($self->{storecfg
}, $vmid, 1, 1); };
617 $self->log('err', "stopping vm failed - $err");
621 if ($self->{tunnel
}) {
622 eval { finish_tunnel
($self, $self->{tunnel
}); };
624 $self->log('err', $err);
629 # always deactivate volumes - avoid lvm LVs to be active on several nodes
631 my $vollist = PVE
::QemuServer
::get_vm_volumes
($conf);
632 PVE
::Storage
::deactivate_volumes
($self->{storecfg
}, $vollist);
635 $self->log('err', $err);
640 my $cmd = [ @{$self->{rem_ssh
}}, 'qm', 'unlock', $vmid ];
641 $self->cmd_logerr($cmd, errmsg
=> "failed to clear migrate lock");
645 my ($self, $vmid) = @_;