xref: /openbmc/phosphor-misc/firstboot/first-boot-set-hostname.sh (revision 37c76da7c277f3531f35ec726ac690e53ab4915a)
1272b453cSPatrick Williams#!/bin/bash -eu
2403e2635SBrad Bishop
3*37c76da7SPatrick Williamsfunction show_error() {
4403e2635SBrad Bishop    if [ -n "${JOURNAL_STREAM-}" ]; then
5403e2635SBrad Bishop        echo "$@" | systemd-cat -t first-boot-set-hostname -p emerg
6403e2635SBrad Bishop    else
7403e2635SBrad Bishop        echo "$@" >&2
8403e2635SBrad Bishop    fi
9403e2635SBrad Bishop}
10403e2635SBrad Bishop
11*37c76da7SPatrick Williamsfunction sync_hostname() {
12403e2635SBrad Bishop    MAPPER_IFACE='xyz.openbmc_project.ObjectMapper'
13403e2635SBrad Bishop    MAPPER_PATH='/xyz/openbmc_project/object_mapper'
14403e2635SBrad Bishop    INVENTORY_PATH='/xyz/openbmc_project/inventory'
15403e2635SBrad Bishop
16403e2635SBrad Bishop    BMC_ITEM_IFACE='xyz.openbmc_project.Inventory.Item.Bmc'
17403e2635SBrad Bishop    INV_ASSET_IFACE='xyz.openbmc_project.Inventory.Decorator.Asset'
18403e2635SBrad Bishop    BMC_SN=''
19403e2635SBrad Bishop    BMC_ITEM_PATH=$(busctl --no-pager --verbose call \
20272b453cSPatrick Williams            ${MAPPER_IFACE} ${MAPPER_PATH} ${MAPPER_IFACE} \
21403e2635SBrad Bishop            GetSubTree sias \
22403e2635SBrad Bishop            ${INVENTORY_PATH} 0 1 ${BMC_ITEM_IFACE} \
23403e2635SBrad Bishop        2>/dev/null | grep ${INVENTORY_PATH} || true)
24403e2635SBrad Bishop
25403e2635SBrad Bishop    # '     STRING "/xyz/openbmc_project/inventory/system/chassis/bmc";'
26403e2635SBrad Bishop    BMC_ITEM_PATH=${BMC_ITEM_PATH#*\"}
27403e2635SBrad Bishop    BMC_ITEM_PATH=${BMC_ITEM_PATH%\"*}
28403e2635SBrad Bishop
29403e2635SBrad Bishop    BMC_ITEM_SERVICE=$(mapper get-service \
30272b453cSPatrick Williams        "${BMC_ITEM_PATH}" 2>/dev/null || true)
31403e2635SBrad Bishop
32403e2635SBrad Bishop    if [[ -n "${BMC_ITEM_SERVICE}" ]]; then
33272b453cSPatrick Williams        BMC_SN=$(busctl get-property "${BMC_ITEM_SERVICE}" \
34272b453cSPatrick Williams            "${BMC_ITEM_PATH}" "${INV_ASSET_IFACE}" SerialNumber)
35403e2635SBrad Bishop        # 's "002B0DH1000"'
36403e2635SBrad Bishop        BMC_SN=${BMC_SN#*\"}
37403e2635SBrad Bishop        BMC_SN=${BMC_SN%\"*}
38403e2635SBrad Bishop    else
39403e2635SBrad Bishop        show_error "No BMC item found in the Inventory. Is VPD EEPROM empty?"
40403e2635SBrad Bishop    fi
41403e2635SBrad Bishop
42403e2635SBrad Bishop    if [[ -z "${BMC_SN}" ]] ; then
43403e2635SBrad Bishop        show_error "BMC Serial Number empty! Setting Hostname as 'hostname + mac address' "
44403e2635SBrad Bishop
45403e2635SBrad Bishop        NETWORK_ITEM_IFACE='xyz.openbmc_project.Inventory.Item.NetworkInterface'
46403e2635SBrad Bishop        NETWORK_ITEM_PATH=$(busctl --no-pager --verbose call \
47272b453cSPatrick Williams                ${MAPPER_IFACE} ${MAPPER_PATH} ${MAPPER_IFACE} \
48403e2635SBrad Bishop                GetSubTree sias \
49403e2635SBrad Bishop                ${INVENTORY_PATH} 0 1 ${NETWORK_ITEM_IFACE} \
50403e2635SBrad Bishop            2>/dev/null | grep ${INVENTORY_PATH} || true)
51403e2635SBrad Bishop
52403e2635SBrad Bishop        NETWORK_ITEM_PATH=${NETWORK_ITEM_PATH#*\"}
53403e2635SBrad Bishop        NETWORK_ITEM_PATH=${NETWORK_ITEM_PATH%\"*}
54403e2635SBrad Bishop
55272b453cSPatrick Williams        NETWORK_ITEM_OBJ=$(mapper get-service "${NETWORK_ITEM_PATH}" 2>/dev/null || true)
56403e2635SBrad Bishop
57403e2635SBrad Bishop        if [[ -z "${NETWORK_ITEM_OBJ}" ]]; then
58403e2635SBrad Bishop            show_error 'No Ethernet interface found in the Inventory. Unique hostname not set!'
59403e2635SBrad Bishop            exit 1
60403e2635SBrad Bishop        fi
61403e2635SBrad Bishop
62272b453cSPatrick Williams        MAC_ADDR=$(busctl get-property "${NETWORK_ITEM_OBJ}" \
63272b453cSPatrick Williams            "${NETWORK_ITEM_PATH}" "${NETWORK_ITEM_IFACE}" MACAddress)
64403e2635SBrad Bishop
65403e2635SBrad Bishop        # 's "54:52:01:02:03:04"'
66403e2635SBrad Bishop        MAC_ADDR=${MAC_ADDR#*\"}
67403e2635SBrad Bishop        MAC_ADDR=${MAC_ADDR%\"*}
68403e2635SBrad Bishop
69272b453cSPatrick Williams        hostnamectl set-hostname "$(hostname)-${MAC_ADDR}"
70403e2635SBrad Bishop    else
71272b453cSPatrick Williams        hostnamectl set-hostname "$(hostname)-${BMC_SN}"
72403e2635SBrad Bishop    fi
73403e2635SBrad Bishop
74403e2635SBrad Bishop}
75403e2635SBrad Bishop
76403e2635SBrad Bishopsync_hostname
77403e2635SBrad Bishop
78403e2635SBrad Bishop# Prevent start at next boot time
79403e2635SBrad Bishoptouch "/var/lib/first-boot-set-hostname"
80