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' |
Ratan Gupta | f1470e9 | 2017-08-28 17:34:53 +0530 | [diff] [blame] | 35 | NETWORK_ROOT = '/xyz/openbmc_project/network' |
| 36 | ETHERNET_INTF_NAME = 'xyz.openbmc_project.Network.EthernetInterface' |
| 37 | MAC_INTF_NAME = 'xyz.openbmc_project.Network.MACAddress' |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 38 | |
Brad Bishop | 3d13c28 | 2016-09-21 08:07:47 -0400 | [diff] [blame] | 39 | FRUS = {} |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 40 | |
| 41 | # IEEE 802 MAC address mask for locally administered. |
| 42 | # This means the admin has set the MAC and is no longer |
| 43 | # the unique number set by the device manufacturer. |
| 44 | MAC_LOCALLY_ADMIN_MASK = 0x20000000000 |
| 45 | |
| 46 | |
Dinesh Chinari | c47c6a6 | 2017-03-29 23:09:59 -0500 | [diff] [blame] | 47 | # Get inventory MACAddress value. |
| 48 | def get_bmc_mac_address(bus, prop): |
| 49 | mapper = obmc.mapper.Mapper(bus) |
| 50 | |
| 51 | # Get the inventory subtree, limited |
| 52 | # to objects that implement NetworkInterface. |
| 53 | for path, info in \ |
| 54 | mapper.get_subtree( |
| 55 | path=INVENTORY_ROOT, |
| 56 | interfaces=[INV_INTF_NAME]).iteritems(): |
| 57 | |
| 58 | # Find a NetworkInterface with 'bmc' in the path. |
| 59 | if 'bmc' not in path: |
| 60 | continue |
| 61 | |
| 62 | # Only expecting a single service to implement |
| 63 | # NetworkInterface. Get the service connection |
| 64 | # from the mapper response |
| 65 | conn = info.keys()[0] |
| 66 | |
| 67 | # Get the inventory object implementing NetworkInterface. |
| 68 | obj = bus.get_object(conn, path) |
| 69 | |
| 70 | # Get the MAC address |
| 71 | mproxy = obj.get_dbus_method('Get', PROP_INTF_NAME) |
| 72 | return mproxy(INV_INTF_NAME, prop) |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 73 | |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 74 | |
Ratan Gupta | f1470e9 | 2017-08-28 17:34:53 +0530 | [diff] [blame] | 75 | # Get Network Interface object. |
| 76 | def get_network_interface_object(bus): |
| 77 | mapper = obmc.mapper.Mapper(bus) |
| 78 | |
| 79 | # Get the network subtree, limited |
| 80 | # to objects that implements EthernetInterface. |
| 81 | for path, info in \ |
| 82 | mapper.get_subtree( |
| 83 | path=NETWORK_ROOT, |
| 84 | interfaces=[ETHERNET_INTF_NAME]).iteritems(): |
| 85 | |
| 86 | # Find the one which is having physical interface,it may happen |
| 87 | # that vlan interface is there and we want the physical |
| 88 | # interface here. |
| 89 | if path.split('/')[-1].find('_') < 0: |
| 90 | service = info.keys()[0] |
| 91 | net_obj = bus.get_object(service, path) |
| 92 | return net_obj |
| 93 | |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 94 | |
Dinesh Chinari | c47c6a6 | 2017-03-29 23:09:59 -0500 | [diff] [blame] | 95 | # Get inventory UUID value. |
| 96 | def get_uuid(bus, prop): |
| 97 | mapper = obmc.mapper.Mapper(bus) |
| 98 | |
| 99 | # Get the inventory subtree, limited |
| 100 | # to objects that implement UUID. |
| 101 | resp = mapper.get_subtree( |
| 102 | path=INVENTORY_ROOT, |
| 103 | interfaces=[CHS_INTF_NAME]) |
| 104 | |
| 105 | # Only expecting a single object to implement UUID. |
| 106 | try: |
| 107 | path, info = resp.items()[0] |
| 108 | except IndexError as e: |
| 109 | return None |
| 110 | |
| 111 | # Only expecting a single service to implement |
| 112 | # UUID. Get the service connection |
| 113 | # from the mapper response |
| 114 | conn = info.keys()[0] |
| 115 | |
| 116 | # Get the inventory object implementing UUID. |
| 117 | obj = bus.get_object(conn, path) |
| 118 | |
| 119 | # Get the uuid |
| 120 | mproxy = obj.get_dbus_method('Get', PROP_INTF_NAME) |
| 121 | return mproxy(CHS_INTF_NAME, prop) |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 122 | |
| 123 | |
Adriana Kobylak | f9deaa0 | 2016-11-01 12:57:55 -0500 | [diff] [blame] | 124 | # 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] | 125 | def get_sys_mac(obj): |
| 126 | sys_mac = '' |
Adriana Kobylak | f9deaa0 | 2016-11-01 12:57:55 -0500 | [diff] [blame] | 127 | try: |
| 128 | sys_mac = subprocess.check_output(["fw_printenv", "-n", "ethaddr"]) |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 129 | except Exception: |
Adriana Kobylak | f9deaa0 | 2016-11-01 12:57:55 -0500 | [diff] [blame] | 130 | # Handle when mac does not exist in u-boot |
| 131 | return sys_mac |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 132 | return sys_mac |
| 133 | |
| 134 | |
| 135 | # Replace the value of the system mac with the value of the inventory |
| 136 | # MAC if the system MAC is not locally administered because this means |
| 137 | # the system admin has purposely set the MAC |
| 138 | def sync_mac(obj, inv_mac, sys_mac): |
Adriana Kobylak | f9deaa0 | 2016-11-01 12:57:55 -0500 | [diff] [blame] | 139 | if sys_mac: |
| 140 | # Convert sys MAC to int to perform bitwise '&' |
Ratan Gupta | f1470e9 | 2017-08-28 17:34:53 +0530 | [diff] [blame] | 141 | sys_mac = sys_mac.replace(":", "") |
Adriana Kobylak | f9deaa0 | 2016-11-01 12:57:55 -0500 | [diff] [blame] | 142 | int_sys_mac = int(sys_mac, 16) |
| 143 | else: |
| 144 | # Set mac to 0 for when u-boot mac is not present |
| 145 | int_sys_mac = 0 |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 146 | if not int_sys_mac & MAC_LOCALLY_ADMIN_MASK: |
| 147 | # Sys MAC is not locally administered, go replace it with inv value |
Ratan Gupta | f1470e9 | 2017-08-28 17:34:53 +0530 | [diff] [blame] | 148 | intf = dbus.Interface(obj, dbus.PROPERTIES_IFACE) |
| 149 | intf.Set(MAC_INTF_NAME, "MACAddress", inv_mac) |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 150 | |
| 151 | |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 152 | # Set sys uuid, this reboots the BMC for the value to take effect |
| 153 | def set_sys_uuid(uuid): |
| 154 | rc = subprocess.call(["fw_setenv", "uuid", uuid]) |
| 155 | if rc == 0: |
| 156 | print "Rebooting BMC to set uuid" |
| 157 | # TODO Uncomment once sync from u-boot to /etc/machine-id is in place |
| 158 | # Issue openbmc/openbmc#479 |
| 159 | # rc = subprocess.call(["reboot"]) |
| 160 | else: |
| 161 | print "Error setting uuid" |
| 162 | |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 163 | |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 164 | if __name__ == '__main__': |
| 165 | arg = argparse.ArgumentParser() |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 166 | arg.add_argument('-p') |
| 167 | arg.add_argument('-s') |
| 168 | |
| 169 | opt = arg.parse_args() |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 170 | prop_name = opt.p |
| 171 | sync_type = opt.s |
| 172 | |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 173 | bus = dbus.SystemBus() |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 174 | if sync_type == "mac": |
Ratan Gupta | f1470e9 | 2017-08-28 17:34:53 +0530 | [diff] [blame] | 175 | inv_mac = get_bmc_mac_address(bus, prop_name) |
| 176 | if not inv_mac: |
| 177 | sys.exit(1) |
| 178 | net_obj = get_network_interface_object(bus) |
| 179 | if not net_obj: |
| 180 | print "Unable to get the network object" |
| 181 | sys.exit(1) |
| 182 | sys_mac = get_sys_mac(net_obj) |
| 183 | if inv_mac != sys_mac: |
| 184 | print "Inventory MAC=%s,System MAC=%s" % (inv_mac, sys_mac) |
| 185 | sync_mac(net_obj, inv_mac, sys_mac) |
Adriana Kobylak | fb4d225 | 2016-06-29 21:44:30 -0500 | [diff] [blame] | 186 | elif sync_type == "uuid": |
Dinesh Chinari | c47c6a6 | 2017-03-29 23:09:59 -0500 | [diff] [blame] | 187 | inv_uuid = get_uuid(bus, prop_name) |
| 188 | if inv_uuid: |
| 189 | inv_uuid = uuid.UUID(inv_uuid) |
| 190 | chs_obj = bus.get_object(CHS_DBUS_NAME, CHS_OBJ_NAME) |
| 191 | chs_uuid = get_sys_uuid(chs_obj) |
| 192 | if inv_uuid != sys_uuid: |
| 193 | set_sys_uuid(inv_uuid) |
Brad Bishop | 5306675 | 2016-09-21 08:48:04 -0400 | [diff] [blame] | 194 | |
| 195 | # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |