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

David Gilmour david at gilmour.net
Fri Sep 22 11:20:34 UTC 2017


Timo, thanks.  Also, Ron, I wanted to add my belated thanks to you for taking this up and digging in so carefully, to the root cause.

Cheers all,

David

-----Original Message-----
From: Timo Lindfors [mailto:timo.lindfors at iki.fi] 
Sent: Thursday, September 21, 2017 11:44 AM
To: 846173 at bugs.debian.org
Cc: David Gilmour <david at gilmour.net>; ron at debian.org
Subject: Re: Bug#846173: libvirt-daemon: Fails to locate existing usb device

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