| 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 |
|
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
import os |
| 7 |
|
| 8 |
from ansible.module_utils.facts.virtual.base import Virtual, VirtualCollector |
| 9 |
|
| 10 |
|
| 11 |
class SunOSVirtual(Virtual): |
| 12 |
""" |
| 13 |
This is a SunOS-specific subclass of Virtual. It defines |
| 14 |
- virtualization_type |
| 15 |
- virtualization_role |
| 16 |
- container |
| 17 |
""" |
| 18 |
platform = 'SunOS' |
| 19 |
|
| 20 |
def get_virtual_facts(self): |
| 21 |
virtual_facts = {} |
| 22 |
host_tech = set() |
| 23 |
guest_tech = set() |
| 24 |
|
| 25 |
# Check if it's a zone |
| 26 |
zonename = self.module.get_bin_path('zonename') |
| 27 |
if zonename: |
| 28 |
rc, out, err = self.module.run_command(zonename) |
| 29 |
if rc == 0: |
| 30 |
if out.rstrip() == "global": |
| 31 |
host_tech.add('zone') |
| 32 |
else: |
| 33 |
guest_tech.add('zone') |
| 34 |
virtual_facts['container'] = 'zone' |
| 35 |
|
| 36 |
# Check if it's a branded zone (i.e. Solaris 8/9 zone) |
| 37 |
if os.path.isdir('/.SUNWnative'): |
| 38 |
guest_tech.add('zone') |
| 39 |
virtual_facts['container'] = 'zone' |
| 40 |
|
| 41 |
# If it's a zone check if we can detect if our global zone is itself virtualized. |
| 42 |
# Relies on the "guest tools" (e.g. vmware tools) to be installed |
| 43 |
if 'container' in virtual_facts and virtual_facts['container'] == 'zone': |
| 44 |
modinfo = self.module.get_bin_path('modinfo') |
| 45 |
if modinfo: |
| 46 |
rc, out, err = self.module.run_command(modinfo) |
| 47 |
if rc == 0: |
| 48 |
for line in out.splitlines(): |
| 49 |
if 'VMware' in line: |
| 50 |
guest_tech.add('vmware') |
| 51 |
virtual_facts['virtualization_type'] = 'vmware' |
| 52 |
virtual_facts['virtualization_role'] = 'guest' |
| 53 |
if 'VirtualBox' in line: |
| 54 |
guest_tech.add('virtualbox') |
| 55 |
virtual_facts['virtualization_type'] = 'virtualbox' |
| 56 |
virtual_facts['virtualization_role'] = 'guest' |
| 57 |
|
| 58 |
if os.path.exists('/proc/vz'): |
| 59 |
guest_tech.add('virtuozzo') |
| 60 |
virtual_facts['virtualization_type'] = 'virtuozzo' |
| 61 |
virtual_facts['virtualization_role'] = 'guest' |
| 62 |
|
| 63 |
# Detect domaining on Sparc hardware |
| 64 |
virtinfo = self.module.get_bin_path('virtinfo') |
| 65 |
if virtinfo: |
| 66 |
# The output of virtinfo is different whether we are on a machine with logical |
| 67 |
# domains ('LDoms') on a T-series or domains ('Domains') on a M-series. Try LDoms first. |
| 68 |
rc, out, err = self.module.run_command([virtinfo, '-p']) |
| 69 |
# The output contains multiple lines with different keys like this: |
| 70 |
# DOMAINROLE|impl=LDoms|control=false|io=false|service=false|root=false |
| 71 |
# The output may also be not formatted and the returncode is set to 0 regardless of the error condition: |
| 72 |
# virtinfo can only be run from the global zone |
| 73 |
if rc == 0: |
| 74 |
try: |
| 75 |
for line in out.splitlines(): |
| 76 |
fields = line.split('|') |
| 77 |
if fields[0] == 'DOMAINROLE' and fields[1] == 'impl=LDoms': |
| 78 |
guest_tech.add('ldom') |
| 79 |
virtual_facts['virtualization_type'] = 'ldom' |
| 80 |
virtual_facts['virtualization_role'] = 'guest' |
| 81 |
hostfeatures = [] |
| 82 |
for field in fields[2:]: |
| 83 |
arg = field.split('=') |
| 84 |
if arg[1] == 'true': |
| 85 |
hostfeatures.append(arg[0]) |
| 86 |
if len(hostfeatures) > 0: |
| 87 |
virtual_facts['virtualization_role'] = 'host (' + ','.join(hostfeatures) + ')' |
| 88 |
except ValueError: |
| 89 |
pass |
| 90 |
|
| 91 |
else: |
| 92 |
smbios = self.module.get_bin_path('smbios') |
| 93 |
if not smbios: |
| 94 |
return |
| 95 |
rc, out, err = self.module.run_command(smbios) |
| 96 |
if rc == 0: |
| 97 |
for line in out.splitlines(): |
| 98 |
if 'VMware' in line: |
| 99 |
guest_tech.add('vmware') |
| 100 |
virtual_facts['virtualization_type'] = 'vmware' |
| 101 |
virtual_facts['virtualization_role'] = 'guest' |
| 102 |
elif 'Parallels' in line: |
| 103 |
guest_tech.add('parallels') |
| 104 |
virtual_facts['virtualization_type'] = 'parallels' |
| 105 |
virtual_facts['virtualization_role'] = 'guest' |
| 106 |
elif 'VirtualBox' in line: |
| 107 |
guest_tech.add('virtualbox') |
| 108 |
virtual_facts['virtualization_type'] = 'virtualbox' |
| 109 |
virtual_facts['virtualization_role'] = 'guest' |
| 110 |
elif 'HVM domU' in line: |
| 111 |
guest_tech.add('xen') |
| 112 |
virtual_facts['virtualization_type'] = 'xen' |
| 113 |
virtual_facts['virtualization_role'] = 'guest' |
| 114 |
elif 'KVM' in line: |
| 115 |
guest_tech.add('kvm') |
| 116 |
virtual_facts['virtualization_type'] = 'kvm' |
| 117 |
virtual_facts['virtualization_role'] = 'guest' |
| 118 |
elif 'BHYVE' in line: |
| 119 |
guest_tech.add('bhyve') |
| 120 |
virtual_facts['virtualization_type'] = 'bhyve' |
| 121 |
virtual_facts['virtualization_role'] = 'guest' |
| 122 |
|
| 123 |
virtual_facts['virtualization_tech_guest'] = guest_tech |
| 124 |
virtual_facts['virtualization_tech_host'] = host_tech |
| 125 |
return virtual_facts |
| 126 |
|
| 127 |
|
| 128 |
class SunOSVirtualCollector(VirtualCollector): |
| 129 |
_fact_class = SunOSVirtual |
| 130 |
_platform = 'SunOS' |