]> gitweb.fluxo.info Git - kvmx.git/commitdiff
Feat: adds usb_attach command
authorSilvio Rhatto <rhatto@riseup.net>
Thu, 17 Sep 2020 21:46:23 +0000 (18:46 -0300)
committerSilvio Rhatto <rhatto@riseup.net>
Thu, 17 Sep 2020 21:46:23 +0000 (18:46 -0300)
README.md
kvmx
kvmxfile

index 7e1c56cae8568dfa3c4f43575fa11eb80245e492..51eb5d3cae8e8c932901778df44eb91cf1aad8c6 100644 (file)
--- a/README.md
+++ b/README.md
@@ -26,9 +26,10 @@ sources, you'll feel welcome here :)
 
 ## Dependencies
 
-KVMX currently needs a Debian-based system and the following dependencies:
+KVMX currently needs a Debian-based system and the following packages depending on what
+features you're interested to run:
 
-    sudo apt install git qemu qemu-kvm virt-viewer spice-client spice-client-gtk socat screen sshfs
+    sudo apt install git qemu qemu-kvm virt-viewer spice-client spice-client-gtk socat screen sshfs usbutils
 
 If you plan to create guest images, you may also want the following packages:
 
diff --git a/kvmx b/kvmx
index dca6ad9dd0c99ce94720f50a1650db3ab3b20484..50ba01b9994f5d3f4b37532f0663d701b9d2890e 100755 (executable)
--- a/kvmx
+++ b/kvmx
@@ -458,6 +458,23 @@ function kvmx_up {
     boot_opts="-boot $boot"
   fi
 
+  if [ ! -z "$usb" ] && [ "$usb" != "0" ]; then
+    # Basic USB support
+    if [ "$usb" == "1" ]; then
+      usb_opts="-usb"
+    fi
+
+    # USB support with 1.0 and 2.0 hub
+    if [ "$usb" == "2" ]; then
+      usb_opts="-usb -device usb-ehci,id=ehci -device usb-host,bus=ehci.0,vendorid=1452"
+    fi
+
+    # USB support with 1.0, 2.0 and 3.0 hubs
+    if [ "$usb" == "3" ]; then
+      usb_opts="-usb -device usb-ehci,id=ehci -device usb-host,bus=ehci.0,vendorid=1452 -device qemu-xhci,id=xhci"
+    fi
+  fi
+
   # Check kvm version
   if kvm --help | grep -q -- "^-balloon"; then
     local new_qemu="0"
@@ -583,6 +600,7 @@ function kvmx_up {
     $boot_opts                                                                                      \
     $net_opts                                                                                       \
     $rng_opts                                                                                       \
+    $usb_opts                                                                                       \
     -pidfile $PIDFILE                                                                               \
     -D $LOGFILE                                                                                     \
     $qemu_opts
@@ -2007,7 +2025,7 @@ function kvmx_monitor {
     socat $MONITORFILE STDIO
   else
     socat STDIO $MONITORFILE <<EOF
-$1
+$*
 EOF
   fi
 }
@@ -2295,6 +2313,64 @@ 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
+
+  local total="`lsusb | wc -l`"
+
+  # Check if USB is available in the system
+  if [ "$total" == "0" ]; then
+    echo "Sorry, but you don't have any available USB devices on your system."
+    exit 1
+  fi
+
+  # Check for usb config option
+  if [ -z "$usb" ] || [ "$usb" == "0" ]; then
+    echo "You need to enable USB support into the kvmxfile."
+    exit 1
+  fi
+
+  # List available devices
+  echo "Available devices:"
+  echo ""
+  lsusb | cat -n
+  echo ""
+
+  # Read option
+  read -rep "Choose a device (1-$total): " choice
+
+  # Validate choice
+  if (($choice < 1)) || (($choice > $total)); then
+    echo "Invalid choice."
+    exit 1
+  fi
+
+  # 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`"
+
+  # Give permission to the device
+  echo "Changing ownership of /dev/bus/usb/${bus}/${device} to you..."
+  sudo chown `whoami` /dev/bus/usb/$bus/$device
+
+  # Attach into the guest
+  echo "Attaching device ${vendor}:${product} into the guest..."
+  kvmx_monitor device_add usb-host,vendorid=0x${vendor},productid=0x${product}
+}
+
 # Dispatch
 if type kvmx_$ACTION 2> /dev/null | grep -q "kvmx_$ACTION ()"; then
   __kvmx_initialize $*
index 60353e566c46ca823c4ba0b6075d1f3f83985a9f..cf945d76a0c9f3db7999f3de64778f13183e8047 100644 (file)
--- a/kvmxfile
+++ b/kvmxfile
@@ -209,14 +209,23 @@ bootloader="grub"
 # See http://www.reactos.org/wiki/QEMU#Setting_up_network
 #nic_model="ne2k_pci"
 
+# USB support
+#
+# Allowed values:
+#
+#   0 - No USB support
+#   1 - Support for USB 1.0
+#   2 - Support for USB 1.0 and 2.0
+#   3 - Support for USB 1.0, 2.0 and 3.0
+#
+#usb="0"
+
 # Additional qemu opts
 # Example: http://www.linux-kvm.org/page/USB_Host_Device_Assigned_to_Guest
 # See also: https://qemu.readthedocs.io/en/latest/system/usb.html
 #           https://wiki.gentoo.org/wiki/QEMU/Windows_guest
-#qemu_opts="-usb" # Basic USB support
-#qemu_opts="-usb -device usb-ehci,id=ehci -device usb-host,bus=ehci.0,vendorid=1452" # USB support with 1.0 and 2.0 hub
-#qemu_opts="-usb -device usb-ehci,id=ehci -device usb-host,bus=ehci.0,vendorid=1452 -device qemu-xhci,id=xhci" # USB support with 1.0, 2.0 and 3.0 hubs
-#qemu_opts="-usb -device usb-ehci,id=ehci -device usb-host,hostbus=2,hostaddr=3" # USB support, attaching an specific device
+#qemu_opts="-device usb-host,hostbus=2,hostaddr=3" # Automatically attach an specific USB device
+#qemu_opts=""
 
 # Number of CPUs
 #smp="4"