Alexander Filippov | 126f667 | 2018-11-28 11:32:04 +0300 | [diff] [blame] | 1 | #!/bin/sh -eu |
| 2 | |
| 3 | show_error() { |
Alexander Filippov | c2102a4 | 2019-06-10 18:44:59 +0300 | [diff] [blame] | 4 | if [ -n "${JOURNAL_STREAM-}" ]; then |
| 5 | echo "$@" | systemd-cat -t first-boot-set-mac -p emerg |
| 6 | else |
| 7 | echo "$@" >&2 |
| 8 | fi |
Alexander Filippov | 126f667 | 2018-11-28 11:32:04 +0300 | [diff] [blame] | 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 | |
Alvin Wang | bca48ea | 2019-08-28 15:18:15 +0800 | [diff] [blame] | 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 ${INVENTORY_PATH} | wc -l || 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 \ |
Alexander Filippov | 126f667 | 2018-11-28 11:32:04 +0300 | [diff] [blame] | 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) |
Alvin Wang | bca48ea | 2019-08-28 15:18:15 +0800 | [diff] [blame] | 45 | fi |
Alexander Filippov | 126f667 | 2018-11-28 11:32:04 +0300 | [diff] [blame] | 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 | |
Alexander Filippov | c2102a4 | 2019-06-10 18:44:59 +0300 | [diff] [blame] | 75 | if [ $# -eq 0 ]; then |
| 76 | show_error 'No Ethernet interface name is given' |
Alexander Filippov | 126f667 | 2018-11-28 11:32:04 +0300 | [diff] [blame] | 77 | exit 1 |
| 78 | fi |
| 79 | |
| 80 | sync_mac $1 |
| 81 | |
Alexander Filippov | 2e78e09 | 2019-10-18 15:10:30 +0300 | [diff] [blame] | 82 | # Prevent start at next boot time |
| 83 | mkdir -p "/var/lib/first-boot-set-mac" |
| 84 | touch "/var/lib/first-boot-set-mac/${1}" |