xref: /openbmc/phosphor-misc/firstboot/first-boot-set-mac.sh (revision 37c76da7c277f3531f35ec726ac690e53ab4915a)
1#!/bin/bash -eu
2
3function 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
11function 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 \
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)
24
25    if [ "$NETWORK_ITEM_PATH_COUNT" -gt 1 ]; then
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 \
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)
33    else
34        NETWORK_ITEM_PATH=$(busctl --no-pager --verbose call \
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)
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 \
46        "${NETWORK_ITEM_PATH}" 2>/dev/null || true)
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
53    MAC_ADDR=$(busctl get-property "${NETWORK_ITEM_SERVICE}" \
54        "${NETWORK_ITEM_PATH}" "${NETWORK_ITEM_IFACE}" MACAddress)
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 \
62            "/xyz/openbmc_project/network/$1" \
63            xyz.openbmc_project.Network.MACAddress \
64            MACAddress s "${MAC_ADDR}"
65    fi
66}
67
68if [ $# -eq 0 ]; then
69    show_error 'No Ethernet interface name is given'
70    exit 1
71fi
72
73sync_mac "$1"
74
75# Prevent start at next boot time
76mkdir -p "/var/lib/first-boot-set-mac"
77touch "/var/lib/first-boot-set-mac/${1}"
78