]>
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
::QemuConfig-
>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
::QemuConfig-
>load_config($vmid);
138 PVE
::QemuConfig-
>check_lock($conf);
141 if (my $pid = PVE
::QemuServer
::check_running
($vmid)) {
142 die "cant migrate running VM without --online\n" if !$online;
145 $self->{forcemachine
} = PVE
::QemuServer
::qemu_machine_pxe
($vmid, $conf);
149 if (my $loc_res = PVE
::QemuServer
::check_local_resources
($conf, 1)) {
150 if ($self->{running
} || !$self->{opts
}->{force
}) {
151 die "can't migrate VM which uses local devices\n";
153 $self->log('info', "migrating VM which uses local devices");
157 my $vollist = PVE
::QemuServer
::get_vm_volumes
($conf);
159 my $need_activate = [];
160 foreach my $volid (@$vollist) {
161 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
163 # check if storage is available on both nodes
164 my $scfg = PVE
::Storage
::storage_check_node
($self->{storecfg
}, $sid);
165 PVE
::Storage
::storage_check_node
($self->{storecfg
}, $sid, $self->{node
});
167 if ($scfg->{shared
}) {
168 # PVE::Storage::activate_storage checks this for non-shared storages
169 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
170 warn "Used shared storage '$sid' is not online on source node!\n"
171 if !$plugin->check_connection($sid, $scfg);
173 # only activate if not shared
174 push @$need_activate, $volid;
179 PVE
::Storage
::activate_volumes
($self->{storecfg
}, $need_activate);
181 # test ssh connection
182 my $cmd = [ @{$self->{rem_ssh
}}, '/bin/true' ];
183 eval { $self->cmd_quiet($cmd); };
184 die "Can't connect to destination address using public key\n" if $@;
190 my ($self, $vmid) = @_;
192 $self->log('info', "copying disk images");
194 my $conf = $self->{vmconf
};
196 $self->{volumes
} = [];
207 my @sids = PVE
::Storage
::storage_ids
($self->{storecfg
});
208 foreach my $storeid (@sids) {
209 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $storeid);
210 next if $scfg->{shared
};
211 next if !PVE
::Storage
::storage_check_enabled
($self->{storecfg
}, $storeid, undef, 1);
213 # get list from PVE::Storage (for unused volumes)
214 my $dl = PVE
::Storage
::vdisk_list
($self->{storecfg
}, $storeid, $vmid);
215 PVE
::Storage
::foreach_volid
($dl, sub {
216 my ($volid, $sid, $volname) = @_;
218 # check if storage is available on target node
219 PVE
::Storage
::storage_check_node
($self->{storecfg
}, $sid, $self->{node
});
221 $volhash->{$volid} = 1;
222 $sharedvm = 0; # there is a non-shared disk
226 # and add used, owned/non-shared disks (just to be sure we have all)
228 PVE
::QemuServer
::foreach_volid
($conf, sub {
229 my ($volid, $is_cdrom) = @_;
233 die "cant migrate local file/device '$volid'\n" if $volid =~ m
|^/|;
236 die "cant migrate local cdrom drive\n" if $volid eq 'cdrom';
237 return if $volid eq 'none';
238 $cdromhash->{$volid} = 1;
241 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
243 # check if storage is available on both nodes
244 my $scfg = PVE
::Storage
::storage_check_node
($self->{storecfg
}, $sid);
245 PVE
::Storage
::storage_check_node
($self->{storecfg
}, $sid, $self->{node
});
247 return if $scfg->{shared
};
249 die "can't migrate local cdrom '$volid'\n" if $cdromhash->{$volid};
253 my ($path, $owner) = PVE
::Storage
::path
($self->{storecfg
}, $volid);
255 die "can't migrate volume '$volid' - owned by other VM (owner = VM $owner)\n"
256 if !$owner || ($owner != $self->{vmid
});
258 $volhash->{$volid} = 1;
261 if ($self->{running
} && !$sharedvm) {
262 die "can't do online migration - VM uses local disks\n";
265 # do some checks first
266 foreach my $volid (keys %$volhash) {
267 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
268 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $sid);
270 die "can't migrate '$volid' - storage type '$scfg->{type}' not supported\n"
271 if (!($scfg->{type
} eq 'dir' || $scfg->{type
} eq 'zfspool') && (!$sharedvm));
273 # if file, check if a backing file exist
274 if (!($scfg->{type
} eq 'dir' || $scfg->{type
} eq 'zfspool') && (!$sharedvm)) {
275 my (undef, undef, undef, $parent) = PVE
::Storage
::volume_size_info
($self->{storecfg
}, $volid, 1);
276 die "can't migrate '$volid' as it's a clone of '$parent'" if $parent;
280 foreach my $volid (keys %$volhash) {
281 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
282 push @{$self->{volumes
}}, $volid;
283 PVE
::Storage
::storage_migrate
($self->{storecfg
}, $volid, $self->{nodeip
}, $sid);
286 die "Failed to sync data - $@" if $@;
290 my ($self, $vmid) = @_;
292 $self->log('info', "starting migration of VM $vmid to node '$self->{node}' ($self->{nodeip})");
294 my $conf = $self->{vmconf
};
296 # set migrate lock in config file
297 $conf->{lock} = 'migrate';
298 PVE
::QemuConfig-
>write_config($vmid, $conf);
300 sync_disks
($self, $vmid);
305 my ($self, $vmid, $err) = @_;
307 $self->log('info', "aborting phase 1 - cleanup resources");
309 my $conf = $self->{vmconf
};
310 delete $conf->{lock};
311 eval { PVE
::QemuConfig-
>write_config($vmid, $conf) };
313 $self->log('err', $err);
316 if ($self->{volumes
}) {
317 foreach my $volid (@{$self->{volumes
}}) {
318 $self->log('err', "found stale volume copy '$volid' on node '$self->{node}'");
319 # fixme: try to remove ?
325 my ($self, $vmid) = @_;
327 my $conf = $self->{vmconf
};
329 $self->log('info', "starting VM $vmid on remote node '$self->{node}'");
333 my $nodename = PVE
::INotify
::nodename
();
335 ## start on remote node
336 my $cmd = [@{$self->{rem_ssh
}}];
339 if (PVE
::QemuServer
::vga_conf_has_spice
($conf->{vga
})) {
340 my $res = PVE
::QemuServer
::vm_mon_cmd
($vmid, 'query-spice');
341 $spice_ticket = $res->{ticket
};
344 push @$cmd , 'qm', 'start', $vmid, '--stateuri', 'tcp', '--skiplock', '--migratedfrom', $nodename;
346 if ($self->{forcemachine
}) {
347 push @$cmd, '--machine', $self->{forcemachine
};
352 # Note: We try to keep $spice_ticket secret (do not pass via command line parameter)
353 # instead we pipe it through STDIN
354 PVE
::Tools
::run_command
($cmd, input
=> $spice_ticket, outfunc
=> sub {
357 if ($line =~ m/^migration listens on tcp:(localhost|[\d\.]+|\[[\d\.:a-fA-F]+\]):(\d+)$/) {
361 elsif ($line =~ m/^migration listens on port (\d+)$/) {
362 $raddr = "localhost";
365 elsif ($line =~ m/^spice listens on port (\d+)$/) {
366 $spice_port = int($1);
370 $self->log('info', $line);
373 die "unable to detect remote migration address\n" if !$raddr;
375 ## create tunnel to remote port
376 $self->log('info', "starting ssh migration tunnel");
377 my $pfamily = PVE
::Tools
::get_host_address_family
($nodename);
378 my $lport = ($raddr eq "localhost") ? PVE
::Tools
::next_migrate_port
($pfamily) : undef;
379 $self->{tunnel
} = $self->fork_tunnel($self->{nodeip
}, $lport, $rport);
382 $self->log('info', "starting online/live migration on $raddr:$rport");
383 $self->{livemigration
} = 1;
386 my $defaults = PVE
::QemuServer
::load_defaults
();
388 # always set migrate speed (overwrite kvm default of 32m)
389 # we set a very hight default of 8192m which is basically unlimited
390 my $migrate_speed = $defaults->{migrate_speed
} || 8192;
391 $migrate_speed = $conf->{migrate_speed
} || $migrate_speed;
392 $migrate_speed = $migrate_speed * 1048576;
393 $self->log('info', "migrate_set_speed: $migrate_speed");
395 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate_set_speed", value
=> int($migrate_speed));
397 $self->log('info', "migrate_set_speed error: $@") if $@;
399 my $migrate_downtime = $defaults->{migrate_downtime
};
400 $migrate_downtime = $conf->{migrate_downtime
} if defined($conf->{migrate_downtime
});
401 if (defined($migrate_downtime)) {
402 $self->log('info', "migrate_set_downtime: $migrate_downtime");
404 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate_set_downtime", value
=> int($migrate_downtime*100)/100);
406 $self->log('info', "migrate_set_downtime error: $@") if $@;
410 PVE
::QemuServer
::set_migration_caps
($vmid);
414 #set cachesize 10% of the total memory
415 my $cachesize = int($conf->{memory
}*1048576/10);
417 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate-set-cache-size", value
=> $cachesize);
420 if (PVE
::QemuServer
::vga_conf_has_spice
($conf->{vga
})) {
421 my $rpcenv = PVE
::RPCEnvironment
::get
();
422 my $authuser = $rpcenv->get_user();
424 my (undef, $proxyticket) = PVE
::AccessControl
::assemble_spice_ticket
($authuser, $vmid, $self->{node
});
426 my $filename = "/etc/pve/nodes/$self->{node}/pve-ssl.pem";
427 my $subject = PVE
::AccessControl
::read_x509_subject_spice
($filename);
429 $self->log('info', "spice client_migrate_info");
432 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "client_migrate_info", protocol
=> 'spice',
433 hostname
=> $proxyticket, 'tls-port' => $spice_port,
434 'cert-subject' => $subject);
436 $self->log('info', "client_migrate_info error: $@") if $@;
441 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate", uri
=> "tcp:$raddr:$rport");
444 $self->log('info', "migrate uri => tcp:$raddr:$rport failed: $merr") if $merr;
447 my $usleep = 2000000;
451 my $downtimecounter = 0;
454 my $avglstat = $lstat/$i if $lstat;
459 $stat = PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "query-migrate");
463 warn "query migrate failed: $err\n";
464 if ($err_count <= 5) {
468 die "too many query migrate failures - aborting\n";
471 if ($stat->{status
} =~ m/^(setup)$/im) {
476 if ($stat->{status
} =~ m/^(active|completed|failed|cancelled)$/im) {
479 if ($stat->{status
} eq 'completed') {
480 my $delay = time() - $start;
482 my $mbps = sprintf "%.2f", $conf->{memory
}/$delay;
483 my $downtime = $stat->{downtime
} || 0;
484 $self->log('info', "migration speed: $mbps MB/s - downtime $downtime ms");
488 if ($stat->{status
} eq 'failed' || $stat->{status
} eq 'cancelled') {
492 if ($stat->{status
} ne 'active') {
493 $self->log('info', "migration status: $stat->{status}");
497 if ($stat->{ram
}->{transferred
} ne $lstat) {
498 my $trans = $stat->{ram
}->{transferred
} || 0;
499 my $rem = $stat->{ram
}->{remaining
} || 0;
500 my $total = $stat->{ram
}->{total
} || 0;
501 my $xbzrlecachesize = $stat->{"xbzrle-cache"}->{"cache-size"} || 0;
502 my $xbzrlebytes = $stat->{"xbzrle-cache"}->{"bytes"} || 0;
503 my $xbzrlepages = $stat->{"xbzrle-cache"}->{"pages"} || 0;
504 my $xbzrlecachemiss = $stat->{"xbzrle-cache"}->{"cache-miss"} || 0;
505 my $xbzrleoverflow = $stat->{"xbzrle-cache"}->{"overflow"} || 0;
506 #reduce sleep if remainig memory if lower than the everage transfert
507 $usleep = 300000 if $avglstat && $rem < $avglstat;
509 $self->log('info', "migration status: $stat->{status} (transferred ${trans}, " .
510 "remaining ${rem}), total ${total})");
512 if (${xbzrlecachesize
}) {
513 $self->log('info', "migration xbzrle cachesize: ${xbzrlecachesize} transferred ${xbzrlebytes} pages ${xbzrlepages} cachemiss ${xbzrlecachemiss} overflow ${xbzrleoverflow}");
516 if (($lastrem && $rem > $lastrem ) || ($rem == 0)) {
521 if ($downtimecounter > 5) {
522 $downtimecounter = 0;
523 $migrate_downtime *= 2;
524 $self->log('info', "migrate_set_downtime: $migrate_downtime");
526 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate_set_downtime", value
=> int($migrate_downtime*100)/100);
528 $self->log('info', "migrate_set_downtime error: $@") if $@;
534 $lstat = $stat->{ram
}->{transferred
};
538 die "unable to parse migration status '$stat->{status}' - aborting\n";
541 #to be sure tat the tunnel is closed
542 if ($self->{tunnel
}) {
543 eval { finish_tunnel
($self, $self->{tunnel
}); };
545 $self->log('err', $err);
552 my ($self, $vmid, $err) = @_;
554 return if !$self->{errors
};
555 $self->{phase2errors
} = 1;
557 $self->log('info', "aborting phase 2 - cleanup resources");
559 $self->log('info', "migrate_cancel");
561 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate_cancel");
563 $self->log('info', "migrate_cancel error: $@") if $@;
565 my $conf = $self->{vmconf
};
566 delete $conf->{lock};
567 eval { PVE
::QemuConfig-
>write_config($vmid, $conf) };
569 $self->log('err', $err);
572 # cleanup ressources on target host
573 my $nodename = PVE
::INotify
::nodename
();
575 my $cmd = [@{$self->{rem_ssh
}}, 'qm', 'stop', $vmid, '--skiplock', '--migratedfrom', $nodename];
576 eval{ PVE
::Tools
::run_command
($cmd, outfunc
=> sub {}, errfunc
=> sub {}) };
578 $self->log('err', $err);
582 if ($self->{tunnel
}) {
583 eval { finish_tunnel
($self, $self->{tunnel
}); };
585 $self->log('err', $err);
592 my ($self, $vmid) = @_;
594 my $volids = $self->{volumes
};
595 return if $self->{phase2errors
};
597 # destroy local copies
598 foreach my $volid (@$volids) {
599 eval { PVE
::Storage
::vdisk_free
($self->{storecfg
}, $volid); };
601 $self->log('err', "removing local copy of '$volid' failed - $err");
603 last if $err =~ /^interrupted by signal$/;
609 my ($self, $vmid, $err) = @_;
611 my $conf = $self->{vmconf
};
612 return if $self->{phase2errors
};
614 # move config to remote node
615 my $conffile = PVE
::QemuConfig-
>config_file($vmid);
616 my $newconffile = PVE
::QemuConfig-
>config_file($vmid, $self->{node
});
618 die "Failed to move config to node '$self->{node}' - rename failed: $!\n"
619 if !rename($conffile, $newconffile);
621 if ($self->{livemigration
}) {
622 # now that config file is move, we can resume vm on target if livemigrate
623 my $cmd = [@{$self->{rem_ssh
}}, 'qm', 'resume', $vmid, '--skiplock', '--nocheck'];
624 eval{ PVE
::Tools
::run_command
($cmd, outfunc
=> sub {},
627 $self->log('err', $line);
631 $self->log('err', $err);
639 if (PVE
::QemuServer
::vga_conf_has_spice
($conf->{vga
}) && $self->{running
}) {
640 $self->log('info', "Waiting for spice server migration");
642 my $res = PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, 'query-spice');
643 last if int($res->{'migrated'}) == 1;
651 # always stop local VM
652 eval { PVE
::QemuServer
::vm_stop
($self->{storecfg
}, $vmid, 1, 1); };
654 $self->log('err', "stopping vm failed - $err");
658 # always deactivate volumes - avoid lvm LVs to be active on several nodes
660 my $vollist = PVE
::QemuServer
::get_vm_volumes
($conf);
661 PVE
::Storage
::deactivate_volumes
($self->{storecfg
}, $vollist);
664 $self->log('err', $err);
669 my $cmd = [ @{$self->{rem_ssh
}}, 'qm', 'unlock', $vmid ];
670 $self->cmd_logerr($cmd, errmsg
=> "failed to clear migrate lock");
674 my ($self, $vmid) = @_;