UTM icon indicating copy to clipboard operation
UTM copied to clipboard

Allow permanent USB pass-through configuration

Open gjherbiet opened this issue 3 years ago • 13 comments

This is a follow-up of this discord discussion.

USB-device pass-through can be configured manually when a VM has stated by clicking on the USB icon at the top right of a running VM window. This has to be done each time.

Note that I added the following to the VM config in the QEMU section: -device usb-host,vendorid=0x067b,productid=0x2303 (I got the vendor/product IDs from macOS "System Information". However this doesn't work and manually attaching the USB device is still required each time the VM is started.

The idea would be to configure this "permanently", i.e. the configured USB device (based on vendorid/productid or hostbus/hostaddr) would be attached as pass-through to the VM when it is started. This is similar to what VirtualBox does with filters.

According the reply I got from @conath on Discord, one implementation idea could be to add “bookmarked” USB device passthrough so the device is automatically passed when the VM starts.

Thanks in advance for considering this feature request.

gjherbiet avatar Dec 21 '21 12:12 gjherbiet

Anyone found a workaround for this? I badly need it as well.

Redferne avatar Nov 17 '22 05:11 Redferne

Workaround: https://gist.github.com/JurajNyiri/df4bacd1ccfc00105843e67f09b2fc80

How to run:

  1. Open Automator
  2. Create new application
  3. Choose "Run AppleScript"
  4. Copy & paste the code above
  5. Change application_name to your VM name
  6. Change usb_select to the prefix (or exact match) of your USB device, for example if you are using ConBee II, you can put in conbee or con as long as you do not have other device like connected usb device, it basically just writes the text and hits enter after context menu is shown.

JurajNyiri avatar Dec 13 '22 09:12 JurajNyiri

I will try @JurajNyiri' solution but it would be great to have this out of the box - why isn't that the case?!

timothe avatar Jan 24 '23 10:01 timothe

Does the workaround work for multiple devices without having to duplicate the script?

protoEvangelion avatar Mar 16 '23 02:03 protoEvangelion

@protoEvangelion it controls your mouse and keyboard so as long as you do not run it at the same time it will work

JurajNyiri avatar Mar 16 '23 07:03 JurajNyiri

This also worked for me (thank you @JurajNyiri for the idea):

tell application "System Events"
	repeat with theButton in button of toolbar 1 of window "ha" of application process "UTM"
		set theButtonDescription to description of theButton
		if theButtonDescription is "USB" then
			click theButton
			keystroke "Sonoff"
			key code 36
		end if
	end repeat
end tell

(replace "ha" with the vm name and "Sonoff" with the start of the menu item text)

mezzario avatar May 29 '23 09:05 mezzario

can u do this in headless mode .. i am thinking not because the usb+ button wont there?

KashifZA avatar Jun 27 '23 19:06 KashifZA

can u do this in headless mode .. i am thinking not because the usb+ button wont there?

@krycor This has also bothered me, but I figured out you can now also connect USB devices to headless VMs via the utmctl command line tool.

Make sure USB Support is on in the VM Settings (Input section).

In a Terminal:

# List VMs - you can use either the ID or the name
utmctl list

# List USB Devices - Copy the ID you want to connect
# The ID is the vendor ID and product ID of the USB device, this won't change
utmctl usb list

# Connect USB device (when VM is running)
# This connects USB device with ID AAAA:BBBB to VM named "Home Assistant"
utmctl usb connect "Home Assistant" "AAAA:BBBB"

If utmctl is not found, make sure to install it: https://docs.getutm.app/scripting/scripting/#command-line-interface If UTM was installed via homebrew, this shouldn't be necessary, since homebrew already adds the binary to its standard bin folder (i.e. /opt/homebrew/bin on Apple Silicon)

So this does not solve the need for a permanent configuration since you still have to run the connect command whenever you restart the VM or after unplugging the device. But it is much nicer than being forced to have a virtual display and running Automator scripts to connect a device via the UI.

remmycat avatar Jan 11 '24 18:01 remmycat

Thanks.. i was actually looking at the AppleScript option but my lack of experience with it meant its a bit awkward for me to use. Found this again via google which helped as i can use the utmctl to quickly get a list of devices and so i script a connect to vm.

one thing i see is you can only really do this when the vm is running which means i'd have make the vm bootable excluding usb device / disk, run a exec script kicked off on guest vm via utmctl to do the mappings etc. not too bad as i can package it in a script on eitherside.

KashifZA avatar Jan 17 '24 10:01 KashifZA

Any hope of getting this feature?

chriscrowe avatar May 02 '24 01:05 chriscrowe

Should help others in this thread but also perhaps the feature in UTM: this can be done with a LaunchAgent.

I did the same for Parallels and just adapted it for UTM: https://github.com/tuna-f1sh/mac-device-connect-daemon. LaunchAgents can call a script on USB device connect matching VID/PID. My linked code is a helper to create these .plist LauchAgents for USB devices. Essentially, utmctl usb connect GUEST VID:PID is called by the LaunchAgent when a device connects.

tuna-f1sh avatar May 03 '24 13:05 tuna-f1sh

Should help others in this thread but also perhaps the feature in UTM: this can be done with a LaunchAgent.

Thanks, that's helpful for connecting a device to an already running VM!

I think one thing the issue is asking for is better UTM support for the opposite order of events, or ideally both. When a USB device is already connected to the host and I start a VM, I want the device to be connected to the VM immediately without running a script.

I believe the automation currently possible is to start the VM and trigger the USB connection both from a script (not sure about the necessary timing here), or to trigger the connection from the guest system on boot in some way.

remmycat avatar May 04 '24 15:05 remmycat

can u do this in headless mode .. i am thinking not because the usb+ button wont there?

@krycor This has also bothered me, but I figured out you can now also connect USB devices to headless VMs via the utmctl command line tool.

Make sure USB Support is on in the VM Settings (Input section).

In a Terminal:

# List VMs - you can use either the ID or the name
utmctl list

# List USB Devices - Copy the ID you want to connect
# The ID is the vendor ID and product ID of the USB device, this won't change
utmctl usb list

# Connect USB device (when VM is running)
# This connects USB device with ID AAAA:BBBB to VM named "Home Assistant"
utmctl usb connect "Home Assistant" "AAAA:BBBB"

If utmctl is not found, make sure to install it: https://docs.getutm.app/scripting/scripting/#command-line-interface If UTM was installed via homebrew, this shouldn't be necessary, since homebrew already adds the binary to its standard bin folder (i.e. /opt/homebrew/bin on Apple Silicon)

So this does not solve the need for a permanent configuration since you still have to run the connect command whenever you restart the VM or after unplugging the device. But it is much nicer than being forced to have a virtual display and running Automator scripts to connect a device via the UI.

Thank you for this information! I was able to use automator to make an "app" to start up at login to start my VM's (including HA) and connect the USB devices to HA once I knew their ID's

Here's a resource on how to create an "automator app": https://truptivbhatt.medium.com/how-to-run-your-shell-script-automatically-after-logging-to-mac-da789bb78d21

This is the shell script that I used and is working great for me:

# Set current working directory
cwd=$(pwd) 
# Change directory to UTM app
cd /Applications/UTM.app/Contents/MacOS/ 
# Start VM machine/s
./utmctl start VM1
./utmctl start VM2 
./utmctl start VM3...
# Add a 45 sec wait timer (not sure if needed but added just in case)
sleep 45 
# Mount Desired USB's
./utmctl usb connect "HomeAssistant (or Name of VM)" "AAAA:BBBB"
./utmctl usb connect "HomeAssistant (or Name of VM)" "AAAA:BBBB"
# Change directory to original location 
cd $cwd

I hope this helps others as well!

ArtsVandelay avatar May 10 '24 04:05 ArtsVandelay