user19597949
user19597949

Reputation: 31

How to pass an android phone (MTP-USB) to a virtual machine using QEMU/KVM?

First I would like to mention that I am a beginner. Also, sorry for any english mistakes.

I tried to pass my Android phone to a Windows 10 guest machine using QEMU/KVM in an Ubuntu 22 host machine, but the guest machine shows as if there is a Driver error. Here is what the Device Manager shows:

Device Manager Windows

MTP USB device properties

This seems to be something related to the MTP protocol used by Android phones, as my flash drives work normally when I pass them to the guest machine.

I also would like to note that I tried this with different phones, so the problem is not the phone.

I went to the QEMU documentation (https://qemu-project.gitlab.io/qemu/system/devices/usb.html), but I confess it is a little advanced for me. The only clue I had is copied below, but I didn't understand where should I change it in the XML file (IF this is changed there...). "usb-mtp,rootdir=dir Media transfer protocol device, using dir as root of the file tree that is presented to the guest."

I have configured my virtual machine using the Virtual Machine Manager, not the Terminal. Is there a way to edit the virtual machine XML to correctly connect my phone, even if the workaround can only be done through the terminal?

Thanks in advance.

Upvotes: 2

Views: 3362

Answers (2)

dtki7
dtki7

Reputation: 1

The problem occurs most likely because the MTP device was still used when the redirection of the device to the VM was requested. You can verify this yourself by typing lsof /dev/libmtp-{number} where {number} is the kernel number assigned to the device. If you have multiple devices you can find the name of the device via cat /sys/bus/usb/devices/{number}/product. The output should show the command gvfsd-mtp or something else indicating that this device is used. (If there is no process listed, then your problem seems to be a bug that can have various reasons.)

The solution is to remove all the uses of the device and this is what you do via the desktop environment by explicitly unmounting it in the end. This could be done by simply killing the processes with kill {PID} where {PID} is the process id taken from the output of the previous lsof command. ATTENTION: Kill a process only in own responsibility. But in case of gvfsd-mtp you can unmount the MTP device with gio mount -u /run/user/1000/gvfs/mtp\:host\={manufacturer}_{product}_{serial}.

This problem is comparable to usual drives which you have to unmount before you can safely detach or redirect them. The redirection of usual drives that are used is yet possible contrary to our MTP devices. Why this is is beyond my knowledge.

Some further notes

  • I recommend using the redirection via Spice (the default display service when using virt-manager). In the VM windows, you'll find it via "Virtual Machine -> Redirect USB device". By doing so, you don't need to repeatedly need to add and remove the hardware.
  • Changing the USB mode of the phone resets the connection. You need to add the hardware again after unmounting the MTP device.
  • You can configure your host to ignore specific devices as discussed here: https://superuser.com/questions/1206664, so you don't have to repeat the process every time.
  • Regarding the referenced documentation in the initial question: The qemu documentation explains how to add USB devices via command line options or the qemu monitor. usb-mtp would be the corresponding type to pass an emulated MTP device to the machine. This is not what we want, because we want to pass an existing host device to the machine. The right option, in this case, would be usb-host. But you already recognized that we actually need the XML since this type of runtime configuration is done in the background by libvirt. So this would be the appropriate documentation: https://libvirt.org/formatdomain.html#usb-pci-scsi-devices. This said, one has to understand that any XML we need is automatically generated by virt-manager in the background. So for this use case, the documentation is only informative.

Upvotes: -4

user19597949
user19597949

Reputation: 31

I don't know if someone will ever have the same problem, but this is how It worked for me:

  • After booting the host machine (Ubuntu 22), boot your virtual machine (Windows 10)

  • Plug your android phone via usb to your PC

  • Go to your android phone, and a notification from USB Preferences will appear. There, the default values are: "USB controlled by" --> "This device" and "Use USB for" --> "No Data Transfer"

  • Change "Use USB for" to "File Transfer".

  • When the android phone icon appears at the ubuntu dock*, right-click it and press "Mount". *the dock is the bar that by default is on the left side of the screen, it is analogous to the windows taskbar

  • Now, right-click it again and press "Unmount"

  • Go to the Virtual Machine Manager, choose the Windows 10 virtual machine you are using and double-click it. Click the "i" icon ("Show virtual hardware details") and then "Add Hardware". Select "USB Host Device", and choose your android phone on the list. Then, click Finish.

  • Now you may go to your Windows 10 virtual machine and you will see your android phone when you open the File Explorer (under "This PC").

I normally remove it from the "Virtual Hardware details" after I transfer my files, so the Virtual Machine Manager won't ask for it whenever I try to start my virtual machine again.

Upvotes: 1

Related Questions