Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[POC] USB sharing with host #1317

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft

Conversation

sheerun
Copy link

@sheerun sheerun commented Jan 22, 2023

ad #1314

I managed to flash USB from lima, but I don't feel this is ready to ship. I don't have time to handle this anymore, but I hope it will help. It only works if instance is started with sudo limactl start (otherwise qemu is not working with admin privileges which it needs for device forwarding), and lima is started with sudo lima (otherwise you get Bad port '0' error). USB attaching / unattaching when physical device is plugged / unplugged seems to be working and I feel it will continue when buses and ports are uses instead of productid / vendorid (see below).

Main issues to solve:

  • USB needs to be inserted when VM is started. This should be solved by binding all buses/ports instead using productid/vendorid of devices, like explained here: https://unix.stackexchange.com/questions/452934/can-i-pass-through-a-usb-port-via-qemu-command-line
  • QEMU vm needs to be started with sudo which is problematic because currently background agent is running with user privileges. It can be solved by asking for password and increasing privileges, but I'm not sure lima is willing to make such drastic changes to current workflow. I guess it would help if machine was started from foreground process, not background process because then just sudo -E can be prepended to qemu process (maybe with -u and -g flags set to current user).

Mind issue is with second point and it would require either managing everithing without agent process or making agent process running as root, or having 2 agent processes, one running as root handling root vms and one in user mode handling user mode qms

Hope it helps!

@AkihiroSuda
Copy link
Member

Is this for Linux host, or does this work on macOS too? 👀

@sheerun
Copy link
Author

sheerun commented Jan 23, 2023

I don't know if it works on linux. I've tested it on Mac M1

@AkihiroSuda
Copy link
Member

Mind issue is with second point and it would require either managing everithing without agent process or making agent process running as root, or having 2 agent processes, one running as root handling root vms and one in user mode handling user mode qms

The latter mode is preferable. We have been doing the same for socket_vmnet.

@dan-corneanu
Copy link

Is adding USB pass through more likely to happen with the new --vm-type=vz?

@AkihiroSuda
Copy link
Member

Is adding USB pass through more likely to happen with the new --vm-type=vz?

Unlikely.
vz doesn't seem to have an API for USB pass through: https://developer.apple.com/documentation/virtualization?language=objc

@dan-corneanu
Copy link

I have tested a linux guest VM running with UTM (https://mac.getutm.app/) on M1, using qemu. UTM's UI has a button to select what USB devices you want to be passed through to the VM. It works as expected. I can select/unselect USB devices and they correctly show up or get removed from the guest VM while the VM is running.

Any idea how is this implemented?

@retpolanne
Copy link

retpolanne commented Feb 5, 2024

@dan-corneanu I guess it's implemented like this [1][2]

(apologies, upon reading your code @sheerun I saw that it's the same api)

So, probably UTM starts with this bus connected so you can select which devices you want within the VM?

qemu-xhci supports hotplugging through the device-add monitor command btw.

[1] https://qemu-project.gitlab.io/qemu/system/devices/usb.html
[2] https://github.com/utmapp/UTM/blob/62bd84c78c045870c580df34c1ba233d75e02995/Configuration/UTMQemuConfiguration%2BArguments.swift#L661
[3] https://qemu-project.gitlab.io/qemu/system/devices/usb.html#hotplugging-usb-storage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants