X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=PVE%2FQemuMigrate.pm;h=cb1639bce70ac215254fbe056ca21f259113966b;hb=da18cc93001bfb70ec0b5f627389809c86effe8a;hp=5caac15f397c3c79dae1a253c151d13b61e2de4a;hpb=d80ad67f9d3be88cb0775865ef3ef06b82907752;p=qemu-server.git diff --git a/PVE/QemuMigrate.pm b/PVE/QemuMigrate.pm index 5caac15..cb1639b 100644 --- a/PVE/QemuMigrate.pm +++ b/PVE/QemuMigrate.pm @@ -13,6 +13,9 @@ use PVE::Storage; use PVE::QemuServer; use Time::HiRes qw( usleep ); use PVE::RPCEnvironment; +use PVE::ReplicationConfig; +use PVE::ReplicationState; +use PVE::Replication; use base qw(PVE::AbstractMigrate); @@ -93,7 +96,7 @@ sub fork_tunnel { my @localtunnelinfo = defined($tunnel_addr) ? ('-L' , $tunnel_addr ) : (); - my $cmd = [@{$self->{rem_ssh}}, '-o ExitOnForwardFailure=yes', @localtunnelinfo, 'qm', 'mtunnel' ]; + my $cmd = [@{$self->{rem_ssh}}, '-o ExitOnForwardFailure=yes', @localtunnelinfo, '/usr/bin/pvecm', 'mtunnel' ]; my $tunnel = $self->fork_command_pipe($cmd); @@ -264,26 +267,27 @@ sub sync_disks { PVE::Storage::foreach_volid($dl, sub { my ($volid, $sid, $volname) = @_; - $local_volumes->{$volid} = 'storage'; + $local_volumes->{$volid}->{ref} = 'storage'; }); } my $test_volid = sub { - my ($volid, $is_cdrom, $snapname) = @_; - - return if !$volid; + my ($volid, $attr) = @_; if ($volid =~ m|^/|) { - $local_volumes->{$volid} = 'config'; + $local_volumes->{$volid}->{ref} = 'config'; die "local file/device\n"; } - if ($is_cdrom) { + my $snaprefs = $attr->{referenced_in_snapshot}; + + if ($attr->{cdrom}) { if ($volid eq 'cdrom') { my $msg = "can't migrate local cdrom drive"; - $msg .= " (referenced in snapshot '$snapname')" - if defined($snapname); - + if (defined($snaprefs) && !$attr->{referenced_in_config}) { + my $snapnames = join(', ', sort keys %$snaprefs); + $msg .= " (referenced in snapshot - $snapnames)"; + } &$log_error("$msg\n"); return; } @@ -301,20 +305,21 @@ sub sync_disks { $sharedvm = 0; - $local_volumes->{$volid} = defined($snapname) ? 'snapshot' : 'config'; + $local_volumes->{$volid}->{ref} = $attr->{referenced_in_config} ? 'config' : 'snapshot'; - die "local cdrom image\n" if $is_cdrom; + die "local cdrom image\n" if $attr->{cdrom}; my ($path, $owner) = PVE::Storage::path($self->{storecfg}, $volid); die "owned by other VM (owner = VM $owner)\n" if !$owner || ($owner != $self->{vmid}); - if (defined($snapname)) { + my $format = PVE::QemuServer::qemu_img_format($scfg, $volname); + $local_volumes->{$volid}->{snapshots} = defined($snaprefs) || ($format =~ /^(?:qcow2|vmdk)$/); + if (defined($snaprefs)) { # we cannot migrate shapshots on local storage # exceptions: 'zfspool' or 'qcow2' files (on directory storage) - my $format = PVE::QemuServer::qemu_img_format($scfg, $volname); die "online storage migration not possible if snapshot exists\n" if $self->{running}; if (!($scfg->{type} eq 'zfspool' || $format eq 'qcow2')) { die "non-migratable snapshot exists\n"; @@ -325,34 +330,23 @@ sub sync_disks { if PVE::Storage::volume_is_base_and_used($self->{storecfg}, $volid); }; - my $test_drive = sub { - my ($ds, $drive, $snapname) = @_; - - eval { - &$test_volid($drive->{file}, PVE::QemuServer::drive_is_cdrom($drive), $snapname); - }; - - &$log_error($@, $drive->{file}) if $@; - }; - - foreach my $snapname (keys %{$conf->{snapshots}}) { - eval { - &$test_volid($conf->{snapshots}->{$snapname}->{'vmstate'}, 0, undef) - if defined($conf->{snapshots}->{$snapname}->{'vmstate'}); - }; - &$log_error($@, $conf->{snapshots}->{$snapname}->{'vmstate'}) if $@; - - PVE::QemuServer::foreach_drive($conf->{snapshots}->{$snapname}, $test_drive, $snapname); - } - PVE::QemuServer::foreach_drive($conf, $test_drive); + PVE::QemuServer::foreach_volid($conf, sub { + my ($volid, $attr) = @_; + eval { $test_volid->($volid, $attr); }; + if (my $err = $@) { + &$log_error($err, $volid); + } + }); foreach my $vol (sort keys %$local_volumes) { - if ($local_volumes->{$vol} eq 'storage') { + my $ref = $local_volumes->{$vol}->{ref}; + if ($ref eq 'storage') { $self->log('info', "found local disk '$vol' (via storage)\n"); - } elsif ($local_volumes->{$vol} eq 'config') { - die "can't live migrate attached local disks without with-local-disks option\n" if $self->{running} && !$self->{opts}->{"with-local-disks"}; + } elsif ($ref eq 'config') { + &$log_error("can't live migrate attached local disks without with-local-disks option\n", $vol) + if $self->{running} && !$self->{opts}->{"with-local-disks"}; $self->log('info', "found local disk '$vol' (in current VM config)\n"); - } elsif ($local_volumes->{$vol} eq 'snapshot') { + } elsif ($ref eq 'snapshot') { $self->log('info', "found local disk '$vol' (referenced by snapshot(s))\n"); } else { $self->log('info', "found local disk '$vol'\n"); @@ -391,15 +385,31 @@ sub sync_disks { } } + my $rep_volumes; + $self->log('info', "copying disk images"); + my $rep_cfg = PVE::ReplicationConfig->new(); + + if (my $jobcfg = $rep_cfg->find_local_replication_job($vmid, $self->{node})) { + die "can't live migrate VM with replicated volumes\n" if $self->{running}; + my $start_time = time(); + my $logfunc = sub { my ($msg) = @_; $self->log('info', $msg); }; + $rep_volumes = PVE::Replication::run_replication( + 'PVE::QemuConfig', $jobcfg, $start_time, $start_time, $logfunc); + } + foreach my $volid (keys %$local_volumes) { my ($sid, $volname) = PVE::Storage::parse_volume_id($volid); - if ($self->{running} && $self->{opts}->{targetstorage} && $local_volumes->{$volid} eq 'config') { + if ($self->{running} && $self->{opts}->{targetstorage} && $local_volumes->{$volid}->{ref} eq 'config') { push @{$self->{online_local_volumes}}, $volid; } else { + next if $rep_volumes->{$volid}; push @{$self->{volumes}}, $volid; - PVE::Storage::storage_migrate($self->{storecfg}, $volid, $self->{nodeip}, $sid); + my $insecure = $self->{opts}->{migration_type} eq 'insecure'; + my $with_snapshots = $local_volumes->{$volid}->{snapshots}; + PVE::Storage::storage_migrate($self->{storecfg}, $volid, $self->{ssh_info}, $sid, + undef, undef, undef, undef, $insecure, $with_snapshots); } } }; @@ -482,18 +492,7 @@ sub phase2 { push @$cmd , 'qm', 'start', $vmid, '--skiplock', '--migratedfrom', $nodename; - # we use TCP only for unsecure migrations as TCP ssh forward tunnels often - # did appeared to late (they are hard, if not impossible, to check for) - # secure migration use UNIX sockets now, this *breaks* compatibilty when trying - # to migrate from new to old but *not* from old to new. - my $datacenterconf = PVE::Cluster::cfs_read_file('datacenter.cfg'); - - my $migration_type = 'secure'; - if (defined($self->{opts}->{migration_type})) { - $migration_type = $self->{opts}->{migration_type}; - } elsif (defined($datacenterconf->{migration}->{type})) { - $migration_type = $datacenterconf->{migration}->{type}; - } + my $migration_type = $self->{opts}->{migration_type}; push @$cmd, '--migration_type', $migration_type; @@ -878,6 +877,9 @@ sub phase3_cleanup { } } + # transfer replication state before move config + $self->transfer_replication_state(); + # move config to remote node my $conffile = PVE::QemuConfig->config_file($vmid); my $newconffile = PVE::QemuConfig->config_file($vmid, $self->{node}); @@ -885,6 +887,8 @@ sub phase3_cleanup { die "Failed to move config to node '$self->{node}' - rename failed: $!\n" if !rename($conffile, $newconffile); + $self->switch_replication_job_target(); + if ($self->{livemigration}) { if ($self->{storage_migration}) { # remove drives referencing the nbd server from source