]>
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 );
15 use base
qw(PVE::AbstractMigrate);
17 sub fork_command_pipe
{
18 my ($self, $cmd) = @_;
20 my $reader = IO
::File-
>new();
21 my $writer = IO
::File-
>new();
27 eval { $cpid = open2
($reader, $writer, @$cmd); };
32 if ($orig_pid != $$) {
33 $self->log('err', "can't fork command pipe\n");
40 return { writer
=> $writer, reader
=> $reader, pid
=> $cpid };
43 sub finish_command_pipe
{
44 my ($self, $cmdpipe, $timeout) = @_;
46 my $writer = $cmdpipe->{writer
};
47 my $reader = $cmdpipe->{reader
};
52 my $cpid = $cmdpipe->{pid
};
55 for (my $i = 0; $i < $timeout; $i++) {
56 return if !PVE
::ProcFSTools
::check_process_running
($cpid);
61 $self->log('info', "ssh tunnel still running - terminating now with SIGTERM\n");
65 for (my $i = 0; $i < 10; $i++) {
66 return if !PVE
::ProcFSTools
::check_process_running
($cpid);
70 $self->log('info', "ssh tunnel still running - terminating now with SIGKILL\n");
76 my ($self, $nodeip, $lport, $rport) = @_;
78 my $cmd = [@{$self->{rem_ssh
}}, '-L', "$lport:localhost:$rport",
81 my $tunnel = $self->fork_command_pipe($cmd);
83 my $reader = $tunnel->{reader
};
87 PVE
::Tools
::run_with_timeout
(60, sub { $helo = <$reader>; });
88 die "no reply\n" if !$helo;
89 die "no quorum on target node\n" if $helo =~ m/^no quorum$/;
90 die "got strange reply from mtunnel ('$helo')\n"
91 if $helo !~ m/^tunnel online$/;
96 $self->finish_command_pipe($tunnel);
97 die "can't open migration tunnel - $err";
103 my ($self, $tunnel) = @_;
105 my $writer = $tunnel->{writer
};
108 PVE
::Tools
::run_with_timeout
(30, sub {
109 print $writer "quit\n";
115 $self->finish_command_pipe($tunnel, 30);
121 my ($self, $vmid, $code, @param) = @_;
123 return PVE
::QemuServer
::lock_config
($vmid, $code, @param);
127 my ($self, $vmid) = @_;
129 my $online = $self->{opts
}->{online
};
131 $self->{storecfg
} = PVE
::Storage
::config
();
134 my $conf = $self->{vmconf
} = PVE
::QemuServer
::load_config
($vmid);
136 PVE
::QemuServer
::check_lock
($conf);
139 if (my $pid = PVE
::QemuServer
::check_running
($vmid)) {
140 die "cant migrate running VM without --online\n" if !$online;
144 if (my $loc_res = PVE
::QemuServer
::check_local_resources
($conf, 1)) {
145 if ($self->{running
} || !$self->{opts
}->{force
}) {
146 die "can't migrate VM which uses local devices\n";
148 $self->log('info', "migrating VM which uses local devices");
153 my $vollist = PVE
::QemuServer
::get_vm_volumes
($conf);
154 PVE
::Storage
::activate_volumes
($self->{storecfg
}, $vollist);
156 # fixme: check if storage is available on both nodes
158 # test ssh connection
159 my $cmd = [ @{$self->{rem_ssh
}}, '/bin/true' ];
160 eval { $self->cmd_quiet($cmd); };
161 die "Can't connect to destination address using public key\n" if $@;
167 my ($self, $vmid) = @_;
169 $self->log('info', "copying disk images");
171 my $conf = $self->{vmconf
};
173 $self->{volumes
} = [];
184 my @sids = PVE
::Storage
::storage_ids
($self->{storecfg
});
185 foreach my $storeid (@sids) {
186 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $storeid);
187 next if $scfg->{shared
};
188 next if !PVE
::Storage
::storage_check_enabled
($self->{storecfg
}, $storeid, undef, 1);
190 # get list from PVE::Storage (for unused volumes)
191 my $dl = PVE
::Storage
::vdisk_list
($self->{storecfg
}, $storeid, $vmid);
192 PVE
::Storage
::foreach_volid
($dl, sub {
193 my ($volid, $sid, $volname) = @_;
195 # check if storage is available on target node
196 PVE
::Storage
::storage_check_node
($self->{storecfg
}, $sid, $self->{node
});
198 $volhash->{$volid} = 1;
199 $sharedvm = 0; # there is a non-shared disk
203 # and add used, owned/non-shared disks (just to be sure we have all)
205 PVE
::QemuServer
::foreach_volid
($conf, sub {
206 my ($volid, $is_cdrom) = @_;
210 die "cant migrate local file/device '$volid'\n" if $volid =~ m
|^/|;
213 die "cant migrate local cdrom drive\n" if $volid eq 'cdrom';
214 return if $volid eq 'none';
215 $cdromhash->{$volid} = 1;
218 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
220 # check if storage is available on both nodes
221 my $scfg = PVE
::Storage
::storage_check_node
($self->{storecfg
}, $sid);
222 PVE
::Storage
::storage_check_node
($self->{storecfg
}, $sid, $self->{node
});
224 return if $scfg->{shared
};
226 die "can't migrate local cdrom '$volid'\n" if $cdromhash->{$volid};
230 my ($path, $owner) = PVE
::Storage
::path
($self->{storecfg
}, $volid);
232 die "can't migrate volume '$volid' - owned by other VM (owner = VM $owner)\n"
233 if !$owner || ($owner != $self->{vmid
});
235 $volhash->{$volid} = 1;
238 if ($self->{running
} && !$sharedvm) {
239 die "can't do online migration - VM uses local disks\n";
242 # do some checks first
243 foreach my $volid (keys %$volhash) {
244 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
245 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $sid);
247 die "can't migrate '$volid' - storagy type '$scfg->{type}' not supported\n"
248 if $scfg->{type
} ne 'dir';
250 # if file, check if a backing file exist
251 if (($scfg->{type
} eq 'dir') && (!$sharedvm)) {
252 my (undef, undef, undef, $parent) = PVE
::Storage
::volume_size_info
($self->{storecfg
}, $volid, 1);
253 die "can't migrate '$volid' as it's a clone of '$parent'" if $parent;
257 foreach my $volid (keys %$volhash) {
258 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
259 push @{$self->{volumes
}}, $volid;
260 PVE
::Storage
::storage_migrate
($self->{storecfg
}, $volid, $self->{nodeip
}, $sid);
263 die "Failed to sync data - $@" if $@;
267 my ($self, $vmid) = @_;
269 $self->log('info', "starting migration of VM $vmid to node '$self->{node}' ($self->{nodeip})");
271 my $conf = $self->{vmconf
};
273 # set migrate lock in config file
274 $conf->{lock} = 'migrate';
275 PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1);
277 sync_disks
($self, $vmid);
282 my ($self, $vmid, $err) = @_;
284 $self->log('info', "aborting phase 1 - cleanup resources");
286 my $conf = $self->{vmconf
};
287 delete $conf->{lock};
288 eval { PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1) };
290 $self->log('err', $err);
293 if ($self->{volumes
}) {
294 foreach my $volid (@{$self->{volumes
}}) {
295 $self->log('err', "found stale volume copy '$volid' on node '$self->{node}'");
296 # fixme: try to remove ?
302 my ($self, $vmid) = @_;
304 my $conf = $self->{vmconf
};
306 $self->log('info', "starting VM $vmid on remote node '$self->{node}'");
310 my $nodename = PVE
::INotify
::nodename
();
312 ## start on remote node
313 my $cmd = [@{$self->{rem_ssh
}}, 'qm', 'start',
314 $vmid, '--stateuri', 'tcp', '--skiplock', '--migratedfrom', $nodename];
316 PVE
::Tools
::run_command
($cmd, outfunc
=> sub {
319 if ($line =~ m/^migration listens on port (\d+)$/) {
324 $self->log('info', $line);
327 die "unable to detect remote migration port\n" if !$rport;
329 $self->log('info', "starting migration tunnel");
331 ## create tunnel to remote port
332 my $lport = PVE
::Tools
::next_migrate_port
();
333 $self->{tunnel
} = $self->fork_tunnel($self->{nodeip
}, $lport, $rport);
335 $self->log('info', "starting online/live migration on port $lport");
341 my $defaults = PVE
::QemuServer
::load_defaults
();
343 # always set migrate speed (overwrite kvm default of 32m)
344 # we set a very hight default of 8192m which is basically unlimited
345 my $migrate_speed = $defaults->{migrate_speed
} || 8192;
346 $migrate_speed = $conf->{migrate_speed
} || $migrate_speed;
347 $migrate_speed = $migrate_speed * 1048576;
348 $self->log('info', "migrate_set_speed: $migrate_speed");
350 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate_set_speed", value
=> int($migrate_speed));
352 $self->log('info', "migrate_set_speed error: $@") if $@;
354 my $migrate_downtime = $defaults->{migrate_downtime
};
355 $migrate_downtime = $conf->{migrate_downtime
} if defined($conf->{migrate_downtime
});
356 if (defined($migrate_downtime)) {
357 $self->log('info', "migrate_set_downtime: $migrate_downtime");
359 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate_set_downtime", value
=> int($migrate_downtime*100)/100);
361 $self->log('info', "migrate_set_downtime error: $@") if $@;
364 my $capabilities = {};
365 $capabilities->{capability
} = "xbzrle";
366 $capabilities->{state} = JSON
::false
;
369 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate-set-capabilities", capabilities
=> [$capabilities]);
372 #set cachesize 10% of the total memory
373 my $cachesize = int($conf->{memory
}*1048576/10);
375 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate-set-cache-size", value
=> $cachesize);
379 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate", uri
=> "tcp:localhost:$lport");
384 my $usleep = 2000000;
388 my $downtimecounter = 0;
391 my $avglstat = $lstat/$i if $lstat;
396 $stat = PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "query-migrate");
400 warn "query migrate failed: $err\n";
401 if ($err_count <= 5) {
405 die "too many query migrate failures - aborting\n";
407 if ($stat->{status
} =~ m/^(active|completed|failed|cancelled)$/im) {
410 if ($stat->{status
} eq 'completed') {
411 my $delay = time() - $start;
413 my $mbps = sprintf "%.2f", $conf->{memory
}/$delay;
414 my $downtime = $stat->{downtime
} || 0;
415 $self->log('info', "migration speed: $mbps MB/s - downtime $downtime ms");
419 if ($stat->{status
} eq 'failed' || $stat->{status
} eq 'cancelled') {
423 if ($stat->{status
} ne 'active') {
424 $self->log('info', "migration status: $stat->{status}");
428 if ($stat->{ram
}->{transferred
} ne $lstat) {
429 my $trans = $stat->{ram
}->{transferred
} || 0;
430 my $rem = $stat->{ram
}->{remaining
} || 0;
431 my $total = $stat->{ram
}->{total
} || 0;
432 my $xbzrlecachesize = $stat->{"xbzrle-cache"}->{"cache-size"} || 0;
433 my $xbzrlebytes = $stat->{"xbzrle-cache"}->{"bytes"} || 0;
434 my $xbzrlepages = $stat->{"xbzrle-cache"}->{"pages"} || 0;
435 my $xbzrlecachemiss = $stat->{"xbzrle-cache"}->{"cache-miss"} || 0;
436 my $xbzrleoverflow = $stat->{"xbzrle-cache"}->{"overflow"} || 0;
437 #reduce sleep if remainig memory if lower than the everage transfert
438 $usleep = 300000 if $avglstat && $rem < $avglstat;
440 $self->log('info', "migration status: $stat->{status} (transferred ${trans}, " .
441 "remaining ${rem}), total ${total})");
443 #$self->log('info', "migration xbzrle cachesize: ${xbzrlecachesize} transferred ${xbzrlebytes} pages ${xbzrlepages} cachemiss ${xbzrlecachemiss} overflow ${xbzrleoverflow}");
444 if (($lastrem && $rem > $lastrem ) || ($rem == 0)) {
449 if ($downtimecounter > 5) {
450 $downtimecounter = 0;
451 $migrate_downtime *= 2;
452 $self->log('info', "migrate_set_downtime: $migrate_downtime");
454 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate_set_downtime", value
=> int($migrate_downtime*100)/100);
456 $self->log('info', "migrate_set_downtime error: $@") if $@;
462 $lstat = $stat->{ram
}->{transferred
};
466 die "unable to parse migration status '$stat->{status}' - aborting\n";
472 my ($self, $vmid, $err) = @_;
474 return if !$self->{errors
};
475 $self->{phase2errors
} = 1;
477 $self->log('info', "aborting phase 2 - cleanup resources");
479 $self->log('info', "migrate_cancel");
481 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate_cancel");
483 $self->log('info', "migrate_cancel error: $@") if $@;
485 my $conf = $self->{vmconf
};
486 delete $conf->{lock};
487 eval { PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1) };
489 $self->log('err', $err);
492 # cleanup ressources on target host
493 my $nodename = PVE
::INotify
::nodename
();
495 my $cmd = [@{$self->{rem_ssh
}}, 'qm', 'stop', $vmid, '--skiplock', '--migratedfrom', $nodename];
496 eval{ PVE
::Tools
::run_command
($cmd, outfunc
=> sub {}, errfunc
=> sub {}) };
498 $self->log('err', $err);
504 my ($self, $vmid) = @_;
506 my $volids = $self->{volumes
};
507 return if $self->{phase2errors
};
509 # destroy local copies
510 foreach my $volid (@$volids) {
511 eval { PVE
::Storage
::vdisk_free
($self->{storecfg
}, $volid); };
513 $self->log('err', "removing local copy of '$volid' failed - $err");
515 last if $err =~ /^interrupted by signal$/;
521 my ($self, $vmid, $err) = @_;
523 my $conf = $self->{vmconf
};
524 return if $self->{phase2errors
};
526 # move config to remote node
527 my $conffile = PVE
::QemuServer
::config_file
($vmid);
528 my $newconffile = PVE
::QemuServer
::config_file
($vmid, $self->{node
});
530 die "Failed to move config to node '$self->{node}' - rename failed: $!\n"
531 if !rename($conffile, $newconffile);
533 # now that config file is move, we can resume vm on target if livemigrate
534 if ($self->{tunnel
}) {
535 my $cmd = [@{$self->{rem_ssh
}}, 'qm', 'resume', $vmid, '--skiplock'];
536 eval{ PVE
::Tools
::run_command
($cmd, outfunc
=> sub {},
539 $self->log('err', $line);
543 $self->log('err', $err);
548 # always stop local VM
549 eval { PVE
::QemuServer
::vm_stop
($self->{storecfg
}, $vmid, 1, 1); };
551 $self->log('err', "stopping vm failed - $err");
555 if ($self->{tunnel
}) {
556 eval { finish_tunnel
($self, $self->{tunnel
}); };
558 $self->log('err', $err);
563 # always deactivate volumes - avoid lvm LVs to be active on several nodes
565 my $vollist = PVE
::QemuServer
::get_vm_volumes
($conf);
566 PVE
::Storage
::deactivate_volumes
($self->{storecfg
}, $vollist);
569 $self->log('err', $err);
574 my $cmd = [ @{$self->{rem_ssh
}}, 'qm', 'unlock', $vmid ];
575 $self->cmd_logerr($cmd, errmsg
=> "failed to clear migrate lock");
579 my ($self, $vmid) = @_;