foxlet / macOS-Simple-KVM

Tools to set up a quick macOS VM in QEMU, accelerated by KVM.
13.59k stars 1.14k forks source link

iPhone passthrough #221

Open Vankata03 opened 4 years ago

Vankata03 commented 4 years ago

Can someone explain, how can I get my iPhone passthrough to the qemu vm? Is it possible at all? Maybe a step by step guide?

egarim commented 4 years ago

I have been trying to do the same, with android is super easy, but with ios there is a problem at least in my current setup, I'm using ubuntu 18.10 and the problem I'm facing is that the camera and documents get mounted on my host machine and I can't unmount them to redirect the USB, I think is more of a problem of my host, not a problem of the guest mac vm

laurentbh commented 4 years ago

I struggled with udev conf for a while, trying to bypass something that looks to be an ios usb mux, and gave up :( The solution I found is to dedicated an USB controller to the VM; I followed some blogs, can't find them anymore, but I've dumped my notes here hope it can help!

note: you must hace a proc that allows io_mmu

Vankata03 commented 4 years ago

I struggled with udev conf for a while, trying to bypass something that looks to be an ios usb mux, and gave up :(

The solution I found is to dedicated an USB controller to the VM; I followed some blogs, can't find them anymore, but I've dumped my notes here

hope it can help!

note: you must hace a proc that allows io_mmu

So I managed to do it, but macOS does not recognize anything? They show up in the usb section in system information, but not in the finder, mouse does not work, too.

laurentbh commented 4 years ago

make sure you don't dedicated a controller used by your host and qEMU

Vankata03 commented 4 years ago

make sure you don't dedicated a controller used by your host and qEMU

I detach it from the host and then attach it to QEMU.

Julioevm commented 4 years ago

I'd be interested to know if someone manages too.

Yarkhan commented 4 years ago

The only way I've managed to get this working was adding a PCI-E USB card to my pc and passthrough it to the VM. I've been using this to develop apps for iOS without problems for about 2 years now. Obviosly, that requires a desktop instead a laptop. image

peterandrewd commented 4 years ago

Well well after a lot of tinkering I figured out a way to mount USB devices in QEMU (not virt(shit) manager). Its not just for 1 time use only. You can disconnect it, replug it and still access it. I tested it with 2 different USB drives.

As you can see in the screenshots, the USB drives show up. Still can’t get iPhone mounted though but at least USB drives mount easily now.

image

image

reinismu commented 4 years ago

Well well after a lot of tinkering I figured out a way to mount USB devices in QEMU (not virt(shit) manager). Its not just for 1 time use only. You can disconnect it, replug it and still access it. I tested it with 2 different USB drives.

As you can see in the screenshots, the USB drives show up. Still can’t get iPhone mounted though but at least USB drives mount easily now.

image

image

Could you please document how you achieved it? :)

Yarkhan commented 4 years ago

The only way I've managed to get this working was adding a PCI-E USB card to my pc and passthrough it to the VM. I've been using this to develop apps for iOS without problems for about 2 years now. Obviosly, that requires a desktop instead a laptop. image

Just a quick update. I've just recently migrated to AMD (ryzen 5 3600 on an asrock b450m steel legend mobo). This mobo has a usb IOMMU group that is not shared with anything else. That makes possible to passthrough it, and it makes 4 of the usb on the motherboard available to the VM. In short, no need of usb pci-e cards :)

hockeymikey commented 3 years ago

Well well after a lot of tinkering I figured out a way to mount USB devices in QEMU (not virt(shit) manager). Its not just for 1 time use only. You can disconnect it, replug it and still access it. I tested it with 2 different USB drives.

As you can see in the screenshots, the USB drives show up. Still can’t get iPhone mounted though but at least USB drives mount easily now.

How did you do this?

themotu commented 3 years ago

The only way I've managed to get this working was adding a PCI-E USB card to my pc and passthrough it to the VM. I've been using this to develop apps for iOS without problems for about 2 years now. Obviosly, that requires a desktop instead a laptop. image

Just a quick update. I've just recently migrated to AMD (ryzen 5 3600 on an asrock b450m steel legend mobo). This mobo has a usb IOMMU group that is not shared with anything else. That makes possible to passthrough it, and it makes 4 of the usb on the motherboard available to the VM. In short, no need of usb pci-e cards :)

same here, lucky us

leandrobattochio commented 3 years ago

how did you passtrhoguh the entire controller?

Malikiah commented 3 years ago

@SheenBR You would need to do a PCI passthough, you would need to make it so that the IOMMU group for the controller were not taken by the host deivce. Depending on which distro you are running and what kind of motherboard you have this will be more or less difficult if your mother board has poor iommu group mapping you would need to recompile your kernel with the ACS patch. Here is some information. They explain how to do it with a graphics card but it works the same for any PCI device. https://forum.level1techs.com/t/vfio-in-2019-fedora-workstation-general-guide-though-branch-draft/145106 https://copr.fedorainfracloud.org/coprs/jlay/kernel-acspatch/

fahimfarhan commented 3 years ago

I edited the basic.sh file and appended this line: -device usb-host,vendorid=0x<your-iphone-vendor-id>,productid=0x<your-iphone-product-id>,guest-reset=false \. Here, the guest-reset=false is the argument that worked for me. I hope it works for others as well. Good luck. Reference: stackoverflow answer

hockeymikey commented 3 years ago

@fahimfarhan And that works fine? The iPhone works in xcode?

fahimfarhan commented 3 years ago

@fahimfarhan And that works fine? The iPhone works in xcode?

Yes. After connecting my iPhone, I opened a tutorial, made a sample app, and installed it on my iPhone. Here is a photo of my app.

USER_SCOPED_TEMP_DATA_MSGR_PHOTO_FOR_UPLOAD_1629525018190_6834723301900083908.jpeg
hockeymikey commented 3 years ago

@fahimfarhan And that works fine? The iPhone works in xcode?

Yes. After connecting my iPhone, I opened a tutorial, made a sample app, and installed it on my iPhone. Here is a photo of my app.)

It just freezes and crashes my vm. What version are you on?

fahimfarhan commented 3 years ago

@fahimfarhan And that works fine? The iPhone works in xcode?

Yes. After connecting my iPhone, I opened a tutorial, made a sample app, and installed it on my iPhone. Here is a photo of my app.)

It just freezes and crashes my vm. What version are you on?

@hockeymikey I am using kvm version 4.2.1 (Debian 1:4.2-3ubuntu6.17), MacOS Catalina, and Xcode version 12.4. My host OS is linux mint 20.2.

(base) soumic@Zephyrus-G14:~/Desktop$ kvm --version
QEMU emulator version 4.2.1 (Debian 1:4.2-3ubuntu6.17)
Copyright (c) 2003-2019 Fabrice Bellard and the QEMU Project developers
(base) soumic@Zephyrus-G14:~/Desktop$ screenfetch
                                       soumic@Zephyrus-G14
 MMMMMMMMMMMMMMMMMMMMMMMMMmds+.        OS: Linuxmint 20.2 uma
 MMm----::-://////////////oymNMd+`     Kernel: x86_64 Linux 5.13.0-1009-oem
 MMd      /++                -sNMd:    Uptime: 47m
 MMNso/`  dMM    `.::-. .-::.` .hMN:   Packages: 2841
 ddddMMh  dMM   :hNMNMNhNMNMNh: `NMm   Shell: bash 5.0.17
     NMm  dMM  .NMN/-+MMM+-/NMN` dMM   Resolution: 1920x1080
     NMm  dMM  -MMm  `MMM   dMM. dMM   DE: Cinnamon 5.0.5
     NMm  dMM  -MMm  `MMM   dMM. dMM   WM: Muffin
     NMm  dMM  .mmd  `mmm   yMM. dMM   WM Theme: Mint-Y (Mint-Y)
     NMm  dMM`  ..`   ...   ydm. dMM   GTK Theme: Mint-Y [GTK2/3]
     hMM- +MMd/-------...-:sdds  dMM   Icon Theme: Paper
     -NMm- :hNMNNNmdddddddddy/`  dMM   Font: Ubuntu 10
      -dMNs-``-::::-------.``    dMM   Disk: 208G / 529G (42%)
       `/dMNmy+/:-------------:/yMMM   CPU: AMD Ryzen 9 4900HS with Radeon Graphics @ 16x 3GHz
          ./ydNMMMMMMMMMMMMMMMMMMMMM   GPU: NVIDIA GeForce RTX 2060 with Max-Q Design
             \.MMMMMMMMMMMMMMMMMMM     RAM: 9776MiB / 31525MiB

(base) soumic@Zephyrus-G14:~/Desktop$ 
hockeymikey commented 3 years ago

@hockeymikey I am using kvm version 4.2.1 (Debian 1:4.2-3ubuntu6.17), MacOS Catalina, and Xcode version 12.4. My host OS is linux mint 20.2.

Probably a G14 then right? I'm using an Omen 15 so you're not passing through any controllers then, just the device. Can I see your startup script if you don't mind? Just wanna double check and rule everything out.

fahimfarhan commented 3 years ago

@hockeymikey Yes, a G14. Sure! This is my basic.sh script:

#!/bin/bash

OSK="ourhardworkbythesewordsguardedpleasedontsteal(c)AppleComputerInc"
VMDIR=$PWD
OVMF=$VMDIR/firmware
#export QEMU_AUDIO_DRV=pa
#QEMU_AUDIO_DRV=pa

qemu-system-x86_64 \
    -enable-kvm \
    -m 20G \
    -machine q35,accel=kvm \
    -smp 16,cores=8 \
    -cpu Penryn,vendor=GenuineIntel,kvm=on,+sse3,+sse4.2,+aes,+xsave,+avx,+xsaveopt,+xsavec,+xgetbv1,+avx2,+bmi2,+smep,+bmi1,+fma,+movbe,+invtsc \
    -device isa-applesmc,osk="$OSK" \
    -smbios type=2 \
    -drive if=pflash,format=raw,readonly,file="$OVMF/OVMF_CODE.fd" \
    -drive if=pflash,format=raw,file="$OVMF/OVMF_VARS-1024x768.fd" \
    -vga qxl \
    -device ich9-intel-hda -device hda-output \
    -usb -device usb-kbd -device usb-mouse \
    -netdev user,id=net0 \
    -device e1000-82545em,netdev=net0,id=net0,mac=52:54:00:c9:18:27 \
    -device ich9-ahci,id=sata \
    -drive id=ESP,if=none,format=qcow2,file=ESP.qcow2 \
    -device ide-hd,bus=sata.2,drive=ESP \
    -drive id=InstallMedia,format=raw,if=none,file=BaseSystem.img \
    -device ide-hd,bus=sata.3,drive=InstallMedia \
    -drive id=SystemDisk,if=none,file=catalina.qcow2 \
    -device ide-hd,bus=sata.4,drive=SystemDisk \
    -device usb-host,vendorid=0x05ac,productid=0x12a8,guest-reset=false \

Here, the last line (-device usb-host,vendorid=0x05ac,productid=0x12a8,guest-reset=false \) is for the iPhone 8.

hockeymikey commented 3 years ago

@fahimfarhan Strange... I have a similar basic.sh but no luck on my end

hockeymikey commented 2 years ago

I still can't get this to work. My basic.sh is identical to @fahimfarhan 's and I've tried different ones such as using an ehci host defined. Tried every port on my laptop too. So what happens is that whenever I plug it into my laptop while the vm is running, it freezes up and the macos system crashes and reboots. If I start the vm with the device plugged in, it doesn't see it either but boots all the way up. The iphone reboots when it gets unplugged from the laptop.

hockeymikey commented 2 years ago

I finally got passthrough working!!!! I used https://github.com/sickcodes/Docker-OSX#usbfluxd-iphone-usb---network-style-passthrough-osx-kvm-docker-osx to do it, works like a charm. Only issue I ran into is that as an Arch user I was compiling from git from the AUR for usbfluxd, usbmuxd, etc so on the MacOS vm I had to do the same compiling from git everything. It was alittle tricky getting the paths right and what not, but works like a charm now. Guest needs to be reachable by the host, I use virt-manager's NAT (which I could never get working in basic.sh), but can just have it bridged directly to whatever network you are on too.

413ph commented 2 years ago

@hockeymikey Yes, a G14. Sure! This is my basic.sh script:

#!/bin/bash

OSK="ourhardworkbythesewordsguardedpleasedontsteal(c)AppleComputerInc"
VMDIR=$PWD
OVMF=$VMDIR/firmware
#export QEMU_AUDIO_DRV=pa
#QEMU_AUDIO_DRV=pa

qemu-system-x86_64 \
    -enable-kvm \
    -m 20G \
    -machine q35,accel=kvm \
    -smp 16,cores=8 \
    -cpu Penryn,vendor=GenuineIntel,kvm=on,+sse3,+sse4.2,+aes,+xsave,+avx,+xsaveopt,+xsavec,+xgetbv1,+avx2,+bmi2,+smep,+bmi1,+fma,+movbe,+invtsc \
    -device isa-applesmc,osk="$OSK" \
    -smbios type=2 \
    -drive if=pflash,format=raw,readonly,file="$OVMF/OVMF_CODE.fd" \
    -drive if=pflash,format=raw,file="$OVMF/OVMF_VARS-1024x768.fd" \
    -vga qxl \
    -device ich9-intel-hda -device hda-output \
    -usb -device usb-kbd -device usb-mouse \
    -netdev user,id=net0 \
    -device e1000-82545em,netdev=net0,id=net0,mac=52:54:00:c9:18:27 \
    -device ich9-ahci,id=sata \
    -drive id=ESP,if=none,format=qcow2,file=ESP.qcow2 \
    -device ide-hd,bus=sata.2,drive=ESP \
    -drive id=InstallMedia,format=raw,if=none,file=BaseSystem.img \
    -device ide-hd,bus=sata.3,drive=InstallMedia \
    -drive id=SystemDisk,if=none,file=catalina.qcow2 \
    -device ide-hd,bus=sata.4,drive=SystemDisk \
    -device usb-host,vendorid=0x05ac,productid=0x12a8,guest-reset=false \

Here, the last line (-device usb-host,vendorid=0x05ac,productid=0x12a8,guest-reset=false \) is for the iPhone 8.

After messing with my vfio and kernel still got nothing done, I saw this thread. This works for me just fine! Thanks!

Midar commented 2 years ago

-device usb-host,vendorid=0x05ac,productid=0x12a8,guest-reset=false doesn't work for me either, verified with lsusb that those are correct. I wonder if usbmuxd could cause issues?

simmons2714 commented 2 years ago

I finally got passthrough working!!!! I used https://github.com/sickcodes/Docker-OSX#usbfluxd-iphone-usb---network-style-passthrough-osx-kvm-docker-osx to do it, works like a charm. Only issue I ran into is that as an Arch user I was compiling from git from the AUR for usbfluxd, usbmuxd, etc so on the MacOS vm I had to do the same compiling from git everything. It was alittle tricky getting the paths right and what not, but works like a charm now. Guest needs to be reachable by the host, I use virt-manager's NAT (which I could never get working in basic.sh), but can just have it bridged directly to whatever network you are on too.

Wait so you just used the AUR version of usbfluxd? I've been trying that but my iPhone won't show up in iTunes or Finder. I even tried compiling usbfluxd and I keep getting configure: error: The file /libplist-2.0.a passed to --with-static-libplist does not exist. Any ideas?

notAperson535 commented 2 years ago

@simmons2714 I think you can just pass through the usb port to the VM in Virt-Manager

simmons2714 commented 2 years ago

@notAperson535 I've tried that and with iphones sometimes that works and sometimes it doesn't.

hockeymikey commented 2 years ago

@simmons2714 No idea. Try compiling them all. I think I use git versions for all of them from AUR.

wpyoga commented 1 year ago

So I figured out the issue. If usbmuxd is installed on the host, the iPhone usb interface will be captured by usbmuxd, and it will be initialized already before being passed through to the VM. This renders the device unusable by Xcode.

The fact that assigning the USB controller to the VM works is because usbmuxd won't even be able to initialize the iPhone. In this way, the iPhone's USB interface is handed over to the VM raw, and then usbmux on the VM takes care of the rest.

However, assigning the whole USB controller seems a little excessive. What I've done instead is disable usbmuxd.

On my PC, there is a file /usr/lib/udev/rules.d/39-usbmuxd.rules. So I created a file /etc/udev/rules.d/39-usbmuxd.rules to override it:

# filename is chosen to overwrite /usr/lib/udev/rules.d/39-usbmuxd.rules
# see RULES FILES section in udev(7)
SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", ENV{PRODUCT}=="5ac/12[9a][0-9a-f]/*|5ac/8600/*", MODE="0666", TAG+="systemd"

Then I reloaded systemd-udevd:

$ sudo systemctl reload systemd-udevd

Now, I can plug the iPhone to my PC, and pass it through to the VM, by issuing this command on the qemu monitor:

(qemu) device_add usb-host,id=iphone,vendorid=0x05ac,productid=0x12a8

This way, the iPhone and VM will behave as if the iPhone is plugged directly into a Mac's USB port. It appears in Xcode just fine, and I can use it just fine. I can also add -device usb-host,vendorid=0x05ac,productid=0x12a8 to the qemu command line.

Note that I didn't add the guest-reset=false option, contrary to @fahimfarhan 's suggestion. That option makes the VM unable to reset the iPhone, so I had to unplug and plug it in again manually after trusting the VM on the iPhone.

That's it. Let me know if it works on your setup!

simmons2714 commented 1 year ago

@wpyoga This method works. Now I’m able to connect my iPhone to the vm. Thanks.

applebrickedmyipad commented 8 months ago

Tried everything on here with no luck

Best I can get is a constant disconnect and reconnect while the terminal spits a bunch of libusb errors at me libusb: error [_open_sysfs_attr] open /sys/bus/usb/devices/1-13.2/bConfigurationValue failed ret=-1 errno=2 libusb: error [udev_hotplug_event] ignoring udev action bind libusb: error [_get_usbfs_fd] libusb couldn't open USB device /dev/bus/usb/001/118: No such device libusb: error [udev_hotplug_event] ignoring udev action bind libusb: error [udev_hotplug_event] ignoring udev action bind libusb: error [_get_usbfs_fd] libusb couldn't open USB device /dev/bus/usb/001/120: No such device libusb: error [udev_hotplug_event] ignoring udev action bind libusb: error [udev_hotplug_event] ignoring udev action bind libusb: error [_get_usbfs_fd] libusb couldn't open USB device /dev/bus/usb/001/122: No such device libusb: error [udev_hotplug_event] ignoring udev action bind libusb: error [udev_hotplug_event] ignoring udev action bind libusb: error [_get_usbfs_fd] libusb couldn't open USB device /dev/bus/usb/001/124: No such device libusb: error [udev_hotplug_event] ignoring udev action bind libusb: error [udev_hotplug_event] ignoring udev action bind libusb: error [_get_usbfs_fd] libusb couldn't open USB device /dev/bus/usb/001/126: No such device libusb: error [udev_hotplug_event] ignoring udev action bind libusb: error [udev_hotplug_event] ignoring udev action bind libusb: error [_get_usbfs_fd] libusb couldn't open USB device /dev/bus/usb/001/003: No such device libusb: error [udev_hotplug_event] ignoring udev action bind

I have no interest in giving up but it's been more than 6 hours and I've exhausted every idea

hockeymikey commented 8 months ago

@applebrickedmyipad permissions error bro on the usb device. Fix that, a simple search of your errors will help.

applebrickedmyipad commented 8 months ago

@applebrickedmyipad permissions error bro on the usb device. Fix that, a simple search of your errors will help.

I don't know what a permission error bro is, but the device connects to everything except for the VM

A simple search of my errors did not help. That's why I am asking here.

bluesailor45 commented 4 months ago

I have inserted the '-device ...' code as suggested in 'https://stackoverflow.com/questions/60930827/qemu-usb-passthrough-iphone-disconnects-repeatedly/66251533#66251533' and was able to see the iPhone on the 'System information -> Hardware -> USB:

iPhone:

Product ID: 0x12a8 Vendor ID: 0x05ac (Apple Inc.) Version: 13.01 Serial Number: (correct Number, deleted) Speed: Up to 480 Mb/s Manufacturer: Apple Inc. Location ID: 0xfd300000 / 4 Current Available (mA): 500 Extra Operating Current (mA):

But unfortunately it is not shown/accessible in the Finder. So it is also not accessible in XCode. What is missing?

rikool050 commented 2 months ago

Hi all,

I'm posting to maybe help people like me that tried for many hours to allow QEMU to have access to the iPhone.

I have an ASUS UX425E, Ubuntu 22.04. Qemu macOS Ventura.

I tried many things:

I tried all of these too :

With the links above I understood that the problem was Ubuntu catching the iPhone before QEMU would access it. Both usbmuxd and gvfs-gphoto would cause problems.

Solution:

  1. Disable usbmuxd on Ubuntu (like @wpyoga)
  2. Commenting the rules in /usr/lib/udev/rules.d/39-usbmuxd.rules
  3. Reload the rules: sudo systemctl reload systemd-udevd
  4. Disable gvfs-gphoto2 sudo chmod 0000 /usr/lib/systemd/user/gvfs-gphoto2-volume-monitor.service sudo kill $(ps -A | grep gvfs.*gphoto.* | awk '{print $1}')
  5. Adding the line -device usb-host,id=iphone,vendorid=0x05ac,guest-reset=false to my boot script.
  6. Run the vm.
  7. Allow/Trust your VM when the pop up appears it should now be working in your VM.

Even though sometimes i see in the terminal 'qemu-system-x86_64: libusb_release_interface: -4 [NO_DEVICE]' it's fully working for me. If your iPhone disappears from your VM try to unplug/plug back your iPhone that worked for me.

If you want to have access to your iPhone from Ubuntu again you need to do those steps:

For the photos : sudo chmod 0644 /usr/lib/systemd/user/gvfs-gphoto2-volume-monitor.service To have access to 'documents':

  1. Commenting the line in that file : /etc/udev/rules.d/39-usbmuxd.rules
  2. Uncommenting the lines in : /usr/lib/udev/rules.d/39-usbmuxd.rules

Unplug/plug and you should access your iPhone from the HOST.

I hope it'll help people that struggled like me.

notAperson535 commented 3 weeks ago

@rikool050 thanks for this. I turned it into a full working guide available here for everyone: https://oneclick-macos-simple-kvm.notaperson535.is-a.dev/docs/guide-phone-passthrough/

w99910 commented 3 weeks ago

@rikool050 thanks for this. I turned it into a full working guide available here for everyone: https://oneclick-macos-simple-kvm.notaperson535.is-a.dev/docs/guide-phone-passthrough/

This is also a working example for me with XCode 15.4 and Sonoma. I have written an article about it. You can find it here if you want to read it.

notAperson535 commented 3 weeks ago

@w99910 Yeah looks very similar to mine, glad people have finally figured out a working method for this.

PixsaOJ commented 2 days ago

I thought it was over, after following @w99910 But then! I killed usbmuxd and VM detected my iphone !