On 10/26/26 12:05, Yaroslav Borbat wrote:
From: Yaroslav Borbat <yaroslav.752@gmail.com>
Inside a container sysfs is usually mounted from the host and exposes all USB devices, while nodes under /dev/bus/usb/ only exist for devices passed into the container's mount namespace. virUSBDeviceSearch() enumerated devices from sysfs alone, so such a device was reported as present: a hostdev with startupPolicy='optional' was not dropped and the domain failed to start.
Skip devices whose /dev node is inaccessible during enumeration. virUSBDeviceFind() now ignores them for optional hostdevs and reports an explicit "not accessible in the current mount namespace" error for mandatory ones. Extend the virusb test to cover both cases.
Signed-off-by: Yaroslav Borbat <yaroslav.752@gmail.com> Resolves: https://gitlab.com/libvirt/libvirt/-/work_items/894 --- v2: - rebased onto current master (no functional changes)
v1: https://lists.libvirt.org/archives/list/devel@lists.libvirt.org/message/2026... (https://patchew.org/Libvirt/20260716213249.3163905-1-yaroslav.752@gmail.com/)
Context: this was hit with libvirt running inside a container (deckhouse/virtualization). Container runtimes mount sysfs from the host, so /sys/bus/usb/devices/ lists every host USB device, while /dev/bus/usb/ only contains the nodes explicitly passed into the container. A self-contained Docker reproducer (no special hardware needed, uses the root hub at bus 1 / device 1) is in the issue above.
src/util/virusb.c | 41 ++++++++++++++++++++++++++++++++++++----- tests/virusbmock.c | 33 ++++++++++++++++++++++++++++++++- tests/virusbtest.c | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 6 deletions(-)
diff --git a/src/util/virusb.c b/src/util/virusb.c index 85ad3d58ce..1d31912971 100644 --- a/src/util/virusb.c +++ b/src/util/virusb.c @@ -120,7 +120,8 @@ virUSBDeviceSearch(unsigned int vendor, unsigned int devno, const char *port, const char *vroot, - unsigned int flags) + unsigned int flags, + char **inaccessiblePath)
We like the flags argument to be the last one.
{ g_autoptr(DIR) dir = NULL; bool found = false; @@ -198,6 +199,28 @@ virUSBDeviceSearch(unsigned int vendor, if (!usb) goto cleanup;
+ /* In containerized environments sysfs may expose USB devices + * from the host kernel while the corresponding device node + * under /dev/bus/usb/ is not present in the current mount + * namespace. Such a device cannot be used, so skip it here. + * Remember its path so that the caller can distinguish "no + * matching device at all" from "device present on the host but + * inaccessible in our mount namespace" when reporting errors. */ + if (virUSBDeviceGetPath(usb) && + !virFileExists(virUSBDeviceGetPath(usb))) { + VIR_DEBUG("USB device %03u:%03u found in sysfs but device " + "node '%s' is not accessible in the current mount " + "namespace, skipping", + found_bus, found_devno, + virUSBDeviceGetPath(usb)); + if (inaccessiblePath && !*inaccessiblePath) + *inaccessiblePath = g_strdup(virUSBDeviceGetPath(usb)); + g_clear_pointer(&usb, virUSBDeviceFree); + if (found) + break; + continue; + } + if (virUSBDeviceListAdd(list, &usb) < 0) goto cleanup;
So if the /dev/... path doesn't exist then usb is cleared but this virUSBDeviceListAdd() is still called. With NULL. I think the logic can be simplified a bit too. If the path exists, just add the device onto the list. Else, report this VIR_DEBUG() and set inaccessiblePath. And if the 'usb' variable declaration is brought into the while() loop then the explicit g_clear_pointer() can be dropped. Let me rewrite the logic a bit and merge. Reviewed-by: Michal Privoznik <mprivozn@redhat.com> Congratulations on your first libvirt contribution! Michal