blob: 42a85b873494fb5ca860ff45befc6ec6576e30f4 [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'
15 BMC_ITEM_IFACE='xyz.openbmc_project.Inventory.Item.Bmc'
asmithakarun57f0a3e2019-06-26 02:31:07 -050016 INV_ASSET_IFACE='xyz.openbmc_project.Inventory.Decorator.Asset'
Alexander Filippov89bd3842019-05-22 11:07:30 +030017
18 BMC_ITEM_PATH=$(busctl --no-pager --verbose call \
19 ${MAPPER_IFACE} \
20 ${MAPPER_PATH} \
21 ${MAPPER_IFACE} \
22 GetSubTree sias \
23 ${INVENTORY_PATH} 0 1 ${BMC_ITEM_IFACE} \
24 2>/dev/null | grep ${INVENTORY_PATH} || true)
25
26 # ' STRING "/xyz/openbmc_project/inventory/system/chassis/bmc";'
27 BMC_ITEM_PATH=${BMC_ITEM_PATH#*\"}
28 BMC_ITEM_PATH=${BMC_ITEM_PATH%\"*}
29
30 BMC_ITEM_SERVICE=$(mapper get-service \
31 ${BMC_ITEM_PATH} 2>/dev/null || true)
32
33 if [[ -z "${BMC_ITEM_SERVICE}" ]]; then
Alexander Filippovc2102a42019-06-10 18:44:59 +030034 show_error "No BMC item found in the Inventory. Is VPD EEPROM empty?"
Alexander Filippov89bd3842019-05-22 11:07:30 +030035 return
36 fi
37
38 BMC_SN=$(busctl get-property ${BMC_ITEM_SERVICE} \
39 ${BMC_ITEM_PATH} \
asmithakarun57f0a3e2019-06-26 02:31:07 -050040 ${INV_ASSET_IFACE} SerialNumber)
Alexander Filippov89bd3842019-05-22 11:07:30 +030041 # 's "002B0DH1000"'
42 BMC_SN=${BMC_SN#*\"}
43 BMC_SN=${BMC_SN%\"*}
44
45 hostnamectl set-hostname {MACHINE}-${BMC_SN}
46}
47
48[ "$(hostname)" = "{MACHINE}" ] && sync_hostname
49
50systemctl disable first-boot-set-hostname.service