blob: 1a1ae22d3718eb45e4fb06ed811904513db9e7dd [file] [log] [blame]
Patrick Williams272b4532022-12-04 15:20:26 -06001#!/bin/bash -eu
Brad Bishop5a3ac182020-01-07 09:03:18 -05002
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 \
Patrick Williams272b4532022-12-04 15:20:26 -060020 ${MAPPER_IFACE} ${MAPPER_PATH} ${MAPPER_IFACE} \
21 GetSubTree sias \
22 ${INVENTORY_PATH} 0 1 ${NETWORK_ITEM_IFACE} \
23 2>/dev/null | grep -c "${INVENTORY_PATH}" || true)
Brad Bishop5a3ac182020-01-07 09:03:18 -050024
Patrick Williams272b4532022-12-04 15:20:26 -060025 if [ "$NETWORK_ITEM_PATH_COUNT" -gt 1 ]; then
Brad Bishop5a3ac182020-01-07 09:03:18 -050026 # If there are more than 2 NETOWRK ITEM and path must contain $1
27 # for finding the right NETWORK ITEM
28 NETWORK_ITEM_PATH=$(busctl --no-pager --verbose call \
Patrick Williams272b4532022-12-04 15:20:26 -060029 ${MAPPER_IFACE} ${MAPPER_PATH} ${MAPPER_IFACE} \
30 GetSubTree sias \
31 ${INVENTORY_PATH} 0 1 ${NETWORK_ITEM_IFACE} \
32 2>/dev/null | grep ${INVENTORY_PATH} | grep "$1" || true)
Brad Bishop5a3ac182020-01-07 09:03:18 -050033 else
34 NETWORK_ITEM_PATH=$(busctl --no-pager --verbose call \
Patrick Williams272b4532022-12-04 15:20:26 -060035 ${MAPPER_IFACE} ${MAPPER_PATH} ${MAPPER_IFACE} \
36 GetSubTree sias \
37 ${INVENTORY_PATH} 0 1 ${NETWORK_ITEM_IFACE} \
38 2>/dev/null | grep ${INVENTORY_PATH} || true)
Brad Bishop5a3ac182020-01-07 09:03:18 -050039 fi
40
41 # ' STRING "/xyz/openbmc_project/inventory/system/chassis/ethernet";'
42 NETWORK_ITEM_PATH=${NETWORK_ITEM_PATH#*\"}
43 NETWORK_ITEM_PATH=${NETWORK_ITEM_PATH%\"*}
44
45 NETWORK_ITEM_SERVICE=$(mapper get-service \
Patrick Williams272b4532022-12-04 15:20:26 -060046 "${NETWORK_ITEM_PATH}" 2>/dev/null || true)
Brad Bishop5a3ac182020-01-07 09:03:18 -050047
48 if [[ -z "${NETWORK_ITEM_SERVICE}" ]]; then
49 show_error 'No Ethernet interface found in the Inventory. Is VPD EEPROM empty?'
50 return
51 fi
52
Patrick Williams272b4532022-12-04 15:20:26 -060053 MAC_ADDR=$(busctl get-property "${NETWORK_ITEM_SERVICE}" \
54 "${NETWORK_ITEM_PATH}" "${NETWORK_ITEM_IFACE}" MACAddress)
Brad Bishop5a3ac182020-01-07 09:03:18 -050055
56 # 's "54:52:01:02:03:04"'
57 MAC_ADDR=${MAC_ADDR#*\"}
58 MAC_ADDR=${MAC_ADDR%\"*}
59
60 if [[ -n "${MAC_ADDR}" ]]; then
61 busctl set-property xyz.openbmc_project.Network \
Patrick Williams272b4532022-12-04 15:20:26 -060062 "/xyz/openbmc_project/network/$1" \
63 xyz.openbmc_project.Network.MACAddress \
64 MACAddress s "${MAC_ADDR}"
Brad Bishop5a3ac182020-01-07 09:03:18 -050065 fi
66}
67
68if [ $# -eq 0 ]; then
69 show_error 'No Ethernet interface name is given'
70 exit 1
71fi
72
Patrick Williams272b4532022-12-04 15:20:26 -060073sync_mac "$1"
Brad Bishop5a3ac182020-01-07 09:03:18 -050074
75# Prevent start at next boot time
76mkdir -p "/var/lib/first-boot-set-mac"
77touch "/var/lib/first-boot-set-mac/${1}"