Skip to content

Add usb support to libvirt-vm role #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 13, 2023
Merged

Add usb support to libvirt-vm role #95

merged 7 commits into from
Mar 13, 2023

Conversation

gavinwill
Copy link
Contributor

@gavinwill gavinwill commented Mar 12, 2023

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

@gavinwill gavinwill marked this pull request as ready for review March 12, 2023 18:13
@gavinwill gavinwill requested a review from a team as a code owner March 12, 2023 18:13
Copy link

@markgoddard markgoddard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@gavinwill gavinwill requested a review from markgoddard March 13, 2023 12:51
Copy link

@markgoddard markgoddard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@markgoddard markgoddard merged commit 022ec84 into stackhpc:master Mar 13, 2023
@gavinwill gavinwill deleted the add_usb_support branch March 13, 2023 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants