user1091141
user1091141

Reputation: 611

How to use USB with QEMU on a Mac host?

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.

Upvotes: 7

Views: 10275

Answers (2)

Dmitry Zhlobo
Dmitry Zhlobo

Reputation: 379

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.

Upvotes: 3

Jonas
Jonas

Reputation: 755

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

Upvotes: 4

Related Questions