1#!/bin/sh -eu 2 3show_error() { 4 if [ -n "${JOURNAL_STREAM-}" ]; then 5 echo "$@" | systemd-cat -t first-boot-set-mac -p emerg 6 else 7 echo "$@" >&2 8 fi 9} 10 11sync_mac() { 12 13 MAPPER_IFACE='xyz.openbmc_project.ObjectMapper' 14 MAPPER_PATH='/xyz/openbmc_project/object_mapper' 15 INVENTORY_PATH='/xyz/openbmc_project/inventory' 16 NETWORK_ITEM_IFACE='xyz.openbmc_project.Inventory.Item.NetworkInterface' 17 18 # Get the NETWORK ITEM count 19 NETWORK_ITEM_PATH_COUNT=$(busctl --no-pager --verbose call \ 20 ${MAPPER_IFACE} \ 21 ${MAPPER_PATH} \ 22 ${MAPPER_IFACE} \ 23 GetSubTree sias \ 24 ${INVENTORY_PATH} 0 1 ${NETWORK_ITEM_IFACE} \ 25 2>/dev/null | grep -c ${INVENTORY_PATH} || true) 26 27 if [ "$NETWORK_ITEM_PATH_COUNT" -gt 1 ]; then 28 # If there are more than 2 NETOWRK ITEM and path must contain $1 29 # for finding the right NETWORK ITEM 30 NETWORK_ITEM_PATH=$(busctl --no-pager --verbose call \ 31 ${MAPPER_IFACE} \ 32 ${MAPPER_PATH} \ 33 ${MAPPER_IFACE} \ 34 GetSubTree sias \ 35 ${INVENTORY_PATH} 0 1 ${NETWORK_ITEM_IFACE} \ 36 2>/dev/null | grep ${INVENTORY_PATH} | grep "$1" || true) 37 else 38 NETWORK_ITEM_PATH=$(busctl --no-pager --verbose call \ 39 ${MAPPER_IFACE} \ 40 ${MAPPER_PATH} \ 41 ${MAPPER_IFACE} \ 42 GetSubTree sias \ 43 ${INVENTORY_PATH} 0 1 ${NETWORK_ITEM_IFACE} \ 44 2>/dev/null | grep ${INVENTORY_PATH} || true) 45 fi 46 47 # ' STRING "/xyz/openbmc_project/inventory/system/chassis/ethernet";' 48 NETWORK_ITEM_PATH=${NETWORK_ITEM_PATH#*\"} 49 NETWORK_ITEM_PATH=${NETWORK_ITEM_PATH%\"*} 50 51 NETWORK_ITEM_SERVICE=$(mapper get-service \ 52 "${NETWORK_ITEM_PATH}" 2>/dev/null || true) 53 54 if [ -z "${NETWORK_ITEM_SERVICE}" ]; then 55 show_error 'No Ethernet interface found in the Inventory. Is VPD EEPROM empty?' 56 return 57 fi 58 59 MAC_ADDR=$(busctl get-property "${NETWORK_ITEM_SERVICE}" \ 60 "${NETWORK_ITEM_PATH}" \ 61 "${NETWORK_ITEM_IFACE}" MACAddress) 62 63 # 's "54:52:01:02:03:04"' 64 MAC_ADDR=${MAC_ADDR#*\"} 65 MAC_ADDR=${MAC_ADDR%\"*} 66 67 if [ -n "${MAC_ADDR}" ]; then 68 busctl set-property xyz.openbmc_project.Network \ 69 "/xyz/openbmc_project/network/$1" \ 70 xyz.openbmc_project.Network.MACAddress \ 71 MACAddress s "${MAC_ADDR}" 72 fi 73} 74 75if [ $# -eq 0 ]; then 76 show_error 'No Ethernet interface name is given' 77 exit 1 78fi 79 80sync_mac "$1" 81 82# Prevent start at next boot time 83mkdir -p "/var/lib/first-boot-set-mac" 84touch "/var/lib/first-boot-set-mac/${1}" 85