From 82d037cdd939c3a7983a645f54a4f2f0a47a9020 Mon Sep 17 00:00:00 2001 From: Anatoly Laskaris Date: Fri, 13 Sep 2024 17:13:48 +0300 Subject: [PATCH 1/2] Add warning about virtualization --- roles/nox/tasks/00-preflight.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/roles/nox/tasks/00-preflight.yml b/roles/nox/tasks/00-preflight.yml index 0f05679..2c368e2 100644 --- a/roles/nox/tasks/00-preflight.yml +++ b/roles/nox/tasks/00-preflight.yml @@ -2,6 +2,12 @@ tags: always fluencelabs.provider.check_tar_type: +- name: check that virtualization is enabled + tags: always + ansible.builtin.warn: + msg: "CPU does not support virtualization or it is disabled in the BIOS/UEFI." + when: "'vmx' not in ansible_processor_flags and 'svm' not in ansible_processor_flags" + - name: check "fluence_instance_id" variable tags: always ansible.builtin.assert: From 690dab56ef50b5b3984ffb15de92ed9f0cf47c17 Mon Sep 17 00:00:00 2001 From: Anatoly Laskaris Date: Sat, 21 Sep 2024 14:19:51 +0300 Subject: [PATCH 2/2] Check virtualization is enabled with python --- plugins/modules/check_virtualization.py | 54 +++++++++++++++++++++++++ roles/nox/tasks/00-preflight.yml | 6 +-- roles/prerequisites/tasks/main.yml | 2 +- 3 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 plugins/modules/check_virtualization.py diff --git a/plugins/modules/check_virtualization.py b/plugins/modules/check_virtualization.py new file mode 100644 index 0000000..bd8045e --- /dev/null +++ b/plugins/modules/check_virtualization.py @@ -0,0 +1,54 @@ +#!/usr/bin/python + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +import subprocess +import shutil +from ansible.module_utils.basic import AnsibleModule + + +def check_kvm_ok(): + """Check if the kvm-ok binary exists and run it to check virtualization.""" + # Check if 'kvm-ok' exists + if shutil.which('kvm-ok') is None: + return False, "The 'kvm-ok' binary is not found. Virtualization support cannot be checked." + + try: + # Run the 'kvm-ok' command + result = subprocess.run( + ['kvm-ok'], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + if result.returncode == 0: + return True, result.stdout.decode('utf-8').strip() + else: + return False, "Virtualization is disabled. Nox will not be able to run VMs." + except Exception as e: + return False, f"Failed to execute 'kvm-ok': {str(e)}" + + +def main(): + # Define the arguments that the module will accept (no arguments in this + # case) + module_args = dict() + + # Initialize the Ansible module + module = AnsibleModule( + argument_spec=module_args, + supports_check_mode=True + ) + + # Check if kvm-ok is available and run it + success, message = check_kvm_ok() + + # If kvm-ok is missing or virtualization is disabled, warn the user + if not success: + module.warn(message) + + # Return success (even if there are warnings) and the appropriate message + module.exit_json(changed=False, msg="Virtualization check completed.") + + +if __name__ == '__main__': + main() diff --git a/roles/nox/tasks/00-preflight.yml b/roles/nox/tasks/00-preflight.yml index 3f74988..f327616 100644 --- a/roles/nox/tasks/00-preflight.yml +++ b/roles/nox/tasks/00-preflight.yml @@ -4,9 +4,7 @@ - name: check that virtualization is enabled tags: always - ansible.builtin.warn: - msg: "CPU does not support virtualization or it is disabled in the BIOS/UEFI." - when: "'vmx' not in ansible_processor_flags and 'svm' not in ansible_processor_flags" + check_virtualization: - name: check "fluence_instance_id" variable tags: always @@ -91,4 +89,4 @@ - name: print nox config file debug: msg: "{{ lookup('ansible.builtin.file', 'files/{{ fluence_project_dir }}/.fluence/configs/{{ fluence_instance_id }}_Config.toml') }}" - when: show_nox_config is defined \ No newline at end of file + when: show_nox_config is defined diff --git a/roles/prerequisites/tasks/main.yml b/roles/prerequisites/tasks/main.yml index 90a26bf..bd38995 100644 --- a/roles/prerequisites/tasks/main.yml +++ b/roles/prerequisites/tasks/main.yml @@ -26,7 +26,7 @@ state: started enabled: true daemon_reload: true - + - name: enable default network community.libvirt.virt_net: autostart: true