When starting a domain without a CPU model specified in the domain XML, QEMU will choose a default one. Which is fine unless the domain gets migrated to another host because libvirt doesn't perform any CPU ABI checks and the virtual CPU provided by QEMU on the destination host can differ from the one on the source host. With QEMU 4.2.0 we can probe for the default CPU model used by QEMU for a particular machine type and store it in the domain XML. This way the chosen CPU model is more visible to users and libvirt will make sure the guest will see the exact same CPU after migration. https://bugzilla.redhat.com/show_bug.cgi?id=1598151 https://bugzilla.redhat.com/show_bug.cgi?id=1598162 Signed-off-by: Jiri Denemark <jdenemar@xxxxxxxxxx> --- src/qemu/qemu_domain.c | 51 +++++++++++++++++++ .../disk-cache.x86_64-latest.args | 1 + .../disk-cdrom-network.x86_64-latest.args | 1 + .../disk-cdrom-tray.x86_64-latest.args | 1 + .../disk-copy_on_read.x86_64-latest.args | 1 + .../disk-detect-zeroes.x86_64-latest.args | 1 + .../disk-floppy-q35-2_11.x86_64-latest.args | 1 + .../disk-floppy-q35-2_9.x86_64-latest.args | 1 + .../os-firmware-bios.x86_64-latest.args | 1 + ...os-firmware-efi-secboot.x86_64-latest.args | 1 + .../os-firmware-efi.x86_64-latest.args | 1 + .../tpm-emulator-tpm2-enc.x86_64-latest.args | 1 + .../tpm-emulator-tpm2.x86_64-latest.args | 1 + .../tpm-emulator.x86_64-latest.args | 1 + .../tseg-explicit-size.x86_64-latest.args | 1 + .../vhost-vsock-auto.x86_64-latest.args | 1 + .../vhost-vsock.x86_64-latest.args | 1 + .../os-firmware-bios.x86_64-latest.xml | 3 ++ .../os-firmware-efi-secboot.x86_64-latest.xml | 3 ++ .../os-firmware-efi.x86_64-latest.xml | 3 ++ .../tpm-emulator-tpm2-enc.x86_64-latest.xml | 3 ++ .../tpm-emulator-tpm2.x86_64-latest.xml | 3 ++ .../tpm-emulator.x86_64-latest.xml | 3 ++ .../tpm-passthrough-crb.x86_64-latest.xml | 3 ++ .../tpm-passthrough.x86_64-latest.xml | 3 ++ 25 files changed, 91 insertions(+) diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index 1d7772ee01..1d10490059 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -4472,6 +4472,54 @@ qemuDomainDefVcpusPostParse(virDomainDefPtr def) } +static int +qemuDomainDefSetDefaultCPU(virDomainDefPtr def, + virQEMUCapsPtr qemuCaps) +{ + VIR_AUTOPTR(virCPUDef) newCPU = NULL; + virCPUDefPtr cpu = def->cpu; + const char *model; + + if (cpu && + (cpu->mode != VIR_CPU_MODE_CUSTOM || + cpu->model)) + return 0; + + model = virQEMUCapsGetMachineDefaultCPU(qemuCaps, def->os.machine, def->virtType); + if (!model) { + VIR_DEBUG("Unknown default CPU model for domain '%s'", def->name); + return 0; + } + + VIR_DEBUG("Setting default CPU model for domain '%s' to %s", + def->name, model); + + if (!cpu) { + if (VIR_ALLOC(newCPU) < 0) + return -1; + cpu = newCPU; + } + + /* We need to turn off all CPU checks when the domain is started because + * the default CPU (e.g., qemu64) may not be runnable on any host. QEMU + * will just disable the unavailable features and we will update the CPU + * definition accordingly and set check to FULL when starting the domain. */ + cpu->type = VIR_CPU_TYPE_GUEST; + cpu->mode = VIR_CPU_MODE_CUSTOM; + cpu->match = VIR_CPU_MATCH_EXACT; + cpu->check = VIR_CPU_CHECK_NONE; + cpu->fallback = VIR_CPU_FALLBACK_FORBID; + + if (VIR_STRDUP(cpu->model, model) < 0) + return -1; + + if (newCPU) + VIR_STEAL_PTR(def->cpu, newCPU); + + return 0; +} + + static int qemuDomainDefCPUPostParse(virDomainDefPtr def) { @@ -4646,6 +4694,9 @@ qemuDomainDefPostParse(virDomainDefPtr def, if (qemuCanonicalizeMachine(def, qemuCaps) < 0) return -1; + if (qemuDomainDefSetDefaultCPU(def, qemuCaps) < 0) + return -1; + qemuDomainDefEnableDefaultFeatures(def, qemuCaps); if (qemuDomainRecheckInternalPaths(def, cfg, parseFlags) < 0) diff --git a/tests/qemuxml2argvdata/disk-cache.x86_64-latest.args b/tests/qemuxml2argvdata/disk-cache.x86_64-latest.args index 7ea6d5b4d5..01d3b9d490 100644 --- a/tests/qemuxml2argvdata/disk-cache.x86_64-latest.args +++ b/tests/qemuxml2argvdata/disk-cache.x86_64-latest.args @@ -13,6 +13,7 @@ QEMU_AUDIO_DRV=none \ -object secret,id=masterKey0,format=raw,\ file=/tmp/lib/domain--1-QEMUGuest1/master-key.aes \ -machine pc-i440fx-2.6,accel=tcg,usb=off,dump-guest-core=off \ +-cpu qemu64 \ -m 214 \ -overcommit mem-lock=off \ -smp 1,sockets=1,cores=1,threads=1 \ diff --git a/tests/qemuxml2argvdata/disk-cdrom-network.x86_64-latest.args b/tests/qemuxml2argvdata/disk-cdrom-network.x86_64-latest.args index cf711c0a3f..e0d2eb049d 100644 --- a/tests/qemuxml2argvdata/disk-cdrom-network.x86_64-latest.args +++ b/tests/qemuxml2argvdata/disk-cdrom-network.x86_64-latest.args @@ -13,6 +13,7 @@ QEMU_AUDIO_DRV=none \ -object secret,id=masterKey0,format=raw,\ file=/tmp/lib/domain--1-QEMUGuest1/master-key.aes \ -machine pc-1.2,accel=kvm,usb=off,dump-guest-core=off \ +-cpu qemu64 \ -m 1024 \ -overcommit mem-lock=off \ -smp 1,sockets=1,cores=1,threads=1 \ diff --git a/tests/qemuxml2argvdata/disk-cdrom-tray.x86_64-latest.args b/tests/qemuxml2argvdata/disk-cdrom-tray.x86_64-latest.args index ad17416851..21542db4c2 100644 --- a/tests/qemuxml2argvdata/disk-cdrom-tray.x86_64-latest.args +++ b/tests/qemuxml2argvdata/disk-cdrom-tray.x86_64-latest.args @@ -13,6 +13,7 @@ QEMU_AUDIO_DRV=none \ -object secret,id=masterKey0,format=raw,\ file=/tmp/lib/domain--1-test/master-key.aes \ -machine pc-0.13,accel=tcg,usb=off,dump-guest-core=off \ +-cpu qemu64 \ -m 1024 \ -overcommit mem-lock=off \ -smp 1,sockets=1,cores=1,threads=1 \ diff --git a/tests/qemuxml2argvdata/disk-copy_on_read.x86_64-latest.args b/tests/qemuxml2argvdata/disk-copy_on_read.x86_64-latest.args index 1f1d37c957..8552f9f00e 100644 --- a/tests/qemuxml2argvdata/disk-copy_on_read.x86_64-latest.args +++ b/tests/qemuxml2argvdata/disk-copy_on_read.x86_64-latest.args @@ -13,6 +13,7 @@ QEMU_AUDIO_DRV=none \ -object secret,id=masterKey0,format=raw,\ file=/tmp/lib/domain--1-test/master-key.aes \ -machine pc-0.13,accel=tcg,usb=off,dump-guest-core=off \ +-cpu qemu64 \ -m 1024 \ -overcommit mem-lock=off \ -smp 1,sockets=1,cores=1,threads=1 \ diff --git a/tests/qemuxml2argvdata/disk-detect-zeroes.x86_64-latest.args b/tests/qemuxml2argvdata/disk-detect-zeroes.x86_64-latest.args index b48c0f8d43..2a01647e50 100644 --- a/tests/qemuxml2argvdata/disk-detect-zeroes.x86_64-latest.args +++ b/tests/qemuxml2argvdata/disk-detect-zeroes.x86_64-latest.args @@ -13,6 +13,7 @@ QEMU_AUDIO_DRV=none \ -object secret,id=masterKey0,format=raw,\ file=/tmp/lib/domain--1-test/master-key.aes \ -machine pc-0.13,accel=tcg,usb=off,dump-guest-core=off \ +-cpu qemu64 \ -m 1024 \ -overcommit mem-lock=off \ -smp 1,sockets=1,cores=1,threads=1 \ diff --git a/tests/qemuxml2argvdata/disk-floppy-q35-2_11.x86_64-latest.args b/tests/qemuxml2argvdata/disk-floppy-q35-2_11.x86_64-latest.args index 02e2b1e76c..fadd5a6d7e 100644 --- a/tests/qemuxml2argvdata/disk-floppy-q35-2_11.x86_64-latest.args +++ b/tests/qemuxml2argvdata/disk-floppy-q35-2_11.x86_64-latest.args @@ -13,6 +13,7 @@ QEMU_AUDIO_DRV=none \ -object secret,id=masterKey0,format=raw,\ file=/tmp/lib/domain--1-QEMUGuest1/master-key.aes \ -machine pc-q35-2.11,accel=tcg,usb=off,dump-guest-core=off \ +-cpu qemu64 \ -m 214 \ -overcommit mem-lock=off \ -smp 1,sockets=1,cores=1,threads=1 \ diff --git a/tests/qemuxml2argvdata/disk-floppy-q35-2_9.x86_64-latest.args b/tests/qemuxml2argvdata/disk-floppy-q35-2_9.x86_64-latest.args index a158e8e3d3..4c932a7365 100644 --- a/tests/qemuxml2argvdata/disk-floppy-q35-2_9.x86_64-latest.args +++ b/tests/qemuxml2argvdata/disk-floppy-q35-2_9.x86_64-latest.args @@ -13,6 +13,7 @@ QEMU_AUDIO_DRV=none \ -object secret,id=masterKey0,format=raw,\ file=/tmp/lib/domain--1-QEMUGuest1/master-key.aes \ -machine pc-q35-2.9,accel=tcg,usb=off,dump-guest-core=off \ +-cpu qemu64 \ -m 214 \ -overcommit mem-lock=off \ -smp 1,sockets=1,cores=1,threads=1 \ diff --git a/tests/qemuxml2argvdata/os-firmware-bios.x86_64-latest.args b/tests/qemuxml2argvdata/os-firmware-bios.x86_64-latest.args index 7d6108ec24..4391c6eda2 100644 --- a/tests/qemuxml2argvdata/os-firmware-bios.x86_64-latest.args +++ b/tests/qemuxml2argvdata/os-firmware-bios.x86_64-latest.args @@ -13,6 +13,7 @@ QEMU_AUDIO_DRV=none \ -object secret,id=masterKey0,format=raw,\ file=/tmp/lib/domain--1-fedora/master-key.aes \ -machine pc-q35-4.0,accel=kvm,usb=off,dump-guest-core=off \ +-cpu qemu64 \ -bios /usr/share/seabios/bios-256k.bin \ -m 8 \ -overcommit mem-lock=off \ diff --git a/tests/qemuxml2argvdata/os-firmware-efi-secboot.x86_64-latest.args b/tests/qemuxml2argvdata/os-firmware-efi-secboot.x86_64-latest.args index d8f356ea07..4de135beb8 100644 --- a/tests/qemuxml2argvdata/os-firmware-efi-secboot.x86_64-latest.args +++ b/tests/qemuxml2argvdata/os-firmware-efi-secboot.x86_64-latest.args @@ -13,6 +13,7 @@ QEMU_AUDIO_DRV=none \ -object secret,id=masterKey0,format=raw,\ file=/tmp/lib/domain--1-fedora/master-key.aes \ -machine pc-q35-4.0,accel=kvm,usb=off,smm=on,dump-guest-core=off \ +-cpu qemu64 \ -global driver=cfi.pflash01,property=secure,value=on \ -drive file=/usr/share/OVMF/OVMF_CODE.secboot.fd,if=pflash,format=raw,unit=0,\ readonly=on \ diff --git a/tests/qemuxml2argvdata/os-firmware-efi.x86_64-latest.args b/tests/qemuxml2argvdata/os-firmware-efi.x86_64-latest.args index e4dda734b0..7eba20e443 100644 --- a/tests/qemuxml2argvdata/os-firmware-efi.x86_64-latest.args +++ b/tests/qemuxml2argvdata/os-firmware-efi.x86_64-latest.args @@ -13,6 +13,7 @@ QEMU_AUDIO_DRV=none \ -object secret,id=masterKey0,format=raw,\ file=/tmp/lib/domain--1-fedora/master-key.aes \ -machine pc-q35-4.0,accel=kvm,usb=off,smm=on,dump-guest-core=off \ +-cpu qemu64 \ -drive file=/usr/share/OVMF/OVMF_CODE.secboot.fd,if=pflash,format=raw,unit=0,\ readonly=on \ -drive file=/var/lib/libvirt/qemu/nvram/fedora_VARS.fd,if=pflash,format=raw,\ diff --git a/tests/qemuxml2argvdata/tpm-emulator-tpm2-enc.x86_64-latest.args b/tests/qemuxml2argvdata/tpm-emulator-tpm2-enc.x86_64-latest.args index 3c8dc8e483..2455e14cfe 100644 --- a/tests/qemuxml2argvdata/tpm-emulator-tpm2-enc.x86_64-latest.args +++ b/tests/qemuxml2argvdata/tpm-emulator-tpm2-enc.x86_64-latest.args @@ -13,6 +13,7 @@ QEMU_AUDIO_DRV=none \ -object secret,id=masterKey0,format=raw,\ file=/tmp/lib/domain--1-TPM-VM/master-key.aes \ -machine pc-i440fx-2.12,accel=tcg,usb=off,dump-guest-core=off \ +-cpu qemu64 \ -m 2048 \ -overcommit mem-lock=off \ -smp 1,sockets=1,cores=1,threads=1 \ diff --git a/tests/qemuxml2argvdata/tpm-emulator-tpm2.x86_64-latest.args b/tests/qemuxml2argvdata/tpm-emulator-tpm2.x86_64-latest.args index 3c8dc8e483..2455e14cfe 100644 --- a/tests/qemuxml2argvdata/tpm-emulator-tpm2.x86_64-latest.args +++ b/tests/qemuxml2argvdata/tpm-emulator-tpm2.x86_64-latest.args @@ -13,6 +13,7 @@ QEMU_AUDIO_DRV=none \ -object secret,id=masterKey0,format=raw,\ file=/tmp/lib/domain--1-TPM-VM/master-key.aes \ -machine pc-i440fx-2.12,accel=tcg,usb=off,dump-guest-core=off \ +-cpu qemu64 \ -m 2048 \ -overcommit mem-lock=off \ -smp 1,sockets=1,cores=1,threads=1 \ diff --git a/tests/qemuxml2argvdata/tpm-emulator.x86_64-latest.args b/tests/qemuxml2argvdata/tpm-emulator.x86_64-latest.args index 3c8dc8e483..2455e14cfe 100644 --- a/tests/qemuxml2argvdata/tpm-emulator.x86_64-latest.args +++ b/tests/qemuxml2argvdata/tpm-emulator.x86_64-latest.args @@ -13,6 +13,7 @@ QEMU_AUDIO_DRV=none \ -object secret,id=masterKey0,format=raw,\ file=/tmp/lib/domain--1-TPM-VM/master-key.aes \ -machine pc-i440fx-2.12,accel=tcg,usb=off,dump-guest-core=off \ +-cpu qemu64 \ -m 2048 \ -overcommit mem-lock=off \ -smp 1,sockets=1,cores=1,threads=1 \ diff --git a/tests/qemuxml2argvdata/tseg-explicit-size.x86_64-latest.args b/tests/qemuxml2argvdata/tseg-explicit-size.x86_64-latest.args index 2fece310e7..53dab283cc 100644 --- a/tests/qemuxml2argvdata/tseg-explicit-size.x86_64-latest.args +++ b/tests/qemuxml2argvdata/tseg-explicit-size.x86_64-latest.args @@ -14,6 +14,7 @@ QEMU_AUDIO_DRV=none \ file=/tmp/lib/domain--1-QEMUGuest1/master-key.aes \ -machine pc-q35-2.10,accel=tcg,usb=off,smm=on,dump-guest-core=off \ -global mch.extended-tseg-mbytes=48 \ +-cpu qemu64 \ -m 214 \ -overcommit mem-lock=off \ -smp 1,sockets=1,cores=1,threads=1 \ diff --git a/tests/qemuxml2argvdata/vhost-vsock-auto.x86_64-latest.args b/tests/qemuxml2argvdata/vhost-vsock-auto.x86_64-latest.args index b5d3ff2204..e083b872c5 100644 --- a/tests/qemuxml2argvdata/vhost-vsock-auto.x86_64-latest.args +++ b/tests/qemuxml2argvdata/vhost-vsock-auto.x86_64-latest.args @@ -13,6 +13,7 @@ QEMU_AUDIO_DRV=none \ -object secret,id=masterKey0,format=raw,\ file=/tmp/lib/domain--1-test/master-key.aes \ -machine pc-i440fx-2.9,accel=tcg,usb=off,dump-guest-core=off \ +-cpu qemu64 \ -m 1024 \ -overcommit mem-lock=off \ -smp 1,sockets=1,cores=1,threads=1 \ diff --git a/tests/qemuxml2argvdata/vhost-vsock.x86_64-latest.args b/tests/qemuxml2argvdata/vhost-vsock.x86_64-latest.args index 76df9d2d4d..1b0ffee6e8 100644 --- a/tests/qemuxml2argvdata/vhost-vsock.x86_64-latest.args +++ b/tests/qemuxml2argvdata/vhost-vsock.x86_64-latest.args @@ -13,6 +13,7 @@ QEMU_AUDIO_DRV=none \ -object secret,id=masterKey0,format=raw,\ file=/tmp/lib/domain--1-test/master-key.aes \ -machine pc-0.13,accel=tcg,usb=off,dump-guest-core=off \ +-cpu qemu64 \ -m 1024 \ -overcommit mem-lock=off \ -smp 1,sockets=1,cores=1,threads=1 \ diff --git a/tests/qemuxml2xmloutdata/os-firmware-bios.x86_64-latest.xml b/tests/qemuxml2xmloutdata/os-firmware-bios.x86_64-latest.xml index 63886666dd..60d3498765 100644 --- a/tests/qemuxml2xmloutdata/os-firmware-bios.x86_64-latest.xml +++ b/tests/qemuxml2xmloutdata/os-firmware-bios.x86_64-latest.xml @@ -16,6 +16,9 @@ <apic/> <pae/> </features> + <cpu mode='custom' match='exact' check='none'> + <model fallback='forbid'>qemu64</model> + </cpu> <clock offset='utc'/> <on_poweroff>destroy</on_poweroff> <on_reboot>restart</on_reboot> diff --git a/tests/qemuxml2xmloutdata/os-firmware-efi-secboot.x86_64-latest.xml b/tests/qemuxml2xmloutdata/os-firmware-efi-secboot.x86_64-latest.xml index a285e06334..938da73711 100644 --- a/tests/qemuxml2xmloutdata/os-firmware-efi-secboot.x86_64-latest.xml +++ b/tests/qemuxml2xmloutdata/os-firmware-efi-secboot.x86_64-latest.xml @@ -16,6 +16,9 @@ <apic/> <pae/> </features> + <cpu mode='custom' match='exact' check='none'> + <model fallback='forbid'>qemu64</model> + </cpu> <clock offset='utc'/> <on_poweroff>destroy</on_poweroff> <on_reboot>restart</on_reboot> diff --git a/tests/qemuxml2xmloutdata/os-firmware-efi.x86_64-latest.xml b/tests/qemuxml2xmloutdata/os-firmware-efi.x86_64-latest.xml index 46a7b1b780..97ce8a75c7 100644 --- a/tests/qemuxml2xmloutdata/os-firmware-efi.x86_64-latest.xml +++ b/tests/qemuxml2xmloutdata/os-firmware-efi.x86_64-latest.xml @@ -16,6 +16,9 @@ <apic/> <pae/> </features> + <cpu mode='custom' match='exact' check='none'> + <model fallback='forbid'>qemu64</model> + </cpu> <clock offset='utc'/> <on_poweroff>destroy</on_poweroff> <on_reboot>restart</on_reboot> diff --git a/tests/qemuxml2xmloutdata/tpm-emulator-tpm2-enc.x86_64-latest.xml b/tests/qemuxml2xmloutdata/tpm-emulator-tpm2-enc.x86_64-latest.xml index dcd504310f..8a842e4d22 100644 --- a/tests/qemuxml2xmloutdata/tpm-emulator-tpm2-enc.x86_64-latest.xml +++ b/tests/qemuxml2xmloutdata/tpm-emulator-tpm2-enc.x86_64-latest.xml @@ -12,6 +12,9 @@ <features> <acpi/> </features> + <cpu mode='custom' match='exact' check='none'> + <model fallback='forbid'>qemu64</model> + </cpu> <clock offset='utc'/> <on_poweroff>destroy</on_poweroff> <on_reboot>restart</on_reboot> diff --git a/tests/qemuxml2xmloutdata/tpm-emulator-tpm2.x86_64-latest.xml b/tests/qemuxml2xmloutdata/tpm-emulator-tpm2.x86_64-latest.xml index 625aaa5f60..9143a1dde1 100644 --- a/tests/qemuxml2xmloutdata/tpm-emulator-tpm2.x86_64-latest.xml +++ b/tests/qemuxml2xmloutdata/tpm-emulator-tpm2.x86_64-latest.xml @@ -12,6 +12,9 @@ <features> <acpi/> </features> + <cpu mode='custom' match='exact' check='none'> + <model fallback='forbid'>qemu64</model> + </cpu> <clock offset='utc'/> <on_poweroff>destroy</on_poweroff> <on_reboot>restart</on_reboot> diff --git a/tests/qemuxml2xmloutdata/tpm-emulator.x86_64-latest.xml b/tests/qemuxml2xmloutdata/tpm-emulator.x86_64-latest.xml index 002af76c21..ff4006b2e3 100644 --- a/tests/qemuxml2xmloutdata/tpm-emulator.x86_64-latest.xml +++ b/tests/qemuxml2xmloutdata/tpm-emulator.x86_64-latest.xml @@ -12,6 +12,9 @@ <features> <acpi/> </features> + <cpu mode='custom' match='exact' check='none'> + <model fallback='forbid'>qemu64</model> + </cpu> <clock offset='utc'/> <on_poweroff>destroy</on_poweroff> <on_reboot>restart</on_reboot> diff --git a/tests/qemuxml2xmloutdata/tpm-passthrough-crb.x86_64-latest.xml b/tests/qemuxml2xmloutdata/tpm-passthrough-crb.x86_64-latest.xml index ef68c2d0d7..b8be67acfd 100644 --- a/tests/qemuxml2xmloutdata/tpm-passthrough-crb.x86_64-latest.xml +++ b/tests/qemuxml2xmloutdata/tpm-passthrough-crb.x86_64-latest.xml @@ -12,6 +12,9 @@ <features> <acpi/> </features> + <cpu mode='custom' match='exact' check='none'> + <model fallback='forbid'>qemu64</model> + </cpu> <clock offset='utc'/> <on_poweroff>destroy</on_poweroff> <on_reboot>restart</on_reboot> diff --git a/tests/qemuxml2xmloutdata/tpm-passthrough.x86_64-latest.xml b/tests/qemuxml2xmloutdata/tpm-passthrough.x86_64-latest.xml index 2a99f35cf8..669992bcf6 100644 --- a/tests/qemuxml2xmloutdata/tpm-passthrough.x86_64-latest.xml +++ b/tests/qemuxml2xmloutdata/tpm-passthrough.x86_64-latest.xml @@ -12,6 +12,9 @@ <features> <acpi/> </features> + <cpu mode='custom' match='exact' check='none'> + <model fallback='forbid'>qemu64</model> + </cpu> <clock offset='utc'/> <on_poweroff>destroy</on_poweroff> <on_reboot>restart</on_reboot> -- 2.23.0 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list