Patrick Williams | 272b453 | 2022-12-04 15:20:26 -0600 | [diff] [blame^] | 1 | #!/bin/bash -eu |
Brad Bishop | 5a3ac18 | 2020-01-07 09:03:18 -0500 | [diff] [blame] | 2 | |
| 3 | show_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 | |
| 11 | sync_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 Williams | 272b453 | 2022-12-04 15:20:26 -0600 | [diff] [blame^] | 20 | ${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 Bishop | 5a3ac18 | 2020-01-07 09:03:18 -0500 | [diff] [blame] | 24 | |
Patrick Williams | 272b453 | 2022-12-04 15:20:26 -0600 | [diff] [blame^] | 25 | if [ "$NETWORK_ITEM_PATH_COUNT" -gt 1 ]; then |
Brad Bishop | 5a3ac18 | 2020-01-07 09:03:18 -0500 | [diff] [blame] | 26 | # 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 Williams | 272b453 | 2022-12-04 15:20:26 -0600 | [diff] [blame^] | 29 | ${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 Bishop | 5a3ac18 | 2020-01-07 09:03:18 -0500 | [diff] [blame] | 33 | else |
| 34 | NETWORK_ITEM_PATH=$(busctl --no-pager --verbose call \ |
Patrick Williams | 272b453 | 2022-12-04 15:20:26 -0600 | [diff] [blame^] | 35 | ${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 Bishop | 5a3ac18 | 2020-01-07 09:03:18 -0500 | [diff] [blame] | 39 | 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 Williams | 272b453 | 2022-12-04 15:20:26 -0600 | [diff] [blame^] | 46 | "${NETWORK_ITEM_PATH}" 2>/dev/null || true) |
Brad Bishop | 5a3ac18 | 2020-01-07 09:03:18 -0500 | [diff] [blame] | 47 | |
| 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 Williams | 272b453 | 2022-12-04 15:20:26 -0600 | [diff] [blame^] | 53 | MAC_ADDR=$(busctl get-property "${NETWORK_ITEM_SERVICE}" \ |
| 54 | "${NETWORK_ITEM_PATH}" "${NETWORK_ITEM_IFACE}" MACAddress) |
Brad Bishop | 5a3ac18 | 2020-01-07 09:03:18 -0500 | [diff] [blame] | 55 | |
| 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 Williams | 272b453 | 2022-12-04 15:20:26 -0600 | [diff] [blame^] | 62 | "/xyz/openbmc_project/network/$1" \ |
| 63 | xyz.openbmc_project.Network.MACAddress \ |
| 64 | MACAddress s "${MAC_ADDR}" |
Brad Bishop | 5a3ac18 | 2020-01-07 09:03:18 -0500 | [diff] [blame] | 65 | fi |
| 66 | } |
| 67 | |
| 68 | if [ $# -eq 0 ]; then |
| 69 | show_error 'No Ethernet interface name is given' |
| 70 | exit 1 |
| 71 | fi |
| 72 | |
Patrick Williams | 272b453 | 2022-12-04 15:20:26 -0600 | [diff] [blame^] | 73 | sync_mac "$1" |
Brad Bishop | 5a3ac18 | 2020-01-07 09:03:18 -0500 | [diff] [blame] | 74 | |
| 75 | # Prevent start at next boot time |
| 76 | mkdir -p "/var/lib/first-boot-set-mac" |
| 77 | touch "/var/lib/first-boot-set-mac/${1}" |