Skip to content

Commit c1c3433

Browse files
committed
Support configuration of engine, emulator and CPU arch
For the engine and emulator configuration, some level of autodetection is supported - if KVM is supported, this will be used, otherwise QEMU will be used. Much of the logic added here has been adapted from the ironic devstack plugin's tooling.
1 parent 3fe9e0a commit c1c3433

File tree

5 files changed

+79
-2
lines changed

5 files changed

+79
-2
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ Defaults to `present`.
2121

2222
`libvirt_vm_vcpus`: the number of VCPU cores to assign to the VM.
2323

24+
`libvirt_vm_engine`: virtualisation engine. If not set, the role will attempt
25+
to auto-detect the optimal engine to use.
26+
27+
`libvirt_vm_emulator`: path to emulator binary. If not set, the role will
28+
attempt to auto-detect the correct emulator to use.
29+
30+
`libvirt_vm_arch`: CPU architecture, default is `x86_64`.
31+
2432
`libvirt_vm_volumes`: a list of volumes to attach to the VM. Each volume is
2533
defined with the following dict:
2634
- `name`: Name to associate with the volume being created.

defaults/main.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ libvirt_vm_memory_mb:
1111
# Number of vCPUs.
1212
libvirt_vm_vcpus:
1313

14+
# Virtualisation engine. If not set, the role will attempt to auto-detect the
15+
# optimal engine to use.
16+
libvirt_vm_engine:
17+
18+
# Path to emulator binary. If not set, the role will attempt to auto-detect the
19+
# correct emulator to use.
20+
libvirt_vm_emulator:
21+
22+
# CPU architecture.
23+
libvirt_vm_arch: x86_64
24+
1425
# List of volumes.
1526
libvirt_vm_volumes: []
1627

tasks/autodetect.yml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
- name: Detect the virtualisation engine
3+
block:
4+
- name: Load the kvm kernel module
5+
modprobe:
6+
name: kvm
7+
become: true
8+
failed_when: false
9+
10+
- name: Check for the KVM device
11+
stat:
12+
path: /dev/kvm
13+
register: stat_kvm
14+
15+
- name: Set a fact containing the virtualisation engine
16+
set_fact:
17+
libvirt_vm_engine: "{% if stat_kvm.stat.exists %}kvm{% else %}qemu{% endif %}"
18+
when: libvirt_vm_engine is none
19+
20+
- name: Detect the virtualisation emulator
21+
block:
22+
- block:
23+
- name: Detect the KVM emulator binary path
24+
stat:
25+
path: "{{ item }}"
26+
register: kvm_emulator_result
27+
with_items:
28+
- /usr/bin/kvm
29+
- /usr/bin/qemu-kvm
30+
- /usr/libexec/qemu-kvm
31+
32+
- name: Set a fact containing the KVM emulator binary path
33+
set_fact:
34+
libvirt_vm_emulator: "{{ item.item }}"
35+
with_items: "{{ kvm_emulator_result.results }}"
36+
when: item.stat.exists
37+
when: libvirt_vm_engine == 'kvm'
38+
39+
- block:
40+
- name: Detect the QEMU emulator binary path
41+
shell: which qemu-system-{{ libvirt_vm_arch }}
42+
register: qemu_emulator_result
43+
44+
- name: Set a fact containing the QEMU emulator binary path
45+
set_fact:
46+
libvirt_vm_emulator: "{{ qemu_emulator_result.stdout }}"
47+
when: libvirt_vm_engine == 'qemu'
48+
49+
- name: Fail if unable to detect the emulator
50+
fail:
51+
msg: Unable to detect emulator for engine {{ libvirt_vm_engine }}.
52+
when: libvirt_vm_emulator is none
53+
when: libvirt_vm_emulator is none

tasks/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
- block:
3+
- include: autodetect.yml
34
- include: volumes.yml
45
- include: vm.yml
56
when: libvirt_vm_state == 'present'

templates/vm.xml.j2

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
<domain type='kvm'>
1+
<domain type='{{ libvirt_vm_engine }}'>
22
<name>{{ libvirt_vm_name }}</name>
33
<memory>{{ libvirt_vm_memory_mb | int * 1024 }}</memory>
44
<vcpu>{{ libvirt_vm_vcpus }}</vcpu>
55
<clock sync="localtime"/>
66
<os>
7-
<type arch='x86_64'>hvm</type>
7+
<type arch='{{ libvirt_vm_arch }}'>hvm</type>
88
</os>
9+
{% if libvirt_vm_engine == 'kvm' %}
10+
<cpu mode='host-passthrough'/>
11+
{% endif %}
912
<devices>
13+
<emulator>{{ libvirt_vm_emulator }}</emulator>
1014
{% for volume in libvirt_vm_volumes %}
1115
<disk type='volume' device='{{ volume.device | default('disk') }}'>
1216
<driver name='qemu' type='{{ volume.format }}'/>

0 commit comments

Comments
 (0)