summaryrefslogtreecommitdiffstats
path: root/core/modules/vbox-src/data/opt/openslx/bin/attach-usb-to-vbox
diff options
context:
space:
mode:
Diffstat (limited to 'core/modules/vbox-src/data/opt/openslx/bin/attach-usb-to-vbox')
-rwxr-xr-xcore/modules/vbox-src/data/opt/openslx/bin/attach-usb-to-vbox55
1 files changed, 21 insertions, 34 deletions
diff --git a/core/modules/vbox-src/data/opt/openslx/bin/attach-usb-to-vbox b/core/modules/vbox-src/data/opt/openslx/bin/attach-usb-to-vbox
index 46452f91..6c3bc65c 100755
--- a/core/modules/vbox-src/data/opt/openslx/bin/attach-usb-to-vbox
+++ b/core/modules/vbox-src/data/opt/openslx/bin/attach-usb-to-vbox
@@ -1,62 +1,49 @@
#!/bin/bash
-# OBJECTIVES
-# udev rules to detect usb type etc.
-# figuring out the bus thing.
-
-# check that udev gave us a serial number to work with
-udev_serial="$1"
-if [ -z "$udev_serial" ]; then
- logger "$0: Did not receive serial from udev"
+# make sure we have the required udev variables
+if [ -z "$BUSNUM" -o -z "$DEVNUM" -o -z "$DEVPATH" ]; then
exit 1
fi
-logger "udev_serial: $udev_serial"
-udev_vendorid="$2"
-if [ -z "$udev_vendorid" ]; then
- logger "$0: Did not receive vendorid from udev"
- exit 1
-fi
-logger "udev_vendorid: $udev_vendorid"
+declare -rg _vboxdir="/usr/lib/virtualbox"
+declare -rg _vboxmanage="${_vboxdir}/VBoxManage"
+declare -rg _vboxruntime="${_vboxdir}/VirtualBox"
-user=$(ps auxf | grep /usr/lib/virtualbox/VirtualBox | grep -v grep | awk '{print $1}')
-if [ -z "$user" ]; then
- logger "$0: could not determine user"
- exit 1
+declare -r user="$(ps aux | grep "$_vboxruntime" | grep -v grep | awk '{print $1}')"
+if [ -z "$user" ] || ! id "$user" &> /dev/null; then
+ # No user session with virtualbox is running, exit silently
+ exit 0
fi
+export VBOX_IPC_SOCKETID="$user"
+
# find UUID of running VM
-vmuuid=$(su -c "/usr/lib/virtualbox/VBoxManage list runningvms | sed -r 's,^\"[^\{]+\{([a-z0-9\-]+)\}$,\1,g'" "$user")
+vmuuid="$("$_vboxmanage" list runningvms | awk '{print $NF}' | tr -d '{}')"
if [ -z "$vmuuid" ]; then
- logger "Failed to find running VirtualBox VM"
+ logger "Failed to find running VirtualBox instance of '$user'."
exit 1
fi
-logger "vmuuid: $vmuuid"
devname="/dev/vboxusb/$BUSNUM/$DEVNUM"
-for tts in 1 1 2 3 5 5 STOP; do
+for tts in 1 1 1 1 1 2 3 5 STOP; do
if [ "x$tts" = "xSTOP" ]; then
logger "$0: Could not find '$devname' as expected..."
exit 1
fi
[ -e "$devname" ] && break
- echo "waiting for $devname..."
+ logger "waiting for $devname..."
sleep $tts
done
-usblistgrep=$(su -c "/usr/lib/virtualbox/VBoxManage list usbhost" "$user")
-logger "USB list: $usblistgrep"
-
-usbaddress=$(su -c "/usr/lib/virtualbox/VBoxManage list usbhost | grep -A1 -E 'SerialNumber:\s*'"$1" | awk '/Address:/ {print \$2}'" "$user")
-logger "USB address: $usbaddress"
+usbaddress="$("$_vboxmanage" list usbhost | grep "$DEVPATH" | awk '$1 = /Address:/ {print $2}')"
-logger "Trying to attach...."
-for tries in 1 2 3 4 5 6 7 8 9 10 STOP; do
+logger "Trying to attach '$usbaddress' to '$vmuuid'..."
+for tries in 1 2 3 STOP; do
if [ "$tries" = "STOP" ]; then
- logger "$0: Failed to attach usb after 5 tries"
- exit
+ logger "$0: Failed after 3 tries!"
+ exit 1
fi
- if su -c '/usr/lib/virtualbox/VBoxManage controlvm '"$vmuuid"' usbattach '"$usbaddress"'' "$user"; then
+ if "$_vboxmanage" controlvm "$vmuuid" usbattach "$usbaddress"; then
exit 0
fi
sleep 1