Debian Codegraph @

lib/ansible/module_utils/facts/virtual/openbsd.py

raw
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 re
19
20 from ansible.module_utils.facts.virtual.base import Virtual, VirtualCollector
21 from ansible.module_utils.facts.virtual.sysctl import VirtualSysctlDetectionMixin
22
23 from ansible.module_utils.facts.utils import get_file_content
24
25
26 class OpenBSDVirtual(Virtual, VirtualSysctlDetectionMixin):
27 """
28 This is a OpenBSD-specific subclass of Virtual. It defines
29 - virtualization_type
30 - virtualization_role
31 """
32 platform = 'OpenBSD'
33 DMESG_BOOT = '/var/run/dmesg.boot'
34
35 def get_virtual_facts(self):
36 virtual_facts = {}
37 host_tech = set()
38 guest_tech = set()
39
40 # Set empty values as default
41 virtual_facts['virtualization_type'] = ''
42 virtual_facts['virtualization_role'] = ''
43
44 virtual_product_facts = self.detect_virt_product('hw.product')
45 guest_tech.update(virtual_product_facts['virtualization_tech_guest'])
46 host_tech.update(virtual_product_facts['virtualization_tech_host'])
47 virtual_facts.update(virtual_product_facts)
48
49 virtual_vendor_facts = self.detect_virt_vendor('hw.vendor')
50 guest_tech.update(virtual_vendor_facts['virtualization_tech_guest'])
51 host_tech.update(virtual_vendor_facts['virtualization_tech_host'])
52
53 if virtual_facts['virtualization_type'] == '':
54 virtual_facts.update(virtual_vendor_facts)
55
56 # Check the dmesg if vmm(4) attached, indicating the host is
57 # capable of virtualization.
58 dmesg_boot = get_file_content(OpenBSDVirtual.DMESG_BOOT)
59 for line in dmesg_boot.splitlines():
60 match = re.match('^vmm0 at mainbus0: (SVM/RVI|VMX/EPT)$', line)
61 if match:
62 host_tech.add('vmm')
63 virtual_facts['virtualization_type'] = 'vmm'
64 virtual_facts['virtualization_role'] = 'host'
65
66 virtual_facts['virtualization_tech_guest'] = guest_tech
67 virtual_facts['virtualization_tech_host'] = host_tech
68 return virtual_facts
69
70
71 class OpenBSDVirtualCollector(VirtualCollector):
72 _fact_class = OpenBSDVirtual
73 _platform = 'OpenBSD'