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 |
| 20 | import dbus |
| 21 | import argparse |
| 22 | import subprocess |
| 23 | import obmc_system_config as System |
| 24 | |
| 25 | INV_DBUS_NAME = 'org.openbmc.Inventory' |
| 26 | INV_INTF_NAME = 'org.openbmc.InventoryItem' |
| 27 | NET_DBUS_NAME = 'org.openbmc.NetworkManager' |
| 28 | NET_OBJ_NAME = '/org/openbmc/NetworkManager/Interface' |
| 29 | CHS_DBUS_NAME = 'org.openbmc.control.Chassis' |
| 30 | CHS_OBJ_NAME = '/org/openbmc/control/chassis0' |
| 31 | PROP_INTF_NAME = 'org.freedesktop.DBus.Properties' |
| 32 | |
| 33 | FRUS = System.FRU_INSTANCES |
| 34 | |
| 35 | # IEEE 802 MAC address mask for locally administered. |
| 36 | # This means the admin has set the MAC and is no longer |
| 37 | # the unique number set by the device manufacturer. |
| 38 | MAC_LOCALLY_ADMIN_MASK = 0x20000000000 |
| 39 | |
| 40 | |
| 41 | # Get the inventory dbus path based on the requested fru |
| 42 | def get_inv_obj_path(fru_type, fru_name): |
| 43 | obj_path = '' |
| 44 | for f in FRUS.keys(): |
| 45 | if (FRUS[f]['fru_type'] == fru_type and f.endswith(fru_name)): |
| 46 | obj_path = f.replace("<inventory_root>", System.INVENTORY_ROOT) |
| 47 | return obj_path |
| 48 | |
| 49 | |
| 50 | # Get the inventory property value |
| 51 | def get_inv_value(obj, prop_name): |
| 52 | value = '' |
| 53 | dbus_method = obj.get_dbus_method("Get", PROP_INTF_NAME) |
| 54 | value = dbus_method(INV_INTF_NAME, prop_name) |
| 55 | return value |
| 56 | |
| 57 | |
| 58 | # Get the value of the mac on the system without ':' separators |
| 59 | def get_sys_mac(obj): |
| 60 | sys_mac = '' |
| 61 | dbus_method = obj.get_dbus_method("GetHwAddress", NET_DBUS_NAME) |
| 62 | sys_mac = dbus_method("eth0") |
| 63 | sys_mac = sys_mac.replace(":", "") |
| 64 | return sys_mac |
| 65 | |
| 66 | |
| 67 | # Replace the value of the system mac with the value of the inventory |
| 68 | # MAC if the system MAC is not locally administered because this means |
| 69 | # the system admin has purposely set the MAC |
| 70 | def sync_mac(obj, inv_mac, sys_mac): |
| 71 | # Convert sys MAC to int to perform bitwise '&' |
| 72 | int_sys_mac = int(sys_mac, 16) |
| 73 | if not int_sys_mac & MAC_LOCALLY_ADMIN_MASK: |
| 74 | # Sys MAC is not locally administered, go replace it with inv value |
| 75 | # Add the ':' separators |
| 76 | mac_str = ':'.join([inv_mac[i]+inv_mac[i+1] for i in range(0, 12, 2)]) |
| 77 | # The Set HW Method already has checking for mac format |
| 78 | dbus_method = obj.get_dbus_method("SetHwAddress", NET_DBUS_NAME) |
| 79 | dbus_method("eth0", mac_str) |
| 80 | |
| 81 | |
| 82 | # Get sys uuid |
| 83 | def get_sys_uuid(obj): |
| 84 | sys_uuid = '' |
| 85 | dbus_method = obj.get_dbus_method("Get", PROP_INTF_NAME) |
| 86 | sys_uuid = dbus_method(CHS_DBUS_NAME, "uuid") |
| 87 | return sys_uuid |
| 88 | |
| 89 | |
| 90 | # Set sys uuid, this reboots the BMC for the value to take effect |
| 91 | def set_sys_uuid(uuid): |
| 92 | rc = subprocess.call(["fw_setenv", "uuid", uuid]) |
| 93 | if rc == 0: |
| 94 | print "Rebooting BMC to set uuid" |
| 95 | # TODO Uncomment once sync from u-boot to /etc/machine-id is in place |
| 96 | # Issue openbmc/openbmc#479 |
| 97 | # rc = subprocess.call(["reboot"]) |
| 98 | else: |
| 99 | print "Error setting uuid" |
| 100 | |
| 101 | if __name__ == '__main__': |
| 102 | arg = argparse.ArgumentParser() |
| 103 | arg.add_argument('-t') |
| 104 | arg.add_argument('-n') |
| 105 | arg.add_argument('-p') |
| 106 | arg.add_argument('-s') |
| 107 | |
| 108 | opt = arg.parse_args() |
| 109 | fru_type = opt.t |
| 110 | fru_name = opt.n |
| 111 | prop_name = opt.p |
| 112 | sync_type = opt.s |
| 113 | |
| 114 | bus = dbus.SystemBus() |
| 115 | inv_obj_path = get_inv_obj_path(fru_type, fru_name) |
| 116 | inv_obj = bus.get_object(INV_DBUS_NAME, inv_obj_path) |
| 117 | net_obj = bus.get_object(NET_DBUS_NAME, NET_OBJ_NAME) |
| 118 | chs_obj = bus.get_object(CHS_DBUS_NAME, CHS_OBJ_NAME) |
| 119 | |
| 120 | # Get the value of the requested inventory property |
| 121 | inv_value = get_inv_value(inv_obj, prop_name) |
| 122 | |
| 123 | if sync_type == "mac": |
| 124 | sys_mac = get_sys_mac(net_obj) |
| 125 | if inv_value != sys_mac: |
| 126 | sync_mac(net_obj, inv_value, sys_mac) |
| 127 | elif sync_type == "uuid": |
| 128 | sys_uuid = get_sys_uuid(chs_obj) |
| 129 | if inv_value != sys_uuid: |
| 130 | set_sys_uuid(inv_value) |