blob: cf67f8b477161fa6abedd9df13e7765e058be440 [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'
16
17 BMC_ITEM_PATH=$(busctl --no-pager --verbose call \
18 ${MAPPER_IFACE} \
19 ${MAPPER_PATH} \
20 ${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 [[ -z "${BMC_ITEM_SERVICE}" ]]; then
Alexander Filippovc2102a42019-06-10 18:44:59 +030033 show_error "No BMC item found in the Inventory. Is VPD EEPROM empty?"
Alexander Filippov89bd3842019-05-22 11:07:30 +030034 return
35 fi
36
37 BMC_SN=$(busctl get-property ${BMC_ITEM_SERVICE} \
38 ${BMC_ITEM_PATH} \
39 ${BMC_ITEM_IFACE} SerialNumber)
40 # 's "002B0DH1000"'
41 BMC_SN=${BMC_SN#*\"}
42 BMC_SN=${BMC_SN%\"*}
43
44 hostnamectl set-hostname {MACHINE}-${BMC_SN}
45}
46
47[ "$(hostname)" = "{MACHINE}" ] && sync_hostname
48
49systemctl disable first-boot-set-hostname.service