QEMU CLI创建macOS虚拟机转Libvirt XML时virsh命令报错求助
I feel your pain—virsh domxml-from-native qemu-argv is notoriously limited, especially for complex VMs like macOS that rely on non-standard QEMU devices and niche configurations. That error message simply means the Libvirt QEMU driver can’t automatically parse your specific argv string into valid XML. Let’s build the XML manually step by step, mapping every part of your existing QEMU command to Libvirt’s structure.
Step 1: Start with a Base Q35 VM Skeleton
Create a new file named macos-highsierra.xml with this core template, matching your VM’s foundational settings:
<domain type='kvm'> <name>macos-highsierra</name> <uuid>$(uuidgen)</uuid> <!-- Run `uuidgen` in terminal to generate a unique UUID --> <memory unit='GiB'>16</memory> <currentMemory unit='GiB'>16</currentMemory> <vcpu placement='static' current='16'>16</vcpu> <os> <type arch='x86_64' machine='pc-q35-2.9'>hvm</type> <boot dev='c'/> <loader readonly='yes' type='pflash'>/full/path/to/OVMF_CODE.fd</loader> <nvram>/full/path/to/OVMF_VARS-3440x1440.fd</nvram> </os> <features> <acpi/> <apic/> <vmport state='off'/> </features>
Step 2: Map Your CPU Configuration
macOS is picky about CPU flags—add this section right after <features> to mirror your QEMU CPU settings:
<cpu mode='custom' match='exact' check='partial'> <model fallback='allow'>Penryn</model> <vendor>GenuineIntel</vendor> <feature policy='require' name='kvm'/> <feature policy='require' name='invtsc'/> <feature policy='require' name='vmware-cpuid-freq'/> <feature policy='require' name='aes'/> <feature policy='require' name='xsave'/> <feature policy='require' name='avx'/> <feature policy='require' name='xsaveopt'/> <feature policy='require' name='avx2'/> <feature policy='require' name='smep'/> <topology sockets='1' cores='8' threads='2'/> </cpu>
Step 3: Add SMBIOS & Clock Settings
Match your -smbios type=2 and -rtc parameters with these sections:
<smbios mode='sysinfo'> <system> <entry name='manufacturer'>Apple Inc.</entry> <entry name='product'>iMac18,3</entry> <!-- Adjust to match your preferred SMBIOS profile --> </system> </smbios> <clock offset='localtime'> <timer name='rtc' tickpolicy='catchup'/> <timer name='pit' tickpolicy='delay'/> <timer name='hpet' present='no'/> </clock>
Step 4: Add Storage Devices
Map your Clover EFI disk and physical MacHDD to Libvirt’s disk format:
<devices> <!-- Critical AppleSMC device with OSK --> <emulator>/usr/bin/qemu-system-x86_64</emulator> <device type='misc'> <driver name='applesmc'/> <property name='osk' value='ourhardworkbythesewordsguardedpleasedontsteal(c)AppleComputerInc'/> </device> <!-- Clover EFI disk (snapshot mode enabled) --> <disk type='file' device='disk'> <driver name='qemu' type='qcow2' snapshot='on'/> <source file='/full/path/to/HighSierra/Clover.qcow2'/> <target dev='hda' bus='ide'/> <address type='drive' controller='0' bus='0' target='0' unit='0'/> </disk> <!-- Physical MacHDD (raw disk access) --> <disk type='block' device='disk'> <driver name='qemu' type='raw'/> <source dev='/dev/disk/by-id/ata-SanDisk_SDSSDA120G_163757447010'/> <target dev='hdb' bus='ide'/> <address type='drive' controller='0' bus='1' target='0' unit='0'/> </disk> <!-- IDE controller for above disks --> <controller type='ide' index='0'> <address type='pci' domain='0x0000' bus='0x01' slot='0x01' function='0x0'/> </controller>
Step 5: Add Audio, Network, and VFIO GPU Passthrough
Include your audio device, network adapter, and GPU passthrough setup:
<!-- Audio device --> <sound model='ich9-intel-hda'> <address type='pci' domain='0x0000' bus='0x00' slot='0x1b' function='0x0'/> </sound> <audio id='1' type='pulseaudio'/> <!-- Network adapter --> <interface type='user'> <mac address='52:54:00:12:34:56'/> <!-- Generate a unique MAC address --> <model type='e1000-82545em'/> </interface> <!-- VFIO GPU passthrough (multifunction device) --> <hostdev mode='subsystem' type='pci' managed='yes'> <source> <address domain='0x0000' bus='0x08' slot='0x00' function='0x0'/> </source> <address type='pci' domain='0x0000' bus='0x02' slot='0x00' function='0x0' multifunction='on'/> </hostdev> <hostdev mode='subsystem' type='pci' managed='yes'> <source> <address domain='0x0000' bus='0x08' slot='0x00' function='0x1'/> </source> <address type='pci' domain='0x0000' bus='0x02' slot='0x00' function='0x1'/> </hostdev>
Step 6: Add USB Passthrough & Headless Settings
Map your USB host devices and headless flags:
<!-- USB host passthrough devices --> <hostdev mode='subsystem' type='usb' managed='yes'> <source> <address bus='5' port='4'/> </source> </hostdev> <hostdev mode='subsystem' type='usb' managed='yes'> <source> <address bus='5' port='4.1'/> </source> </hostdev> <hostdev mode='subsystem' type='usb' managed='yes'> <source> <address bus='5' port='4.2'/> </source> </hostdev> <hostdev mode='subsystem' type='usb' managed='yes'> <source> <address bus='5' port='4.3'/> </source> </hostdev> <hostdev mode='subsystem' type='usb' managed='yes'> <source> <address bus='5' port='4.4'/> </source> </hostdev> <!-- Headless settings (match -vga none, -nographic etc.) --> <input type='keyboard' bus='usb'/> <input type='tablet' bus='usb'/> <graphics type='none'/> <console type='pty'> <target type='serial' port='0'/> </console> <serial type='none'/> <parallel type='none'/> </devices> </domain>
Step 7: Validate and Import the XML
- Replace all
/full/path/to/placeholders with your actual file locations. - Validate the XML syntax to catch errors:
virsh validate macos-highsierra.xml - Define the VM in Libvirt:
virsh define macos-highsierra.xml - Start the VM:
virsh start macos-highsierra
Key Notes
- Ensure your Fedora 31 system has VFIO properly configured (add
intel_iommu=onoramd_iommu=onto kernel parameters, bind your GPU to thevfio-pcidriver). - If USB passthrough fails, verify the host isn’t using those USB devices.
- Adjust the SMBIOS product name (e.g.,
iMac19,1) to match your macOS version for better compatibility.
内容的提问来源于stack exchange,提问作者cosmin.danisor

