How do I pass the real/physical NVMe to a qemu machine? (Host MacOS, guest Windows)

Viewed 1018

I just wanna boot my windows install (already installed) via Qemu. I was able to do it by VMware Fusion. But it got buggy and after days trying to solve it. I give it up and thought about Qemu.

I have this lines

qemu-system-x86_64 -m 9072 -cpu Penryn,+invtsc,vmware-cpuid-freq=on,$MY_OPTIONS\
      -machine q35 \
      -smp 4,cores=2 \
      -usb -device usb-kbd -device usb-mouse \
      -smbios type=2 \
      -device ich9-ahci,id=sata \
      -drive id=WIN,format=raw,if=none,file="/dev/disk2s4",index=0,media=disk \
      -device ide-hd,bus=sata.4,drive=WIN \
      -monitor stdio \
      -vga vmware

It is "draft". I was trying out. But my issue is that I wanna pass my SSD NVMe to this machine. I couldn't find anything useful for MacOS in the internet, searching for hours. Those lines are what I found. Not even in Qemu docs I couldn't find anything.

I got "Booting from Hard Disk..." forever...

2 Answers

Well, I've tried the same but with SATA SSD drive, I just pointed to the main drive (not specific partition), and with other configurations in the args, it booted normally. Actually this SSD drive had a Windows 11 installed on it already, so just attaching it via QEMU took a little bit to configure new hardware then worked properly.

First, you need to find the disk name in Disk Utility, right click on the drive itself, not the partition. In my case it's disk1, so I'll be using /dev/disk1 when running the command.

enter image description here

See below in order, you can save this text to boot-windows.sh file, then run it from terminal with sudo.

DISK="/dev/disk1"
OVMFDIR="usr/share/edk2/ovmf" #for enabling secure EFI boot
diskutil umountDisk "$DISK" #to make sure it's forcibly unmounted
MY_OPTIONS="+ssse3,+sse4.2,+popcnt,+avx,+aes,+xsave,+xsaveopt,check"
ALLOCATED_RAM="8G" #GB
CPU_SOCKETS="2"
CPU_CORES="4"
CPU_THREADS="4"

args=(
  -m "$ALLOCATED_RAM"
  -vga virtio
  -display cocoa #default,show-cursor=off,gl=es
  -usb
  -device usb-tablet
  -smp "$CPU_THREADS",cores="$CPU_CORES",sockets="$CPU_SOCKETS"
  -drive if=ide,index=2,file="$DISK",format=raw
  -machine type=q35
  -accel hvf
  #-drive file=/Volumes/OSes/win/21H1.iso,media=cdrom,index=0
  #-drive file=virtio-win-0.1.208.iso,media=cdrom
  -nic user,model=virtio
  -rtc base=localtime,clock=host
  -cpu Nehalem,hv_relaxed,hv_spinlocks=0x1fff,hv_vapic,hv_time 
  -device intel-hda
  -drive if=pflash,format=raw,readonly=on,file="$OVMFDIR"/OVMF_CODE.fd
  -drive if=pflash,format=raw,readonly=on,file="$OVMFDIR"/OVMF_VARS.fd
  -boot c
)

qemu-system-x86_64 "${args[@]}"

Make sure you download the win-virtio drivers if needed. Files needed for enabling secure EFI boot can be downloaded here.

Credits:

  1. Kholia's detailed GitHub project.
  2. Hikmat Ustad's amazing video tutorial.
Related