nix-da/options/services/udev/hotplug.sh

42 lines
912 B
Bash

# Refer to https://github.com/olavmrk/usb-libvirt-hotplug/blob/master/usb-libvirt-hotplug.sh
set -e
exec > >(systemd-cat -t "$0") 2>&1
PROG="$(basename "$0")"
DOMAIN="$1"
if [ -z "${DOMAIN}" ]; then
echo "Missing libvirt domain parameter for $PROG." >&2
exit 1
fi
if [ "$ACTION" == 'add' ]; then
COMMAND='attach-device'
elif [ "$ACTION" == 'remove' ]; then
COMMAND='detach-device'
else
echo "Invalid udev ACTION: $ACTION" >&2
exit 1
fi
if [ -z "$BUSNUM" ]; then
echo "Missing udev BUSNUM environment variable." >&2
exit 1
fi
if [ -z "$DEVNUM" ]; then
echo "Missing udev DEVNUM environment variable." >&2
exit 1
fi
BUSNUM=$((10#$BUSNUM))
DEVNUM=$((10#$DEVNUM))
virsh -c qemu:///system $COMMAND archlinux --live --file /dev/stdin <<END
<hostdev mode="subsystem" type="usb">
<source>
<address bus="$BUSNUM" device="$DEVNUM"/>
</source>
</hostdev>
END