Debian Codegraph @

lib/ansible/module_utils/facts/virtual/linux.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 glob
19 import os
20 import re
21
22 from ansible.module_utils.facts.virtual.base import Virtual, VirtualCollector
23 from ansible.module_utils.facts.utils import get_file_content, get_file_lines
24
25
26 class LinuxVirtual(Virtual):
27 """
28 This is a Linux-specific subclass of Virtual. It defines
29 - virtualization_type
30 - virtualization_role
31 """
32 platform = 'Linux'
33
34 # For more information, check: http://people.redhat.com/~rjones/virt-what/
35 def get_virtual_facts(self):
36 virtual_facts = {}
37
38 # We want to maintain compatibility with the old "virtualization_type"
39 # and "virtualization_role" entries, so we need to track if we found
40 # them. We won't return them until the end, but if we found them early,
41 # we should avoid updating them again.
42 found_virt = False
43
44 # But as we go along, we also want to track virt tech the new way.
45 host_tech = set()
46 guest_tech = set()
47
48 # lxc/docker
49 if os.path.exists('/proc/1/cgroup'):
50 for line in get_file_lines('/proc/1/cgroup'):
51 if re.search(r'/docker(/|-[0-9a-f]+\.scope)', line):
52 guest_tech.add('docker')
53 if not found_virt:
54 virtual_facts['virtualization_type'] = 'docker'
55 virtual_facts['virtualization_role'] = 'guest'
56 found_virt = True
57 if re.search('/lxc/', line) or re.search('/machine.slice/machine-lxc', line):
58 guest_tech.add('lxc')
59 if not found_virt:
60 virtual_facts['virtualization_type'] = 'lxc'
61 virtual_facts['virtualization_role'] = 'guest'
62 found_virt = True
63 if re.search('/system.slice/containerd.service', line):
64 guest_tech.add('containerd')
65 if not found_virt:
66 virtual_facts['virtualization_type'] = 'containerd'
67 virtual_facts['virtualization_role'] = 'guest'
68 found_virt = True
69
70 # lxc does not always appear in cgroups anymore but sets 'container=lxc' environment var, requires root privs
71 if os.path.exists('/proc/1/environ'):
72 for line in get_file_lines('/proc/1/environ', line_sep='\x00'):
73 if re.search('container=lxc', line):
74 guest_tech.add('lxc')
75 if not found_virt:
76 virtual_facts['virtualization_type'] = 'lxc'
77 virtual_facts['virtualization_role'] = 'guest'
78 found_virt = True
79 if re.search('container=podman', line):
80 guest_tech.add('podman')
81 if not found_virt:
82 virtual_facts['virtualization_type'] = 'podman'
83 virtual_facts['virtualization_role'] = 'guest'
84 found_virt = True
85 if re.search('^container=.', line):
86 guest_tech.add('container')
87 if not found_virt:
88 virtual_facts['virtualization_type'] = 'container'
89 virtual_facts['virtualization_role'] = 'guest'
90 found_virt = True
91
92 if os.path.exists('/proc/vz') and not os.path.exists('/proc/lve'):
93 virtual_facts['virtualization_type'] = 'openvz'
94 if os.path.exists('/proc/bc'):
95 host_tech.add('openvz')
96 if not found_virt:
97 virtual_facts['virtualization_role'] = 'host'
98 else:
99 guest_tech.add('openvz')
100 if not found_virt:
101 virtual_facts['virtualization_role'] = 'guest'
102 found_virt = True
103
104 systemd_container = get_file_content('/run/systemd/container')
105 if systemd_container:
106 guest_tech.add(systemd_container)
107 if not found_virt:
108 virtual_facts['virtualization_type'] = systemd_container
109 virtual_facts['virtualization_role'] = 'guest'
110 found_virt = True
111
112 # If docker/containerd has a custom cgroup parent, checking /proc/1/cgroup (above) might fail.
113 # https://docs.docker.com/engine/reference/commandline/dockerd/#default-cgroup-parent
114 # Fallback to more rudimentary checks.
115 if os.path.exists('/.dockerenv') or os.path.exists('/.dockerinit'):
116 guest_tech.add('docker')
117 if not found_virt:
118 virtual_facts['virtualization_type'] = 'docker'
119 virtual_facts['virtualization_role'] = 'guest'
120 found_virt = True
121
122 # ensure 'container' guest_tech is appropriately set
123 if guest_tech.intersection(set(['docker', 'lxc', 'podman', 'openvz', 'containerd'])) or systemd_container:
124 guest_tech.add('container')
125
126 if os.path.exists("/proc/xen"):
127 is_xen_host = False
128 try:
129 for line in get_file_lines('/proc/xen/capabilities'):
130 if "control_d" in line:
131 is_xen_host = True
132 except OSError:
133 pass
134
135 if is_xen_host:
136 host_tech.add('xen')
137 if not found_virt:
138 virtual_facts['virtualization_type'] = 'xen'
139 virtual_facts['virtualization_role'] = 'host'
140 else:
141 if not found_virt:
142 virtual_facts['virtualization_type'] = 'xen'
143 virtual_facts['virtualization_role'] = 'guest'
144 found_virt = True
145
146 # assume guest for this block
147 if not found_virt:
148 virtual_facts['virtualization_role'] = 'guest'
149
150 product_name = get_file_content('/sys/devices/virtual/dmi/id/product_name')
151 sys_vendor = get_file_content('/sys/devices/virtual/dmi/id/sys_vendor')
152 product_family = get_file_content('/sys/devices/virtual/dmi/id/product_family')
153
154 if product_name in ('KVM', 'KVM Server', 'Bochs', 'AHV', 'CloudStack KVM Hypervisor'):
155 guest_tech.add('kvm')
156 if not found_virt:
157 virtual_facts['virtualization_type'] = 'kvm'
158 found_virt = True
159
160 if sys_vendor == 'oVirt':
161 guest_tech.add('oVirt')
162 if not found_virt:
163 virtual_facts['virtualization_type'] = 'oVirt'
164 found_virt = True
165
166 if sys_vendor == 'Red Hat':
167 if product_family == 'RHV':
168 guest_tech.add('RHV')
169 if not found_virt:
170 virtual_facts['virtualization_type'] = 'RHV'
171 found_virt = True
172 elif product_name == 'RHEV Hypervisor':
173 guest_tech.add('RHEV')
174 if not found_virt:
175 virtual_facts['virtualization_type'] = 'RHEV'
176 found_virt = True
177
178 if product_name and product_name.startswith(("VMware",)):
179 guest_tech.add('VMware')
180 if not found_virt:
181 virtual_facts['virtualization_type'] = 'VMware'
182 found_virt = True
183
184 if product_name in ('OpenStack Compute', 'OpenStack Nova'):
185 guest_tech.add('openstack')
186 if not found_virt:
187 virtual_facts['virtualization_type'] = 'openstack'
188 found_virt = True
189
190 if product_name == 'BHYVE':
191 guest_tech.add('bhyve')
192 if not found_virt:
193 virtual_facts['virtualization_type'] = 'bhyve'
194 found_virt = True
195
196 bios_vendor = get_file_content('/sys/devices/virtual/dmi/id/bios_vendor')
197
198 if bios_vendor == 'Xen':
199 guest_tech.add('xen')
200 if not found_virt:
201 virtual_facts['virtualization_type'] = 'xen'
202 found_virt = True
203
204 if bios_vendor == 'innotek GmbH':
205 guest_tech.add('virtualbox')
206 if not found_virt:
207 virtual_facts['virtualization_type'] = 'virtualbox'
208 found_virt = True
209
210 if bios_vendor in ('Amazon EC2', 'DigitalOcean', 'Hetzner', 'Linode'):
211 guest_tech.add('kvm')
212 if not found_virt:
213 virtual_facts['virtualization_type'] = 'kvm'
214 found_virt = True
215
216 KVM_SYS_VENDORS = ('QEMU', 'Amazon EC2', 'DigitalOcean', 'Google', 'Scaleway', 'Nutanix')
217 if sys_vendor in KVM_SYS_VENDORS:
218 guest_tech.add('kvm')
219 if not found_virt:
220 virtual_facts['virtualization_type'] = 'kvm'
221 found_virt = True
222
223 if sys_vendor == 'KubeVirt':
224 guest_tech.add('KubeVirt')
225 if not found_virt:
226 virtual_facts['virtualization_type'] = 'KubeVirt'
227 found_virt = True
228
229 # FIXME: This does also match hyperv
230 if sys_vendor == 'Microsoft Corporation':
231 guest_tech.add('VirtualPC')
232 if not found_virt:
233 virtual_facts['virtualization_type'] = 'VirtualPC'
234 found_virt = True
235
236 if sys_vendor == 'Parallels Software International Inc.':
237 guest_tech.add('parallels')
238 if not found_virt:
239 virtual_facts['virtualization_type'] = 'parallels'
240 found_virt = True
241
242 if sys_vendor == 'OpenStack Foundation':
243 guest_tech.add('openstack')
244 if not found_virt:
245 virtual_facts['virtualization_type'] = 'openstack'
246 found_virt = True
247
248 # unassume guest
249 if not found_virt:
250 del virtual_facts['virtualization_role']
251
252 if os.path.exists('/proc/self/status'):
253 for line in get_file_lines('/proc/self/status'):
254 if re.match(r'^VxID:\s+\d+', line):
255 if not found_virt:
256 virtual_facts['virtualization_type'] = 'linux_vserver'
257 if re.match(r'^VxID:\s+0', line):
258 host_tech.add('linux_vserver')
259 if not found_virt:
260 virtual_facts['virtualization_role'] = 'host'
261 else:
262 guest_tech.add('linux_vserver')
263 if not found_virt:
264 virtual_facts['virtualization_role'] = 'guest'
265 found_virt = True
266
267 if os.path.exists('/proc/cpuinfo'):
268 for line in get_file_lines('/proc/cpuinfo'):
269 if re.match('^model name.*QEMU Virtual CPU', line):
270 guest_tech.add('kvm')
271 if not found_virt:
272 virtual_facts['virtualization_type'] = 'kvm'
273 elif re.match('^vendor_id.*User Mode Linux', line):
274 guest_tech.add('uml')
275 if not found_virt:
276 virtual_facts['virtualization_type'] = 'uml'
277 elif re.match('^model name.*UML', line):
278 guest_tech.add('uml')
279 if not found_virt:
280 virtual_facts['virtualization_type'] = 'uml'
281 elif re.match('^machine.*CHRP IBM pSeries .emulated by qemu.', line):
282 guest_tech.add('kvm')
283 if not found_virt:
284 virtual_facts['virtualization_type'] = 'kvm'
285 elif re.match('^vendor_id.*PowerVM Lx86', line):
286 guest_tech.add('powervm_lx86')
287 if not found_virt:
288 virtual_facts['virtualization_type'] = 'powervm_lx86'
289 elif re.match('^vendor_id.*IBM/S390', line):
290 guest_tech.add('PR/SM')
291 if not found_virt:
292 virtual_facts['virtualization_type'] = 'PR/SM'
293 lscpu = self.module.get_bin_path('lscpu')
294 if lscpu:
295 rc, out, err = self.module.run_command(["lscpu"])
296 if rc == 0:
297 for line in out.splitlines():
298 data = line.split(":", 1)
299 key = data[0].strip()
300 if key == 'Hypervisor':
301 tech = data[1].strip()
302 guest_tech.add(tech)
303 if not found_virt:
304 virtual_facts['virtualization_type'] = tech
305 else:
306 guest_tech.add('ibm_systemz')
307 if not found_virt:
308 virtual_facts['virtualization_type'] = 'ibm_systemz'
309 else:
310 continue
311 if virtual_facts['virtualization_type'] == 'PR/SM':
312 if not found_virt:
313 virtual_facts['virtualization_role'] = 'LPAR'
314 else:
315 if not found_virt:
316 virtual_facts['virtualization_role'] = 'guest'
317 if not found_virt:
318 found_virt = True
319
320 # Beware that we can have both kvm and virtualbox running on a single system
321 if os.path.exists("/proc/modules") and os.access('/proc/modules', os.R_OK):
322 modules = []
323 for line in get_file_lines("/proc/modules"):
324 data = line.split(" ", 1)
325 modules.append(data[0])
326
327 if 'kvm' in modules:
328 host_tech.add('kvm')
329 if not found_virt:
330 virtual_facts['virtualization_type'] = 'kvm'
331 virtual_facts['virtualization_role'] = 'host'
332
333 if os.path.isdir('/rhev/'):
334 # Check whether this is a RHEV hypervisor (is vdsm running ?)
335 for f in glob.glob('/proc/[0-9]*/comm'):
336 try:
337 with open(f) as virt_fh:
338 comm_content = virt_fh.read().rstrip()
339
340 if comm_content in ('vdsm', 'vdsmd'):
341 # We add both kvm and RHEV to host_tech in this case.
342 # It's accurate. RHEV uses KVM.
343 host_tech.add('RHEV')
344 if not found_virt:
345 virtual_facts['virtualization_type'] = 'RHEV'
346 break
347 except Exception:
348 pass
349
350 found_virt = True
351
352 if 'vboxdrv' in modules:
353 host_tech.add('virtualbox')
354 if not found_virt:
355 virtual_facts['virtualization_type'] = 'virtualbox'
356 virtual_facts['virtualization_role'] = 'host'
357 found_virt = True
358
359 if 'virtio' in modules:
360 host_tech.add('kvm')
361 if not found_virt:
362 virtual_facts['virtualization_type'] = 'kvm'
363 virtual_facts['virtualization_role'] = 'guest'
364 found_virt = True
365
366 # In older Linux Kernel versions, /sys filesystem is not available
367 # dmidecode is the safest option to parse virtualization related values
368 dmi_bin = self.module.get_bin_path('dmidecode')
369 # We still want to continue even if dmidecode is not available
370 if dmi_bin is not None:
371 (rc, out, err) = self.module.run_command('%s -s system-product-name' % dmi_bin)
372 if rc == 0:
373 # Strip out commented lines (specific dmidecode output)
374 vendor_name = ''.join([line.strip() for line in out.splitlines() if not line.startswith('#')])
375 if vendor_name.startswith('VMware'):
376 guest_tech.add('VMware')
377 if not found_virt:
378 virtual_facts['virtualization_type'] = 'VMware'
379 virtual_facts['virtualization_role'] = 'guest'
380 found_virt = True
381
382 if 'BHYVE' in out:
383 guest_tech.add('bhyve')
384 if not found_virt:
385 virtual_facts['virtualization_type'] = 'bhyve'
386 virtual_facts['virtualization_role'] = 'guest'
387 found_virt = True
388
389 if os.path.exists('/dev/kvm'):
390 host_tech.add('kvm')
391 if not found_virt:
392 virtual_facts['virtualization_type'] = 'kvm'
393 virtual_facts['virtualization_role'] = 'host'
394 found_virt = True
395
396 # If none of the above matches, return 'NA' for virtualization_type
397 # and virtualization_role. This allows for proper grouping.
398 if not found_virt:
399 virtual_facts['virtualization_type'] = 'NA'
400 virtual_facts['virtualization_role'] = 'NA'
401 found_virt = True
402
403 virtual_facts['virtualization_tech_guest'] = guest_tech
404 virtual_facts['virtualization_tech_host'] = host_tech
405 return virtual_facts
406
407
408 class LinuxVirtualCollector(VirtualCollector):
409 _fact_class = LinuxVirtual
410 _platform = 'Linux'