Removing USB Device from a KVM Virtual Machine without Restart
If you have added a USB device to your KVM virtual machine using the QEMU monitor command device_add without specifying an id, you might want to unplug the device without having to restart the virtual machine. This article will guide you through the process of safely removing a USB device from a running KVM virtual machine.
Understanding QEMU and KVM
QEMU (Quick Emulator) is a free and open-source machine emulator and virtualizer. It can emulate a variety of architectures, including x86, ARM, PowerPC, MIPS, and SPARC. KVM (Kernel-based Virtual Machine) is a full virtualization solution for Linux on x86 hardware that uses QEMU to handle I/O operations.
Finding the Device to Remove
To find the USB device you want to remove, you can use the QEMU monitor command info usb. This command will display a list of all USB devices currently attached to the virtual machine. The output will look something like this:
bus <0>,dev <0>: '/devices/platform/soc/3c9b000.usb/usb1/1-1'
drv 'uhci_hcd'
bus <0>,dev <1>: '/devices/platform/soc/3c9b000.usb/usb1/1-2'
drv 'uhci_hcd'
bus <0>,dev <2>: '/devices/platform/soc/3c9b000.usb/usb1/1-3'
drv 'uhci_hcd'
In this example, there are three USB devices attached to the virtual machine. You can identify the device you want to remove by its bus and dev numbers, or by the device path provided in the output.
Removing the USB Device
Once you have identified the USB device you want to remove, you can use the QEMU monitor command device_del to remove it. The syntax for this command is:
device_del bus.deviceFor example, if you wanted to remove the USB device at bus 0, dev 2, you would use the following command:
device_del 0.2This command will unplug the USB device from the virtual machine, without requiring a restart.
Verifying the Removal
After removing the USB device, you can verify that it has been successfully removed by running the info usb command again. If the device is no longer listed, then it has been removed.
KVM virtual machines can have USB devices added or removed without the need for a restart. By using the QEMU monitor commands info usb and device_del, you can find and remove USB devices from a running virtual machine. This can be especially useful if you have added a device accidentally or need to troubleshoot a problem related to a specific USB device.