stackhpc / ansible-role-libvirt-vm

This role configures and creates VMs on a KVM hypervisor.
128 stars 67 forks source link

Add usb support to libvirt-vm role #95

Closed gavinwill closed 1 year ago

gavinwill commented 1 year ago

Hi

This PR adds functionality to pass through USB devices from Host to VM. I appreciate in a datacenter environment with many VMs this use case may be minimal but can still be a handy option to have for the VMs. As such I have made it transparent and skip over if there is not any usb_devices defined.

I have also tried to follow the style and logic of this role in the way you use includes and generate vars for the additional USB parts.

This has been tested with ansible without any usb_devices defined (skips tasks and is otherwise "transparent"), along with a combination of 1 and 2 USB devices with no errors. I have not tested if there is an upper limit for USB devices presented to a machine. Documentation for upper limit in KVM is a bit sparse. Happy to add an assert or validation if a limit is found. I would hope most users want only a couple of devices to present to VMs

root@kvm-host-5ab7ea:~# virsh list
 Id   Name      State
-------------------------
 3    pfsense   running

root@kvm-host-5ab7ea:~# virsh dumpxml pfsense | grep -B2 -A5 0x0781
    <hostdev mode='subsystem' type='usb' managed='yes'>
      <source>
        <vendor id='0x0781'/>
        <product id='0x5567'/>
        <address bus='3' device='13'/>
      </source>
      <alias name='hostdev0'/>
      <address type='usb' bus='0' port='1'/>

The main thing to be aware about is that Libvirt will complain if the devices are not present when the machine XML is generated - This is noted in the commit for the REAMDE.

Therefore to use the same approach as pre validating the network config with the include to task check-interface.yml, I have an include to check-usb-devices.yml which will validate the USB config before the VM XML task is generated / run. I have had to ignore errors for the output of lsusb and then present the "error" in a bit more of a user friendly message.

TASK [ansible-role-libvirt-vm : List USB hardware] ***********************************************************************************************************************************
task path: /Users/gavin/Documents/GIT/HomeLab/ansible/roles/ansible-role-libvirt-vm/tasks/check-usb-devices.yml:2
fatal: [kvm.gavinwill.me.uk]: FAILED! => {"changed": false, "cmd": ["lsusb", "-d", "0x0781:0x5567"], "delta": "0:00:00.008653", "end": "2023-03-12 17:54:24.967753", "msg": "non-zero return code", "rc": 1, "start": "2023-03-12 17:54:24.959100", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
...ignoring

TASK [ansible-role-libvirt-vm : Check USB device is present on Host system] **********************************************************************************************************
task path: /Users/gavin/Documents/GIT/HomeLab/ansible/roles/ansible-role-libvirt-vm/tasks/check-usb-devices.yml:9
fatal: [kvm.gavinwill.me.uk]: FAILED! => {"changed": false, "msg": "The USB Device with Vendor ID:0x0781 and Product ID:0x5567 is not seen on host system Is the USB device plugged in correctly ?\n"}

Happy to adjust this PR to match your requirements if there is anything else that may need changed otherwise hope you approve this PR and we can get the USB functionality added to the role in a new release.

Many thanks for this role - It works well for my requirements at home Gavin