use Time::HiRes qw( usleep );
use PVE::Cluster;
+use PVE::GuestHelpers qw(safe_boolean_ne safe_string_ne);
use PVE::INotify;
use PVE::RPCEnvironment;
use PVE::Replication;
}
sub fork_tunnel {
- my ($self, $tunnel_addr) = @_;
+ my ($self, $ssh_forward_info) = @_;
my @localtunnelinfo = ();
- foreach my $addr (@$tunnel_addr) {
+ foreach my $addr (@$ssh_forward_info) {
push @localtunnelinfo, '-L', $addr;
}
$self->finish_command_pipe($tunnel, 30);
- if ($tunnel->{sock_addr}) {
+ if (my $unix_sockets = $tunnel->{unix_sockets}) {
# ssh does not clean up on local host
- my $cmd = ['rm', '-f', @{$tunnel->{sock_addr}}]; #
+ my $cmd = ['rm', '-f', @$unix_sockets];
PVE::Tools::run_command($cmd);
# .. and just to be sure check on remote side
die $err if $err;
}
+sub start_remote_tunnel {
+ my ($self, $raddr, $rport, $ruri, $unix_socket_info) = @_;
+
+ my $nodename = PVE::INotify::nodename();
+ my $migration_type = $self->{opts}->{migration_type};
+
+ if ($migration_type eq 'secure') {
+
+ if ($ruri =~ /^unix:/) {
+ my $ssh_forward_info = ["$raddr:$raddr"];
+ $unix_socket_info->{$raddr} = 1;
+
+ my $unix_sockets = [ keys %$unix_socket_info ];
+ for my $sock (@$unix_sockets) {
+ push @$ssh_forward_info, "$sock:$sock";
+ unlink $sock;
+ }
+
+ $self->{tunnel} = $self->fork_tunnel($ssh_forward_info);
+
+ my $unix_socket_try = 0; # wait for the socket to become ready
+ while ($unix_socket_try <= 100) {
+ $unix_socket_try++;
+ my $available = 0;
+ foreach my $sock (@$unix_sockets) {
+ if (-S $sock) {
+ $available++;
+ }
+ }
+
+ if ($available == @$unix_sockets) {
+ last;
+ }
+
+ usleep(50000);
+ }
+ if ($unix_socket_try > 100) {
+ $self->{errors} = 1;
+ $self->finish_tunnel($self->{tunnel});
+ die "Timeout, migration socket $ruri did not get ready";
+ }
+ $self->{tunnel}->{unix_sockets} = $unix_sockets if (@$unix_sockets);
+
+ } elsif ($ruri =~ /^tcp:/) {
+ my $ssh_forward_info = [];
+ if ($raddr eq "localhost") {
+ # for backwards compatibility with older qemu-server versions
+ my $pfamily = PVE::Tools::get_host_address_family($nodename);
+ my $lport = PVE::Tools::next_migrate_port($pfamily);
+ push @$ssh_forward_info, "$lport:localhost:$rport";
+ }
+
+ $self->{tunnel} = $self->fork_tunnel($ssh_forward_info);
+
+ } else {
+ die "unsupported protocol in migration URI: $ruri\n";
+ }
+ } else {
+ #fork tunnel for insecure migration, to send faster commands like resume
+ $self->{tunnel} = $self->fork_tunnel();
+ }
+}
+
sub lock_vm {
my ($self, $vmid, $code, @param) = @_;
# test if VM exists
my $conf = $self->{vmconf} = PVE::QemuConfig->load_config($vmid);
+ my $repl_conf = PVE::ReplicationConfig->new();
+ $self->{replication_jobcfg} = $repl_conf->find_local_replication_job($vmid, $self->{node});
+ $self->{is_replicated} = $repl_conf->check_for_existing_jobs($vmid, 1);
+
+ if ($self->{replication_jobcfg} && defined($self->{replication_jobcfg}->{remove_job})) {
+ die "refusing to migrate replicated VM whose replication job is marked for removal\n";
+ }
+
PVE::QemuConfig->check_lock($conf);
my $running = 0;
die "can't migrate running VM without --online\n" if !$online;
$running = $pid;
+ if ($self->{is_replicated} && !$self->{replication_jobcfg}) {
+ if ($self->{opts}->{force}) {
+ $self->log('warn', "WARNING: Node '$self->{node}' is not a replication target. Existing " .
+ "replication jobs will fail after migration!\n");
+ } else {
+ die "Cannot live-migrate replicated VM to node '$self->{node}' - not a replication " .
+ "target. Use 'force' to override.\n";
+ }
+ }
+
$self->{forcemachine} = PVE::QemuServer::Machine::qemu_machine_pxe($vmid, $conf);
# To support custom CPU types, we keep QEMU's "-cpu" parameter intact.
# Since the parameter itself contains no reference to a custom model,
# this makes migration independent of changes to "cpu-models.conf".
if ($conf->{cpu}) {
- my $cpuconf = PVE::QemuServer::CPUConfig::parse_cpu_conf_basic($conf->{cpu});
+ my $cpuconf = PVE::JSONSchema::parse_property_string('pve-cpu-conf', $conf->{cpu});
if ($cpuconf && PVE::QemuServer::CPUConfig::is_custom_model($cpuconf->{cputype})) {
$self->{forcecpu} = PVE::QemuServer::CPUConfig::get_cpu_from_running_vm($pid);
}
my $vollist = PVE::QemuServer::get_vm_volumes($conf);
- my $need_activate = [];
foreach my $volid (@$vollist) {
my ($sid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
warn "Used shared storage '$sid' is not online on source node!\n"
if !$plugin->check_connection($sid, $scfg);
- } else {
- # only activate if not shared
- next if ($volid =~ m/vm-\d+-cloudinit/);
- push @$need_activate, $volid;
}
}
- # activate volumes
- PVE::Storage::activate_volumes($self->{storecfg}, $need_activate);
-
# test ssh connection
my $cmd = [ @{$self->{rem_ssh}}, '/bin/true' ];
eval { $self->cmd_quiet($cmd); };
return $running;
}
-sub sync_disks {
+sub scan_local_volumes {
my ($self, $vmid) = @_;
my $conf = $self->{vmconf};
# local volumes which have been copied
# and their old_id => new_id pairs
- $self->{volumes} = [];
$self->{volume_map} = {};
+ $self->{local_volumes} = {};
my $storecfg = $self->{storecfg};
eval {
# found local volumes and their origin
- my $local_volumes = {};
+ my $local_volumes = $self->{local_volumes};
my $local_volumes_errors = {};
my $other_errors = [];
my $abort = 0;
if !$target_scfg->{content}->{images};
}
+ my $bwlimit = PVE::Storage::get_bandwidth_limit(
+ 'migration',
+ [$targetsid, $storeid],
+ $self->{opts}->{bwlimit},
+ );
+
PVE::Storage::foreach_volid($dl, sub {
my ($volid, $sid, $volinfo) = @_;
$local_volumes->{$volid}->{ref} = 'storage';
+ $local_volumes->{$volid}->{size} = $volinfo->{size};
+ $local_volumes->{$volid}->{targetsid} = $targetsid;
+ $local_volumes->{$volid}->{bwlimit} = $bwlimit;
# If with_snapshots is not set for storage migrate, it tries to use
# a raw+size stream, but on-the-fly conversion from qcow2 to raw+size
});
}
- my $rep_cfg = PVE::ReplicationConfig->new();
- my $replication_jobcfg = $rep_cfg->find_local_replication_job($vmid, $self->{node});
- my $replicatable_volumes = !$replication_jobcfg ? {}
+ my $replicatable_volumes = !$self->{replication_jobcfg} ? {}
: PVE::QemuConfig->get_replicatable_volumes($storecfg, $vmid, $conf, 0, 1);
+ foreach my $volid (keys %{$replicatable_volumes}) {
+ $local_volumes->{$volid}->{replicated} = 1;
+ }
my $test_volid = sub {
my ($volid, $attr) = @_;
return if $scfg->{shared};
$local_volumes->{$volid}->{ref} = $attr->{referenced_in_config} ? 'config' : 'snapshot';
+ $local_volumes->{$volid}->{ref} = 'storage' if $attr->{is_unused};
$local_volumes->{$volid}->{is_vmstate} = $attr->{is_vmstate} ? 1 : 0;
+ $local_volumes->{$volid}->{drivename} = $attr->{drivename}
+ if $attr->{drivename};
+
if ($attr->{cdrom}) {
if ($volid =~ /vm-\d+-cloudinit/) {
$local_volumes->{$volid}->{ref} = 'generated';
die "owned by other VM (owner = VM $owner)\n"
if !$owner || ($owner != $vmid);
+ return if $attr->{is_vmstate};
+
if (defined($snaprefs)) {
$local_volumes->{$volid}->{snapshots} = 1;
}
}
- if ($replication_jobcfg) {
- if ($self->{running}) {
+ foreach my $volid (sort keys %$local_volumes) {
+ my $ref = $local_volumes->{$volid}->{ref};
+ if ($self->{running} && $ref eq 'config') {
+ $local_volumes->{$volid}->{migration_mode} = 'online';
+ } elsif ($self->{running} && $ref eq 'generated') {
+ die "can't live migrate VM with local cloudinit disk. use a shared storage instead\n";
+ } else {
+ $local_volumes->{$volid}->{migration_mode} = 'offline';
+ }
+ }
+ };
+ die "Problem found while scanning volumes - $@" if $@;
+}
- my $version = PVE::QemuServer::kvm_user_version();
- if (!min_version($version, 4, 2)) {
- die "can't live migrate VM with replicated volumes, pve-qemu to old (< 4.2)!\n"
- }
+sub handle_replication {
+ my ($self, $vmid) = @_;
- my $live_replicatable_volumes = {};
- PVE::QemuConfig->foreach_volume($conf, sub {
- my ($ds, $drive) = @_;
+ my $conf = $self->{vmconf};
+ my $local_volumes = $self->{local_volumes};
- my $volid = $drive->{file};
- $live_replicatable_volumes->{$ds} = $volid
- if defined($replicatable_volumes->{$volid});
- });
- foreach my $drive (keys %$live_replicatable_volumes) {
- my $volid = $live_replicatable_volumes->{$drive};
+ return if !$self->{replication_jobcfg};
+ if ($self->{running}) {
- my $bitmap = "repl_$drive";
+ my $version = PVE::QemuServer::kvm_user_version();
+ if (!min_version($version, 4, 2)) {
+ die "can't live migrate VM with replicated volumes, pve-qemu to old (< 4.2)!\n"
+ }
- # start tracking before replication to get full delta + a few duplicates
- $self->log('info', "$drive: start tracking writes using block-dirty-bitmap '$bitmap'");
- mon_cmd($vmid, 'block-dirty-bitmap-add', node => "drive-$drive", name => $bitmap);
+ my @live_replicatable_volumes = $self->filter_local_volumes('online', 1);
+ foreach my $volid (@live_replicatable_volumes) {
+ my $drive = $local_volumes->{$volid}->{drivename};
+ die "internal error - no drive for '$volid'\n" if !defined($drive);
- # other info comes from target node in phase 2
- $self->{target_drive}->{$drive}->{bitmap} = $bitmap;
- }
- }
- $self->log('info', "replicating disk images");
+ my $bitmap = "repl_$drive";
+
+ # start tracking before replication to get full delta + a few duplicates
+ $self->log('info', "$drive: start tracking writes using block-dirty-bitmap '$bitmap'");
+ mon_cmd($vmid, 'block-dirty-bitmap-add', node => "drive-$drive", name => $bitmap);
- my $start_time = time();
- my $logfunc = sub { $self->log('info', shift) };
- $self->{replicated_volumes} = PVE::Replication::run_replication(
- 'PVE::QemuConfig', $replication_jobcfg, $start_time, $start_time, $logfunc);
+ # other info comes from target node in phase 2
+ $self->{target_drive}->{$drive}->{bitmap} = $bitmap;
}
+ }
+ $self->log('info', "replicating disk images");
+
+ my $start_time = time();
+ my $logfunc = sub { $self->log('info', shift) };
+ my $actual_replicated_volumes = PVE::Replication::run_replication(
+ 'PVE::QemuConfig', $self->{replication_jobcfg}, $start_time, $start_time, $logfunc);
+
+ # extra safety check
+ my @replicated_volumes = $self->filter_local_volumes(undef, 1);
+ foreach my $volid (@replicated_volumes) {
+ die "expected volume '$volid' to get replicated, but it wasn't\n"
+ if !$actual_replicated_volumes->{$volid};
+ }
+}
- # sizes in config have to be accurate for remote node to correctly
- # allocate disks, rescan to be sure
- my $volid_hash = PVE::QemuServer::scan_volids($storecfg, $vmid);
- PVE::QemuConfig->foreach_volume($conf, sub {
- my ($key, $drive) = @_;
- my ($updated, $old_size, $new_size) = PVE::QemuServer::Drive::update_disksize($drive, $volid_hash);
- if (defined($updated)) {
- $conf->{$key} = PVE::QemuServer::print_drive($updated);
- $self->log('info', "size of disk '$updated->{file}' ($key) updated from $old_size to $new_size\n");
- }
- });
+sub config_update_local_disksizes {
+ my ($self) = @_;
- # we want to set the efidisk size in the config to the size of the
- # real OVMF_VARS.fd image, else we can create a too big image, which does not work
- if (defined($conf->{efidisk0})) {
- PVE::QemuServer::update_efidisk_size($conf);
+ my $conf = $self->{vmconf};
+ my $local_volumes = $self->{local_volumes};
+
+ PVE::QemuConfig->foreach_volume($conf, sub {
+ my ($key, $drive) = @_;
+ return if $key eq 'efidisk0'; # skip efidisk, will be handled later
+
+ my $volid = $drive->{file};
+ return if !defined($local_volumes->{$volid}); # only update sizes for local volumes
+
+ my ($updated, $msg) = PVE::QemuServer::Drive::update_disksize($drive, $local_volumes->{$volid}->{size});
+ if (defined($updated)) {
+ $conf->{$key} = PVE::QemuServer::print_drive($updated);
+ $self->log('info', "drive '$key': $msg");
}
+ });
- $self->log('info', "copying local disk images") if scalar(%$local_volumes);
+ # we want to set the efidisk size in the config to the size of the
+ # real OVMF_VARS.fd image, else we can create a too big image, which does not work
+ if (defined($conf->{efidisk0})) {
+ PVE::QemuServer::update_efidisk_size($conf);
+ }
+}
- foreach my $volid (keys %$local_volumes) {
- my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
- my $targetsid = PVE::QemuServer::map_storage($self->{opts}->{storagemap}, $sid);
- my $ref = $local_volumes->{$volid}->{ref};
- if ($self->{running} && $ref eq 'config') {
- push @{$self->{online_local_volumes}}, $volid;
- } elsif ($ref eq 'generated') {
- die "can't live migrate VM with local cloudinit disk. use a shared storage instead\n" if $self->{running};
- # skip all generated volumes but queue them for deletion in phase3_cleanup
- push @{$self->{volumes}}, $volid;
- next;
- } else {
- next if $self->{replicated_volumes}->{$volid};
- push @{$self->{volumes}}, $volid;
- my $opts = $self->{opts};
- # use 'migrate' limit for transfer to other node
- my $bwlimit = PVE::Storage::get_bandwidth_limit('migration', [$targetsid, $sid], $opts->{bwlimit});
- # JSONSchema and get_bandwidth_limit use kbps - storage_migrate bps
- $bwlimit = $bwlimit * 1024 if defined($bwlimit);
-
- my $storage_migrate_opts = {
- 'bwlimit' => $bwlimit,
- 'insecure' => $opts->{migration_type} eq 'insecure',
- 'with_snapshots' => $local_volumes->{$volid}->{snapshots},
- 'allow_rename' => !$local_volumes->{$volid}->{is_vmstate},
- };
+sub filter_local_volumes {
+ my ($self, $migration_mode, $replicated) = @_;
- my $new_volid = eval {
- PVE::Storage::storage_migrate($storecfg, $volid, $self->{ssh_info},
- $targetsid, $storage_migrate_opts);
- };
- if (my $err = $@) {
- die "storage migration for '$volid' to storage '$targetsid' failed - $err\n";
- }
+ my $volumes = $self->{local_volumes};
+ my @filtered_volids;
- $self->{volume_map}->{$volid} = $new_volid;
- $self->log('info', "volume '$volid' is '$new_volid' on the target\n");
- }
+ foreach my $volid (sort keys %{$volumes}) {
+ next if defined($migration_mode) && safe_string_ne($volumes->{$volid}->{migration_mode}, $migration_mode);
+ next if defined($replicated) && safe_boolean_ne($volumes->{$volid}->{replicated}, $replicated);
+ push @filtered_volids, $volid;
+ }
+
+ return @filtered_volids;
+}
+
+sub sync_offline_local_volumes {
+ my ($self) = @_;
+
+ my $local_volumes = $self->{local_volumes};
+ my @volids = $self->filter_local_volumes('offline', 0);
+
+ my $storecfg = $self->{storecfg};
+ my $opts = $self->{opts};
+
+ $self->log('info', "copying local disk images") if scalar(@volids);
+
+ foreach my $volid (@volids) {
+ my $targetsid = $local_volumes->{$volid}->{targetsid};
+ my $bwlimit = $local_volumes->{$volid}->{bwlimit};
+ $bwlimit = $bwlimit * 1024 if defined($bwlimit); # storage_migrate uses bps
+
+ my $storage_migrate_opts = {
+ 'ratelimit_bps' => $bwlimit,
+ 'insecure' => $opts->{migration_type} eq 'insecure',
+ 'with_snapshots' => $local_volumes->{$volid}->{snapshots},
+ 'allow_rename' => !$local_volumes->{$volid}->{is_vmstate},
+ };
+
+ my $logfunc = sub { $self->log('info', $_[0]); };
+ my $new_volid = eval {
+ PVE::Storage::storage_migrate($storecfg, $volid, $self->{ssh_info},
+ $targetsid, $storage_migrate_opts, $logfunc);
+ };
+ if (my $err = $@) {
+ die "storage migration for '$volid' to storage '$targetsid' failed - $err\n";
}
- };
- die "Failed to sync data - $@" if $@;
+
+ $self->{volume_map}->{$volid} = $new_volid;
+ $self->log('info', "volume '$volid' is '$new_volid' on the target\n");
+
+ eval { PVE::Storage::deactivate_volumes($storecfg, [$volid]); };
+ if (my $err = $@) {
+ $self->log('warn', $err);
+ }
+ }
}
sub cleanup_remotedisks {
my ($self) = @_;
- foreach my $target_drive (keys %{$self->{target_drive}}) {
- my $drivestr = $self->{target_drive}->{$target_drive}->{drivestr};
- next if !defined($drivestr);
-
- my $drive = PVE::QemuServer::parse_drive($target_drive, $drivestr);
+ my $local_volumes = $self->{local_volumes};
+ foreach my $volid (values %{$self->{volume_map}}) {
# don't clean up replicated disks!
- next if defined($self->{replicated_volumes}->{$drive->{file}});
+ next if $local_volumes->{$volid}->{replicated};
- my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file});
+ my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid);
my $cmd = [@{$self->{rem_ssh}}, 'pvesm', 'free', "$storeid:$volname"];
$conf->{lock} = 'migrate';
PVE::QemuConfig->write_config($vmid, $conf);
- sync_disks($self, $vmid);
+ $self->scan_local_volumes($vmid);
- # sync_disks fixes disk sizes to match their actual size, write changes so
- # target allocates correct volumes
+ # fix disk sizes to match their actual size and write changes,
+ # so that the target allocates the correct volumes
+ $self->config_update_local_disksizes();
PVE::QemuConfig->write_config($vmid, $conf);
+
+ $self->handle_replication($vmid);
+
+ $self->sync_offline_local_volumes();
};
sub phase1_cleanup {
$self->log('err', $err);
}
- if ($self->{volumes}) {
- foreach my $volid (@{$self->{volumes}}) {
- $self->log('err', "found stale volume copy '$volid' on node '$self->{node}'");
- # fixme: try to remove ?
- }
+ eval { $self->cleanup_remotedisks() };
+ if (my $err = $@) {
+ $self->log('err', $err);
}
eval { $self->cleanup_bitmaps() };
if (my $err =$@) {
$self->log('err', $err);
}
-
}
sub phase2 {
my ($self, $vmid) = @_;
my $conf = $self->{vmconf};
+ my $local_volumes = $self->{local_volumes};
+ my @online_local_volumes = $self->filter_local_volumes('online');
+
+ $self->{storage_migration} = 1 if scalar(@online_local_volumes);
$self->log('info', "starting VM $vmid on remote node '$self->{node}'");
push @$cmd, '--force-cpu', $self->{forcecpu};
}
- if ($self->{online_local_volumes}) {
+ if ($self->{storage_migration}) {
push @$cmd, '--targetstorage', ($self->{opts}->{targetstorage} // '1');
}
my $spice_port;
- my $tunnel_addr = [];
- my $sock_addr = [];
+ my $unix_socket_info = {};
# version > 0 for unix socket support
my $nbd_protocol_version = 1;
# TODO change to 'spice_ticket: <ticket>\n' in 7.0
my $input = $spice_ticket ? "$spice_ticket\n" : "\n";
$input .= "nbd_protocol_version: $nbd_protocol_version\n";
- foreach my $volid (keys %{$self->{replicated_volumes}}) {
+
+ my @online_replicated_volumes = $self->filter_local_volumes('online', 1);
+ foreach my $volid (@online_replicated_volumes) {
$input .= "replicated_volume: $volid\n";
}
+ my $handle_storage_migration_listens = sub {
+ my ($drive_key, $drivestr, $nbd_uri) = @_;
+
+ $self->{stopnbd} = 1;
+ $self->{target_drive}->{$drive_key}->{drivestr} = $drivestr;
+ $self->{target_drive}->{$drive_key}->{nbd_uri} = $nbd_uri;
+
+ my $source_drive = PVE::QemuServer::parse_drive($drive_key, $conf->{$drive_key});
+ my $target_drive = PVE::QemuServer::parse_drive($drive_key, $drivestr);
+ my $source_volid = $source_drive->{file};
+ my $target_volid = $target_drive->{file};
+
+ $self->{volume_map}->{$source_volid} = $target_volid;
+ $self->log('info', "volume '$source_volid' is '$target_volid' on the target\n");
+ };
+
my $target_replicated_volumes = {};
# Note: We try to keep $spice_ticket secret (do not pass via command line parameter)
my $targetdrive = $3;
$targetdrive =~ s/drive-//g;
- $self->{target_drive}->{$targetdrive}->{drivestr} = $drivestr;
- $self->{target_drive}->{$targetdrive}->{nbd_uri} = $nbd_uri;
+ $handle_storage_migration_listens->($targetdrive, $drivestr, $nbd_uri);
} elsif ($line =~ m!^storage migration listens on nbd:unix:(/run/qemu-server/(\d+)_nbd\.migrate):exportname=(\S+) volume:(\S+)$!) {
my $drivestr = $4;
die "Destination UNIX socket's VMID does not match source VMID" if $vmid ne $2;
my $targetdrive = $3;
$targetdrive =~ s/drive-//g;
- $self->{target_drive}->{$targetdrive}->{drivestr} = $drivestr;
- $self->{target_drive}->{$targetdrive}->{nbd_uri} = $nbd_uri;
- push @$tunnel_addr, "$nbd_unix_addr:$nbd_unix_addr";
- push @$sock_addr, $nbd_unix_addr;
+ $handle_storage_migration_listens->($targetdrive, $drivestr, $nbd_uri);
+ $unix_socket_info->{$nbd_unix_addr} = 1;
} elsif ($line =~ m/^re-using replicated volume: (\S+) - (.*)$/) {
my $drive = $1;
my $volid = $2;
die "unable to detect remote migration address\n" if !$raddr;
- if (scalar(keys %$target_replicated_volumes) != scalar(keys %{$self->{replicated_volumes}})) {
+ if (scalar(keys %$target_replicated_volumes) != scalar(@online_replicated_volumes)) {
die "number of replicated disks on source and target node do not match - target node too old?\n"
}
$self->log('info', "start remote tunnel");
-
- if ($migration_type eq 'secure') {
-
- if ($ruri =~ /^unix:/) {
- unlink $raddr;
- push @$tunnel_addr, "$raddr:$raddr";
- $self->{tunnel} = $self->fork_tunnel($tunnel_addr);
- push @$sock_addr, $raddr;
-
- my $unix_socket_try = 0; # wait for the socket to become ready
- while ($unix_socket_try <= 100) {
- $unix_socket_try++;
- my $available = 0;
- foreach my $sock (@$sock_addr) {
- if (-S $sock) {
- $available++;
- }
- }
-
- if ($available == @$sock_addr) {
- last;
- }
-
- usleep(50000);
- }
- if ($unix_socket_try > 100) {
- $self->{errors} = 1;
- $self->finish_tunnel($self->{tunnel});
- die "Timeout, migration socket $ruri did not get ready";
- }
-
- } elsif ($ruri =~ /^tcp:/) {
- my $tunnel_addr;
- if ($raddr eq "localhost") {
- # for backwards compatibility with older qemu-server versions
- my $pfamily = PVE::Tools::get_host_address_family($nodename);
- my $lport = PVE::Tools::next_migrate_port($pfamily);
- $tunnel_addr = "$lport:localhost:$rport";
- }
-
- $self->{tunnel} = $self->fork_tunnel($tunnel_addr);
-
- } else {
- die "unsupported protocol in migration URI: $ruri\n";
- }
- } else {
- #fork tunnel for insecure migration, to send faster commands like resume
- $self->{tunnel} = $self->fork_tunnel();
- }
- $self->{tunnel}->{sock_addr} = $sock_addr if (@$sock_addr);
+ $self->start_remote_tunnel($raddr, $rport, $ruri, $unix_socket_info);
my $start = time();
- my $opt_bwlimit = $self->{opts}->{bwlimit};
-
- if (defined($self->{online_local_volumes})) {
- $self->{storage_migration} = 1;
+ if ($self->{storage_migration}) {
$self->{storage_migration_jobs} = {};
$self->log('info', "starting storage migration");
die "The number of local disks does not match between the source and the destination.\n"
- if (scalar(keys %{$self->{target_drive}}) != scalar @{$self->{online_local_volumes}});
+ if (scalar(keys %{$self->{target_drive}}) != scalar(@online_local_volumes));
foreach my $drive (keys %{$self->{target_drive}}){
my $target = $self->{target_drive}->{$drive};
my $nbd_uri = $target->{nbd_uri};
my $source_drive = PVE::QemuServer::parse_drive($drive, $conf->{$drive});
- my $target_drive = PVE::QemuServer::parse_drive($drive, $target->{drivestr});
-
my $source_volid = $source_drive->{file};
- my $target_volid = $target_drive->{file};
-
- my $source_sid = PVE::Storage::Plugin::parse_volume_id($source_volid);
- my $target_sid = PVE::Storage::Plugin::parse_volume_id($target_volid);
- my $bwlimit = PVE::Storage::get_bandwidth_limit('migration', [$source_sid, $target_sid], $opt_bwlimit);
+ my $bwlimit = $local_volumes->{$source_volid}->{bwlimit};
my $bitmap = $target->{bitmap};
$self->log('info', "$drive: start migration to $nbd_uri");
PVE::QemuServer::qemu_drive_mirror($vmid, $drive, $nbd_uri, $vmid, undef, $self->{storage_migration_jobs}, 'skip', undef, $bwlimit, $bitmap);
-
- $self->{volume_map}->{$source_volid} = $target_volid;
- $self->log('info', "volume '$source_volid' is '$target_volid' on the target\n");
}
}
# load_defaults
my $defaults = PVE::QemuServer::load_defaults();
- $self->log('info', "set migration_caps");
- eval {
- PVE::QemuServer::set_migration_caps($vmid);
- };
+ $self->log('info', "set migration capabilities");
+ eval { PVE::QemuServer::set_migration_caps($vmid) };
warn $@ if $@;
my $qemu_migrate_params = {};
# migrate speed can be set via bwlimit (datacenter.cfg and API) and via the
# migrate_speed parameter in qm.conf - take the lower of the two.
- my $bwlimit = PVE::Storage::get_bandwidth_limit('migration', undef, $opt_bwlimit) // 0;
- my $migrate_speed = $conf->{migrate_speed} // $bwlimit;
+ my $bwlimit = PVE::Storage::get_bandwidth_limit('migration', undef, $self->{opts}->{bwlimit}) // 0;
+ my $migrate_speed = $conf->{migrate_speed} // 0;
# migrate_speed is in MB/s, bwlimit in KB/s
$migrate_speed *= 1024;
- $migrate_speed = ($bwlimit < $migrate_speed) ? $bwlimit : $migrate_speed;
-
- # always set migrate speed (overwrite kvm default of 32m) we set a very high
- # default of 8192m which is basically unlimited
- $migrate_speed ||= ($defaults->{migrate_speed} || 8192) * 1024;
+ if ($bwlimit && $migrate_speed) {
+ $migrate_speed = ($bwlimit < $migrate_speed) ? $bwlimit : $migrate_speed;
+ } else {
+ $migrate_speed ||= $bwlimit;
+ }
+ $migrate_speed ||= ($defaults->{migrate_speed} || 0) * 1024;
- # qmp takes migrate_speed in B/s.
- $migrate_speed *= 1024;
- $self->log('info', "migration speed limit: $migrate_speed B/s");
- $qemu_migrate_params->{'max-bandwidth'} = int($migrate_speed);
+ if ($migrate_speed) {
+ $migrate_speed *= 1024; # qmp takes migrate_speed in B/s.
+ $self->log('info', "migration speed limit: $migrate_speed B/s");
+ $qemu_migrate_params->{'max-bandwidth'} = int($migrate_speed);
+ }
my $migrate_downtime = $defaults->{migrate_downtime};
$migrate_downtime = $conf->{migrate_downtime} if defined($conf->{migrate_downtime});
my $downtimecounter = 0;
while (1) {
$i++;
- my $avglstat = $lstat/$i if $lstat;
+ my $avglstat = $lstat ? $lstat / $i : 0;
usleep($usleep);
- my $stat;
- eval {
- $stat = mon_cmd($vmid, "query-migrate");
- };
+
+ my $stat = eval { mon_cmd($vmid, "query-migrate") };
if (my $err = $@) {
$err_count++;
warn "query migrate failed: $err\n";
$self->log('info', "query migrate failed: $err");
if ($err_count <= 5) {
- usleep(1000000);
+ usleep(1_000_000);
next;
}
die "too many query migrate failures - aborting\n";
}
- if (defined($stat->{status}) && $stat->{status} =~ m/^(setup)$/im) {
- sleep(1);
- next;
- }
-
- if (defined($stat->{status}) && $stat->{status} =~ m/^(active|completed|failed|cancelled)$/im) {
- $merr = undef;
- $err_count = 0;
- if ($stat->{status} eq 'completed') {
- my $delay = time() - $start;
- if ($delay > 0) {
- my $mbps = sprintf "%.2f", $memory / $delay;
- my $downtime = $stat->{downtime} || 0;
- $self->log('info', "migration speed: $mbps MB/s - downtime $downtime ms");
- }
- }
+ my $status = $stat->{status};
+ if (defined($status) && $status =~ m/^(setup)$/im) {
+ sleep(1);
+ next;
+ }
- if ($stat->{status} eq 'failed' || $stat->{status} eq 'cancelled') {
- $self->log('info', "migration status error: $stat->{status}");
- die "aborting\n"
+ if (!defined($status) || $status !~ m/^(active|completed|failed|cancelled)$/im) {
+ die $merr if $merr;
+ die "unable to parse migration status '$status' - aborting\n";
+ }
+ $merr = undef;
+ $err_count = 0;
+
+ if ($status eq 'completed') {
+ my $delay = time() - $start;
+ if ($delay > 0) {
+ my $mbps = sprintf "%.2f", $memory / $delay;
+ my $downtime = $stat->{downtime} || 0;
+ $self->log('info', "migration speed: $mbps MB/s - downtime $downtime ms");
}
+ }
- if ($stat->{status} ne 'active') {
- $self->log('info', "migration status: $stat->{status}");
- last;
- }
+ if ($status eq 'failed' || $status eq 'cancelled') {
+ $self->log('info', "migration status error: $status");
+ die "aborting\n"
+ }
- if ($stat->{ram}->{transferred} ne $lstat) {
- my $trans = $stat->{ram}->{transferred} || 0;
- my $rem = $stat->{ram}->{remaining} || 0;
- my $total = $stat->{ram}->{total} || 0;
- my $xbzrlecachesize = $stat->{"xbzrle-cache"}->{"cache-size"} || 0;
- my $xbzrlebytes = $stat->{"xbzrle-cache"}->{"bytes"} || 0;
- my $xbzrlepages = $stat->{"xbzrle-cache"}->{"pages"} || 0;
- my $xbzrlecachemiss = $stat->{"xbzrle-cache"}->{"cache-miss"} || 0;
- my $xbzrleoverflow = $stat->{"xbzrle-cache"}->{"overflow"} || 0;
- # reduce sleep if remainig memory is lower than the average transfer speed
- $usleep = 100000 if $avglstat && $rem < $avglstat;
-
- $self->log('info', "migration status: $stat->{status} (transferred ${trans}, " .
- "remaining ${rem}), total ${total})");
-
- if (${xbzrlecachesize}) {
- $self->log('info', "migration xbzrle cachesize: ${xbzrlecachesize} transferred ${xbzrlebytes} pages ${xbzrlepages} cachemiss ${xbzrlecachemiss} overflow ${xbzrleoverflow}");
- }
+ if ($status ne 'active') {
+ $self->log('info', "migration status: $status");
+ last;
+ }
- if (($lastrem && $rem > $lastrem ) || ($rem == 0)) {
- $downtimecounter++;
- }
- $lastrem = $rem;
-
- if ($downtimecounter > 5) {
- $downtimecounter = 0;
- $migrate_downtime *= 2;
- $self->log('info', "auto-increased downtime to continue migration: $migrate_downtime ms");
- eval {
- # migrate-set-parameters does not touch values not
- # specified, so this only changes downtime-limit
- mon_cmd($vmid, "migrate-set-parameters", 'downtime-limit' => int($migrate_downtime));
- };
- $self->log('info', "migrate-set-parameters error: $@") if $@;
- }
+ if ($stat->{ram}->{transferred} ne $lstat) {
+ my $trans = $stat->{ram}->{transferred} || 0;
+ my $rem = $stat->{ram}->{remaining} || 0;
+ my $total = $stat->{ram}->{total} || 0;
+
+ my $xbzrle = $stat->{"xbzrle-cache"} || {};
+ my $xbzrlecachesize = $xbzrle->{"cache-size"} || 0;
+ my $xbzrlebytes = $xbzrle->{"bytes"} || 0;
+ my $xbzrlepages = $xbzrle->{"pages"} || 0;
+ my $xbzrlecachemiss = $xbzrle->{"cache-miss"} || 0;
+ my $xbzrleoverflow = $xbzrle->{"overflow"} || 0;
+
+ # reduce sleep if remainig memory is lower than the average transfer speed
+ $usleep = 100_000 if $avglstat && $rem < $avglstat;
+
+ $self->log(
+ 'info',
+ "migration $status (transferred ${trans}, remaining ${rem}), total ${total})"
+ );
+ if ($xbzrlecachesize) {
+ $self->log('info', "migration xbzrle cachesize: ${xbzrlecachesize} transferred ${xbzrlebytes} pages ${xbzrlepages} cachemiss ${xbzrlecachemiss} overflow ${xbzrleoverflow}");
}
+ if (($lastrem && $rem > $lastrem ) || ($rem == 0)) {
+ $downtimecounter++;
+ }
+ $lastrem = $rem;
+
+ if ($downtimecounter > 5) {
+ $downtimecounter = 0;
+ $migrate_downtime *= 2;
+ $self->log('info', "auto-increased downtime to continue migration: $migrate_downtime ms");
+ eval {
+ # migrate-set-parameters does not touch values not
+ # specified, so this only changes downtime-limit
+ mon_cmd($vmid, "migrate-set-parameters", 'downtime-limit' => int($migrate_downtime));
+ };
+ $self->log('info', "migrate-set-parameters error: $@") if $@;
+ }
+ }
- $lstat = $stat->{ram}->{transferred};
+ $lstat = $stat->{ram}->{transferred};
+ }
- } else {
- die $merr if $merr;
- die "unable to parse migration status '$stat->{status}' - aborting\n";
+ if ($self->{storage_migration}) {
+ # finish block-job with block-job-cancel, to disconnect source VM from NBD
+ # to avoid it trying to re-establish it. We are in blockjob ready state,
+ # thus, this command changes to it to blockjob complete (see qapi docs)
+ eval { PVE::QemuServer::qemu_drive_mirror_monitor($vmid, undef, $self->{storage_migration_jobs}, 'cancel'); };
+ if (my $err = $@) {
+ die "Failed to complete storage migration: $err\n";
}
}
}
sub phase3 {
my ($self, $vmid) = @_;
- my $volids = $self->{volumes};
- return if $self->{phase2errors};
-
- # destroy local copies
- foreach my $volid (@$volids) {
- eval { PVE::Storage::vdisk_free($self->{storecfg}, $volid); };
- if (my $err = $@) {
- $self->log('err', "removing local copy of '$volid' failed - $err");
- $self->{errors} = 1;
- last if $err =~ /^interrupted by signal$/;
- }
- }
+ return;
}
sub phase3_cleanup {
my $tunnel = $self->{tunnel};
- if ($self->{storage_migration}) {
- # finish block-job with block-job-cancel, to disconnect source VM from NBD
- # to avoid it trying to re-establish it. We are in blockjob ready state,
- # thus, this command changes to it to blockjob complete (see qapi docs)
- eval { PVE::QemuServer::qemu_drive_mirror_monitor($vmid, undef, $self->{storage_migration_jobs}, 'cancel'); };
-
- if (my $err = $@) {
- eval { PVE::QemuServer::qemu_blockjobs_cancel($vmid, $self->{storage_migration_jobs}) };
- eval { PVE::QemuMigrate::cleanup_remotedisks($self) };
- die "Failed to complete storage migration: $err\n";
- }
- }
-
if ($self->{volume_map}) {
+ my $target_drives = $self->{target_drive};
+
+ # FIXME: for NBD storage migration we now only update the volid, and
+ # not the full drivestr from the target node. Workaround that until we
+ # got some real rescan, to avoid things like wrong format in the drive
+ delete $conf->{$_} for keys %$target_drives;
PVE::QemuConfig->update_volume_ids($conf, $self->{volume_map});
+
+ for my $drive (keys %$target_drives) {
+ $conf->{$drive} = $target_drives->{$drive}->{drivestr};
+ }
PVE::QemuConfig->write_config($vmid, $conf);
}
# transfer replication state before move config
- $self->transfer_replication_state() if $self->{replicated_volumes};
-
- # move config to remote node
- my $conffile = PVE::QemuConfig->config_file($vmid);
- my $newconffile = PVE::QemuConfig->config_file($vmid, $self->{node});
-
- die "Failed to move config to node '$self->{node}' - rename failed: $!\n"
- if !rename($conffile, $newconffile);
-
- $self->switch_replication_job_target() if $self->{replicated_volumes};
+ $self->transfer_replication_state() if $self->{is_replicated};
+ PVE::QemuConfig->move_config_to_node($vmid, $self->{node});
+ $self->switch_replication_job_target() if $self->{is_replicated};
if ($self->{livemigration}) {
- if ($self->{storage_migration}) {
+ if ($self->{stopnbd}) {
+ $self->log('info', "stopping NBD storage migration server on target.");
# stop nbd server on remote vm - requirement for resume since 2.9
my $cmd = [@{$self->{rem_ssh}}, 'qm', 'nbdstop', $vmid];
$self->{errors} = 1;
}
- if($self->{storage_migration}) {
- # destroy local copies
- my $volids = $self->{online_local_volumes};
-
- foreach my $volid (@$volids) {
- # keep replicated volumes!
- next if $self->{replicated_volumes}->{$volid};
+ my @not_replicated_volumes = $self->filter_local_volumes(undef, 0);
- eval { PVE::Storage::vdisk_free($self->{storecfg}, $volid); };
- if (my $err = $@) {
- $self->log('err', "removing local copy of '$volid' failed - $err");
- $self->{errors} = 1;
- last if $err =~ /^interrupted by signal$/;
- }
+ # destroy local copies
+ foreach my $volid (@not_replicated_volumes) {
+ eval { PVE::Storage::vdisk_free($self->{storecfg}, $volid); };
+ if (my $err = $@) {
+ $self->log('err', "removing local copy of '$volid' failed - $err");
+ $self->{errors} = 1;
+ last if $err =~ /^interrupted by signal$/;
}
-
}
# clear migrate lock