| 1 |
# This file is part of Ansible |
| 2 |
# |
| 3 |
# Ansible is free software: you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License as published by |
| 5 |
# the Free Software Foundation, either version 3 of the License, or |
| 6 |
# (at your option) any later version. |
| 7 |
# |
| 8 |
# Ansible is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
# GNU General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU General Public License |
| 14 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
|
| 16 |
from __future__ import annotations |
| 17 |
|
| 18 |
import os |
| 19 |
|
| 20 |
from ansible.module_utils.facts.virtual.base import Virtual, VirtualCollector |
| 21 |
from ansible.module_utils.facts.virtual.sysctl import VirtualSysctlDetectionMixin |
| 22 |
|
| 23 |
|
| 24 |
class FreeBSDVirtual(Virtual, VirtualSysctlDetectionMixin): |
| 25 |
""" |
| 26 |
This is a FreeBSD-specific subclass of Virtual. It defines |
| 27 |
- virtualization_type |
| 28 |
- virtualization_role |
| 29 |
""" |
| 30 |
platform = 'FreeBSD' |
| 31 |
|
| 32 |
def get_virtual_facts(self): |
| 33 |
virtual_facts = {} |
| 34 |
host_tech = set() |
| 35 |
guest_tech = set() |
| 36 |
|
| 37 |
# Set empty values as default |
| 38 |
virtual_facts['virtualization_type'] = '' |
| 39 |
virtual_facts['virtualization_role'] = '' |
| 40 |
|
| 41 |
if os.path.exists('/dev/xen/xenstore'): |
| 42 |
guest_tech.add('xen') |
| 43 |
virtual_facts['virtualization_type'] = 'xen' |
| 44 |
virtual_facts['virtualization_role'] = 'guest' |
| 45 |
|
| 46 |
kern_vm_guest = self.detect_virt_product('kern.vm_guest') |
| 47 |
guest_tech.update(kern_vm_guest['virtualization_tech_guest']) |
| 48 |
host_tech.update(kern_vm_guest['virtualization_tech_host']) |
| 49 |
|
| 50 |
hw_hv_vendor = self.detect_virt_product('hw.hv_vendor') |
| 51 |
guest_tech.update(hw_hv_vendor['virtualization_tech_guest']) |
| 52 |
host_tech.update(hw_hv_vendor['virtualization_tech_host']) |
| 53 |
|
| 54 |
sec_jail_jailed = self.detect_virt_product('security.jail.jailed') |
| 55 |
guest_tech.update(sec_jail_jailed['virtualization_tech_guest']) |
| 56 |
host_tech.update(sec_jail_jailed['virtualization_tech_host']) |
| 57 |
|
| 58 |
if virtual_facts['virtualization_type'] == '': |
| 59 |
# We call update here, then re-set virtualization_tech_host/guest |
| 60 |
# later. |
| 61 |
for sysctl in [kern_vm_guest, hw_hv_vendor, sec_jail_jailed]: |
| 62 |
if sysctl: |
| 63 |
virtual_facts.update(sysctl) |
| 64 |
|
| 65 |
virtual_vendor_facts = self.detect_virt_vendor('hw.model') |
| 66 |
guest_tech.update(virtual_vendor_facts['virtualization_tech_guest']) |
| 67 |
host_tech.update(virtual_vendor_facts['virtualization_tech_host']) |
| 68 |
|
| 69 |
if virtual_facts['virtualization_type'] == '': |
| 70 |
virtual_facts.update(virtual_vendor_facts) |
| 71 |
|
| 72 |
# if vmm.ko kernel module is loaded |
| 73 |
kldstat_bin = self.module.get_bin_path('kldstat') |
| 74 |
|
| 75 |
if kldstat_bin is not None: |
| 76 |
(rc, out, err) = self.module.run_command('%s -q -m vmm' % kldstat_bin) |
| 77 |
if rc == 0: |
| 78 |
host_tech.add('bhyve') |
| 79 |
virtual_facts['virtualization_type'] = 'bhyve' |
| 80 |
virtual_facts['virtualization_role'] = 'host' |
| 81 |
|
| 82 |
virtual_facts['virtualization_tech_guest'] = guest_tech |
| 83 |
virtual_facts['virtualization_tech_host'] = host_tech |
| 84 |
return virtual_facts |
| 85 |
|
| 86 |
|
| 87 |
class FreeBSDVirtualCollector(VirtualCollector): |
| 88 |
_fact_class = FreeBSDVirtual |
| 89 |
_platform = 'FreeBSD' |