Below is a revised walkthrough that’s largely the same as before but with the steps adjusted for a system using systemd-boot instead of GRUB.
1. Install Prerequisites
Install the required packages:
sudo pacman -S qemu libvirt virt-manager ovmf dnsmasq edk2-ovmf
Enable and start the libvirt service:
sudo systemctl enable libvirtd.service
sudo systemctl start libvirtd.service
2. Enable IOMMU in systemd-boot
Instead of editing GRUB’s configuration, you’ll need to update the kernel command line for systemd-boot. There are a couple of ways to do this on Arch:
Option A: Edit the Loader Entry File
-
Locate the loader entry in your systemd-boot configuration (usually in /boot/loader/entries/). For example, if you have an arch.conf, open it with your editor:
sudo nano /boot/loader/entries/arch.conf
-
Find the line beginning with options and append the IOMMU parameters to it. For example:
-
Save and exit the editor.
Option B: Use a Dedicated Cmdline File
On some Arch installations you might have a dedicated /etc/kernel/cmdline file that systemd-boot uses to build the final kernel command line. In that case, simply add your IOMMU parameters to that file. For example:
# For Intel:
intel_iommu=on iommu=pt
# Or for AMD:
# amd_iommu=on iommu=pt
After updating the kernel command line, reboot your system for the changes to take effect.
3. Configure VFIO for GPU Passthrough
-
Identify your GPU and its Audio Device IDs:
Run:
lspci -nnv | grep -i vga
lspci -nnv | grep -i audio
Note the PCI IDs (format: 1234:5678).
-
Create a modprobe configuration file for VFIO:
Edit (or create) the file /etc/modprobe.d/vfio.conf:
sudo nano /etc/modprobe.d/vfio.conf
Add a line similar to:
options vfio-pci ids=1234:5678,1234:5679
Replace the IDs with your GPU and its associated audio device's IDs.
-
Add VFIO modules to the initramfs:
Edit /etc/mkinitcpio.conf and add the modules (if not already present) to the MODULES array:
MODULES="vfio vfio_iommu_type1 vfio_pci vfio_virqfd"
-
Regenerate your initramfs:
sudo mkinitcpio -P
4. Create GPU Detach/Reattach Scripts
These scripts help to detach the GPU from the host before starting the VM and reattach it after you’re done.
Detach Script
Create a script at /usr/local/bin/gpu-detach.sh:
sudo nano /usr/local/bin/gpu-detach.sh
Paste the following (adjust as needed):
#!/bin/bash
set -x
# Unbind VTconsoles
echo 0 > /sys/class/vtconsole/vtcon0/bind
echo 0 > /sys/class/vtconsole/vtcon1/bind
# Unbind EFI Framebuffer
echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/unbind
# Uncomment and adjust if you need to unload your GPU driver, for example:
# modprobe -r nvidia_drm nvidia_modeset nvidia
# modprobe -r amdgpu
# Unbind the GPU from the host (adjust the PCI address accordingly)
virsh nodedev-detach pci_0000_XX_XX_X
Replace pci_0000_XX_XX_X with your GPU PCI address in the proper format.
Reattach Script
Create a script at /usr/local/bin/gpu-reattach.sh:
sudo nano /usr/local/bin/gpu-reattach.sh
Paste the following:
#!/bin/bash
set -x
# Reattach the GPU to the host (adjust the PCI address accordingly)
virsh nodedev-reattach pci_0000_XX_XX_X
# Rebind the framebuffer
echo 1 > /sys/class/vtconsole/vtcon0/bind
echo "efi-framebuffer.0" > /sys/bus/platform/drivers/efi-framebuffer/bind
# Optionally, reload graphics drivers if needed:
# modprobe nvidia_drm nvidia_modeset nvidia
# modprobe amdgpu
# Restart your display manager
systemctl restart display-manager
Again, change pci_0000_XX_XX_X to your GPU’s PCI address.
Make both scripts executable:
sudo chmod +x /usr/local/bin/gpu-detach.sh
sudo chmod +x /usr/local/bin/gpu-reattach.sh
5. Prepare macOS Installation Files
Follow a macOS installation guide (such as macOS-Simple-KVM or OpenCore-Install-Guide) to obtain the macOS installation files and ensure you have your BaseSystem image (often named BaseSystem.img or similar).
6. Create the QEMU VM Definition
Create an XML configuration file (e.g., macos.xml) for your VM. Here’s a sample:
<domain type='kvm'>
<name>macos</name>
<memory unit='KiB'>8388608</memory>
<vcpu placement='static'>8</vcpu>
<!-- CPU configuration with Penryn model -->
<cpu mode='custom' match='exact'>
<model>Penryn</model>
</cpu>
<os>
<type arch='x86_64'>hvm</type>
<loader readonly='yes' type='pflash'>/usr/share/edk2-ovmf/x64/OVMF_CODE.fd</loader>
<nvram>/var/lib/libvirt/qemu/nvram/macos_VARS.fd</nvram>
</os>
<devices>
<!-- Disk for macOS -->
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/path/to/macos.qcow2'/>
<target dev='sda' bus='sata'/>
</disk>
<!-- BaseSystem installation image -->
<disk type='file' device='cdrom'>
<source file='/path/to/BaseSystem.img'/>
<target dev='sdb' bus='sata'/>
</disk>
<!-- GPU passthrough configuration -->
<hostdev mode='subsystem' type='pci' managed='yes'>
<source>
<address domain='0x0000' bus='0x01' slot='0x00' function='0x0'/>
</source>
</hostdev>
<!-- GPU audio function -->
<hostdev mode='subsystem' type='pci' managed='yes'>
<source>
<address domain='0x0000' bus='0x01' slot='0x00' function='0x1'/>
</source>
</hostdev>
</devices>
</domain>
Make sure to update:
- Paths for your disk images.
- The number of vCPUs/memory.
- The
<address> elements for the GPU and its audio function (using your actual PCI addresses).
7. Create the macOS QCOW2 Disk Image
Create your virtual hard disk image:
qemu-img create -f qcow2 /path/to/macos.qcow2 100G
8. Configure libvirt Hooks for GPU Passthrough
Use libvirt’s hook mechanism to automatically detach your GPU when starting the VM and reattach it after shutdown.
-
Create/edit the hook file at /etc/libvirt/hooks/qemu:
sudo nano /etc/libvirt/hooks/qemu
Place the following content:
#!/bin/bash
if [ "$1" = "macos" ]; then
if [ "$2" = "prepare" ]; then
/usr/local/bin/gpu-detach.sh
elif [ "$2" = "release" ]; then
/usr/local/bin/gpu-reattach.sh
fi
fi
-
Make this hook script executable:
sudo chmod +x /etc/libvirt/hooks/qemu
9. Define and Start Your VM
-
Define the virtual machine in libvirt:
virsh define macos.xml
-
Start the VM:
virsh start macos
During the startup process, the hook should automatically invoke your GPU detach script, and upon shutdown, it will reattach the GPU.
Final Steps
- Post-Installation: After installing macOS, you might need to further configure the OpenCore bootloader or make adjustments (patches, additional drivers, etc.) for optimal performance.
- Troubleshooting: Double-check your IOMMU groups with
find /sys/kernel/iommu_groups/ -type l to ensure your GPU is in an isolated group.
- Backups: Always have a backup of important configuration files before making changes.
This revised guide should help you set up a QEMU VM with single GPU passthrough for macOS on an Arch Linux system running systemd-boot. If you have any questions or need further clarification on any step, feel free to ask!