blob: 51d2f043a0374170af10374427cccce983bd8145 [file] [log] [blame]
Patrick Williams272b4532022-12-04 15:20:26 -06001#!/bin/bash -eu
Brad Bishop403e2632020-01-07 09:44:35 -05002
Patrick Williams37c76da2022-12-08 06:23:48 -06003function show_error() {
Brad Bishop403e2632020-01-07 09:44:35 -05004 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
Patrick Williams37c76da2022-12-08 06:23:48 -060011function sync_hostname() {
Brad Bishop403e2632020-01-07 09:44:35 -050012 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 \
Patrick Williams272b4532022-12-04 15:20:26 -060020 ${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)
Brad Bishop403e2632020-01-07 09:44:35 -050024
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 \
Patrick Williams272b4532022-12-04 15:20:26 -060030 "${BMC_ITEM_PATH}" 2>/dev/null || true)
Brad Bishop403e2632020-01-07 09:44:35 -050031
32 if [[ -n "${BMC_ITEM_SERVICE}" ]]; then
Patrick Williams272b4532022-12-04 15:20:26 -060033 BMC_SN=$(busctl get-property "${BMC_ITEM_SERVICE}" \
34 "${BMC_ITEM_PATH}" "${INV_ASSET_IFACE}" SerialNumber)
Brad Bishop403e2632020-01-07 09:44:35 -050035 # '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 \
Patrick Williams272b4532022-12-04 15:20:26 -060047 ${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)
Brad Bishop403e2632020-01-07 09:44:35 -050051
52 NETWORK_ITEM_PATH=${NETWORK_ITEM_PATH#*\"}
53 NETWORK_ITEM_PATH=${NETWORK_ITEM_PATH%\"*}
54
Patrick Williams272b4532022-12-04 15:20:26 -060055 NETWORK_ITEM_OBJ=$(mapper get-service "${NETWORK_ITEM_PATH}" 2>/dev/null || true)
Brad Bishop403e2632020-01-07 09:44:35 -050056
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
Patrick Williams272b4532022-12-04 15:20:26 -060062 MAC_ADDR=$(busctl get-property "${NETWORK_ITEM_OBJ}" \
63 "${NETWORK_ITEM_PATH}" "${NETWORK_ITEM_IFACE}" MACAddress)
Brad Bishop403e2632020-01-07 09:44:35 -050064
65 # 's "54:52:01:02:03:04"'
66 MAC_ADDR=${MAC_ADDR#*\"}
67 MAC_ADDR=${MAC_ADDR%\"*}
68
Patrick Williams272b4532022-12-04 15:20:26 -060069 hostnamectl set-hostname "$(hostname)-${MAC_ADDR}"
Brad Bishop403e2632020-01-07 09:44:35 -050070 else
Patrick Williams272b4532022-12-04 15:20:26 -060071 hostnamectl set-hostname "$(hostname)-${BMC_SN}"
Brad Bishop403e2632020-01-07 09:44:35 -050072 fi
73
74}
75
76sync_hostname
77
78# Prevent start at next boot time
79touch "/var/lib/first-boot-set-hostname"