Debian Codegraph @

lib/ansible/module_utils/facts/hardware/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 collections
19 import errno
20 import glob
21 import json
22 import os
23 import re
24 import sys
25 import time
26
27 from ansible.module_utils._internal import _no_six
28 from ansible.module_utils._internal._concurrent import _futures
29 from ansible.module_utils.common.locale import get_best_parsable_locale
30 from ansible.module_utils.common.text.converters import to_text
31 from ansible.module_utils.common.text.formatters import bytes_to_human
32 from ansible.module_utils.facts.hardware.base import Hardware, HardwareCollector
33 from ansible.module_utils.facts.utils import get_file_content, get_file_lines, get_mount_size
34
35 # import this as a module to ensure we get the same module instance
36 from ansible.module_utils.facts import timeout
37
38
39 def get_partition_uuid(partname):
40 try:
41 uuids = os.listdir("/dev/disk/by-uuid")
42 except OSError:
43 return
44
45 for uuid in uuids:
46 dev = os.path.realpath("/dev/disk/by-uuid/" + uuid)
47 if dev == ("/dev/" + partname):
48 return uuid
49
50 return None
51
52
53 class LinuxHardware(Hardware):
54 """
55 Linux-specific subclass of Hardware. Defines memory and CPU facts:
56 - memfree_mb
57 - memtotal_mb
58 - swapfree_mb
59 - swaptotal_mb
60 - processor (a list)
61 - processor_cores
62 - processor_count
63
64 In addition, it also defines number of DMI facts and device facts.
65 """
66
67 platform = 'Linux'
68
69 # Originally only had these four as toplevelfacts
70 ORIGINAL_MEMORY_FACTS = frozenset(('MemTotal', 'SwapTotal', 'MemFree', 'SwapFree'))
71 # Now we have all of these in a dict structure
72 MEMORY_FACTS = ORIGINAL_MEMORY_FACTS.union(('Buffers', 'Cached', 'SwapCached'))
73
74 # regex used against findmnt output to detect bind mounts
75 BIND_MOUNT_RE = re.compile(r'.*\]')
76
77 # regex used against mtab content to find entries that are bind mounts
78 MTAB_BIND_MOUNT_RE = re.compile(r'.*bind.*"')
79
80 # regex used for replacing octal escape sequences
81 OCTAL_ESCAPE_RE = re.compile(r'\\[0-9]{3}')
82
83 def populate(self, collected_facts=None):
84 hardware_facts = {}
85 locale = get_best_parsable_locale(self.module)
86 self.module.run_command_environ_update = {'LANG': locale, 'LC_ALL': locale, 'LC_NUMERIC': locale}
87
88 cpu_facts = self.get_cpu_facts(collected_facts=collected_facts)
89 memory_facts = self.get_memory_facts()
90 dmi_facts = self.get_dmi_facts()
91 sysinfo_facts = self.get_sysinfo_facts()
92 device_facts = self.get_device_facts()
93 uptime_facts = self.get_uptime_facts()
94 lvm_facts = self.get_lvm_facts()
95
96 mount_facts = {}
97 try:
98 mount_facts = self.get_mount_facts()
99 except timeout.TimeoutError:
100 self.module.warn("No mount facts were gathered due to timeout.")
101
102 hardware_facts.update(cpu_facts)
103 hardware_facts.update(memory_facts)
104 hardware_facts.update(dmi_facts)
105 hardware_facts.update(sysinfo_facts)
106 hardware_facts.update(device_facts)
107 hardware_facts.update(uptime_facts)
108 hardware_facts.update(lvm_facts)
109 hardware_facts.update(mount_facts)
110
111 return hardware_facts
112
113 def get_memory_facts(self):
114 memory_facts = {}
115 if not os.access("/proc/meminfo", os.R_OK):
116 return memory_facts
117
118 memstats = {}
119 for line in get_file_lines("/proc/meminfo"):
120 data = line.split(":", 1)
121 key = data[0]
122 if key in self.ORIGINAL_MEMORY_FACTS:
123 val = data[1].strip().split(' ')[0]
124 memory_facts["%s_mb" % key.lower()] = int(val) // 1024
125
126 if key in self.MEMORY_FACTS:
127 val = data[1].strip().split(' ')[0]
128 memstats[key.lower()] = int(val) // 1024
129
130 if None not in (memstats.get('memtotal'), memstats.get('memfree')):
131 memstats['real:used'] = memstats['memtotal'] - memstats['memfree']
132 if None not in (memstats.get('cached'), memstats.get('memfree'), memstats.get('buffers')):
133 memstats['nocache:free'] = memstats['cached'] + memstats['memfree'] + memstats['buffers']
134 if None not in (memstats.get('memtotal'), memstats.get('nocache:free')):
135 memstats['nocache:used'] = memstats['memtotal'] - memstats['nocache:free']
136 if None not in (memstats.get('swaptotal'), memstats.get('swapfree')):
137 memstats['swap:used'] = memstats['swaptotal'] - memstats['swapfree']
138
139 memory_facts['memory_mb'] = {
140 'real': {
141 'total': memstats.get('memtotal'),
142 'used': memstats.get('real:used'),
143 'free': memstats.get('memfree'),
144 },
145 'nocache': {
146 'free': memstats.get('nocache:free'),
147 'used': memstats.get('nocache:used'),
148 },
149 'swap': {
150 'total': memstats.get('swaptotal'),
151 'free': memstats.get('swapfree'),
152 'used': memstats.get('swap:used'),
153 'cached': memstats.get('swapcached'),
154 },
155 }
156
157 return memory_facts
158
159 def get_cpu_facts(self, collected_facts=None):
160 cpu_facts = {}
161 collected_facts = collected_facts or {}
162
163 i = 0
164 vendor_id_occurrence = 0
165 model_name_occurrence = 0
166 processor_occurrence = 0
167 physid = 0
168 coreid = 0
169 sockets = {}
170 cores = {}
171 zp = 0
172 zmt = 0
173
174 xen = False
175 xen_paravirt = False
176 try:
177 if os.path.exists('/proc/xen'):
178 xen = True
179 else:
180 for line in get_file_lines('/sys/hypervisor/type'):
181 if line.strip() == 'xen':
182 xen = True
183 # Only interested in the first line
184 break
185 except OSError:
186 pass
187
188 if not os.access("/proc/cpuinfo", os.R_OK):
189 return cpu_facts
190
191 cpu_facts['processor'] = []
192 for line in get_file_lines('/proc/cpuinfo'):
193 data = line.split(":", 1)
194 key = data[0].strip()
195
196 try:
197 val = data[1].strip()
198 except IndexError:
199 val = ""
200
201 if xen:
202 if key == 'flags':
203 # Check for vme cpu flag, Xen paravirt does not expose this.
204 # Need to detect Xen paravirt because it exposes cpuinfo
205 # differently than Xen HVM or KVM and causes reporting of
206 # only a single cpu core.
207 if 'vme' not in val:
208 xen_paravirt = True
209
210 if key == "flags":
211 cpu_facts['flags'] = val.split()
212
213 # model name is for Intel arch, Processor (mind the uppercase P)
214 # works for some ARM devices, like the Sheevaplug.
215 if key in ['model name', 'Processor', 'vendor_id', 'cpu', 'Vendor', 'processor']:
216 if 'processor' not in cpu_facts:
217 cpu_facts['processor'] = []
218 cpu_facts['processor'].append(val)
219 if key == 'vendor_id':
220 vendor_id_occurrence += 1
221 if key == 'model name':
222 model_name_occurrence += 1
223 if key == 'processor':
224 processor_occurrence += 1
225 i += 1
226 elif key == 'physical id':
227 physid = val
228 if physid not in sockets:
229 sockets[physid] = 1
230 elif key == 'core id':
231 coreid = val
232 if coreid not in sockets:
233 cores[coreid] = 1
234 elif key == 'cpu cores':
235 sockets[physid] = int(val)
236 elif key == 'siblings':
237 cores[coreid] = int(val)
238 # S390x classic cpuinfo
239 elif key == '# processors':
240 zp = int(val)
241 elif key == 'max thread id':
242 zmt = int(val) + 1
243 # SPARC
244 elif key == 'ncpus active':
245 i = int(val)
246
247 # Skip for platforms without vendor_id/model_name in cpuinfo (e.g ppc64le)
248 if vendor_id_occurrence > 0:
249 if vendor_id_occurrence == model_name_occurrence:
250 i = vendor_id_occurrence
251
252 # The fields for ARM CPUs do not always include 'vendor_id' or 'model name',
253 # and sometimes includes both 'processor' and 'Processor'.
254 # The fields for Power CPUs include 'processor' and 'cpu'.
255 # Always use 'processor' count for ARM and Power systems
256 if collected_facts.get('ansible_architecture', '').startswith(('armv', 'aarch', 'ppc')):
257 i = processor_occurrence
258
259 if collected_facts.get('ansible_architecture') == 's390x':
260 # getting sockets would require 5.7+ with CONFIG_SCHED_TOPOLOGY
261 cpu_facts['processor_count'] = 1
262 cpu_facts['processor_cores'] = round(zp / zmt)
263 cpu_facts['processor_threads_per_core'] = zmt
264 cpu_facts['processor_vcpus'] = zp
265 cpu_facts['processor_nproc'] = zp
266 else:
267 if xen_paravirt:
268 cpu_facts['processor_count'] = i
269 cpu_facts['processor_cores'] = i
270 cpu_facts['processor_threads_per_core'] = 1
271 cpu_facts['processor_vcpus'] = i
272 cpu_facts['processor_nproc'] = i
273 else:
274 if sockets:
275 cpu_facts['processor_count'] = len(sockets)
276 else:
277 cpu_facts['processor_count'] = i
278
279 socket_values = list(sockets.values())
280 if socket_values and socket_values[0]:
281 cpu_facts['processor_cores'] = socket_values[0]
282 else:
283 cpu_facts['processor_cores'] = 1
284
285 core_values = list(cores.values())
286 if core_values:
287 cpu_facts['processor_threads_per_core'] = round(core_values[0] / cpu_facts['processor_cores'])
288 else:
289 cpu_facts['processor_threads_per_core'] = round(1 / cpu_facts['processor_cores'])
290
291 cpu_facts['processor_vcpus'] = (cpu_facts['processor_threads_per_core'] *
292 cpu_facts['processor_count'] * cpu_facts['processor_cores'])
293
294 cpu_facts['processor_nproc'] = processor_occurrence
295
296 # if the number of processors available to the module's
297 # thread cannot be determined, the processor count
298 # reported by /proc will be the default (as previously defined)
299 try:
300 cpu_facts['processor_nproc'] = len(
301 os.sched_getaffinity(0)
302 )
303 except AttributeError:
304 # In Python < 3.3, os.sched_getaffinity() is not available
305 nproc_cmd = self.module.get_bin_path('nproc')
306 if nproc_cmd is not None:
307 rc, out, _err = self.module.run_command(nproc_cmd)
308 if rc == 0:
309 cpu_facts['processor_nproc'] = int(out)
310
311 return cpu_facts
312
313 def get_dmi_facts(self):
314 """ learn dmi facts from system
315
316 Try /sys first for dmi related facts.
317 If that is not available, fall back to dmidecode executable """
318
319 dmi_facts = {}
320
321 if os.path.exists('/sys/devices/virtual/dmi/id/product_name'):
322 # Use kernel DMI info, if available
323
324 # DMI SPEC -- https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.2.0.pdf
325 FORM_FACTOR = ["Unknown", "Other", "Unknown", "Desktop",
326 "Low Profile Desktop", "Pizza Box", "Mini Tower", "Tower",
327 "Portable", "Laptop", "Notebook", "Hand Held", "Docking Station",
328 "All In One", "Sub Notebook", "Space-saving", "Lunch Box",
329 "Main Server Chassis", "Expansion Chassis", "Sub Chassis",
330 "Bus Expansion Chassis", "Peripheral Chassis", "RAID Chassis",
331 "Rack Mount Chassis", "Sealed-case PC", "Multi-system",
332 "CompactPCI", "AdvancedTCA", "Blade", "Blade Enclosure",
333 "Tablet", "Convertible", "Detachable", "IoT Gateway",
334 "Embedded PC", "Mini PC", "Stick PC"]
335
336 DMI_DICT = {
337 'bios_date': '/sys/devices/virtual/dmi/id/bios_date',
338 'bios_vendor': '/sys/devices/virtual/dmi/id/bios_vendor',
339 'bios_version': '/sys/devices/virtual/dmi/id/bios_version',
340 'board_asset_tag': '/sys/devices/virtual/dmi/id/board_asset_tag',
341 'board_name': '/sys/devices/virtual/dmi/id/board_name',
342 'board_serial': '/sys/devices/virtual/dmi/id/board_serial',
343 'board_vendor': '/sys/devices/virtual/dmi/id/board_vendor',
344 'board_version': '/sys/devices/virtual/dmi/id/board_version',
345 'chassis_asset_tag': '/sys/devices/virtual/dmi/id/chassis_asset_tag',
346 'chassis_serial': '/sys/devices/virtual/dmi/id/chassis_serial',
347 'chassis_vendor': '/sys/devices/virtual/dmi/id/chassis_vendor',
348 'chassis_version': '/sys/devices/virtual/dmi/id/chassis_version',
349 'form_factor': '/sys/devices/virtual/dmi/id/chassis_type',
350 'product_name': '/sys/devices/virtual/dmi/id/product_name',
351 'product_serial': '/sys/devices/virtual/dmi/id/product_serial',
352 'product_uuid': '/sys/devices/virtual/dmi/id/product_uuid',
353 'product_version': '/sys/devices/virtual/dmi/id/product_version',
354 'system_vendor': '/sys/devices/virtual/dmi/id/sys_vendor',
355 }
356
357 for (key, path) in DMI_DICT.items():
358 data = get_file_content(path)
359 if data is not None:
360 if key == 'form_factor':
361 try:
362 dmi_facts['form_factor'] = FORM_FACTOR[int(data)]
363 except IndexError:
364 dmi_facts['form_factor'] = 'unknown (%s)' % data
365 else:
366 dmi_facts[key] = data
367 else:
368 dmi_facts[key] = 'NA'
369
370 else:
371 # Fall back to using dmidecode, if available
372 DMI_DICT = {
373 'bios_date': 'bios-release-date',
374 'bios_vendor': 'bios-vendor',
375 'bios_version': 'bios-version',
376 'board_asset_tag': 'baseboard-asset-tag',
377 'board_name': 'baseboard-product-name',
378 'board_serial': 'baseboard-serial-number',
379 'board_vendor': 'baseboard-manufacturer',
380 'board_version': 'baseboard-version',
381 'chassis_asset_tag': 'chassis-asset-tag',
382 'chassis_serial': 'chassis-serial-number',
383 'chassis_vendor': 'chassis-manufacturer',
384 'chassis_version': 'chassis-version',
385 'form_factor': 'chassis-type',
386 'product_name': 'system-product-name',
387 'product_serial': 'system-serial-number',
388 'product_uuid': 'system-uuid',
389 'product_version': 'system-version',
390 'system_vendor': 'system-manufacturer',
391 }
392 dmi_bin = self.module.get_bin_path('dmidecode')
393 if dmi_bin is None:
394 dmi_facts = dict.fromkeys(
395 DMI_DICT.keys(),
396 'NA'
397 )
398 return dmi_facts
399
400 for (k, v) in DMI_DICT.items():
401 (rc, out, err) = self.module.run_command('%s -s %s' % (dmi_bin, v))
402 if rc == 0:
403 # Strip out commented lines (specific dmidecode output)
404 thisvalue = ''.join([line for line in out.splitlines() if not line.startswith('#')])
405 try:
406 json.dumps(thisvalue)
407 except UnicodeDecodeError:
408 thisvalue = "NA"
409
410 dmi_facts[k] = thisvalue
411 else:
412 dmi_facts[k] = 'NA'
413
414 return dmi_facts
415
416 def get_sysinfo_facts(self):
417 """Fetch /proc/sysinfo facts from s390 Linux on IBM Z"""
418 if not os.path.exists('/proc/sysinfo'):
419 return {}
420
421 sysinfo_facts = dict.fromkeys(
422 ('system_vendor', 'product_version', 'product_serial', 'product_name', 'product_uuid'),
423 'NA'
424 )
425 sysinfo_re = re.compile(
426 r"""
427 ^
428 (?:Manufacturer:\s+(?P<system_vendor>.+))|
429 (?:Type:\s+(?P<product_name>.+))|
430 (?:Sequence\ Code:\s+0+(?P<product_serial>.+))
431 $
432 """,
433 re.VERBOSE | re.MULTILINE
434 )
435 data = get_file_content('/proc/sysinfo')
436 for match in sysinfo_re.finditer(data):
437 sysinfo_facts.update({k: v for k, v in match.groupdict().items() if v is not None})
438 return sysinfo_facts
439
440 def _run_lsblk(self, lsblk_path):
441 # call lsblk and collect all uuids
442 # --exclude 2 makes lsblk ignore floppy disks, which are slower to answer than typical timeouts
443 # this uses the linux major device number
444 # for details see https://www.kernel.org/doc/Documentation/devices.txt
445 args = ['--list', '--noheadings', '--paths', '--output', 'NAME,UUID', '--exclude', '2']
446 cmd = [lsblk_path] + args
447 rc, out, err = self.module.run_command(cmd)
448 return rc, out, err
449
450 def _lsblk_uuid(self):
451 uuids = {}
452 lsblk_path = self.module.get_bin_path("lsblk")
453 if not lsblk_path:
454 return uuids
455
456 rc, out, err = self._run_lsblk(lsblk_path)
457 if rc != 0:
458 return uuids
459
460 # each line will be in format:
461 # <devicename><some whitespace><uuid>
462 # /dev/sda1 32caaec3-ef40-4691-a3b6-438c3f9bc1c0
463 for lsblk_line in out.splitlines():
464 if not lsblk_line:
465 continue
466
467 line = lsblk_line.strip()
468 fields = line.rsplit(None, 1)
469
470 if len(fields) < 2:
471 continue
472
473 device_name, uuid = fields[0].strip(), fields[1].strip()
474 if device_name in uuids:
475 continue
476 uuids[device_name] = uuid
477
478 return uuids
479
480 def _udevadm_uuid(self, device):
481 # fallback for versions of lsblk <= 2.23 that don't have --paths, see _run_lsblk() above
482 uuid = 'N/A'
483
484 udevadm_path = self.module.get_bin_path('udevadm')
485 if not udevadm_path:
486 return uuid
487
488 cmd = [udevadm_path, 'info', '--query', 'property', '--name', device]
489 rc, out, err = self.module.run_command(cmd)
490 if rc != 0:
491 return uuid
492
493 # a snippet of the output of the udevadm command below will be:
494 # ...
495 # ID_FS_TYPE=ext4
496 # ID_FS_USAGE=filesystem
497 # ID_FS_UUID=57b1a3e7-9019-4747-9809-7ec52bba9179
498 # ...
499 m = re.search('ID_FS_UUID=(.*)\n', out)
500 if m:
501 uuid = m.group(1)
502
503 return uuid
504
505 def _run_findmnt(self, findmnt_path):
506 args = ['--list', '--noheadings', '--notruncate']
507 cmd = [findmnt_path] + args
508 rc, out, err = self.module.run_command(cmd, errors='surrogate_then_replace')
509 return rc, out, err
510
511 def _find_bind_mounts(self):
512 bind_mounts = set()
513 findmnt_path = self.module.get_bin_path("findmnt")
514 if not findmnt_path:
515 return bind_mounts
516
517 rc, out, err = self._run_findmnt(findmnt_path)
518 if rc != 0:
519 return bind_mounts
520
521 # find bind mounts, in case /etc/mtab is a symlink to /proc/mounts
522 for line in out.splitlines():
523 fields = line.split()
524 # fields[0] is the TARGET, fields[1] is the SOURCE
525 if len(fields) < 2:
526 continue
527
528 # bind mounts will have a [/directory_name] in the SOURCE column
529 if self.BIND_MOUNT_RE.match(fields[1]):
530 bind_mounts.add(fields[0])
531
532 return bind_mounts
533
534 def _mtab_entries(self):
535 mtab_file = '/etc/mtab'
536 if not os.path.exists(mtab_file):
537 mtab_file = '/proc/mounts'
538
539 mtab = get_file_content(mtab_file, '')
540 mtab_entries = []
541 for line in mtab.splitlines():
542 fields = line.split()
543 if len(fields) < 4:
544 continue
545 mtab_entries.append(fields)
546 return mtab_entries
547
548 @staticmethod
549 def _replace_octal_escapes_helper(match):
550 # Convert to integer using base8 and then convert to character
551 return chr(int(match.group()[1:], 8))
552
553 def _replace_octal_escapes(self, value):
554 return self.OCTAL_ESCAPE_RE.sub(self._replace_octal_escapes_helper, value)
555
556 def get_mount_info(self, mount, device, uuids):
557
558 mount_size = get_mount_size(mount)
559
560 # _udevadm_uuid is a fallback for versions of lsblk <= 2.23 that don't have --paths
561 # see _run_lsblk() above
562 # https://github.com/ansible/ansible/issues/36077
563 uuid = uuids.get(device, self._udevadm_uuid(device))
564
565 return mount_size, uuid
566
567 def get_mount_facts(self):
568
569 mounts = []
570
571 # gather system lists
572 bind_mounts = self._find_bind_mounts()
573 uuids = self._lsblk_uuid()
574 mtab_entries = self._mtab_entries()
575
576 # start threads to query each mount
577 results = {}
578 executor = _futures.DaemonThreadPoolExecutor()
579 maxtime = timeout.GATHER_TIMEOUT or timeout.DEFAULT_GATHER_TIMEOUT
580 for fields in mtab_entries:
581 # Transform octal escape sequences
582 fields = [self._replace_octal_escapes(field) for field in fields]
583
584 device, mount, fstype, options = fields[0], fields[1], fields[2], fields[3]
585 dump, passno = int(fields[4]), int(fields[5])
586
587 if not device.startswith(('/', '\\')) and ':/' not in device or fstype == 'none':
588 continue
589
590 mount_info = {'mount': mount,
591 'device': device,
592 'fstype': fstype,
593 'options': options,
594 'dump': dump,
595 'passno': passno}
596
597 if mount in bind_mounts:
598 # only add if not already there, we might have a plain /etc/mtab
599 if not self.MTAB_BIND_MOUNT_RE.match(options):
600 mount_info['options'] += ",bind"
601
602 results[mount] = {'info': mount_info, 'timelimit': time.monotonic() + maxtime}
603 results[mount]['extra'] = executor.submit(self.get_mount_info, mount, device, uuids)
604
605 # done with spawning new workers, start gc
606 executor.shutdown()
607
608 while results: # wait for workers and get results
609 for mount in list(results):
610 done = False
611 res = results[mount]['extra']
612 try:
613 if res.done():
614 done = True
615 if res.exception() is None:
616 mount_size, uuid = res.result()
617 if mount_size:
618 results[mount]['info'].update(mount_size)
619 results[mount]['info']['uuid'] = uuid or 'N/A'
620 else:
621 # failed, try to find out why, if 'res.successful' we know there are no exceptions
622 results[mount]['info']['note'] = f'Could not get extra information: {res.exception()}'
623
624 elif time.monotonic() > results[mount]['timelimit']:
625 done = True
626 self.module.warn("Timeout exceeded when getting mount info for %s" % mount)
627 results[mount]['info']['note'] = 'Could not get extra information due to timeout'
628 except Exception as e:
629 import traceback
630 done = True
631 results[mount]['info'] = 'N/A'
632 self.module.warn("Error prevented getting extra info for mount %s: [%s] %s." % (mount, type(e), to_text(e)))
633 self.module.debug(traceback.format_exc())
634
635 if done:
636 # move results outside and make loop only handle pending
637 mounts.append(results[mount]['info'])
638 del results[mount]
639
640 # avoid cpu churn, sleep between retrying for loop with remaining mounts
641 time.sleep(0.1)
642
643 return {'mounts': mounts}
644
645 def get_device_links(self, link_dir):
646 if not os.path.exists(link_dir):
647 return {}
648 try:
649 retval = collections.defaultdict(set)
650 for entry in os.listdir(link_dir):
651 try:
652 target = os.path.basename(os.readlink(os.path.join(link_dir, entry)))
653 retval[target].add(entry)
654 except OSError:
655 continue
656 return dict((k, list(sorted(v))) for (k, v) in retval.items())
657 except OSError:
658 return {}
659
660 def get_all_device_owners(self):
661 try:
662 retval = collections.defaultdict(set)
663 for path in glob.glob('/sys/block/*/slaves/*'):
664 elements = path.split('/')
665 device = elements[3]
666 target = elements[5]
667 retval[target].add(device)
668 return dict((k, list(sorted(v))) for (k, v) in retval.items())
669 except OSError:
670 return {}
671
672 def get_all_device_links(self):
673 return {
674 'ids': self.get_device_links('/dev/disk/by-id'),
675 'uuids': self.get_device_links('/dev/disk/by-uuid'),
676 'labels': self.get_device_links('/dev/disk/by-label'),
677 'masters': self.get_all_device_owners(),
678 }
679
680 def get_holders(self, block_dev_dict, sysdir):
681 block_dev_dict['holders'] = []
682 if os.path.isdir(sysdir + "/holders"):
683 for folder in os.listdir(sysdir + "/holders"):
684 if not folder.startswith("dm-"):
685 continue
686 name = get_file_content(sysdir + "/holders/" + folder + "/dm/name")
687 if name:
688 block_dev_dict['holders'].append(name)
689 else:
690 block_dev_dict['holders'].append(folder)
691
692 def _get_sg_inq_serial(self, sg_inq, block):
693 device = "/dev/%s" % (block)
694 rc, drivedata, err = self.module.run_command([sg_inq, device])
695 if rc == 0:
696 serial = re.search(r"(?:Unit serial|Serial) number:\s+(\w+)", drivedata)
697 if serial:
698 return serial.group(1)
699
700 def get_device_facts(self):
701 device_facts = {}
702
703 device_facts['devices'] = {}
704 lspci = self.module.get_bin_path('lspci')
705 if lspci:
706 rc, pcidata, err = self.module.run_command([lspci, '-D'], errors='surrogate_then_replace')
707 else:
708 pcidata = None
709
710 try:
711 block_devs = os.listdir("/sys/block")
712 except OSError:
713 return device_facts
714
715 devs_wwn = {}
716 try:
717 devs_by_id = os.listdir("/dev/disk/by-id")
718 except OSError:
719 pass
720 else:
721 for link_name in devs_by_id:
722 if link_name.startswith("wwn-"):
723 try:
724 wwn_link = os.readlink(os.path.join("/dev/disk/by-id", link_name))
725 except OSError:
726 continue
727 devs_wwn[os.path.basename(wwn_link)] = link_name[4:]
728
729 links = self.get_all_device_links()
730 device_facts['device_links'] = links
731
732 for block in block_devs:
733 virtual = 1
734 sysfs_no_links = 0
735 try:
736 path = os.readlink(os.path.join("/sys/block/", block))
737 except OSError:
738 e = sys.exc_info()[1]
739 if e.errno == errno.EINVAL:
740 path = block
741 sysfs_no_links = 1
742 else:
743 continue
744 sysdir = os.path.join("/sys/block", path)
745 if sysfs_no_links == 1:
746 for folder in os.listdir(sysdir):
747 if "device" in folder:
748 virtual = 0
749 break
750 d = {}
751 d['virtual'] = virtual
752 d['links'] = {}
753 for (link_type, link_values) in links.items():
754 d['links'][link_type] = link_values.get(block, [])
755 diskname = os.path.basename(sysdir)
756 for key in ['vendor', 'model', 'sas_address', 'sas_device_handle']:
757 d[key] = get_file_content(sysdir + "/device/" + key)
758
759 sg_inq = self.module.get_bin_path('sg_inq')
760
761 # we can get NVMe device's serial number from /sys/block/<name>/device/serial
762 serial_path = "/sys/block/%s/device/serial" % (block)
763
764 if sg_inq:
765 serial = self._get_sg_inq_serial(sg_inq, block)
766 if serial:
767 d['serial'] = serial
768 else:
769 serial = get_file_content(serial_path)
770 if serial:
771 d['serial'] = serial
772
773 d['removable'] = get_file_content(sysdir + '/removable')
774
775 # Historically, `support_discard` simply returned the value of
776 # `/sys/block/{device}/queue/discard_granularity`. When its value
777 # is `0`, then the block device doesn't support discards;
778 # _however_, it being greater than zero doesn't necessarily mean
779 # that the block device _does_ support discards.
780 #
781 # Another indication that a block device doesn't support discards
782 # is `/sys/block/{device}/queue/discard_max_hw_bytes` being equal
783 # to `0` (with the same caveat as above). So if either of those are
784 # `0`, set `support_discard` to zero, otherwise set it to the value
785 # of `discard_granularity` for backwards compatibility.
786 d['support_discard'] = (
787 '0'
788 if get_file_content(sysdir + '/queue/discard_max_hw_bytes') == '0'
789 else get_file_content(sysdir + '/queue/discard_granularity')
790 )
791
792 if diskname in devs_wwn:
793 d['wwn'] = devs_wwn[diskname]
794
795 d['partitions'] = {}
796 for folder in os.listdir(sysdir):
797 m = re.search("(" + diskname + r"[p]?\d+)", folder)
798 if m:
799 part = {}
800 partname = m.group(1)
801 part_sysdir = sysdir + "/" + partname
802
803 part['links'] = {}
804 for (link_type, link_values) in links.items():
805 part['links'][link_type] = link_values.get(partname, [])
806
807 part['start'] = get_file_content(part_sysdir + "/start", 0)
808 part['sectorsize'] = get_file_content(part_sysdir + "/queue/logical_block_size")
809 if not part['sectorsize']:
810 part['sectorsize'] = get_file_content(part_sysdir + "/queue/hw_sector_size", 512)
811 # sysfs sectorcount assumes 512 blocksize. Convert using the correct sectorsize
812 part['sectors'] = int(get_file_content(part_sysdir + "/size", 0)) * 512 // int(part['sectorsize'])
813 part['size'] = bytes_to_human(float(part['sectors']) * float(part['sectorsize']))
814 part['uuid'] = get_partition_uuid(partname)
815 self.get_holders(part, part_sysdir)
816
817 d['partitions'][partname] = part
818
819 d['rotational'] = get_file_content(sysdir + "/queue/rotational")
820 d['scheduler_mode'] = ""
821 scheduler = get_file_content(sysdir + "/queue/scheduler")
822 if scheduler is not None:
823 m = re.match(r".*?(\[(.*)\])", scheduler)
824 if m:
825 d['scheduler_mode'] = m.group(2)
826
827 d['sectorsize'] = get_file_content(sysdir + "/queue/logical_block_size")
828 if not d['sectorsize']:
829 d['sectorsize'] = get_file_content(sysdir + "/queue/hw_sector_size", 512)
830 # sysfs sectorcount assumes 512 blocksize. Convert using the correct sectorsize
831 d['sectors'] = int(get_file_content(sysdir + "/size")) * 512 // int(d['sectorsize'])
832 if not d['sectors']:
833 d['sectors'] = 0
834 d['size'] = bytes_to_human(float(d['sectors']) * float(d['sectorsize']))
835
836 d['host'] = ""
837
838 # domains are numbered (0 to ffff), bus (0 to ff), slot (0 to 1f), and function (0 to 7).
839 m = re.match(r".+/([a-f0-9]{4}:[a-f0-9]{2}:[0|1][a-f0-9]\.[0-7])/", sysdir)
840 if m and pcidata:
841 pciid = m.group(1)
842 did = re.escape(pciid)
843 m = re.search("^" + did + r"\s(.*)$", pcidata, re.MULTILINE)
844 if m:
845 d['host'] = m.group(1)
846
847 self.get_holders(d, sysdir)
848
849 device_facts['devices'][diskname] = d
850
851 return device_facts
852
853 def get_uptime_facts(self):
854 uptime_facts = {}
855 uptime_file_content = get_file_content('/proc/uptime')
856 if uptime_file_content:
857 uptime_seconds_string = uptime_file_content.split(' ')[0]
858 uptime_facts['uptime_seconds'] = int(float(uptime_seconds_string))
859
860 return uptime_facts
861
862 def _find_mapper_device_name(self, dm_device):
863 dm_prefix = '/dev/dm-'
864 mapper_device = dm_device
865 if dm_device.startswith(dm_prefix):
866 dmsetup_cmd = self.module.get_bin_path('dmsetup', True)
867 mapper_prefix = '/dev/mapper/'
868 rc, dm_name, err = self.module.run_command("%s info -C --noheadings -o name %s" % (dmsetup_cmd, dm_device))
869 if rc == 0:
870 mapper_device = mapper_prefix + dm_name.rstrip()
871 return mapper_device
872
873 def get_lvm_facts(self):
874 """ Get LVM Facts if running as root and lvm utils are available """
875
876 lvm_facts = {'lvm': 'N/A'}
877 vgs_cmd = self.module.get_bin_path('vgs')
878 if vgs_cmd is None:
879 return lvm_facts
880
881 if os.getuid() == 0:
882 lvm_util_options = '--noheadings --nosuffix --units g --separator ,'
883
884 # vgs fields: VG #PV #LV #SN Attr VSize VFree
885 vgs = {}
886 rc, vg_lines, err = self.module.run_command('%s %s' % (vgs_cmd, lvm_util_options))
887 for vg_line in vg_lines.splitlines():
888 items = vg_line.strip().split(',')
889 vgs[items[0]] = {
890 'size_g': items[-2],
891 'free_g': items[-1],
892 'num_lvs': items[2],
893 'num_pvs': items[1],
894 'lvs': {},
895 }
896
897 lvs_path = self.module.get_bin_path('lvs')
898 # lvs fields:
899 # LV VG Attr LSize Pool Origin Data% Move Log Copy% Convert
900 lvs = {}
901 if lvs_path:
902 rc, lv_lines, err = self.module.run_command('%s %s' % (lvs_path, lvm_util_options))
903 for lv_line in lv_lines.splitlines():
904 items = lv_line.strip().split(',')
905 vg_name = items[1]
906 lv_name = items[0]
907 # The LV name is only unique per VG, so the top level fact lvs can be misleading.
908 # TODO: deprecate lvs in favor of vgs
909 lvs[lv_name] = {'size_g': items[3], 'vg': vg_name}
910 try:
911 vgs[vg_name]['lvs'][lv_name] = {'size_g': items[3]}
912 except KeyError:
913 self.module.warn(
914 "An LVM volume group was created while gathering LVM facts, "
915 "and is not included in ansible_facts['vgs']."
916 )
917
918 pvs_path = self.module.get_bin_path('pvs')
919 # pvs fields: PV VG #Fmt #Attr PSize PFree
920 pvs = {}
921 if pvs_path:
922 rc, pv_lines, err = self.module.run_command('%s %s' % (pvs_path, lvm_util_options))
923 for pv_line in pv_lines.splitlines():
924 items = pv_line.strip().split(',')
925 pvs[self._find_mapper_device_name(items[0])] = {
926 'size_g': items[4],
927 'free_g': items[5],
928 'vg': items[1]}
929
930 lvm_facts['lvm'] = {'lvs': lvs, 'vgs': vgs, 'pvs': pvs}
931
932 return lvm_facts
933
934
935 class LinuxHardwareCollector(HardwareCollector):
936 _platform = 'Linux'
937 _fact_class = LinuxHardware
938
939 required_facts = set(['platform'])
940
941
942 def __getattr__(importable_name):
943 return _no_six.deprecate(importable_name, __name__, "iteritems")