aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xkvmx68
1 files changed, 54 insertions, 14 deletions
diff --git a/kvmx b/kvmx
index 50ba01b..4f47403 100755
--- a/kvmx
+++ b/kvmx
@@ -2313,19 +2313,8 @@ function kvmx_disposable {
done
}
-# USB attach
-function kvmx_usb_attach {
- if ! kvmx_running; then
- echo "$BASENAME: guest $VM is not running"
- exit 1
- fi
-
- # Check for lsusb
- if ! which lsusb &> /dev/null; then
- echo "Please install usbutils package."
- exit 1
- fi
-
+# Helper function to chose a device
+function __kvmx_usb_devices_check {
local total="`lsusb | wc -l`"
# Check if USB is available in the system
@@ -2339,12 +2328,20 @@ function kvmx_usb_attach {
echo "You need to enable USB support into the kvmxfile."
exit 1
fi
+}
+# Helper function to list available USB devices
+function __kvmx_usb_devices_list {
# List available devices
echo "Available devices:"
echo ""
lsusb | cat -n
echo ""
+}
+
+# Helper function to choose an USB device
+function __kvmx_usb_devices_choose {
+ local total="`lsusb | wc -l`"
# Read option
read -rep "Choose a device (1-$total): " choice
@@ -2355,6 +2352,21 @@ function kvmx_usb_attach {
exit 1
fi
+ echo $choice
+}
+
+# USB attach
+function kvmx_usb_attach {
+ if ! kvmx_running; then
+ echo "$BASENAME: guest $VM is not running"
+ exit 1
+ fi
+
+ # Get a device
+ __kvmx_usb_devices_check
+ __kvmx_usb_devices_list
+ local choice="`__kvmx_usb_devices_choose`"
+
# Get bus, device, vendor and product IDs
local option="`lsusb | cat -n | grep \"^ $choice\"`"
local bus="`echo $option | awk '{ print $3 }'`"
@@ -2368,7 +2380,35 @@ function kvmx_usb_attach {
# Attach into the guest
echo "Attaching device ${vendor}:${product} into the guest..."
- kvmx_monitor device_add usb-host,vendorid=0x${vendor},productid=0x${product}
+ kvmx_monitor device_add usb-host,vendorid=0x${vendor},productid=0x${product},id=usb-0x${vendor}-0x${product}
+}
+
+# USB detach
+function kvmx_usb_detach {
+ if ! kvmx_running; then
+ echo "$BASENAME: guest $VM is not running"
+ exit 1
+ fi
+
+ # Get a device
+ __kvmx_usb_devices_check
+ __kvmx_usb_devices_list
+ local choice="`__kvmx_usb_devices_choose`"
+
+ # Get bus, device, vendor and product IDs
+ local option="`lsusb | cat -n | grep \"^ $choice\"`"
+ local bus="`echo $option | awk '{ print $3 }'`"
+ local device="`echo $option | awk '{ print $5 }' | cut -d ':' -f 1`"
+ local vendor="`echo $option | awk '{ print $7 }' | cut -d ':' -f 1`"
+ local product="`echo $option | awk '{ print $7 }' | cut -d ':' -f 2`"
+
+ # Detach into the guest
+ echo "Detaching device ${vendor}:${product} into the guest..."
+ kvmx_monitor device_del usb-0x${vendor}-0x${product}
+
+ # Restore permission to the device
+ echo "Restoring ownership of /dev/bus/usb/${bus}/${device}..."
+ sudo chown root /dev/bus/usb/$bus/$device
}
# Dispatch