From d6c22c0c45363e046ffdd05aab17da43154c1957 Mon Sep 17 00:00:00 2001 From: Sheldon Lee Date: Mon, 2 Oct 2023 06:14:59 +0800 Subject: [PATCH] Added helper script to attach/detach USB devices --- .config/scripts/bin/attach-usb | 1 + .config/scripts/libvirt/attach-usb | 64 +++++++++++++++++++ .config/scripts/libvirt/usb-devices/ds4.xml | 6 ++ .../usb-devices/logitech-lightspeed.xml | 6 ++ .config/scripts/libvirt/usb-devices/lulu.xml | 6 ++ 5 files changed, 83 insertions(+) create mode 120000 .config/scripts/bin/attach-usb create mode 100755 .config/scripts/libvirt/attach-usb create mode 100644 .config/scripts/libvirt/usb-devices/ds4.xml create mode 100644 .config/scripts/libvirt/usb-devices/logitech-lightspeed.xml create mode 100644 .config/scripts/libvirt/usb-devices/lulu.xml diff --git a/.config/scripts/bin/attach-usb b/.config/scripts/bin/attach-usb new file mode 120000 index 0000000..fd522c2 --- /dev/null +++ b/.config/scripts/bin/attach-usb @@ -0,0 +1 @@ +../libvirt/attach-usb \ No newline at end of file diff --git a/.config/scripts/libvirt/attach-usb b/.config/scripts/libvirt/attach-usb new file mode 100755 index 0000000..57d32c1 --- /dev/null +++ b/.config/scripts/libvirt/attach-usb @@ -0,0 +1,64 @@ +#!/bin/bash +if [ "$(id -u)" -ne "0" ]; then + pkexec --keep-cwd sh -c "\ + WAYLAND_DISPLAY=\"$WAYLAND_DISPLAY\"\ + DISPLAY=\"$DISPLAY\"\ + XDG_RUNTIME_DIR=\"$XDG_RUNTIME_DIR\"\ + \"$0\"" + exit 0 +fi + +run_as_user() { + if [ -z "$PKEXEC_UID" ]; then + USER_ID=1000 + else + USER_ID="$PKEXEC_UID" + fi + username="$(id -nu "$USER_ID")" + cmd="$1" + echo "$(su "$username" sh -c "source \"/home/$username/.bash_profile\"; $cmd")" +} + +csv="$(virsh list | tail --line=+3 | tr ' ' '\n' | uniq | grep -v -e "^[[:space:]]*$" | tr '\n' ',')" + +vms="$(echo "$csv" | awk -F ',' -v n='3' '{ +for (i = 1; i+1 <= NF; i++) { + printf "%s ", $i + if (i % n == 0) { + printf "\n" + } +} +}')" + +if [ -z "$vms" ]; then + run_as_user "echo 'No VMs running' | menucmd -p 'Error'" &> /dev/null + exit 1 +fi + +vm="$(run_as_user "echo -e '$vms' | menucmd -p VM")" +vm_name=$(echo "$vm" | awk '{print $2}') + +directory="./usb-devices" + +item_list="" + +for file in "$directory"/*; do + item_list="${item_list}${file}\n" +done + +# strip last newline +end=${#item_list}-2 +item_list=${item_list:0:end} + +device="$(run_as_user "echo -e '$item_list' | menucmd")" +[ -z "$device" ] && exit 1 + +output=$(virsh attach-device "$vm_name" "$device" 2>&1) && echo "Attached $device to $vm_name" +if [ $? -ne 0 ]; then + virsh detach-device "$vm_name" "$device" &> /dev/null && echo "Detached $device from $vm_name" + if [ $? -ne 0 ]; then + attached_vm_name="$(echo "$output" | grep domain | rev | awk '{print $1}' | rev)" + virsh detach-device "$attached_vm_name" "$device" &> /dev/null && echo "Detached $device from $attached_vm_name" + virsh attach-device "$vm_name" "$device" &> /dev/null && echo "Attached $device to $vm_name" + fi +fi diff --git a/.config/scripts/libvirt/usb-devices/ds4.xml b/.config/scripts/libvirt/usb-devices/ds4.xml new file mode 100644 index 0000000..1a618ac --- /dev/null +++ b/.config/scripts/libvirt/usb-devices/ds4.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/.config/scripts/libvirt/usb-devices/logitech-lightspeed.xml b/.config/scripts/libvirt/usb-devices/logitech-lightspeed.xml new file mode 100644 index 0000000..eeeceff --- /dev/null +++ b/.config/scripts/libvirt/usb-devices/logitech-lightspeed.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/.config/scripts/libvirt/usb-devices/lulu.xml b/.config/scripts/libvirt/usb-devices/lulu.xml new file mode 100644 index 0000000..65c9f8d --- /dev/null +++ b/.config/scripts/libvirt/usb-devices/lulu.xml @@ -0,0 +1,6 @@ + + + + + +