| 1 |
# Copyright: Contributors to the Ansible project |
| 2 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import re |
| 6 |
|
| 7 |
|
| 8 |
class VirtualSysctlDetectionMixin: |
| 9 |
def detect_sysctl(self): |
| 10 |
self.sysctl_path = self.module.get_bin_path('sysctl') |
| 11 |
|
| 12 |
def detect_virt_product(self, key): |
| 13 |
virtual_product_facts = {} |
| 14 |
host_tech = set() |
| 15 |
guest_tech = set() |
| 16 |
|
| 17 |
# We do similar to what we do in linux.py -- We want to allow multiple |
| 18 |
# virt techs to show up, but maintain compatibility, so we have to track |
| 19 |
# when we would have stopped, even though now we go through everything. |
| 20 |
found_virt = False |
| 21 |
|
| 22 |
self.detect_sysctl() |
| 23 |
if self.sysctl_path: |
| 24 |
rc, out, err = self.module.run_command("%s -n %s" % (self.sysctl_path, key)) |
| 25 |
if rc == 0: |
| 26 |
if re.match('(KVM|kvm|Bochs|SmartDC).*', out): |
| 27 |
guest_tech.add('kvm') |
| 28 |
if not found_virt: |
| 29 |
virtual_product_facts['virtualization_type'] = 'kvm' |
| 30 |
virtual_product_facts['virtualization_role'] = 'guest' |
| 31 |
found_virt = True |
| 32 |
if re.match('.*VMware.*', out): |
| 33 |
guest_tech.add('VMware') |
| 34 |
if not found_virt: |
| 35 |
virtual_product_facts['virtualization_type'] = 'VMware' |
| 36 |
virtual_product_facts['virtualization_role'] = 'guest' |
| 37 |
found_virt = True |
| 38 |
if out.rstrip() == 'VirtualBox': |
| 39 |
guest_tech.add('virtualbox') |
| 40 |
if not found_virt: |
| 41 |
virtual_product_facts['virtualization_type'] = 'virtualbox' |
| 42 |
virtual_product_facts['virtualization_role'] = 'guest' |
| 43 |
found_virt = True |
| 44 |
if re.match('(HVM domU|XenPVH|XenPV|XenPVHVM).*', out): |
| 45 |
guest_tech.add('xen') |
| 46 |
if not found_virt: |
| 47 |
virtual_product_facts['virtualization_type'] = 'xen' |
| 48 |
virtual_product_facts['virtualization_role'] = 'guest' |
| 49 |
found_virt = True |
| 50 |
if out.rstrip() == 'Hyper-V': |
| 51 |
guest_tech.add('Hyper-V') |
| 52 |
if not found_virt: |
| 53 |
virtual_product_facts['virtualization_type'] = 'Hyper-V' |
| 54 |
virtual_product_facts['virtualization_role'] = 'guest' |
| 55 |
found_virt = True |
| 56 |
if out.rstrip() == 'Parallels': |
| 57 |
guest_tech.add('parallels') |
| 58 |
if not found_virt: |
| 59 |
virtual_product_facts['virtualization_type'] = 'parallels' |
| 60 |
virtual_product_facts['virtualization_role'] = 'guest' |
| 61 |
found_virt = True |
| 62 |
if out.rstrip() == 'RHEV Hypervisor': |
| 63 |
guest_tech.add('RHEV') |
| 64 |
if not found_virt: |
| 65 |
virtual_product_facts['virtualization_type'] = 'RHEV' |
| 66 |
virtual_product_facts['virtualization_role'] = 'guest' |
| 67 |
found_virt = True |
| 68 |
if out.rstrip() == 'bhyve': |
| 69 |
guest_tech.add('bhyve') |
| 70 |
if not found_virt: |
| 71 |
virtual_product_facts['virtualization_type'] = 'bhyve' |
| 72 |
virtual_product_facts['virtualization_role'] = 'guest' |
| 73 |
found_virt = True |
| 74 |
if (key == 'security.jail.jailed') and (out.rstrip() == '1'): |
| 75 |
guest_tech.add('jails') |
| 76 |
if not found_virt: |
| 77 |
virtual_product_facts['virtualization_type'] = 'jails' |
| 78 |
virtual_product_facts['virtualization_role'] = 'guest' |
| 79 |
found_virt = True |
| 80 |
|
| 81 |
virtual_product_facts['virtualization_tech_guest'] = guest_tech |
| 82 |
virtual_product_facts['virtualization_tech_host'] = host_tech |
| 83 |
return virtual_product_facts |
| 84 |
|
| 85 |
def detect_virt_vendor(self, key): |
| 86 |
virtual_vendor_facts = {} |
| 87 |
host_tech = set() |
| 88 |
guest_tech = set() |
| 89 |
self.detect_sysctl() |
| 90 |
if self.sysctl_path: |
| 91 |
rc, out, err = self.module.run_command("%s -n %s" % (self.sysctl_path, key)) |
| 92 |
if rc == 0: |
| 93 |
if out.rstrip() == 'QEMU': |
| 94 |
guest_tech.add('kvm') |
| 95 |
virtual_vendor_facts['virtualization_type'] = 'kvm' |
| 96 |
virtual_vendor_facts['virtualization_role'] = 'guest' |
| 97 |
if out.rstrip() == 'OpenBSD': |
| 98 |
guest_tech.add('vmm') |
| 99 |
virtual_vendor_facts['virtualization_type'] = 'vmm' |
| 100 |
virtual_vendor_facts['virtualization_role'] = 'guest' |
| 101 |
|
| 102 |
virtual_vendor_facts['virtualization_tech_guest'] = guest_tech |
| 103 |
virtual_vendor_facts['virtualization_tech_host'] = host_tech |
| 104 |
return virtual_vendor_facts |