How to use USB with QEMU on a Mac host?
Asked Answered
C

3

7

I tried the following to access a USB storage device via an Ubuntu guest running on macOS host:

sudo qemu-system-x86_64 -m 8G -boot d -smp 4 -net nic -net user \
   -hda Ubuntu/ubuntu.img -machine type=q35,accel=hvf \
   -device intel-hda -device hda-duplex \
   -device nec-usb-xhci -device usb-host,vendorid=0x0781,productid=0x5580

Unfortunately I can not access the USB device from the guest. Guest syslog says:

... kernel: [...] usb 5-1: USB new high-speed USB device number 3 using xhci_hcd
... kernel: [...] usb 5-1: New USB device found, idVendor=0781, idProduct=5580, bcdDevice= 0.10
... kernel: [...] usb 5-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
... kernel: [...] usb 5-1: Product: Extreme
... kernel: [...] usb 5-1: Manufacturer: SanDisk
... kernel: [...] usb 5-1: SerialNumber: AA010829152XXXXXXX
... kernel: [...] usb 5-1: can't set config #1, error -32
... mtp-probe: checking bus 5 device 3: "/sys/devices/pci0000:00/0000:00:04.0/usb5/5-1"
... mtp-probe: bus: 5, device:3 was not an MTP device

How can I successfully access the USB device?

USB is required for doing Android development via Android Studio with a physical device.

I tried two USB-sticks and an Android smartphone in file transfer mode.

Version information: macOS: 10.13.6, qemu: 5.1.0, Ubuntu: 20.04.

Chaucerian answered 20/1, 2021 at 19:16 Comment(0)
S
3

Before we begin: Make sure that your vendorid and productid matches the one of the device you want to share. On you macOS guest, you can do that by running

system_profiler SPUSBDataType

You should get something like

    USB 3.1 Bus:

      Host Controller Driver: AppleUSB...
      PCI Device ID: 0x1234
      PCI Revision ID: 0x1234
      PCI Vendor ID: 0x1234
      Bus Number: 0x00

        USB 3.1 Storage Device:

          Product ID: 0x4242
          Vendor ID: 0x2424
          Version: 42
          Serial Number: ABCDE
          Speed: Up to 10 Gb/s
          Manufacturer: FooBar

It might look a bit different for you but that is okay. The Product and Vendor ID that is of importance for you is the one of the actual device you want to share, not the bus. So in this case, it would be

vendorid=0x2424,productid=0x4242

Once that is all correct, make sure to run qemu-system-x86_64 as root. Otherwise, you won't have the permission for USB passthrough. I assume this is the problem you encountered. So, run qemu-system-x86_64 using sudo:

sudo qemu-system-x86_64 ... -device nec-usb-xhci -device usb-host,vendorid=0x2424,productid=0x4242
Szeged answered 6/2, 2021 at 13:19 Comment(6)
Thank you Jonas! Unfortunately neither sudo nor the vendorid/productid resolve the issue (I guess the guest's syslog would not show anything if vendorid or productid were incorrect). I edited the question to include sudo.Chaucerian
Yeah, you're probably right. It worked for me, I haven't tested it using a storage device though. Have you made sure that the volumes are ejected? Run disk unmountDisk /dev/... to unmount the device. I'm sure it can't be mounted in macOS when passing through to the guest OS.Szeged
Thank you again Jonas! I tried unmounting the storage device via diskutil unmount /dev/disk2s1 before starting qemu, but passthrough was without success and also tried a Logitech C525 USB webcam without success -- same kernel log error message on the guest. Could you share version information on your macos host and guest Linux?Chaucerian
I have the same problem with HID devices, I could manage to passthrough a usb-host WIFI device for which I didn't have a driver installed in MacOS. FYI: I didn't need to run qemu as root.Exequies
What about qemu on Mac/silicon/arm64? when I use qemu-system-aarch64 it return: qemu-system-aarch64: No machine specified, and there is no default Use -machine help to list supported machines. even with qemu-system-armPostcard
There should be little to no difference when using QEMU on ARM64. The error you got tells you that you did not specify the machine (say, the processor that is emulated/virtualized). You could use qemu-system-aarch64 -machine virt to use the latest "QEMU ARM Virtual Machine". In my example above I used ... to indicate all the parameters that you have to specify. But this is specific to your use-case.Szeged
C
3

qemu 6.0.0 uses libusb to add usb-host devices to virtual machines. There is a problem, libusb cannot claim a device on macOS if it is already claimed by another kernel extension. It seems that some kernel extension claims any attached device automatically. So authors of libusb created a workaround: https://github.com/libusb/libusb/pull/911.

The workaround isn't released yet but you might build the latest version of libusb from github using homebrew and link it instead of the stable one:

brew install --head libusb
brew unlink libusb
brew link --head libusb

When you done this, running qemu with sudo and usb-host device should work well.

Alternatively to specifying vendorid and productid you might want to specify hostbus and hostport. You may acquire them in qemu monitor using info usbhost.

Cumae answered 14/8, 2021 at 14:21 Comment(1)
Warning: No HEAD keg installed for libusb ;;;;;; To install, run: brew install --HEAD libusbPostcard
M
0

Theres Options: -device qemu-xhci -device usb-host,hostbus=002,hostaddr=007 and it may require being run as root: meaning BUS 2 Device 7: That and if not requiring root must dwelve deeply into the configuration of allowing users to do these things PAM/Groups:

Magical answered 10/12, 2023 at 5:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.