From 96ac6d6ec865b79a5fa77048d634a2a904b43721 Mon Sep 17 00:00:00 2001 From: Silvio Rhatto Date: Thu, 17 Sep 2020 18:46:23 -0300 Subject: Feat: adds usb_attach command --- README.md | 5 ++-- kvmx | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- kvmxfile | 17 ++++++++++---- 3 files changed, 93 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 7e1c56c..51eb5d3 100644 --- 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 dca6ad9..50ba01b 100755 --- 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 < /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 $* diff --git a/kvmxfile b/kvmxfile index 60353e5..cf945d7 100644 --- 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" -- cgit v1.2.3