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 | |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 18 | import sys |
Brad Bishop | 3205742 | 2016-10-05 22:48:39 -0400 | [diff] [blame] | 19 | import os |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 20 | import dbus |
Dinesh Chinari | c47c6a6 | 2017-03-29 23:09:59 -0500 | [diff] [blame^] | 21 | import uuid |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 22 | import argparse |
| 23 | import subprocess |
Dinesh Chinari | c47c6a6 | 2017-03-29 23:09:59 -0500 | [diff] [blame^] | 24 | import obmc.mapper |
| 25 | import shutil |
Brad Bishop | 3d13c28 | 2016-09-21 08:07:47 -0400 | [diff] [blame] | 26 | |
Dinesh Chinari | c47c6a6 | 2017-03-29 23:09:59 -0500 | [diff] [blame^] | 27 | INV_INTF_NAME = 'xyz.openbmc_project.Inventory.Item.NetworkInterface' |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 28 | NET_DBUS_NAME = 'org.openbmc.NetworkManager' |
| 29 | NET_OBJ_NAME = '/org/openbmc/NetworkManager/Interface' |
| 30 | CHS_DBUS_NAME = 'org.openbmc.control.Chassis' |
Dinesh Chinari | c47c6a6 | 2017-03-29 23:09:59 -0500 | [diff] [blame^] | 31 | CHS_INTF_NAME = 'xyz.openbmc_project.Common.UUID' |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 32 | CHS_OBJ_NAME = '/org/openbmc/control/chassis0' |
| 33 | PROP_INTF_NAME = 'org.freedesktop.DBus.Properties' |
Dinesh Chinari | c47c6a6 | 2017-03-29 23:09:59 -0500 | [diff] [blame^] | 34 | INVENTORY_ROOT = '/xyz/openbmc_project/inventory' |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 35 | |
Brad Bishop | 3d13c28 | 2016-09-21 08:07:47 -0400 | [diff] [blame] | 36 | FRUS = {} |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 37 | |
| 38 | # IEEE 802 MAC address mask for locally administered. |
| 39 | # This means the admin has set the MAC and is no longer |
| 40 | # the unique number set by the device manufacturer. |
| 41 | MAC_LOCALLY_ADMIN_MASK = 0x20000000000 |
| 42 | |
| 43 | |
Dinesh Chinari | c47c6a6 | 2017-03-29 23:09:59 -0500 | [diff] [blame^] | 44 | # Get inventory MACAddress value. |
| 45 | def get_bmc_mac_address(bus, prop): |
| 46 | mapper = obmc.mapper.Mapper(bus) |
| 47 | |
| 48 | # Get the inventory subtree, limited |
| 49 | # to objects that implement NetworkInterface. |
| 50 | for path, info in \ |
| 51 | mapper.get_subtree( |
| 52 | path=INVENTORY_ROOT, |
| 53 | interfaces=[INV_INTF_NAME]).iteritems(): |
| 54 | |
| 55 | # Find a NetworkInterface with 'bmc' in the path. |
| 56 | if 'bmc' not in path: |
| 57 | continue |
| 58 | |
| 59 | # Only expecting a single service to implement |
| 60 | # NetworkInterface. Get the service connection |
| 61 | # from the mapper response |
| 62 | conn = info.keys()[0] |
| 63 | |
| 64 | # Get the inventory object implementing NetworkInterface. |
| 65 | obj = bus.get_object(conn, path) |
| 66 | |
| 67 | # Get the MAC address |
| 68 | mproxy = obj.get_dbus_method('Get', PROP_INTF_NAME) |
| 69 | return mproxy(INV_INTF_NAME, prop) |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 70 | |
| 71 | |
Dinesh Chinari | c47c6a6 | 2017-03-29 23:09:59 -0500 | [diff] [blame^] | 72 | # Get inventory UUID value. |
| 73 | def get_uuid(bus, prop): |
| 74 | mapper = obmc.mapper.Mapper(bus) |
| 75 | |
| 76 | # Get the inventory subtree, limited |
| 77 | # to objects that implement UUID. |
| 78 | resp = mapper.get_subtree( |
| 79 | path=INVENTORY_ROOT, |
| 80 | interfaces=[CHS_INTF_NAME]) |
| 81 | |
| 82 | # Only expecting a single object to implement UUID. |
| 83 | try: |
| 84 | path, info = resp.items()[0] |
| 85 | except IndexError as e: |
| 86 | return None |
| 87 | |
| 88 | # Only expecting a single service to implement |
| 89 | # UUID. Get the service connection |
| 90 | # from the mapper response |
| 91 | conn = info.keys()[0] |
| 92 | |
| 93 | # Get the inventory object implementing UUID. |
| 94 | obj = bus.get_object(conn, path) |
| 95 | |
| 96 | # Get the uuid |
| 97 | mproxy = obj.get_dbus_method('Get', PROP_INTF_NAME) |
| 98 | return mproxy(CHS_INTF_NAME, prop) |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 99 | |
| 100 | |
Adriana Kobylak | f9deaa0 | 2016-11-01 12:57:55 -0500 | [diff] [blame] | 101 | # Get the value of the mac on the system (from u-boot) without ':' separators |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 102 | def get_sys_mac(obj): |
| 103 | sys_mac = '' |
Adriana Kobylak | f9deaa0 | 2016-11-01 12:57:55 -0500 | [diff] [blame] | 104 | try: |
| 105 | sys_mac = subprocess.check_output(["fw_printenv", "-n", "ethaddr"]) |
| 106 | except: |
| 107 | # Handle when mac does not exist in u-boot |
| 108 | return sys_mac |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 109 | sys_mac = sys_mac.replace(":", "") |
| 110 | return sys_mac |
| 111 | |
| 112 | |
| 113 | # Replace the value of the system mac with the value of the inventory |
| 114 | # MAC if the system MAC is not locally administered because this means |
| 115 | # the system admin has purposely set the MAC |
| 116 | def sync_mac(obj, inv_mac, sys_mac): |
Adriana Kobylak | f9deaa0 | 2016-11-01 12:57:55 -0500 | [diff] [blame] | 117 | if sys_mac: |
| 118 | # Convert sys MAC to int to perform bitwise '&' |
| 119 | int_sys_mac = int(sys_mac, 16) |
| 120 | else: |
| 121 | # Set mac to 0 for when u-boot mac is not present |
| 122 | int_sys_mac = 0 |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 123 | if not int_sys_mac & MAC_LOCALLY_ADMIN_MASK: |
| 124 | # Sys MAC is not locally administered, go replace it with inv value |
| 125 | # Add the ':' separators |
| 126 | mac_str = ':'.join([inv_mac[i]+inv_mac[i+1] for i in range(0, 12, 2)]) |
| 127 | # The Set HW Method already has checking for mac format |
| 128 | dbus_method = obj.get_dbus_method("SetHwAddress", NET_DBUS_NAME) |
| 129 | dbus_method("eth0", mac_str) |
| 130 | |
| 131 | |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 132 | # Set sys uuid, this reboots the BMC for the value to take effect |
| 133 | def set_sys_uuid(uuid): |
| 134 | rc = subprocess.call(["fw_setenv", "uuid", uuid]) |
| 135 | if rc == 0: |
| 136 | print "Rebooting BMC to set uuid" |
| 137 | # TODO Uncomment once sync from u-boot to /etc/machine-id is in place |
| 138 | # Issue openbmc/openbmc#479 |
| 139 | # rc = subprocess.call(["reboot"]) |
| 140 | else: |
| 141 | print "Error setting uuid" |
| 142 | |
| 143 | if __name__ == '__main__': |
| 144 | arg = argparse.ArgumentParser() |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 145 | arg.add_argument('-p') |
| 146 | arg.add_argument('-s') |
| 147 | |
| 148 | opt = arg.parse_args() |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 149 | prop_name = opt.p |
| 150 | sync_type = opt.s |
| 151 | |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 152 | bus = dbus.SystemBus() |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 153 | if sync_type == "mac": |
Dinesh Chinari | c47c6a6 | 2017-03-29 23:09:59 -0500 | [diff] [blame^] | 154 | inv_mac = get_bmc_mac_address(bus, prop_name) |
| 155 | if inv_mac: |
| 156 | net_obj = bus.get_object(NET_DBUS_NAME, NET_OBJ_NAME) |
| 157 | sys_mac = get_sys_mac(net_obj) |
| 158 | if inv_mac != sys_mac: |
| 159 | sync_mac(net_obj, inv_mac, sys_mac) |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 160 | elif sync_type == "uuid": |
Dinesh Chinari | c47c6a6 | 2017-03-29 23:09:59 -0500 | [diff] [blame^] | 161 | inv_uuid = get_uuid(bus, prop_name) |
| 162 | if inv_uuid: |
| 163 | inv_uuid = uuid.UUID(inv_uuid) |
| 164 | chs_obj = bus.get_object(CHS_DBUS_NAME, CHS_OBJ_NAME) |
| 165 | chs_uuid = get_sys_uuid(chs_obj) |
| 166 | if inv_uuid != sys_uuid: |
| 167 | set_sys_uuid(inv_uuid) |
Brad Bishop | 5306675 | 2016-09-21 08:48:04 -0400 | [diff] [blame] | 168 | |
| 169 | # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |