blob: 5879fed126646ddde83c9528f2137e3c7e9b79a8 [file] [log] [blame]
Alexander Filippov89bd3842019-05-22 11:07:30 +03001#!/bin/sh -eu
2
3show_error() {
Alexander Filippovc2102a42019-06-10 18:44:59 +03004 if [ -n "${JOURNAL_STREAM-}" ]; then
5 echo "$@" | systemd-cat -t first-boot-set-hostname -p emerg
6 else
7 echo "$@" >&2
8 fi
Alexander Filippov89bd3842019-05-22 11:07:30 +03009}
10
11sync_hostname() {
12 MAPPER_IFACE='xyz.openbmc_project.ObjectMapper'
13 MAPPER_PATH='/xyz/openbmc_project/object_mapper'
14 INVENTORY_PATH='/xyz/openbmc_project/inventory'
asmithakarun173965e2019-06-26 02:04:53 -050015
Alexander Filippov89bd3842019-05-22 11:07:30 +030016 BMC_ITEM_IFACE='xyz.openbmc_project.Inventory.Item.Bmc'
asmithakarun57f0a3e2019-06-26 02:31:07 -050017 INV_ASSET_IFACE='xyz.openbmc_project.Inventory.Decorator.Asset'
asmithakarun173965e2019-06-26 02:04:53 -050018 BMC_SN=''
Alexander Filippov89bd3842019-05-22 11:07:30 +030019 BMC_ITEM_PATH=$(busctl --no-pager --verbose call \
20 ${MAPPER_IFACE} \
21 ${MAPPER_PATH} \
22 ${MAPPER_IFACE} \
23 GetSubTree sias \
24 ${INVENTORY_PATH} 0 1 ${BMC_ITEM_IFACE} \
25 2>/dev/null | grep ${INVENTORY_PATH} || true)
26
27 # ' STRING "/xyz/openbmc_project/inventory/system/chassis/bmc";'
28 BMC_ITEM_PATH=${BMC_ITEM_PATH#*\"}
29 BMC_ITEM_PATH=${BMC_ITEM_PATH%\"*}
30
31 BMC_ITEM_SERVICE=$(mapper get-service \
Patrick Williams42347492023-04-14 14:51:53 -050032 "${BMC_ITEM_PATH}" 2>/dev/null || true)
Alexander Filippov89bd3842019-05-22 11:07:30 +030033
Patrick Williams42347492023-04-14 14:51:53 -050034 if [ -n "${BMC_ITEM_SERVICE}" ]; then
35 BMC_SN=$(busctl get-property "${BMC_ITEM_SERVICE}" \
36 "${BMC_ITEM_PATH}" \
asmithakarun57f0a3e2019-06-26 02:31:07 -050037 ${INV_ASSET_IFACE} SerialNumber)
asmithakarun173965e2019-06-26 02:04:53 -050038 # 's "002B0DH1000"'
39 BMC_SN=${BMC_SN#*\"}
40 BMC_SN=${BMC_SN%\"*}
41 else
42 show_error "No BMC item found in the Inventory. Is VPD EEPROM empty?"
43 fi
Alexander Filippov89bd3842019-05-22 11:07:30 +030044
Patrick Williams42347492023-04-14 14:51:53 -050045 if [ -z "${BMC_SN}" ] ; then
asmithakarun173965e2019-06-26 02:04:53 -050046 show_error "BMC Serial Number empty! Setting Hostname as 'hostname + mac address' "
47
48 NETWORK_ITEM_IFACE='xyz.openbmc_project.Inventory.Item.NetworkInterface'
49 NETWORK_ITEM_PATH=$(busctl --no-pager --verbose call \
50 ${MAPPER_IFACE} \
51 ${MAPPER_PATH} \
52 ${MAPPER_IFACE} \
53 GetSubTree sias \
54 ${INVENTORY_PATH} 0 1 ${NETWORK_ITEM_IFACE} \
55 2>/dev/null | grep ${INVENTORY_PATH} || true)
56
57 NETWORK_ITEM_PATH=${NETWORK_ITEM_PATH#*\"}
58 NETWORK_ITEM_PATH=${NETWORK_ITEM_PATH%\"*}
59
Patrick Williams42347492023-04-14 14:51:53 -050060 NETWORK_ITEM_OBJ=$(mapper get-service "${NETWORK_ITEM_PATH}" 2>/dev/null || true)
asmithakarun173965e2019-06-26 02:04:53 -050061
Patrick Williams42347492023-04-14 14:51:53 -050062 if [ -z "${NETWORK_ITEM_OBJ}" ]; then
asmithakarun173965e2019-06-26 02:04:53 -050063 show_error 'No Ethernet interface found in the Inventory. Unique hostname not set!'
64 exit 1
65 fi
66
Patrick Williams42347492023-04-14 14:51:53 -050067 MAC_ADDR=$(busctl get-property "${NETWORK_ITEM_OBJ}" \
68 "${NETWORK_ITEM_PATH}" \
69 "${NETWORK_ITEM_IFACE}" MACAddress)
asmithakarun173965e2019-06-26 02:04:53 -050070
71 # 's "54:52:01:02:03:04"'
72 MAC_ADDR=${MAC_ADDR#*\"}
73 MAC_ADDR=${MAC_ADDR%\"*}
74
Patrick Williams42347492023-04-14 14:51:53 -050075 hostnamectl set-hostname "$(hostname)-${MAC_ADDR}"
asmithakarun173965e2019-06-26 02:04:53 -050076 else
Patrick Williams42347492023-04-14 14:51:53 -050077 hostnamectl set-hostname "$(hostname)-${BMC_SN}"
asmithakarun173965e2019-06-26 02:04:53 -050078 fi
79
Alexander Filippov89bd3842019-05-22 11:07:30 +030080}
81
asmithakarun173965e2019-06-26 02:04:53 -050082sync_hostname
Alexander Filippov89bd3842019-05-22 11:07:30 +030083
Alexander Filippov2e78e092019-10-18 15:10:30 +030084# Prevent start at next boot time
85touch "/var/lib/first-boot-set-hostname"