1#!/bin/bash -eu 2 3function show_error() { 4 if [ -n "${JOURNAL_STREAM-}" ]; then 5 echo "$@" | systemd-cat -t first-boot-set-hostname -p emerg 6 else 7 echo "$@" >&2 8 fi 9} 10 11function sync_hostname() { 12 MAPPER_IFACE='xyz.openbmc_project.ObjectMapper' 13 MAPPER_PATH='/xyz/openbmc_project/object_mapper' 14 INVENTORY_PATH='/xyz/openbmc_project/inventory' 15 16 BMC_ITEM_IFACE='xyz.openbmc_project.Inventory.Item.Bmc' 17 INV_ASSET_IFACE='xyz.openbmc_project.Inventory.Decorator.Asset' 18 BMC_SN='' 19 BMC_ITEM_PATH=$(busctl --no-pager --verbose call \ 20 ${MAPPER_IFACE} ${MAPPER_PATH} ${MAPPER_IFACE} \ 21 GetSubTree sias \ 22 ${INVENTORY_PATH} 0 1 ${BMC_ITEM_IFACE} \ 23 2>/dev/null | grep ${INVENTORY_PATH} || true) 24 25 # ' STRING "/xyz/openbmc_project/inventory/system/chassis/bmc";' 26 BMC_ITEM_PATH=${BMC_ITEM_PATH#*\"} 27 BMC_ITEM_PATH=${BMC_ITEM_PATH%\"*} 28 29 BMC_ITEM_SERVICE=$(mapper get-service \ 30 "${BMC_ITEM_PATH}" 2>/dev/null || true) 31 32 if [[ -n "${BMC_ITEM_SERVICE}" ]]; then 33 BMC_SN=$(busctl get-property "${BMC_ITEM_SERVICE}" \ 34 "${BMC_ITEM_PATH}" "${INV_ASSET_IFACE}" SerialNumber) 35 # 's "002B0DH1000"' 36 BMC_SN=${BMC_SN#*\"} 37 BMC_SN=${BMC_SN%\"*} 38 else 39 show_error "No BMC item found in the Inventory. Is VPD EEPROM empty?" 40 fi 41 42 if [[ -z "${BMC_SN}" ]] ; then 43 show_error "BMC Serial Number empty! Setting Hostname as 'hostname + mac address' " 44 45 NETWORK_ITEM_IFACE='xyz.openbmc_project.Inventory.Item.NetworkInterface' 46 NETWORK_ITEM_PATH=$(busctl --no-pager --verbose call \ 47 ${MAPPER_IFACE} ${MAPPER_PATH} ${MAPPER_IFACE} \ 48 GetSubTree sias \ 49 ${INVENTORY_PATH} 0 1 ${NETWORK_ITEM_IFACE} \ 50 2>/dev/null | grep ${INVENTORY_PATH} || true) 51 52 NETWORK_ITEM_PATH=${NETWORK_ITEM_PATH#*\"} 53 NETWORK_ITEM_PATH=${NETWORK_ITEM_PATH%\"*} 54 55 NETWORK_ITEM_OBJ=$(mapper get-service "${NETWORK_ITEM_PATH}" 2>/dev/null || true) 56 57 if [[ -z "${NETWORK_ITEM_OBJ}" ]]; then 58 show_error 'No Ethernet interface found in the Inventory. Unique hostname not set!' 59 exit 1 60 fi 61 62 MAC_ADDR=$(busctl get-property "${NETWORK_ITEM_OBJ}" \ 63 "${NETWORK_ITEM_PATH}" "${NETWORK_ITEM_IFACE}" MACAddress) 64 65 # 's "54:52:01:02:03:04"' 66 MAC_ADDR=${MAC_ADDR#*\"} 67 MAC_ADDR=${MAC_ADDR%\"*} 68 69 hostnamectl set-hostname "$(hostname)-${MAC_ADDR}" 70 else 71 hostnamectl set-hostname "$(hostname)-${BMC_SN}" 72 fi 73 74} 75 76sync_hostname 77 78# Prevent start at next boot time 79touch "/var/lib/first-boot-set-hostname" 80