[Pkg-libvirt-maintainers] Bug#846173: libvirt-daemon: Fails to locate existing usb device

Timo Lindfors timo.lindfors at iki.fi
Thu Sep 21 18:44:05 UTC 2017


Hi,

thanks for the detailed investigation. I hit this bug when I upgraded my 
home server. I have USB devices assigned to VMs based on the USB topology 
(hub and port number). Since I really need my USB printer to work I came up
with the following very hacky solution. I hope I can migrate to something 
better in the future :)

(The following still has the bug that if I shutdown my server libvirt
will save my VMs to disk along with information on the attached USB
devices which probably is completely bogus when the guests are later
restored. I have not found a hook that'd get run when the VM is about to 
be saved to disk but is still running.)

==== /etc/udev/rules.d/90-usb-libvirt-hotplug.rules

# 433 MHz transmitter for home automation
SUBSYSTEM=="usb",DEVPATH=="/devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.1/1-9.1.1",RUN+="/root/udev-usb-hotplug radio.home"
# USB printer
SUBSYSTEM=="usb",DEVPATH=="/devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.1/1-9.1.2",RUN+="/root/udev-usb-hotplug print.home"
# DVB capture cards
SUBSYSTEM=="usb",DEVPATH=="/devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.1/1-9.1.4",RUN+="/root/udev-usb-hotplug tv.home"
SUBSYSTEM=="usb",DEVPATH=="/devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.1/1-9.1.3",RUN+="/root/udev-usb-hotplug tv.home"
# 4G modem
SUBSYSTEM=="usb",DEVPATH=="/devices/pci0000:00/0000:00:14.0/usb1/1-4",RUN+="/root/udev-usb-hotplug backupgateway"

==== /root/udev-usb-hotplug

#!/bin/bash

domain="$1"

if [ "$ACTION" = "remove" ]; then
     /root/usb-libvirt-hotplug/usb-libvirt-hotplug.sh "$domain"
else
     # Delay "add" events to workaround https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=846173
     /usr/bin/systemd-run --on-active=5 --timer-property=AccuracySec=100ms env SUBSYSTEM="$SUBSYSTEM" DEVTYPE="$DEVTYPE" ACTION="$ACTION" BUSNUM="$BUSNUM" DEVNUM="$DEVNUM" /root/usb-libvirt-hotplug/usb-libvirt-hotplug.sh "$domain"
fi


==== /root/usb-libvirt-hotplug/usb-libvirt-hotplug.sh

See https://github.com/olavmrk/usb-libvirt-hotplug

==== /etc/libvirt/hooks/qemu


#!/bin/bash

add_usb_device() {
     sysdir="$1"
     vm="$2"
     if [ -e "$sysdir" ]; then
 	bus="$(grep BUSNUM= $sysdir/uevent | cut -d = -f2)"
 	dev="$(grep DEVNUM= $sysdir/uevent | cut -d = -f2)"
 	# hack: avoid deadlocks that happen if you call libvirt API from a libvirt hook..
 	setsid nohup env ACTION=add SUBSYSTEM=usb DEVTYPE=usb_device BUSNUM=$bus DEVNUM=$dev /root/usb-libvirt-hotplug/usb-libvirt-hotplug.sh "$vm" < /dev/null > /dev/null 2>&1 &

     fi
}

if [ "$2" = "started" -o "$2" = "restore" ]; then
     case "$1" in
 	radio.home)
 	    add_usb_device '/sys/devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.1/1-9.1.1' radio.home
 	    ;;
 	print.home)
 	    add_usb_device '/sys/devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.1/1-9.1.2' print.home
 	    ;;
 	tv.home)
 	    add_usb_device '/sys/devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.1/1-9.1.3' tv.home
 	    add_usb_device '/sys/devices/pci0000:00/0000:00:14.0/usb1/1-9/1-9.1/1-9.1.4' tv.home
 	    ;;
 	backupgateway)
 	    add_usb_device '/sys/devices/pci0000:00/0000:00:14.0/usb1/1-4' backupgateway
 	    ;;
     esac
fi

cat

exit 0


-Timo



More information about the Pkg-libvirt-maintainers mailing list