Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 1 | #!/usr/bin/python -u |
| 2 | # |
| 3 | # Copyright 2016 IBM Corporation |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 14 | # implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | |
| 18 | |
| 19 | import sys |
Brad Bishop | 3205742 | 2016-10-05 22:48:39 -0400 | [diff] [blame] | 20 | import os |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 21 | import dbus |
| 22 | import argparse |
| 23 | import subprocess |
Brad Bishop | 3d13c28 | 2016-09-21 08:07:47 -0400 | [diff] [blame] | 24 | |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 25 | |
| 26 | INV_DBUS_NAME = 'org.openbmc.Inventory' |
| 27 | INV_INTF_NAME = 'org.openbmc.InventoryItem' |
| 28 | NET_DBUS_NAME = 'org.openbmc.NetworkManager' |
| 29 | NET_OBJ_NAME = '/org/openbmc/NetworkManager/Interface' |
| 30 | CHS_DBUS_NAME = 'org.openbmc.control.Chassis' |
| 31 | CHS_OBJ_NAME = '/org/openbmc/control/chassis0' |
| 32 | PROP_INTF_NAME = 'org.freedesktop.DBus.Properties' |
| 33 | |
Brad Bishop | 3d13c28 | 2016-09-21 08:07:47 -0400 | [diff] [blame] | 34 | FRUS = {} |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 35 | |
| 36 | # IEEE 802 MAC address mask for locally administered. |
| 37 | # This means the admin has set the MAC and is no longer |
| 38 | # the unique number set by the device manufacturer. |
| 39 | MAC_LOCALLY_ADMIN_MASK = 0x20000000000 |
| 40 | |
| 41 | |
| 42 | # Get the inventory dbus path based on the requested fru |
| 43 | def get_inv_obj_path(fru_type, fru_name): |
| 44 | obj_path = '' |
| 45 | for f in FRUS.keys(): |
Brad Bishop | 7e5ec46 | 2016-09-21 09:09:48 -0400 | [diff] [blame] | 46 | import obmc.inventory |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 47 | if (FRUS[f]['fru_type'] == fru_type and f.endswith(fru_name)): |
Brad Bishop | 7e5ec46 | 2016-09-21 09:09:48 -0400 | [diff] [blame] | 48 | obj_path = f.replace("<inventory_root>", obmc.inventory.INVENTORY_ROOT) |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 49 | return obj_path |
| 50 | |
| 51 | |
| 52 | # Get the inventory property value |
| 53 | def get_inv_value(obj, prop_name): |
| 54 | value = '' |
| 55 | dbus_method = obj.get_dbus_method("Get", PROP_INTF_NAME) |
| 56 | value = dbus_method(INV_INTF_NAME, prop_name) |
| 57 | return value |
| 58 | |
| 59 | |
| 60 | # Get the value of the mac on the system without ':' separators |
| 61 | def get_sys_mac(obj): |
| 62 | sys_mac = '' |
| 63 | dbus_method = obj.get_dbus_method("GetHwAddress", NET_DBUS_NAME) |
| 64 | sys_mac = dbus_method("eth0") |
| 65 | sys_mac = sys_mac.replace(":", "") |
| 66 | return sys_mac |
| 67 | |
| 68 | |
| 69 | # Replace the value of the system mac with the value of the inventory |
| 70 | # MAC if the system MAC is not locally administered because this means |
| 71 | # the system admin has purposely set the MAC |
| 72 | def sync_mac(obj, inv_mac, sys_mac): |
| 73 | # Convert sys MAC to int to perform bitwise '&' |
| 74 | int_sys_mac = int(sys_mac, 16) |
| 75 | if not int_sys_mac & MAC_LOCALLY_ADMIN_MASK: |
| 76 | # Sys MAC is not locally administered, go replace it with inv value |
| 77 | # Add the ':' separators |
| 78 | mac_str = ':'.join([inv_mac[i]+inv_mac[i+1] for i in range(0, 12, 2)]) |
| 79 | # The Set HW Method already has checking for mac format |
| 80 | dbus_method = obj.get_dbus_method("SetHwAddress", NET_DBUS_NAME) |
| 81 | dbus_method("eth0", mac_str) |
| 82 | |
| 83 | |
| 84 | # Get sys uuid |
| 85 | def get_sys_uuid(obj): |
| 86 | sys_uuid = '' |
| 87 | dbus_method = obj.get_dbus_method("Get", PROP_INTF_NAME) |
| 88 | sys_uuid = dbus_method(CHS_DBUS_NAME, "uuid") |
| 89 | return sys_uuid |
| 90 | |
| 91 | |
| 92 | # Set sys uuid, this reboots the BMC for the value to take effect |
| 93 | def set_sys_uuid(uuid): |
| 94 | rc = subprocess.call(["fw_setenv", "uuid", uuid]) |
| 95 | if rc == 0: |
| 96 | print "Rebooting BMC to set uuid" |
| 97 | # TODO Uncomment once sync from u-boot to /etc/machine-id is in place |
| 98 | # Issue openbmc/openbmc#479 |
| 99 | # rc = subprocess.call(["reboot"]) |
| 100 | else: |
| 101 | print "Error setting uuid" |
| 102 | |
| 103 | if __name__ == '__main__': |
| 104 | arg = argparse.ArgumentParser() |
| 105 | arg.add_argument('-t') |
| 106 | arg.add_argument('-n') |
| 107 | arg.add_argument('-p') |
| 108 | arg.add_argument('-s') |
| 109 | |
| 110 | opt = arg.parse_args() |
| 111 | fru_type = opt.t |
| 112 | fru_name = opt.n |
| 113 | prop_name = opt.p |
| 114 | sync_type = opt.s |
| 115 | |
Brad Bishop | 3d13c28 | 2016-09-21 08:07:47 -0400 | [diff] [blame] | 116 | inventory = os.path.join( |
| 117 | sys.prefix, 'share', 'inventory', 'inventory.json') |
| 118 | if os.path.exists(inventory): |
| 119 | import json |
| 120 | with open(inventory, 'r') as f: |
| 121 | try: |
| 122 | inv = json.load(f) |
| 123 | except ValueError: |
| 124 | print "Invalid JSON detected in " + inventory |
| 125 | else: |
| 126 | FRUS = inv |
| 127 | else: |
| 128 | import obmc_system_config as System |
| 129 | FRUS = System.FRU_INSTANCES |
| 130 | |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 131 | bus = dbus.SystemBus() |
| 132 | inv_obj_path = get_inv_obj_path(fru_type, fru_name) |
| 133 | inv_obj = bus.get_object(INV_DBUS_NAME, inv_obj_path) |
| 134 | net_obj = bus.get_object(NET_DBUS_NAME, NET_OBJ_NAME) |
| 135 | chs_obj = bus.get_object(CHS_DBUS_NAME, CHS_OBJ_NAME) |
| 136 | |
| 137 | # Get the value of the requested inventory property |
| 138 | inv_value = get_inv_value(inv_obj, prop_name) |
| 139 | |
| 140 | if sync_type == "mac": |
| 141 | sys_mac = get_sys_mac(net_obj) |
| 142 | if inv_value != sys_mac: |
| 143 | sync_mac(net_obj, inv_value, sys_mac) |
| 144 | elif sync_type == "uuid": |
| 145 | sys_uuid = get_sys_uuid(chs_obj) |
| 146 | if inv_value != sys_uuid: |
| 147 | set_sys_uuid(inv_value) |
Brad Bishop | 5306675 | 2016-09-21 08:48:04 -0400 | [diff] [blame] | 148 | |
| 149 | # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |