BBVIRT(1) | General Commands Manual | BBVIRT(1) |
bbvirt - hotplug BitBabbler devices into libvirt managed domains
bbvirt action [options]
bbvirt attach|detach device [options]
bbvirt attach-all|detach-all [domain] [options]
The bbvirt program is an attempt to take some of the pain out of what is currently required to distribute multiple USB devices between the host and guest virtual machines. While there are several ways in which this may be configured and managed, at present none of them actually provide a complete and coherent solution on their own, all of them fall short of the mark in some significant and annoying way. The aim here is to piece together enough of those hacks to actually get all of the functionality that we want now, until the libvirt native support for this improves enough to not need it anymore.
At present this deals with libvirt managed QEMU/KVM virtual machines.
The ideal behaviour here is pretty simple. Given some arbitrary number of BitBabbler devices, we should be able to assign them to either the host machine, or to a guest VM running on it, and once we do that they should behave in the normal manner expected of any USB device.
Right now, libvirt gives us two ways that we can assign USB devices from the host to a guest domain.
There is a third way, but it relies on bypassing the normal libvirt configuration to make direct use of the QEMU ability to assign a device by its physical address on the bus. Which is better, but still not a magic bullet since it relies on plugging exactly the same devices into exactly the same ports every time (and on having those ports enumerated in the same way by the host on every reboot, which isn't guaranteed either). It also forces us to jump through other hoops, since we then need additional complication to manage the access permissions of the device manually outside of libvirt, but still in coordination with it.
The even bigger failing, which all of those methods have in common, is they all depend on the device already being plugged in before the guest is started. If it is inserted after the guest is started, or removed and replugged while the guest is running, or if the host bus or a hub bounces causing a reconnect, then the device will not be (re)attached to the guest. The only way to fix that if it happens is to manually reattach the device with an arcane incantation in XML (which relies on you knowing the new address of the device), or to completely power down and restart the guest. Not the pinnacle of user-friendly operation that we are looking for here.
There was a patch submitted to libvirt some years back which would have allowed a device to be specified by both its USB product ID and its serial number, but that got some push-back, and so far has still not been applied upstream. That would have gone a long way toward making this both easy and clean, leaving us only with the hotplug aspect to deal with. We'll leave grumpy snark about that as an exercise for the reader ...
Another alternative is we can delegate finding the device's logical address to a hotplug manager like udev(7). This is attractive in the sense that we can know when the address of a device changes and what it changes to, but udev itself isn't very friendly to the idea of local admin customisation (while it is possible to do, it seems to be getting increasingly strongly discouraged) and using it still requires some external glue to translate its events into something that libvirt can act on to configure the guest machine.
The bbvirt program provides that glue, and a user friendly method of assigning which devices should belong to which guest domains, and a front end that can be invoked manually or by other admin controlled tasks to quickly and easily add or remove BitBabbler devices from any of the running guest machines.
But the limitation this approach has, is that it can't easily know when a guest machine is started which should have devices that are already plugged in added to it. In theory we could add them to its persistent domain definition, but that has its own problems because we can only add devices by their ephemeral logical address, and we can't guarantee that we will get called to remove them from the domain again when that address becomes invalid (like if the host is suddenly powered off or it is otherwise not cleanly shut down), so we could end up with many stale entries accumulating in the persistent domain configuration, which could later match some completely different device to what we had wanted attached to it. Which means until that somehow gets fixed, it's only safe to add them to a live guest domain, so that they will always be removed again when it is halted, no matter how it ended up getting halted.
Clearly we've still got some way to go to get to our ideal here.
There appears to be only two ways that we can get notified of a guest machine being started at present. One involves running yet another daemon process, which would do little more than just sit around waiting for someone to start a guest so it could tell us about that. But then we'd have yet another thing to configure, yet another process running, and yet more problems with figuring out how to ensure we don't lose a race when the host is booted, between getting the initial set of device events, that process being ready and active, and any guests that will be autostarted at boot actually starting.
The other way is to use a libvirt hook. Which in turn has the problem of not actually allowing us to run any libvirt functions from it, which we need to do in order to attach the device to the host. And which we can't guarantee that we can just install by default, because there can be only one such hook on the system, which the local admin may already be using ...
There is a third way, but that would involve requiring the local admin to start all guest machines through a wrapper of our own, instead of via whatever mechanism they already know and use. Which doesn't scale to support other USB devices in the same situation, among the many ways that would be a horrible solution to inflict on people.
But there is a loophole we can exploit. We can use the libvirt qemu hook to trigger a change event for udev, which can in turn invoke bbvirt in much the same way that would happen if the device was really hotplugged, which gives us the extra layer of indirection we need to be able to safely do that from the hook. Rube Goldberg would be proud, and some of the pieces may require hand-assembly, but with all of this in place, we can have something resembling normal USB functionality in the guest machines.
It's not pretty, but it will work with what we have to work with.
To string this together, you'll need to ensure all of the following:
Until there is some safe way we can install this without conflicting with or overwriting an existing hook, everyone will need to do this step manually. If you have installed the Debian packages, then the example hook script that we've provided for this can be found in /usr/share/doc/bit-babbler/examples/qemu-hook. If you didn't it can be found in libvirt/qemu-hook of the source package.
You will need to install that file as /etc/libvirt/hooks/qemu, or merge its content with the existing qemu file there if you already have that hook set. If that file did not previously exist, you will need to restart libvirtd(8) to get it to begin using it.
That should cover all of the needed automation, but you can also attach and detach devices manually at any time too. The details of doing that will be described in the following section. Otherwise, with all the above done, there is no other reason to need to invoke bbvirt directly.
There are two primary modes of operation for bbvirt which are selected by the initial action option. If the action to perform is attach or detach then only a single device will be acted upon, and which device that should be must be specified explicitly, even if there is only one device present on the host at the time. When invoking bbvirt manually, the device may be specified by its serial number, its logical address on the bus (in the form busnum:devnum, given as decimal integers), or its physical address on the bus (in the form busnum-port[.port ...]).
If the action to perform is attach-all or detach-all, then the device(s) to act upon are selected by domain association instead. If a domain is explicitly specified, then all devices which are assigned to that guest domain in the configuration file will be acted upon in the same way as if bbvirt was invoked for each of them individually with the attach or detach action. If no domain is provided, then all of the configured guest domains will be acted upon in this way.
The following additional options are available:
The bbvirt configuration file contains variable assignments using the bash(1) shell syntax. It is sourced as a shell snippet, so you could in principle construct the configuration for each domain dynamically, but most typically a simple static assignment of devices to domains will suffice. If you do elect to run code in it, you should be very defensive about namespacing any other variables you use, or any other side effects you might cause to happen. Any number of guest domains may be configured in it.
For each guest domain, three variables control the behaviour of bbvirt:
When specifying a domain option for bbvirt to act upon, you may use either of the domain or guestname identifiers interchangeably.
The device serial number must always be used here. You cannot specify a device by its logical or physical address on the bus (like you can in most other places where we take a device ID).
bbvirt was written by Ron <ron@debian.org>. You can send bug reports, feature requests, praise and complaints to support@bitbabbler.org.
January 2, 2018 |