USB Hotplugging via UDEV Rules


raiditup

Recommended Posts

Sorry originally posted this in the general forum thread.

 

Before I updated to the latest build (6.3.2), I had a custom udev rule copied to /etc/udev/rules.d on boot.  It allowed me to use a mouse and keyboard hooked up via KVM like USB switch to be shared between my main PC and a VM in unRAID.  On the latest build however this doesn't seem to work anymore.  I can attach the mouse and keyboard manually when switching between my PC and VM but this is a major hassle.  I'm not sure if its an issue with the latest version of libvirt or udev that's causing the problem.

 

If anyone has a custom udev rule working in the latest build and can provide assistance with this it would be much appreciated.

Link to comment
  • 3 months later...

I found this article on a blog site that explained why this isn't working in the latest version of unRAID

 

http://kicherer.org/joomla/index.php/en/blog/48-automatic-hotplugging-of-usb-devices-for-libvirt-managed-vms

 

It appears that when you run a command fired from a UDEV rule, the rule waits for the command to finish before completing the event such as mounting or attaching a USB device.  This is why my attach command kept failing as my USB device wasn't attached when it ran.  The clever blogger was able to workaround this issue by using systemd to run his command.  Unfortunately, unRAID is based on Slackware which still uses init so I instead wrote 2 scripts for each USB device.  One to attach/detach the device and another to call that script.  My UDEV rule now fires the 2nd script which calls the real script.

 

The call script looks something like this

#!/bin/bash

/boot/config/attach-keyboard.sh & disown

 

The key is the call script includes "& disown" which will run the script in the background and disassociate it from the shell.  Theoretically I should've just needed the "&" but it still didn't appear to work without the disown.  This allows the UDEV rule to not wait for the script to finish running before attaching the USB device.  I'm sure there's probably a cleaner way to do all of this, but this is what has been working for me.  I'm going to try and write an article on my blog and will post a link when I'm done.  In the interim, if anyone needs more info on how to automate USB Hotplugging for VM's, I'll be more than happy to help. 

  • Upvote 1
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.