Skip to content

Commit 33f5f72

Browse files
authored
Merge pull request #2 from stackhpc/support-virt-engine
Support virt engine
2 parents 3fe9e0a + da56e8c commit 33f5f72

File tree

5 files changed

+93
-2
lines changed

5 files changed

+93
-2
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ 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+
32+
`libvirt_vm_machine`: Virtual machine type. Default is `None` if
33+
`libvirt_vm_engine` is `kvm`, otherwise `pc-1.0`.
34+
35+
`libvirt_vm_cpu_mode`: Virtual machine CPU mode. Default is `host-passthrough`
36+
if `libvirt_vm_engine` is `kvm`, otherwise `host-model`.
37+
2438
`libvirt_vm_volumes`: a list of volumes to attach to the VM. Each volume is
2539
defined with the following dict:
2640
- `name`: Name to associate with the volume being created.

defaults/main.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@ 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+
25+
# Virtual machine type.
26+
libvirt_vm_machine: "{{ None if libvirt_vm_engine == 'kvm' else 'pc-1.0' }}"
27+
28+
# Virtual machine CPU mode.
29+
libvirt_vm_cpu_mode: "{{ 'host-passthrough' if libvirt_vm_engine == 'kvm' else 'host-model' }}"
30+
1431
# List of volumes.
1532
libvirt_vm_volumes: []
1633

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: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
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 }}'{% if libvirt_vm_machine is not none %} machine='{{ libvirt_vm_machine }}'{% endif %}>hvm</type>
8+
<bootmenu enable='no'/>
9+
<bios useserial='yes'/>
810
</os>
11+
<cpu{% if libvirt_vm_cpu_mode is not none %} mode='{{ libvirt_vm_cpu_mode }}'{% endif %}>
12+
<model fallback='allow'/>
13+
</cpu>
914
<devices>
15+
<emulator>{{ libvirt_vm_emulator }}</emulator>
1016
{% for volume in libvirt_vm_volumes %}
1117
<disk type='volume' device='{{ volume.device | default('disk') }}'>
1218
<driver name='qemu' type='{{ volume.format }}'/>

0 commit comments

Comments
 (0)