]>
git.proxmox.com Git - qemu-server.git/blob - PVE/QemuMigrate.pm
c6302b05b10b2cd9ecfd05cb652c0f169948abc9
1 package PVE
::QemuMigrate
;
5 use PVE
::AbstractMigrate
;
12 use Time
::HiRes
qw( usleep );
14 use base
qw(PVE::AbstractMigrate);
16 sub fork_command_pipe
{
17 my ($self, $cmd) = @_;
19 my $reader = IO
::File-
>new();
20 my $writer = IO
::File-
>new();
26 eval { $cpid = open2
($reader, $writer, @$cmd); };
31 if ($orig_pid != $$) {
32 $self->log('err', "can't fork command pipe\n");
39 return { writer
=> $writer, reader
=> $reader, pid
=> $cpid };
42 sub finish_command_pipe
{
43 my ($self, $cmdpipe, $timeout) = @_;
45 my $writer = $cmdpipe->{writer
};
46 my $reader = $cmdpipe->{reader
};
51 my $cpid = $cmdpipe->{pid
};
54 for (my $i = 0; $i < $timeout; $i++) {
55 return if !PVE
::ProcFSTools
::check_process_running
($cpid);
60 $self->log('info', "ssh tunnel still running - terminating now with SIGTERM\n");
64 for (my $i = 0; $i < 10; $i++) {
65 return if !PVE
::ProcFSTools
::check_process_running
($cpid);
69 $self->log('info', "ssh tunnel still running - terminating now with SIGKILL\n");
75 my ($self, $nodeip, $lport, $rport) = @_;
77 my $cmd = [@{$self->{rem_ssh
}}, '-L', "$lport:localhost:$rport",
80 my $tunnel = $self->fork_command_pipe($cmd);
82 my $reader = $tunnel->{reader
};
86 PVE
::Tools
::run_with_timeout
(60, sub { $helo = <$reader>; });
87 die "no reply\n" if !$helo;
88 die "no quorum on target node\n" if $helo =~ m/^no quorum$/;
89 die "got strange reply from mtunnel ('$helo')\n"
90 if $helo !~ m/^tunnel online$/;
95 $self->finish_command_pipe($tunnel);
96 die "can't open migration tunnel - $err";
102 my ($self, $tunnel) = @_;
104 my $writer = $tunnel->{writer
};
107 PVE
::Tools
::run_with_timeout
(30, sub {
108 print $writer "quit\n";
114 $self->finish_command_pipe($tunnel, 30);
120 my ($self, $vmid, $code, @param) = @_;
122 return PVE
::QemuServer
::lock_config
($vmid, $code, @param);
126 my ($self, $vmid) = @_;
128 my $online = $self->{opts
}->{online
};
130 $self->{storecfg
} = PVE
::Storage
::config
();
133 my $conf = $self->{vmconf
} = PVE
::QemuServer
::load_config
($vmid);
135 PVE
::QemuServer
::check_lock
($conf);
138 if (my $pid = PVE
::QemuServer
::check_running
($vmid)) {
139 die "cant migrate running VM without --online\n" if !$online;
143 if (my $loc_res = PVE
::QemuServer
::check_local_resources
($conf, 1)) {
144 if ($self->{running
} || !$self->{opts
}->{force
}) {
145 die "can't migrate VM which uses local devices\n";
147 $self->log('info', "migrating VM which uses local devices");
152 my $vollist = PVE
::QemuServer
::get_vm_volumes
($conf);
153 PVE
::Storage
::activate_volumes
($self->{storecfg
}, $vollist);
155 # fixme: check if storage is available on both nodes
157 # test ssh connection
158 my $cmd = [ @{$self->{rem_ssh
}}, '/bin/true' ];
159 eval { $self->cmd_quiet($cmd); };
160 die "Can't connect to destination address using public key\n" if $@;
166 my ($self, $vmid) = @_;
168 $self->log('info', "copying disk images");
170 my $conf = $self->{vmconf
};
172 $self->{volumes
} = [];
181 my @sids = PVE
::Storage
::storage_ids
($self->{storecfg
});
182 foreach my $storeid (@sids) {
183 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $storeid);
184 next if $scfg->{shared
};
185 next if !PVE
::Storage
::storage_check_enabled
($self->{storecfg
}, $storeid, undef, 1);
187 # get list from PVE::Storage (for unused volumes)
188 my $dl = PVE
::Storage
::vdisk_list
($self->{storecfg
}, $storeid, $vmid);
189 PVE
::Storage
::foreach_volid
($dl, sub {
190 my ($volid, $sid, $volname) = @_;
192 # check if storage is available on target node
193 PVE
::Storage
::storage_check_node
($self->{storecfg
}, $sid, $self->{node
});
195 $volhash->{$volid} = 1;
199 # and add used,owned/non-shared disks (just to be sure we have all)
202 PVE
::QemuServer
::foreach_drive
($conf, sub {
203 my ($ds, $drive) = @_;
205 my $volid = $drive->{file
};
208 die "cant migrate local file/device '$volid'\n" if $volid =~ m
|^/|;
210 if (PVE
::QemuServer
::drive_is_cdrom
($drive)) {
211 die "cant migrate local cdrom drive\n" if $volid eq 'cdrom';
212 return if $volid eq 'none';
213 $cdromhash->{$volid} = 1;
216 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
218 # check if storage is available on both nodes
219 my $scfg = PVE
::Storage
::storage_check_node
($self->{storecfg
}, $sid);
220 PVE
::Storage
::storage_check_node
($self->{storecfg
}, $sid, $self->{node
});
222 return if $scfg->{shared
};
224 die "can't migrate local cdrom '$volid'\n" if $cdromhash->{$volid};
228 my ($path, $owner) = PVE
::Storage
::path
($self->{storecfg
}, $volid);
230 die "can't migrate volume '$volid' - owned by other VM (owner = VM $owner)\n"
231 if !$owner || ($owner != $self->{vmid
});
233 $volhash->{$volid} = 1;
236 if ($self->{running
} && !$sharedvm) {
237 die "can't do online migration - VM uses local disks\n";
240 # do some checks first
241 foreach my $volid (keys %$volhash) {
242 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
243 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $sid);
245 die "can't migrate '$volid' - storagy type '$scfg->{type}' not supported\n"
246 if $scfg->{type
} ne 'dir';
249 foreach my $volid (keys %$volhash) {
250 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volid);
251 push @{$self->{volumes
}}, $volid;
252 PVE
::Storage
::storage_migrate
($self->{storecfg
}, $volid, $self->{nodeip
}, $sid);
255 die "Failed to sync data - $@" if $@;
259 my ($self, $vmid) = @_;
261 $self->log('info', "starting migration of VM $vmid to node '$self->{node}' ($self->{nodeip})");
263 my $conf = $self->{vmconf
};
265 # set migrate lock in config file
266 $conf->{lock} = 'migrate';
267 PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1);
269 sync_disks
($self, $vmid);
274 my ($self, $vmid, $err) = @_;
276 $self->log('info', "aborting phase 1 - cleanup resources");
278 my $conf = $self->{vmconf
};
279 delete $conf->{lock};
280 eval { PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1) };
282 $self->log('err', $err);
285 if ($self->{volumes
}) {
286 foreach my $volid (@{$self->{volumes
}}) {
287 $self->log('err', "found stale volume copy '$volid' on node '$self->{node}'");
288 # fixme: try to remove ?
294 my ($self, $vmid) = @_;
296 my $conf = $self->{vmconf
};
298 $self->log('info', "starting VM $vmid on remote node '$self->{node}'");
302 my $nodename = PVE
::INotify
::nodename
();
304 ## start on remote node
305 my $cmd = [@{$self->{rem_ssh
}}, 'qm', 'start',
306 $vmid, '--stateuri', 'tcp', '--skiplock', '--migratedfrom', $nodename];
308 PVE
::Tools
::run_command
($cmd, outfunc
=> sub {
311 if ($line =~ m/^migration listens on port (\d+)$/) {
314 }, errfunc
=> sub {});
316 die "unable to detect remote migration port\n" if !$rport;
318 $self->log('info', "starting migration tunnel");
320 ## create tunnel to remote port
321 my $lport = PVE
::QemuServer
::next_migrate_port
();
322 $self->{tunnel
} = $self->fork_tunnel($self->{nodeip
}, $lport, $rport);
324 $self->log('info', "starting online/live migration on port $lport");
329 my $capabilities = {};
330 $capabilities->{capability
} = "xbzrle";
331 $capabilities->{state} = JSON
::false
;
334 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate-set-capabilities", capabilities
=> [$capabilities]);
337 #set cachesize 10% of the total memory
338 my $cachesize = int($conf->{memory
}*1048576/10);
340 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate-set-cache-size", value
=> $cachesize);
344 PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "migrate", uri
=> "tcp:localhost:$lport");
349 my $usleep = 2000000;
354 my $avglstat = $lstat/$i if $lstat;
359 $stat = PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "query-migrate");
363 warn "query migrate failed: $err\n";
364 if ($err_count <= 5) {
368 die "too many query migrate failures - aborting\n";
370 if ($stat->{status
} =~ m/^(active|completed|failed|cancelled)$/im) {
373 if ($stat->{status
} eq 'completed') {
374 my $delay = time() - $start;
376 my $mbps = sprintf "%.2f", $conf->{memory
}/$delay;
377 $self->log('info', "migration speed: $mbps MB/s");
381 if ($stat->{status
} eq 'failed' || $stat->{status
} eq 'cancelled') {
385 if ($stat->{status
} ne 'active') {
386 $self->log('info', "migration status: $stat->{status}");
390 if ($stat->{ram
}->{transferred
} ne $lstat) {
391 my $trans = $stat->{ram
}->{transferred
} || 0;
392 my $rem = $stat->{ram
}->{remaining
} || 0;
393 my $total = $stat->{ram
}->{total
} || 0;
394 my $xbzrlecachesize = $stat->{"xbzrle-cache"}->{"cache-size"} || 0;
395 my $xbzrlebytes = $stat->{"xbzrle-cache"}->{"bytes"} || 0;
396 my $xbzrlepages = $stat->{"xbzrle-cache"}->{"pages"} || 0;
397 my $xbzrlecachemiss = $stat->{"xbzrle-cache"}->{"cache-miss"} || 0;
398 my $xbzrleoverflow = $stat->{"xbzrle-cache"}->{"overflow"} || 0;
399 #reduce sleep if remainig memory if lower than the everage transfert
400 $usleep = 300000 if $avglstat && $rem < $avglstat;
402 $self->log('info', "migration status: $stat->{status} (transferred ${trans}, " .
403 "remaining ${rem}), total ${total})");
405 #$self->log('info', "migration xbzrle cachesize: ${xbzrlecachesize} transferred ${xbzrlebytes} pages ${xbzrlepages} cachemiss ${xbzrlecachemiss} overflow ${xbzrleoverflow}");
408 $lstat = $stat->{ram
}->{transferred
};
412 die "unable to parse migration status '$stat->{status}' - aborting\n";
418 my ($self, $vmid, $err) = @_;
420 return if !$self->{errors
};
421 $self->{phase2errors
} = 1;
423 $self->log('info', "aborting phase 2 - cleanup resources");
425 my $conf = $self->{vmconf
};
426 delete $conf->{lock};
427 eval { PVE
::QemuServer
::update_config_nolock
($vmid, $conf, 1) };
429 $self->log('err', $err);
432 # cleanup ressources on target host
433 my $nodename = PVE
::INotify
::nodename
();
435 my $cmd = [@{$self->{rem_ssh
}}, 'qm', 'stop', $vmid, '--skiplock', '--migratedfrom', $nodename];
436 eval{ PVE
::Tools
::run_command
($cmd, outfunc
=> sub {}, errfunc
=> sub {}) };
438 $self->log('err', $err);
444 my ($self, $vmid) = @_;
446 my $volids = $self->{volumes
};
447 return if $self->{phase2errors
};
449 # destroy local copies
450 foreach my $volid (@$volids) {
451 eval { PVE
::Storage
::vdisk_free
($self->{storecfg
}, $volid); };
453 $self->log('err', "removing local copy of '$volid' failed - $err");
455 last if $err =~ /^interrupted by signal$/;
461 my ($self, $vmid, $err) = @_;
463 my $conf = $self->{vmconf
};
464 return if $self->{phase2errors
};
466 # move config to remote node
467 my $conffile = PVE
::QemuServer
::config_file
($vmid);
468 my $newconffile = PVE
::QemuServer
::config_file
($vmid, $self->{node
});
470 die "Failed to move config to node '$self->{node}' - rename failed: $!\n"
471 if !rename($conffile, $newconffile);
473 # now that config file is move, we can resume vm on target if livemigrate
474 if ($self->{tunnel
}) {
475 my $cmd = [@{$self->{rem_ssh
}}, 'qm', 'resume', $vmid, '--skiplock'];
476 eval{ PVE
::Tools
::run_command
($cmd, outfunc
=> sub {}, errfunc
=> sub {}) };
478 $self->log('err', $err);
483 # always stop local VM
484 eval { PVE
::QemuServer
::vm_stop
($self->{storecfg
}, $vmid, 1, 1); };
486 $self->log('err', "stopping vm failed - $err");
490 if ($self->{tunnel
}) {
491 eval { finish_tunnel
($self, $self->{tunnel
}); };
493 $self->log('err', $err);
498 # always deactivate volumes - avoid lvm LVs to be active on several nodes
500 my $vollist = PVE
::QemuServer
::get_vm_volumes
($conf);
501 PVE
::Storage
::deactivate_volumes
($self->{storecfg
}, $vollist);
504 $self->log('err', $err);
509 my $cmd = [ @{$self->{rem_ssh
}}, 'qm', 'unlock', $vmid ];
510 $self->cmd_logerr($cmd, errmsg
=> "failed to clear migrate lock");
514 my ($self, $vmid) = @_;