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 --- kvmx | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) (limited to 'kvmx') 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 $* -- cgit v1.2.3