From c1c34332e062acb2431ae6ac443a18b5fce7ff07 Mon Sep 17 00:00:00 2001 From: Mark Goddard Date: Wed, 21 Mar 2018 19:49:31 +0000 Subject: [PATCH 1/4] 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. --- README.md | 8 +++++++ defaults/main.yml | 11 +++++++++ tasks/autodetect.yml | 53 ++++++++++++++++++++++++++++++++++++++++++++ tasks/main.yml | 1 + templates/vm.xml.j2 | 8 +++++-- 5 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 tasks/autodetect.yml diff --git a/README.md b/README.md index 6aca023..cbcbbe4 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,14 @@ Defaults to `present`. `libvirt_vm_vcpus`: the number of VCPU cores to assign to the VM. +`libvirt_vm_engine`: virtualisation engine. If not set, the role will attempt +to auto-detect the optimal engine to use. + +`libvirt_vm_emulator`: path to emulator binary. If not set, the role will +attempt to auto-detect the correct emulator to use. + +`libvirt_vm_arch`: CPU architecture, default is `x86_64`. + `libvirt_vm_volumes`: a list of volumes to attach to the VM. Each volume is defined with the following dict: - `name`: Name to associate with the volume being created. diff --git a/defaults/main.yml b/defaults/main.yml index 054a1c2..d37ccfe 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -11,6 +11,17 @@ libvirt_vm_memory_mb: # Number of vCPUs. libvirt_vm_vcpus: +# Virtualisation engine. If not set, the role will attempt to auto-detect the +# optimal engine to use. +libvirt_vm_engine: + +# Path to emulator binary. If not set, the role will attempt to auto-detect the +# correct emulator to use. +libvirt_vm_emulator: + +# CPU architecture. +libvirt_vm_arch: x86_64 + # List of volumes. libvirt_vm_volumes: [] diff --git a/tasks/autodetect.yml b/tasks/autodetect.yml new file mode 100644 index 0000000..404ef7d --- /dev/null +++ b/tasks/autodetect.yml @@ -0,0 +1,53 @@ +--- +- name: Detect the virtualisation engine + block: + - name: Load the kvm kernel module + modprobe: + name: kvm + become: true + failed_when: false + + - name: Check for the KVM device + stat: + path: /dev/kvm + register: stat_kvm + + - name: Set a fact containing the virtualisation engine + set_fact: + libvirt_vm_engine: "{% if stat_kvm.stat.exists %}kvm{% else %}qemu{% endif %}" + when: libvirt_vm_engine is none + +- name: Detect the virtualisation emulator + block: + - block: + - name: Detect the KVM emulator binary path + stat: + path: "{{ item }}" + register: kvm_emulator_result + with_items: + - /usr/bin/kvm + - /usr/bin/qemu-kvm + - /usr/libexec/qemu-kvm + + - name: Set a fact containing the KVM emulator binary path + set_fact: + libvirt_vm_emulator: "{{ item.item }}" + with_items: "{{ kvm_emulator_result.results }}" + when: item.stat.exists + when: libvirt_vm_engine == 'kvm' + + - block: + - name: Detect the QEMU emulator binary path + shell: which qemu-system-{{ libvirt_vm_arch }} + register: qemu_emulator_result + + - name: Set a fact containing the QEMU emulator binary path + set_fact: + libvirt_vm_emulator: "{{ qemu_emulator_result.stdout }}" + when: libvirt_vm_engine == 'qemu' + + - name: Fail if unable to detect the emulator + fail: + msg: Unable to detect emulator for engine {{ libvirt_vm_engine }}. + when: libvirt_vm_emulator is none + when: libvirt_vm_emulator is none diff --git a/tasks/main.yml b/tasks/main.yml index 6808418..edb2766 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,5 +1,6 @@ --- - block: + - include: autodetect.yml - include: volumes.yml - include: vm.yml when: libvirt_vm_state == 'present' diff --git a/templates/vm.xml.j2 b/templates/vm.xml.j2 index aac3318..dac6162 100644 --- a/templates/vm.xml.j2 +++ b/templates/vm.xml.j2 @@ -1,12 +1,16 @@ - + {{ libvirt_vm_name }} {{ libvirt_vm_memory_mb | int * 1024 }} {{ libvirt_vm_vcpus }} - hvm + hvm + {% if libvirt_vm_engine == 'kvm' %} + + {% endif %} + {{ libvirt_vm_emulator }} {% for volume in libvirt_vm_volumes %} From 31b18a785b12dae3ca8b019671d7427e0486ea17 Mon Sep 17 00:00:00 2001 From: Mark Goddard Date: Fri, 27 Apr 2018 08:38:55 +0100 Subject: [PATCH 2/4] Add VM machine and CPU mode support Machine default is pc-1.0. Perhaps this should be none by default? Mode default is 'host-passthrough' for KVM, 'host-model' otherwise. --- README.md | 6 ++++++ defaults/main.yml | 6 ++++++ templates/vm.xml.j2 | 8 ++++---- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index cbcbbe4..42d3c0d 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,12 @@ attempt to auto-detect the correct emulator to use. `libvirt_vm_arch`: CPU architecture, default is `x86_64`. +`libvirt_vm_machine`: Virtual machine type. Default is `None` if +`libvirt_vm_engine` is `kvm`, otherwise `pc-1.0`. + +`libvirt_vm_cpu_mode`: Virtual machine CPU mode. Default is `host-passthrough` +if `libvirt_vm_engine` is `kvm`, otherwise `host-model`. + `libvirt_vm_volumes`: a list of volumes to attach to the VM. Each volume is defined with the following dict: - `name`: Name to associate with the volume being created. diff --git a/defaults/main.yml b/defaults/main.yml index d37ccfe..1685407 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -22,6 +22,12 @@ libvirt_vm_emulator: # CPU architecture. libvirt_vm_arch: x86_64 +# Virtual machine type. +libvirt_vm_machine: "{{ None if libvirt_vm_engine == 'kvm' else 'pc-1.0' }}" + +# Virtual machine CPU mode. +libvirt_vm_cpu_mode: "{{ 'host-passthrough' if libvirt_vm_engine == 'kvm' else 'host-model' }}" + # List of volumes. libvirt_vm_volumes: [] diff --git a/templates/vm.xml.j2 b/templates/vm.xml.j2 index dac6162..0186e97 100644 --- a/templates/vm.xml.j2 +++ b/templates/vm.xml.j2 @@ -4,11 +4,11 @@ {{ libvirt_vm_vcpus }} - hvm + hvm - {% if libvirt_vm_engine == 'kvm' %} - - {% endif %} + + + {{ libvirt_vm_emulator }} {% for volume in libvirt_vm_volumes %} From 87a4d307ad2a976733bcfa4c601b11826f1ae951 Mon Sep 17 00:00:00 2001 From: Mark Goddard Date: Fri, 27 Apr 2018 08:39:42 +0100 Subject: [PATCH 3/4] Disable boot menu --- templates/vm.xml.j2 | 1 + 1 file changed, 1 insertion(+) diff --git a/templates/vm.xml.j2 b/templates/vm.xml.j2 index 0186e97..b3f3445 100644 --- a/templates/vm.xml.j2 +++ b/templates/vm.xml.j2 @@ -5,6 +5,7 @@ hvm + From da56e8c1b508d43fe0e182de51b90d6c88bc0f2d Mon Sep 17 00:00:00 2001 From: Mark Goddard Date: Fri, 27 Apr 2018 08:40:01 +0100 Subject: [PATCH 4/4] Use serial port for BIOS output --- templates/vm.xml.j2 | 1 + 1 file changed, 1 insertion(+) diff --git a/templates/vm.xml.j2 b/templates/vm.xml.j2 index b3f3445..494d7ed 100644 --- a/templates/vm.xml.j2 +++ b/templates/vm.xml.j2 @@ -6,6 +6,7 @@ hvm +