blob: eaabb0a171954862573e49743d455067182b303c [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 \
32 ${BMC_ITEM_PATH} 2>/dev/null || true)
33
asmithakarun173965e2019-06-26 02:04:53 -050034 if [[ -n "${BMC_ITEM_SERVICE}" ]]; then
35 BMC_SN=$(busctl get-property ${BMC_ITEM_SERVICE} \
Alexander Filippov89bd3842019-05-22 11:07:30 +030036 ${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
asmithakarun173965e2019-06-26 02:04:53 -050045 if [[ -z "${BMC_SN}" ]] ; then
46 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
60 NETWORK_ITEM_OBJ=$(mapper get-service ${NETWORK_ITEM_PATH} 2>/dev/null || true)
61
62 if [[ -z "${NETWORK_ITEM_OBJ}" ]]; then
63 show_error 'No Ethernet interface found in the Inventory. Unique hostname not set!'
64 exit 1
65 fi
66
67 MAC_ADDR=$(busctl get-property ${NETWORK_ITEM_OBJ} \
68 ${NETWORK_ITEM_PATH} \
69 ${NETWORK_ITEM_IFACE} MACAddress)
70
71 # 's "54:52:01:02:03:04"'
72 MAC_ADDR=${MAC_ADDR#*\"}
73 MAC_ADDR=${MAC_ADDR%\"*}
74
75 hostnamectl set-hostname $(hostname)-${MAC_ADDR}
76 else
77 hostnamectl set-hostname $(hostname)-${BMC_SN}
78 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"