]> git.proxmox.com Git - qemu-server.git/blobdiff - PVE/QemuMigrate.pm
sync_disks: be more verbose if storage_migrate fails
[qemu-server.git] / PVE / QemuMigrate.pm
index e954eca5a0ac29d78f7d0e481ee72d28c5fcd3a4..5ed953add834e76d3680028cf48bc4a803156592 100644 (file)
@@ -17,6 +17,8 @@ use PVE::ReplicationState;
 use PVE::Storage;
 use PVE::Tools;
 
+use PVE::QemuConfig;
+use PVE::QemuServer::CPUConfig;
 use PVE::QemuServer::Drive;
 use PVE::QemuServer::Helpers qw(min_version);
 use PVE::QemuServer::Machine;
@@ -227,7 +229,17 @@ sub prepare {
 
        $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});
+           if ($cpuconf && PVE::QemuServer::CPUConfig::is_custom_model($cpuconf->{cputype})) {
+               $self->{forcecpu} = PVE::QemuServer::CPUConfig::get_cpu_from_running_vm($pid);
+           }
+       }
     }
+
     my $loc_res = PVE::QemuServer::check_local_resources($conf, 1);
     if (scalar @$loc_res) {
        if ($self->{running} || !$self->{opts}->{force}) {
@@ -244,7 +256,7 @@ sub prepare {
        my ($sid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
 
        # check if storage is available on both nodes
-       my $targetsid = $self->{opts}->{targetstorage} // $sid;
+       my $targetsid = PVE::QemuServer::map_storage($self->{opts}->{storagemap}, $sid);
 
        my $scfg = PVE::Storage::storage_check_node($self->{storecfg}, $sid);
        PVE::Storage::storage_check_node($self->{storecfg}, $targetsid, $self->{node});
@@ -278,17 +290,11 @@ sub sync_disks {
     my $conf = $self->{vmconf};
 
     # local volumes which have been copied
+    # and their old_id => new_id pairs
     $self->{volumes} = [];
+    $self->{volume_map} = {};
 
     my $storecfg = $self->{storecfg};
-    my $override_targetsid = $self->{opts}->{targetstorage};
-
-    if (defined($override_targetsid)) {
-       my $scfg = PVE::Storage::storage_config($storecfg, $override_targetsid);
-       die "content type 'images' is not available on storage '$override_targetsid'\n"
-           if !$scfg->{content}->{images};
-    }
-
     eval {
 
        # found local volumes and their origin
@@ -319,11 +325,17 @@ sub sync_disks {
 
            next if @{$dl->{$storeid}} == 0;
 
-           my $targetsid = $override_targetsid // $storeid;
-
+           my $targetsid = PVE::QemuServer::map_storage($self->{opts}->{storagemap}, $storeid);
            # check if storage is available on target node
            PVE::Storage::storage_check_node($storecfg, $targetsid, $self->{node});
 
+           # grandfather in existing mismatches
+           if ($targetsid ne $storeid) {
+               my $target_scfg = PVE::Storage::storage_config($storecfg, $targetsid);
+               die "content type 'images' is not available on storage '$targetsid'\n"
+                   if !$target_scfg->{content}->{images};
+           }
+
            PVE::Storage::foreach_volid($dl, sub {
                my ($volid, $sid, $volinfo) = @_;
 
@@ -368,7 +380,7 @@ sub sync_disks {
 
            my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
 
-           my $targetsid = $override_targetsid // $sid;
+           my $targetsid = PVE::QemuServer::map_storage($self->{opts}->{storagemap}, $sid);
            # check if storage is available on both nodes
            my $scfg = PVE::Storage::storage_check_node($storecfg, $sid);
            PVE::Storage::storage_check_node($storecfg, $targetsid, $self->{node});
@@ -377,6 +389,8 @@ sub sync_disks {
 
            $local_volumes->{$volid}->{ref} = $attr->{referenced_in_config} ? 'config' : 'snapshot';
 
+           $local_volumes->{$volid}->{is_vmstate} = $attr->{is_vmstate} ? 1 : 0;
+
            if ($attr->{cdrom}) {
                if ($volid =~ /vm-\d+-cloudinit/) {
                    $local_volumes->{$volid}->{ref} = 'generated';
@@ -468,7 +482,7 @@ sub sync_disks {
                }
 
                my $live_replicatable_volumes = {};
-               PVE::QemuServer::foreach_drive($conf, sub {
+               PVE::QemuConfig->foreach_volume($conf, sub {
                    my ($ds, $drive) = @_;
 
                    my $volid = $drive->{file};
@@ -499,7 +513,7 @@ sub sync_disks {
        # 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::QemuServer::foreach_drive($conf, sub {
+       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)) {
@@ -518,7 +532,7 @@ sub sync_disks {
 
        foreach my $volid (keys %$local_volumes) {
            my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
-           my $targetsid = $override_targetsid // $sid;
+           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;
@@ -531,15 +545,28 @@ sub sync_disks {
                next if $self->{replicated_volumes}->{$volid};
                push @{$self->{volumes}}, $volid;
                my $opts = $self->{opts};
-               my $insecure = $opts->{migration_type} eq 'insecure';
-               my $with_snapshots = $local_volumes->{$volid}->{snapshots};
                # 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);
 
-               PVE::Storage::storage_migrate($storecfg, $volid, $self->{ssh_info}, $targetsid,
-                                             undef, undef, undef, $bwlimit, $insecure, $with_snapshots);
+               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},
+               };
+
+               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";
+               }
+
+               $self->{volume_map}->{$volid} = $new_volid;
+               $self->log('info', "volume '$volid' is '$new_volid' on the target\n");
            }
        }
     };
@@ -664,6 +691,10 @@ sub phase2 {
        push @$cmd, '--machine', $self->{forcemachine};
     }
 
+    if ($self->{forcecpu}) {
+       push @$cmd, '--force-cpu', $self->{forcecpu};
+    }
+
     if ($self->{online_local_volumes}) {
        push @$cmd, '--targetstorage', ($self->{opts}->{targetstorage} // '1');
     }
@@ -815,14 +846,20 @@ sub phase2 {
            my $source_drive = PVE::QemuServer::parse_drive($drive, $conf->{$drive});
            my $target_drive = PVE::QemuServer::parse_drive($drive, $target->{drivestr});
 
-           my $source_sid = PVE::Storage::Plugin::parse_volume_id($source_drive->{file});
-           my $target_sid = PVE::Storage::Plugin::parse_volume_id($target_drive->{file});
+           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 $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");
        }
     }
 
@@ -1101,15 +1138,14 @@ sub phase3_cleanup {
            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";
-       } else {
-           foreach my $target_drive (keys %{$self->{target_drive}}) {
-               my $drive = PVE::QemuServer::parse_drive($target_drive, $self->{target_drive}->{$target_drive}->{drivestr});
-               $conf->{$target_drive} = PVE::QemuServer::print_drive($drive);
-               PVE::QemuConfig->write_config($vmid, $conf);
-           }
        }
     }
 
+    if ($self->{volume_map}) {
+       PVE::QemuConfig->update_volume_ids($conf, $self->{volume_map});
+       PVE::QemuConfig->write_config($vmid, $conf);
+    }
+
     # transfer replication state before move config
     $self->transfer_replication_state() if $self->{replicated_volumes};