/
redfish-finder
executable file
·568 lines (502 loc) · 18.3 KB
/
redfish-finder
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
#!/usr/bin/python3
import sys
import os
import subprocess
import ipaddress
import argparse
#######################################################
# Helper function to consume the dmidecode output
# Accepts a string as input and truncates the string
# to the point immediately following needle
######################################################
def cursor_consume_next(cursor, needle):
idx = cursor.find(needle)
if idx == -1:
return None
return cursor[idx + len(needle):]
######################################################
# The AssignType class. Ennumerates the various
# Types of Host and Redfish IP Address assignments
# That are possible. Taken from the Redfish Host API
# Specification
######################################################
class AssignType():
UNKNOWN = 0
STATIC = 1
DHCP = 2
AUTOCONF = 3
HOSTSEL = 4
typestring = ["Unknwon", "Static", "DHCP", "Autoconf", "Host Selected"]
######################################################
# NetDevice class, Superclass of bus specific Device Classes
# USBNetDevice, and PCINetDevice. Provides the getifcname
# interface for use in configuring the OS via nmcli
######################################################
class NetDevice(object):
def __init(self):
self.name = "Unknown"
#
# Superclass function to get OS interface name
# agnostic to Physical interface type
#
def getifcname(self):
return self.name
def merge(self, newdev):
return self
def __str__(self):
return "Interface: " + self.name
######################################################
# Subclass of NetDevice, parses the dmidecode output
# to discover interface name for USB type devices
######################################################
class USBNetDevice(NetDevice):
def __init__(self, dmioutput):
super(NetDevice, self).__init__()
dmioutput = cursor_consume_next(dmioutput, "idVendor: ")
# Strip the 0x off the front of the vendor and product ids
self.vendor = int((dmioutput.split()[0])[2:], 16)
self.product = int((dmioutput.split()[2])[2:], 16)
# Now we need to find the corresponding device name in sysfs
if self._find_device() == False:
raise RuntimeError("Unable to find USB network device")
def _getname(self, dpath):
for root, dirs, files in os.walk(dpath, topdown=False):
for d in dirs:
try:
if d != "net":
continue
dr = os.listdir(os.path.join(root, d))
self.name = dr[0]
return True
except:
continue
return False
def _find_device(self):
for root,dirs,files in os.walk("/sys/bus/usb/devices", topdown=False):
for d in dirs:
try:
f = open(os.path.join(root, d, "idVendor"))
lines = f.readlines()
if int(lines[0][0:4],16) != self.vendor:
f.close()
continue
f.close()
f = open(os.path.join(root, d, "idProduct"))
lines = f.readlines()
if int(lines[0][0:4],16) != self.product:
f.close()
continue
f.close()
# We found a matching product and vendor, go find the net directory
# and get the interface name
return self._getname(os.path.join(root, d))
except:
continue
print("redfish-finder: Unable to find usb network device with vendor:product %s:%s" % (hex(self.vendor), hex(self.product)))
return False
######################################################
# Parses out HostConfig information from SMBIOS for use in
# Configuring OS network interface
######################################################
class HostConfig():
def __init__(self, cursor):
self.address = []
self.mask = []
self.network = []
try:
cursor = cursor_consume_next(cursor, "Host IP Assignment Type: ")
if cursor == None:
raise RuntimeError("redfish-finder: Unable to parse SMBIOS Host IP Assignment Type")
self.assigntype = []
if cursor.split()[0] == "Static" or cursor.split()[0] == "AutoConf":
assigntype = AssignType.STATIC
if cursor.split()[0] == "AutoConf":
assigntype = AssignType.AUTOCONF
self.assigntype.append(assigntype)
cursor = cursor_consume_next(cursor, "Host IP Address Format: ")
if cursor.split()[0] == "IPv4":
cursor = cursor_consume_next(cursor, "IPv4 Address: ")
addr = cursor.split()[0]
self.address.append(ipaddress.IPv4Address(addr))
cursor = cursor_consume_next(cursor, "IPv4 Mask: ")
mask = cursor.split()[0]
self.mask.append(ipaddress.IPv4Address(mask))
self.network.append(ipaddress.IPv4Network(addr + "/" + mask, strict=False))
elif cursor.split()[0] == "IPv6":
cursor = cursor_consume_next(cursor, "IPv6 Address: ")
addr = cursor.split()[0]
self.address.append(ipaddress.IPv6Address(addr))
cursor = cursor_consume_next(cursor, "IPv6 Mask: ")
mask = cursor.split()[0]
self.mask.append(ipaddress.IPv4Address(mask))
self.network.append(ipaddress.IPv6Network(addr + "/" + mask, strict=False))
elif cursor.split()[0] == "DHCP":
self.assigntype.append(AssignType.DHCP)
self.address.append(0)
self.mask.append(0)
self.network.append(0)
else:
# Support the other types later
raise RuntimeError("redfish-finder: Unable to parse SMBIOS Host configuaration")
except:
raise RuntimeError("redfish-finder: Unexpected error while parsing HostConfig!")
def merge(self, newconfig):
self.assigntype.extend(newconfig.assigntype)
self.address.extend(newconfig.address)
self.mask.extend(newconfig.mask)
self.network.extend(newconfig.network)
return self
#
# Using the smbios host config info, set the appropriate
# attributes of the network manager connection object
#
def generate_nm_config(self, device, nmcon):
assignmap = { AssignType.STATIC: "manual", AssignType.DHCP: "auto", AssignType.AUTOCONF: "auto"}
methodp = "ipv4.method"
for i in range(len(self.assigntype)):
assigntype = self.assigntype[i]
if assigntype == AssignType.STATIC or assigntype == AssignType.AUTOCONF:
if self.address[i].version == 4:
methodp = "ipv4.method"
addrp = "ipv4.addresses"
else:
methodp = "ipv6.method"
addrp = "ipv6.addresses"
try:
nmcon.update_property(methodp, assignmap[assigntype])
if assigntype == AssignType.STATIC or assigntype == AssignType.AUTOCONF:
nmcon.update_property(addrp, str(self.address[i]) + "/" + str(self.network[i].prefixlen))
except:
print("redfish-finder: Error generating nm_config")
return False
return True
def __str__(self):
val = "Host Config(" + AssignType.typestring[self.assigntype] + ")"
if (self.assigntype == AssignType.STATIC):
val = val + " " + str(self.address) + "/" + str(self.mask)
return val
######################################################
# Class to hold Redfish service information, as extracted
# From the smbios data from dmidecode
######################################################
class ServiceConfig():
def __init__(self, cursor):
self.address = []
self.mask = []
self.dhcp_format = []
try:
cursor = cursor_consume_next(cursor, "Redfish Service IP Discovery Type: ")
if cursor == None:
raise RuntimeError("redfish-finder: Unable to find Redfish Service Info")
if cursor.split()[0] == "Static" or cursor.split()[0] == "AutoConf":
self.assigntype = AssignType.STATIC
if cursor.split()[0] == "AutoConf":
self.assigntype = AssignType.AUTOCONF
cursor = cursor_consume_next(cursor, "Redfish Service IP Address Format: ")
if cursor.split()[0] == "IPv4":
cursor = cursor_consume_next(cursor, "IPv4 Redfish Service Address: ")
self.address.append(ipaddress.IPv4Address(cursor.split()[0]))
cursor = cursor_consume_next(cursor, "IPv4 Redfish Service Mask: ")
self.mask.append(ipaddress.IPv4Address(cursor.split()[0]))
elif cursor.split()[0] == "IPv6":
cursor = cursor_consume_next(cursor, "IPv6 Redfish Service Address: ")
self.address.append(ipaddress.IPv6Address(unicode(cursor.split()[0], "utf-8")))
cursor = cursor_consume_next(cursor, "IPv6 Mask: ")
self.mask.append(ipaddress.IPv4Address(unicode(cursor.split()[0], "utf-8")))
cursor = cursor_consume_next(cursor, "Redfish Service Port: ")
self.port = int(cursor.split()[0])
cursor = cursor_consume_next(cursor, "Redfish Service Vlan: ")
self.vlan = int(cursor.split()[0])
elif cursor.split()[0] == "DHCP":
self.assigntype = AssignType.DHCP
cursor = cursor_consume_next(cursor, "Redfish Service IP Address Format: ")
if cursor.split()[0] == "IPv4":
self.dhcp_format.append("DHCP4")
elif cursor.split()[0] == "IPv6":
self.dhcp_format.append("DHCP6")
else:
# Support the other types later
print("redfish-finder: Unable to parse SMBIOS Service Config info")
return None
cursor = cursor_consume_next(cursor, "Redfish Service Hostname: ")
#
# Sanity check: If it contains the consecutive spaces
# only, reference to the index '0' will throw an
# exception.
#
if len(cursor.split()) != 0:
self.hostname = cursor.split()[0]
else:
self.hostname = ""
except:
raise RuntimeError("redfish-finder: Unexpected error parsing ServiceConfig")
def merge(self, newconfig):
self.address.extend(newconfig.address)
self.mask.extend(newconfig.mask)
return self
def __str__(self):
val = "Service Config(" + AssignType.typestring[self.assigntype] + ")"
if (self.assigntype == AssignType.STATIC):
val = val + " " + str(self.address) + "/" + str(self.mask)
return val
######################################################
# Object to hold the information parsed from smbios
# Split into a Netdevice object, a HostConfig object
# and a ServiceConfig object
######################################################
class dmiobject():
def __init__(self, dmioutput):
self.device = None
self.hostconfig = None
self.serviceconfig = None
cursor = dmioutput
# Find the type 42 header, if not found, nothing to do here
cursor = cursor_consume_next(cursor, "Management Controller Host Interface\n")
if cursor == None:
raise RuntimeError("Unable to find Management Controller Host Interface block in SMBIOS")
while (cursor != None):
if (cursor == None):
raise RuntimeError("No Redfish Host support detected")
cursor = cursor_consume_next(cursor, "Host Interface Type: Network\n")
if (cursor == None):
raise RuntimeError("No supported Redfish host interface type found")
# If we get here then we know this is a network interface device
cursor = cursor_consume_next(cursor, "Device Type: ")
# The next token should either be:
# USB
# PCI/PCIe
# OEM
# Unknown
dtype = cursor.split()[0]
if (dtype == "USB"):
newdev = USBNetDevice(cursor)
if self.device == None:
self.device = newdev
else:
self.device.merge(newdev)
# Now find the Redfish over IP section
cursor = cursor_consume_next(cursor, "Protocol ID: 04 (Redfish over IP)\n")
if (cursor == None):
raise RuntimeError("No Redfish over IP Protocol support")
newhostconfig = HostConfig(cursor)
if self.hostconfig == None:
self.hostconfig = newhostconfig
else:
self.hostconfig.merge(newhostconfig)
serviceconfig = ServiceConfig(cursor)
if self.serviceconfig == None:
self.serviceconfig = serviceconfig
elif self.serviceconfig == None:
self.serviceconfig.merge(serviceconfig)
cursor = cursor_consume_next(cursor, "Management Controller Host Interface\n")
def __str__(self):
return str(self.device) + " | " + str(self.hostconfig) + " | " + str(self.serviceconfig)
######################################################
# Service Data represents the config data that gets written to
# Various OS config files (/etc/host)
######################################################
class OSServiceData():
def __init__(self, sconf):
self.sconf = sconf
try:
f = open("/etc/hosts", "r")
self.host_entries = f.readlines()
self.constant_name = "redfish-localhost"
f.close()
except:
print("Unable to read OS Config Files")
return None
#
# Method to read in /etc/hosts, remove old redfish entries
# and insert new ones based on ServiceConfig
#
def update_redfish_info(self, conn):
# strip any redfish localhost entry from host_entries
# as well as any entries for the smbios exported host name
for h in self.host_entries:
if h.find(self.constant_name) != -1:
self.host_entries.remove(h)
continue
if h.find(self.sconf.hostname) != -1:
self.host_entries.remove(h)
continue
# Now add the new entries in
addresses=""
if self.sconf.assigntype == AssignType.DHCP:
for i in self.sconf.dhcp_format:
dhcp_ip = conn.get_dhcp_server_identifier(i)
addresses = addresses + str(dhcp_ip) + " "
else:
for i in self.sconf.address:
addresses = addresses + str(i) + " "
newentry = addresses + " " + self.constant_name
newentry = newentry + " " + self.sconf.hostname
self.host_entries.append(newentry)
#
# write out /etc/hosts with updated redfish info
#
def output_redfish_config(self):
try:
f = open("/etc/hosts", "w")
f.writelines(self.host_entries)
f.close()
except:
print("Unalbe to open OS Config Files for writing")
return False
return True
#
# Remove redfish information from /etc/hosts, and write it back to file
#
def remove_redfish_config(self):
for h in self.host_entries:
if h.find(self.constant_name) != -1:
self.host_entries.remove(h)
continue
if h.find(self.sconf.hostname) != -1:
self.host_entries.remove(h)
continue
return self.output_redfish_config()
######################################################
# Represents an nm connection, pulls in the config from nmi con show <ifc>
# Creates the connection if it doesn't exist, and updates it to reflect the host
# Config data from the HostConfig class
######################################################
class nmConnection():
def __init__(self, ifc):
self.ifc = ifc
self.cmdlinedown = "nmcli con down id " + self.ifc.getifcname()
self.cmdlineup = "nmcli con up id " + self.ifc.getifcname()
try:
propstr = subprocess.check_output(["nmcli", "con", "show", ifc.getifcname()])
except:
# Need to create a connection
try:
create = [ "nmcli", "con", "add",
"connection.id", ifc.getifcname(),
"connection.type", "802-3-ethernet",
"connection.interface-name", ifc.getifcname()]
subprocess.check_call(create)
propstr = subprocess.check_output(["nmcli", "con", "show", ifc.getifcname()])
except:
raise RuntimeError("redfish-finder: Unexpected error building connection to %s" % self.ifc)
try:
self.properties = {}
self.updates = []
lines = propstr.splitlines()
for l in lines:
la = l.decode("utf-8").split()
if len(la) < 2:
continue
self.properties[la[0].strip(":")] = la[1]
except:
raise RuntimeError("Unexpected error parsing connection for %s" % self.ifc)
#
# Update a property value in this network manager object
#
def update_property(self, prop, val):
if self.get_property(prop) == val:
return
self.properties[prop] = val
self.updates.append(prop)
#
# Read a network manager object property
#
def get_property(self, prop):
return self.properties[prop]
#
# Get DHCP server identifier
#
def get_dhcp_server_identifier(self, dhcp_format):
propstr = subprocess.check_output(["nmcli", "-f", dhcp_format, "con", "show", self.ifc.getifcname()])
lines = propstr.splitlines()
for data in lines:
elements = data.decode("utf-8").split()
if len(elements) < 2:
continue
for key in elements:
if key == "dhcp_server_identifier":
return elements[3]
return None
#
# Using this object, run nmcli to update the os so that the
# interface represented here is in sync with our desired
# configuration
#
def sync_to_os(self):
cmdlinemod = "nmcli con modify id " + self.ifc.getifcname() + " "
for i in self.updates:
cmdlinemod = cmdlinemod + i + " " + self.properties[i] + " "
try:
subprocess.check_call(self.cmdlinedown.split())
except subprocess.CalledProcessError as e:
if e.returncode != 10:
print("redfish-finder: Unable to take down interface: error %d" % e.returncode)
return False
except:
print("redfish-finder: Unexpected error running nmcli while dropping connection")
return False
try:
if len(self.updates) != 0:
subprocess.check_call(cmdlinemod.split())
subprocess.check_call(self.cmdlineup.split())
except:
print("redfish-finder: Unexpected error running nmcli while updating connection")
return False
return True
#
# Down the represented interface
#
def shutdown(self):
try:
subprocess.check_call(self.cmdlinedown.split())
except:
print("redfish-finder: Unexpected error running nmcli while closing connection")
return False
return True
def __str__(self):
return str(self.properties)
def get_info_from_dmidecode():
try:
dmioutput = subprocess.check_output(["/usr/sbin/dmidecode", "-t42"])
except:
raise RuntimeError("Failed to run dmidecode. Make sure you run as root")
return dmiobject(dmioutput.decode())
def main():
parser = argparse.ArgumentParser(description='Use SMBios info to setup canonical BMC access from the host')
parser.add_argument('-s', '--shutdown', action='store_true')
args = parser.parse_args(sys.argv[1:])
print("redfish-finder: Getting dmidecode info")
try:
smbios_info = get_info_from_dmidecode()
except RuntimeError as e:
print("Error parsing dmidecode information: %s\n" % e)
sys.exit(1)
print("redfish-finder: Building NetworkManager connection info")
try:
conn = nmConnection(smbios_info.device)
except RuntimeError as e:
print("redfish-finder: Error building nm connection: %s\n" % e)
sys.exit(1)
print("redfish-finder: Obtaining OS config info")
svc = OSServiceData(smbios_info.serviceconfig)
if svc == None:
sys.exit(1)
if args.shutdown == True:
print("Shutting down interface %s" % smbios_info.device.getifcname())
conn.shutdown()
print("Scrubbing host info from OS config")
svc.remove_redfish_config()
sys.exit(0)
print("redfish-finder: Converting SMBIOS Host Config to NetworkManager Connection info")
if smbios_info.hostconfig.generate_nm_config(smbios_info.device, conn) == False:
sys.exit(1)
print("redfish-finder: Applying NetworkManager connection configuration changes")
if conn.sync_to_os() == False:
sys.exit(1)
print("redfish-finder: Adding redfish host info to OS config")
svc.update_redfish_info(conn)
if svc.output_redfish_config() == False:
sys.exit(1)
print("redfish-finder: Done, BMC is now reachable via hostname redfish-localhost")
if __name__ == "__main__":
main()