Commands · reference

vmlab usb

4 min read · 2026-10-19 · vmlab 0.9

vmlab usb passes USB devices plugged into the host through to running VMs. It lists what the host has and which VM holds each device, attaches one to a VM, and gives it back. The device leaves the host while a VM holds it. See Host USB devices for the walkthrough.

sh
vmlab usb <COMMAND>
SubcommandMeaning
listList the host's USB devices (hubs hidden) and the VM holding each.
attachHot-plug a host USB device into a running VM that has usb = true.
detachUnplug a host USB device from a VM and give it back to the host.
-h, --helpPrint help.

Selectors

attach and detach name a device with a selector. vmlab usb list shows the parts of each.

SelectorNames
<vid>:<pid>The device with that vendor and product id, four hex digits each, e.g. 046d:c547.
<vid>:<pid>@<serial>The same, narrowed to one serial number.
port:<bus>-<port>[.<port>...]The device in that socket, e.g. port:1-2.1.4. Always exactly one device.

A selector that matches no device is refused. One that matches several, such as two identical flash drives without a serial, is refused with every candidate listed by its port: selector.

vmlab usb list

sh
vmlab usb list [OPTIONS]
OptionMeaning
--jsonEmit a JSON array instead of a table.
-h, --helpPrint help.

One row per device, in port order: PORT, ID (vid:pid), SERIAL, SPEED (low, full, high, super, super+), ATTACHED TO (lab/vm, or - while free) and DEVICE (manufacturer and product). Root hubs and hubs are hidden. The holders come from the supervisor, which asks every running lab. With no supervisor running there are no labs, so the host's own device list is printed with nothing held, and no supervisor is started.

text
PORT         ID         SERIAL  SPEED  ATTACHED TO  DEVICE
1-2.1.4.1.4  046d:c547  -       full   usbtest/u1   Logitech USB Receiver
1-3          0781:5581  BB22    super  -            SanDisk Ultra

vmlab usb attach

sh
vmlab usb attach [OPTIONS] <VM> <SELECTOR>
OptionMeaning
<VM>The VM, as [lab/]vm.
<SELECTOR>The device; see Selectors above.
--forceAttach a HID keyboard or mouse, which the host is probably using.
--jsonEmit the attachment as JSON.
-h, --helpPrint help.

The VM must be running and must have been started with usb = true on its vm block or its profile (Lab file: vm). The attach is refused, in this order, when:

On success it prints `attached 046d:c547 Logitech USB Receiver at port 1-2.1.4.1.4 to usbtest/u1 and emits usb.attached`. The attachment is pinned to that plug-in of the device: unplugging it from the host detaches it from the VM within about two seconds (usb.detached, reason unplugged), and plugging it back in does not reattach it.

vmlab usb detach

sh
vmlab usb detach [OPTIONS] <VM> [SELECTOR]
OptionMeaning
<VM>The VM, as [lab/]vm.
[SELECTOR]The device, as for attach. Required unless --all is given.
--allDetach every device the VM holds.
--jsonEmit what was detached as JSON.
-h, --helpPrint help.

The selector is matched against what the VM holds, not against the host, so a device that has already gone can still be named. The verb returns once QEMU reports the device deleted, and the device is back on the host. Stopping the VM releases every device it holds as well. Online snapshots are refused while a VM holds any device; see Snapshots.

Examples

Find a device and hand it to a VM, then take it back:

sh
vmlab usb list
vmlab usb attach u1 0781:5581@BB22
vmlab usb detach u1 0781:5581@BB22

Lend a VM the host's wireless mouse receiver, then release everything:

sh
vmlab usb attach usbtest/u1 port:1-2.1.4.1.4 --force
vmlab usb detach usbtest/u1 --all

Exit status

Exit status is 0 on success. Exit 4 (not_found) means the lab declares no machine by that name. Every refusal above exits 1 (failed). A qualified reference to a lab that is not running exits 1. A usage error, such as detach with neither a selector nor --all, exits 2.