Debian Codegraph @

pkg/systemd/hw-detect.js

raw
1 /*
2 * Copyright (C) 2018 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5 import cockpit from "cockpit";
6
7 import * as machine_info from "machine-info.js";
8 const _ = cockpit.gettext;
9
10 // map an info.system key to a /sys/class/dmi/id/* attribute name
11 const InfoDMIKey = {
12 version: "product_version",
13 name: "product_name",
14 alt_version: "board_vendor",
15 alt_name: "board_name",
16 type: "chassis_type_str",
17 bios_vendor: "bios_vendor",
18 bios_version: "bios_version",
19 bios_date: "bios_date",
20 };
21
22 const getDMI = info => machine_info.dmi_info()
23 .then(fields => {
24 Object.keys(InfoDMIKey).forEach(key => {
25 info.system[key] = fields[InfoDMIKey[key]];
26 });
27 return true;
28 });
29
30 const getDeviceTree = info => machine_info.devicetree_info()
31 .then(fields => {
32 // if getDMI sets a field first, let that win
33 if (fields.model && !info.system.name)
34 info.system.name = fields.model;
35 return true;
36 });
37
38 // Add info.pci [{slot, cls, vendor, model}] list
39 function findPCI(udevdb, info) {
40 for (const syspath in udevdb) {
41 const props = udevdb[syspath];
42 if (props.SUBSYSTEM === "pci")
43 info.pci.push({
44 slot: props.PCI_SLOT_NAME || syspath.split("/").pop() || "",
45 cls: props.ID_PCI_CLASS_FROM_DATABASE || props.PCI_CLASS.toString() || "",
46 vendor: props.ID_VENDOR_FROM_DATABASE || "",
47 model: props.ID_MODEL_FROM_DATABASE || props.PCI_ID || ""
48 });
49 }
50 }
51
52 function findMemoryDevices(udevdb, info) {
53 const memoryArray = [];
54 const dmipath = '/devices/virtual/dmi/id';
55 if (!(dmipath in udevdb))
56 return;
57
58 const props = udevdb[dmipath];
59 // Systemd now exposes memory information in udev, introduced in systemd => 248
60 // https://github.com/systemd/systemd/blob/main/NEWS#L1713
61 if (!('MEMORY_ARRAY_NUM_DEVICES' in props)) {
62 return;
63 }
64
65 const devices = parseInt(props.MEMORY_ARRAY_NUM_DEVICES, 10);
66 for (let slot = 0; slot < devices; slot++) {
67 let memorySize = parseInt(props[`MEMORY_DEVICE_${slot}_SIZE`], 10);
68 if (memorySize) {
69 memorySize = cockpit.format_bytes(memorySize, { base2: true });
70 } else {
71 memorySize = _("Unknown");
72 }
73
74 let memoryRank = props[`MEMORY_DEVICE_${slot}_RANK`];
75 if (memoryRank == 1) {
76 memoryRank = _("Single rank");
77 } else if (memoryRank == 2) {
78 memoryRank = _("Dual rank");
79 } else {
80 memoryRank = _("Unknown");
81 }
82
83 let speed = props[`MEMORY_DEVICE_${slot}_SPEED_MTS`];
84 if (speed) {
85 speed += ' MT/s';
86 } else {
87 speed = _("Unknown");
88 }
89
90 let locator = _("Unknown");
91 if (props[`MEMORY_DEVICE_${slot}_BANK_LOCATOR`] && props[`MEMORY_DEVICE_${slot}_LOCATOR`]) {
92 locator = props[`MEMORY_DEVICE_${slot}_BANK_LOCATOR`] + ': ' + props[`MEMORY_DEVICE_${slot}_LOCATOR`];
93 }
94
95 memoryArray.push({
96 locator,
97 technology: props[`MEMORY_DEVICE_${slot}_MEMORY_TECHNOLOGY`] || _("Unknown"),
98 type: props[`MEMORY_DEVICE_${slot}_TYPE`] || _("Unknown"),
99 size: memorySize,
100 state: props[`MEMORY_DEVICE_${slot}_TOTAL_WIDTH`] ? _("Present") : _("Absent"),
101 rank: memoryRank,
102 speed,
103 });
104 }
105
106 info.memory = memoryArray;
107 }
108
109 export default function detect() {
110 const info = { system: {}, pci: [], memory: [] };
111 const tasks = [];
112
113 tasks.push(machine_info.cpu_ram_info()
114 .then(result => {
115 info.system.cpu_model = result.cpu_model || _("unknown");
116 info.system.nproc = result.cpus;
117 return true;
118 }));
119
120 tasks.push(getDMI(info)
121 .catch(error => {
122 console.warn("Failed to get DMI information:", error.toString());
123 return true;
124 }));
125
126 tasks.push(getDeviceTree(info)
127 .catch(error => {
128 console.debug("Failed to get DeviceTree information:", error.toString());
129 return true;
130 }));
131
132 tasks.push(machine_info.udev_info()
133 .then(result => {
134 findPCI(result, info);
135 findMemoryDevices(result, info);
136 return true;
137 })
138 .catch(error => {
139 console.warn("Failed to get udev information:", error.toString());
140 return true;
141 }));
142
143 // Fallback if systemd < 248
144 if (info.memory.length === 0) {
145 tasks.push(machine_info.memory_info()
146 .then(result => {
147 info.memory = result;
148 return true;
149 })
150 .catch(error => {
151 console.warn("Failed to get dmidecode information: ", error.toString());
152 return true;
153 }));
154 }
155
156 // return info after all task promises got done
157 return Promise.all(tasks).then(() => info);
158 }