Compare commits

...

3 Commits

6 changed files with 99 additions and 5 deletions

View File

@ -0,0 +1 @@
../libvirt/attach-usb

View File

@ -0,0 +1,75 @@
#!/bin/bash
script_location="$(readlink -f "$0")"
cd "$(dirname $script_location)"
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")"
}
menu() {
if [ -z "$DISPLAY" ]; then
echo -e "$1" | fzf --prompt="$2"
return 0
fi
run_as_user "echo -e '$1' | menucmd -p '$2'"
}
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
menu "No VMs running" "Error"
exit 1
fi
vm="$(menu "$vms" "VM")"
vm_name=$(echo "$vm" | awk '{print $2}')
directory="./usb-devices"
item_list=""
for file in "$directory"/*; do
item_list="${item_list}$(basename $file)\n"
done
# strip last newline
end=${#item_list}-2
item_list=${item_list:0:end}
device="${directory}/$(menu "$item_list" "Device")"
[ -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

View File

@ -0,0 +1,6 @@
<hostdev mode='subsystem' type='usb' managed='no'>
<source>
<vendor id='0x054c'/>
<product id='0x05c4'/>
</source>
</hostdev>

View File

@ -0,0 +1,6 @@
<hostdev mode='subsystem' type='usb' managed='no'>
<source>
<vendor id='0x046d'/>
<product id='0xc547'/>
</source>
</hostdev>

View File

@ -0,0 +1,6 @@
<hostdev mode='subsystem' type='usb' managed='no'>
<source>
<vendor id='0x4273'/>
<product id='0x7685'/>
</source>
</hostdev>

View File

@ -47,8 +47,8 @@ bemenu_cmd()
--sb $base02 --sf $cyan\ --sb $base02 --sf $cyan\
--hb $base02 --hf $cyan\ --hb $base02 --hf $cyan\
--scb $base02 --scf $cyan\ --scb $base02 --scf $cyan\
--bdr $violet --bdr $violet\
$@ "$@"
} }
dmenu_cmd() dmenu_cmd()
@ -57,7 +57,7 @@ dmenu_cmd()
-fn "FiraCodeSemibold-9"\ -fn "FiraCodeSemibold-9"\
-nb $base03 -nf $base0\ -nb $base03 -nf $base0\
-sb $base02 -sf $cyan\ -sb $base02 -sf $cyan\
$@ "$@"
} }
rofi_cmd() rofi_cmd()
@ -66,9 +66,9 @@ rofi_cmd()
} }
if [[ -n $WAYLAND_DISPLAY ]]; then if [[ -n $WAYLAND_DISPLAY ]]; then
bemenu_cmd $@ bemenu_cmd "$@"
elif [[ -n $DISPLAY ]]; then elif [[ -n $DISPLAY ]]; then
rofi_cmd $@ rofi_cmd "$@"
else else
echo "Error: No Wayland or X11 display detected" >&2 echo "Error: No Wayland or X11 display detected" >&2
exit 1 exit 1