Skip to content

Commit 022ec84

Browse files
authored
Add usb support to libvirt-vm role (#95)
* Update Readme for USB support * Add usb_devices var to include vm.yml * adding include to validate presence of usb devices on host * adding usb section to vm.xml template * adding include to validate usb devices * moving to faile_when for cleaner handling of errors * remove loop index as Libvirt fills in data
1 parent fc36877 commit 022ec84

File tree

5 files changed

+55
-0
lines changed

5 files changed

+55
-0
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,27 @@ Role Variables
120120
- `dev`: (optional) Block device path when type is `block`.
121121
- `remote_src`: (optional) When type is `file` or `block`, specify wether `image` points to a remote file (true) or a file local to the host that launched the playbook (false). Defaults to true.
122122

123+
- `usb_devices`: a list of usb devices to present to the vm from the host.
124+
125+
Each usb device is defined with the following dict:
126+
127+
- `vendor`: The vendor id of the USB device.
128+
- `product`: The product id of the USB device.
129+
130+
Note - Libvirt will error if the VM is provisioned and the USB device is not attached.
131+
132+
To obtain the vendor id and product id of the usb device from the host running as sudo / root with the usb device plugged in
133+
run `lsusb -v`. Example below with an attached Sandisk USB Memory Stick with vendor id: `0x0781` and product id: `0x5567`
134+
135+
```
136+
lsusb -v | grep -A4 -i sandisk
137+
138+
idVendor 0x0781 SanDisk Corp.
139+
idProduct 0x5567 Cruzer Blade
140+
bcdDevice 1.00
141+
iManufacturer 1
142+
iProduct 2
143+
```
123144
124145
- `interfaces`: a list of network interfaces to attach to the VM.
125146
Each network interface is defined with the following dict:
@@ -231,6 +252,10 @@ Example Playbook
231252
232253
interfaces:
233254
- network: 'br-datacentre'
255+
256+
usb_devices:
257+
- vendor: '0x0781'
258+
product: '0x5567'
234259
235260
- state: present
236261
name: 'vm2'

tasks/check-usb-devices.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
- name: List USB hardware
3+
command: lsusb -d {{ usb_device.vendor }}:{{ usb_device.product }}
4+
register: host_attached_usb_device
5+
become: true
6+
changed_when: false
7+
failed_when: false
8+
9+
- name: Check USB device is present on Host system
10+
fail:
11+
msg: >
12+
The USB Device with Vendor ID:{{ usb_device.vendor }} and Product ID:{{ usb_device.product }} is not seen on host system
13+
Is the USB device plugged in correctly ?
14+
when:
15+
- host_attached_usb_device.rc != 0

tasks/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
cpu_mode: "{{ vm.cpu_mode | default(libvirt_cpu_mode_default) }}"
4242
volumes: "{{ vm.volumes | default([], true) }}"
4343
interfaces: "{{ vm.interfaces | default([], true) }}"
44+
usb_devices: "{{ vm.usb_devices | default([], false) }}"
4445
start: "{{ vm.start | default(true) }}"
4546
autostart: "{{ vm.autostart | default(true) }}"
4647
enable_vnc: "{{ vm.enable_vnc | default(false) }}"

tasks/vm.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
interface: "{{ item }}"
1717
with_items: "{{ interfaces }}"
1818

19+
- name: Validate Host USB Devices
20+
include_tasks: check-usb-devices.yml
21+
vars:
22+
usb_device: "{{ item }}"
23+
with_items: "{{ usb_devices }}"
24+
1925
- name: Ensure the VM is defined
2026
virt:
2127
command: define

templates/vm.xml.j2

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@
124124
<listen type='address'/>
125125
</graphics>
126126
{% endif %}
127+
{% for usb_device in usb_devices %}
128+
<hostdev mode='subsystem' type='usb' managed='yes'>
129+
<source>
130+
<vendor id='{{ usb_device.vendor }}'/>
131+
<product id='{{ usb_device.product }}'/>
132+
</source>
133+
</hostdev>
134+
{% endfor %}
127135
<rng model="virtio"><backend model="random">/dev/urandom</backend></rng>
128136
</devices>
129137
</domain>

0 commit comments

Comments
 (0)