Skip to content

Instantly share code, notes, and snippets.

@nickpegg
Last active June 9, 2024 17:13
Show Gist options
  • Save nickpegg/417cf5024b765c3c92cbfbd725310091 to your computer and use it in GitHub Desktop.
Save nickpegg/417cf5024b765c3c92cbfbd725310091 to your computer and use it in GitHub Desktop.
KVM USB auto-passthrough using udev
# File location: /etc/udev/rules.d/90-libvirt-usb.rules
ACTION=="bind", \
SUBSYSTEM=="usb", \
ENV{ID_VENDOR_ID}=="6b62", \
ENV{ID_MODEL_ID}=="6869", \
RUN+="/usr/local/bin/kvm-udev attach <domain>"
ACTION=="remove", \
SUBSYSTEM=="usb", \
ENV{ID_VENDOR_ID}=="6b62", \
ENV{ID_MODEL_ID}=="6869", \
RUN+="/usr/local/bin/kvm-udev detach <domain>"
#!/bin/bash
# File location: /usr/local/bin/kvm-udev.sh
# Usage: ./kvm-udev.sh attach|detach <domain>
set -e
ACTION=$1
DOMAIN=$2
CONF_FILE=$(mktemp --suffix=.kvm-udev)
cat << EOF >$CONF_FILE
<hostdev mode='subsystem' type='${SUBSYSTEM}'>
<source>
<vendor id='0x${ID_VENDOR_ID}' />
<product id='0x${ID_MODEL_ID}' />
</source>
</hostdev>
EOF
virsh "${ACTION}-device" "$DOMAIN" "$conf_file"
rm "$CONF_FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment