use PVE::Cluster;
use PVE::Storage;
use PVE::QemuServer;
+use Time::HiRes qw( usleep );
use base qw(PVE::AbstractMigrate);
sub lock_vm {
my ($self, $vmid, $code, @param) = @_;
-
+
return PVE::QemuServer::lock_config($vmid, $code, @param);
}
my $volhash = {};
my $cdromhash = {};
- # get list from PVE::Storage (for unused volumes)
- my $dl = PVE::Storage::vdisk_list($self->{storecfg}, undef, $vmid);
- PVE::Storage::foreach_volid($dl, sub {
- my ($volid, $sid, $volname) = @_;
+ my @sids = PVE::Storage::storage_ids($self->{storecfg});
+ foreach my $storeid (@sids) {
+ my $scfg = PVE::Storage::storage_config($self->{storecfg}, $storeid);
+ next if $scfg->{shared};
+ next if !PVE::Storage::storage_check_enabled($self->{storecfg}, $storeid, undef, 1);
- # check if storage is available on both nodes
- my $scfg = PVE::Storage::storage_check_node($self->{storecfg}, $sid);
- PVE::Storage::storage_check_node($self->{storecfg}, $sid, $self->{node});
+ # get list from PVE::Storage (for unused volumes)
+ my $dl = PVE::Storage::vdisk_list($self->{storecfg}, $storeid, $vmid);
+ PVE::Storage::foreach_volid($dl, sub {
+ my ($volid, $sid, $volname) = @_;
- return if $scfg->{shared};
+ # check if storage is available on target node
+ PVE::Storage::storage_check_node($self->{storecfg}, $sid, $self->{node});
- $volhash->{$volid} = 1;
- });
+ $volhash->{$volid} = 1;
+ });
+ }
# and add used,owned/non-shared disks (just to be sure we have all)
sync_disks($self, $vmid);
- # move config to remote node
- my $conffile = PVE::QemuServer::config_file($vmid);
- my $newconffile = PVE::QemuServer::config_file($vmid, $self->{node});
-
- die "Failed to move config to node '$self->{node}' - rename failed: $!\n"
- if !rename($conffile, $newconffile);
};
sub phase1_cleanup {
if (my $err = $@) {
$self->log('err', $err);
}
-
+
if ($self->{volumes}) {
foreach my $volid (@{$self->{volumes}}) {
$self->log('err', "found stale volume copy '$volid' on node '$self->{node}'");
my $rport;
+ my $nodename = PVE::INotify::nodename();
+
## start on remote node
- my $cmd = [@{$self->{rem_ssh}}, 'qm', 'start',
- $vmid, '--stateuri', 'tcp', '--skiplock'];
+ my $cmd = [@{$self->{rem_ssh}}, 'qm', 'start',
+ $vmid, '--stateuri', 'tcp', '--skiplock', '--migratedfrom', $nodename];
PVE::Tools::run_command($cmd, outfunc => sub {
my $line = shift;
# start migration
my $start = time();
+ eval {
+ PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "migrate", uri => "tcp:localhost:$lport");
+ };
+ my $merr = $@;
- my $merr = PVE::QemuServer::vm_monitor_command($vmid, "migrate -d \"tcp:localhost:$lport\"", 1);
-
- my $lstat = '';
+ my $lstat = 0;
+ my $usleep = 2000000;
+ my $i = 0;
while (1) {
- sleep (2);
- my $stat = PVE::QemuServer::vm_monitor_command($vmid, "info migrate", 1);
- if ($stat =~ m/^Migration status: (active|completed|failed|cancelled)$/im) {
+ $i++;
+ my $avglstat = $lstat/$i if $lstat;
+
+ usleep ($usleep);
+ my $stat = PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "query-migrate");
+ if ($stat->{status} =~ m/^(active|completed|failed|cancelled)$/im) {
$merr = undef;
- my $ms = $1;
-
- if ($stat ne $lstat) {
- if ($ms eq 'active') {
- my ($trans, $rem, $total) = (0, 0, 0);
- $trans = $1 if $stat =~ m/^transferred ram: (\d+) kbytes$/im;
- $rem = $1 if $stat =~ m/^remaining ram: (\d+) kbytes$/im;
- $total = $1 if $stat =~ m/^total ram: (\d+) kbytes$/im;
-
- $self->log('info', "migration status: $ms (transferred ${trans}KB, " .
- "remaining ${rem}KB), total ${total}KB)");
- } else {
- $self->log('info', "migration status: $ms");
- }
- }
- if ($ms eq 'completed') {
+ if ($stat->{status} eq 'completed') {
my $delay = time() - $start;
if ($delay > 0) {
my $mbps = sprintf "%.2f", $conf->{memory}/$delay;
$self->log('info', "migration speed: $mbps MB/s");
}
}
-
- if ($ms eq 'failed' || $ms eq 'cancelled') {
+
+ if ($stat->{status} eq 'failed' || $stat->{status} eq 'cancelled') {
die "aborting\n"
}
- last if $ms ne 'active';
+ if ($stat->{status} ne 'active') {
+ $self->log('info', "migration status: $stat->{status}");
+ last;
+ }
+
+ if ($stat->{ram}->{transferred} ne $lstat) {
+ my $trans = $stat->{ram}->{transferred} || 0;
+ my $rem = $stat->{ram}->{remaining} || 0;
+ my $total = $stat->{ram}->{total} || 0;
+ #reduce sleep if remainig memory if lower than the everage transfert
+ $usleep = 300000 if $rem < $avglstat;
+
+ $self->log('info', "migration status: $stat->{status} (transferred ${trans}, " .
+ "remaining ${rem}), total ${total})");
+ }
+
+ $lstat = $stat->{ram}->{transferred};
+
} else {
die $merr if $merr;
- die "unable to parse migration status '$stat' - aborting\n";
+ die "unable to parse migration status '$stat->{status}' - aborting\n";
}
- $lstat = $stat;
- };
+ }
+}
+
+sub phase2_cleanup {
+ my ($self, $vmid, $err) = @_;
+
+ $self->log('info', "aborting phase 2 - cleanup resources");
+
+ my $conf = $self->{vmconf};
+ delete $conf->{lock};
+ eval { PVE::QemuServer::update_config_nolock($vmid, $conf, 1) };
+ if (my $err = $@) {
+ $self->log('err', $err);
+ }
+
+ ## fixme : vm_stop_cleanup on target vm
+
+
}
sub phase3 {
my ($self, $vmid) = @_;
-
+
my $volids = $self->{volumes};
# destroy local copies
my $conf = $self->{vmconf};
+ # move config to remote node
+ my $conffile = PVE::QemuServer::config_file($vmid);
+ my $newconffile = PVE::QemuServer::config_file($vmid, $self->{node});
+
+ die "Failed to move config to node '$self->{node}' - rename failed: $!\n"
+ if !rename($conffile, $newconffile);
+
+ # now that config file is move, we can resume vm on target if livemigrate
+ if ($self->{tunnel}) {
+ my $cmd = [@{$self->{rem_ssh}}, 'qm', 'resume', $vmid, '--skiplock'];
+ eval{ PVE::Tools::run_command($cmd, outfunc => sub {}, errfunc => sub {}) };
+ if (my $err = $@) {
+ $self->log('err', $err);
+ $self->{errors} = 1;
+ }
+ }
+
# always stop local VM
eval { PVE::QemuServer::vm_stop($self->{storecfg}, $vmid, 1, 1); };
if (my $err = $@) {