xref: /openbmc/phosphor-misc/firstboot/first-boot-set-hostname.sh (revision 403e26351c06783fac9de012167d408a89d333ee)
1*403e2635SBrad Bishop#!/bin/sh -eu
2*403e2635SBrad Bishop
3*403e2635SBrad Bishopshow_error() {
4*403e2635SBrad Bishop    if [ -n "${JOURNAL_STREAM-}" ]; then
5*403e2635SBrad Bishop        echo "$@" | systemd-cat -t first-boot-set-hostname -p emerg
6*403e2635SBrad Bishop    else
7*403e2635SBrad Bishop        echo "$@" >&2
8*403e2635SBrad Bishop    fi
9*403e2635SBrad Bishop}
10*403e2635SBrad Bishop
11*403e2635SBrad Bishopsync_hostname() {
12*403e2635SBrad Bishop    MAPPER_IFACE='xyz.openbmc_project.ObjectMapper'
13*403e2635SBrad Bishop    MAPPER_PATH='/xyz/openbmc_project/object_mapper'
14*403e2635SBrad Bishop    INVENTORY_PATH='/xyz/openbmc_project/inventory'
15*403e2635SBrad Bishop
16*403e2635SBrad Bishop    BMC_ITEM_IFACE='xyz.openbmc_project.Inventory.Item.Bmc'
17*403e2635SBrad Bishop    INV_ASSET_IFACE='xyz.openbmc_project.Inventory.Decorator.Asset'
18*403e2635SBrad Bishop    BMC_SN=''
19*403e2635SBrad Bishop    BMC_ITEM_PATH=$(busctl --no-pager --verbose call \
20*403e2635SBrad Bishop                            ${MAPPER_IFACE} \
21*403e2635SBrad Bishop                            ${MAPPER_PATH} \
22*403e2635SBrad Bishop                            ${MAPPER_IFACE} \
23*403e2635SBrad Bishop                            GetSubTree sias \
24*403e2635SBrad Bishop                            ${INVENTORY_PATH} 0 1 ${BMC_ITEM_IFACE} \
25*403e2635SBrad Bishop                        2>/dev/null | grep ${INVENTORY_PATH} || true)
26*403e2635SBrad Bishop
27*403e2635SBrad Bishop    # '     STRING "/xyz/openbmc_project/inventory/system/chassis/bmc";'
28*403e2635SBrad Bishop    BMC_ITEM_PATH=${BMC_ITEM_PATH#*\"}
29*403e2635SBrad Bishop    BMC_ITEM_PATH=${BMC_ITEM_PATH%\"*}
30*403e2635SBrad Bishop
31*403e2635SBrad Bishop    BMC_ITEM_SERVICE=$(mapper get-service \
32*403e2635SBrad Bishop                                ${BMC_ITEM_PATH} 2>/dev/null || true)
33*403e2635SBrad Bishop
34*403e2635SBrad Bishop    if [[ -n "${BMC_ITEM_SERVICE}" ]]; then
35*403e2635SBrad Bishop        BMC_SN=$(busctl get-property ${BMC_ITEM_SERVICE} \
36*403e2635SBrad Bishop                            ${BMC_ITEM_PATH} \
37*403e2635SBrad Bishop                            ${INV_ASSET_IFACE} SerialNumber)
38*403e2635SBrad Bishop        # 's "002B0DH1000"'
39*403e2635SBrad Bishop        BMC_SN=${BMC_SN#*\"}
40*403e2635SBrad Bishop        BMC_SN=${BMC_SN%\"*}
41*403e2635SBrad Bishop    else
42*403e2635SBrad Bishop        show_error "No BMC item found in the Inventory. Is VPD EEPROM empty?"
43*403e2635SBrad Bishop    fi
44*403e2635SBrad Bishop
45*403e2635SBrad Bishop    if [[ -z "${BMC_SN}" ]] ; then
46*403e2635SBrad Bishop        show_error "BMC Serial Number empty! Setting Hostname as 'hostname + mac address' "
47*403e2635SBrad Bishop
48*403e2635SBrad Bishop        NETWORK_ITEM_IFACE='xyz.openbmc_project.Inventory.Item.NetworkInterface'
49*403e2635SBrad Bishop        NETWORK_ITEM_PATH=$(busctl --no-pager --verbose call \
50*403e2635SBrad Bishop                           ${MAPPER_IFACE} \
51*403e2635SBrad Bishop                           ${MAPPER_PATH} \
52*403e2635SBrad Bishop                           ${MAPPER_IFACE} \
53*403e2635SBrad Bishop                           GetSubTree sias \
54*403e2635SBrad Bishop                                ${INVENTORY_PATH} 0 1 ${NETWORK_ITEM_IFACE} \
55*403e2635SBrad Bishop                    2>/dev/null | grep ${INVENTORY_PATH} || true)
56*403e2635SBrad Bishop
57*403e2635SBrad Bishop        NETWORK_ITEM_PATH=${NETWORK_ITEM_PATH#*\"}
58*403e2635SBrad Bishop        NETWORK_ITEM_PATH=${NETWORK_ITEM_PATH%\"*}
59*403e2635SBrad Bishop
60*403e2635SBrad Bishop        NETWORK_ITEM_OBJ=$(mapper get-service ${NETWORK_ITEM_PATH} 2>/dev/null || true)
61*403e2635SBrad Bishop
62*403e2635SBrad Bishop        if [[ -z "${NETWORK_ITEM_OBJ}" ]]; then
63*403e2635SBrad Bishop            show_error 'No Ethernet interface found in the Inventory. Unique hostname not set!'
64*403e2635SBrad Bishop            exit 1
65*403e2635SBrad Bishop        fi
66*403e2635SBrad Bishop
67*403e2635SBrad Bishop        MAC_ADDR=$(busctl get-property ${NETWORK_ITEM_OBJ} \
68*403e2635SBrad Bishop                               ${NETWORK_ITEM_PATH} \
69*403e2635SBrad Bishop                               ${NETWORK_ITEM_IFACE} MACAddress)
70*403e2635SBrad Bishop
71*403e2635SBrad Bishop        # 's "54:52:01:02:03:04"'
72*403e2635SBrad Bishop        MAC_ADDR=${MAC_ADDR#*\"}
73*403e2635SBrad Bishop        MAC_ADDR=${MAC_ADDR%\"*}
74*403e2635SBrad Bishop
75*403e2635SBrad Bishop        hostnamectl set-hostname $(hostname)-${MAC_ADDR}
76*403e2635SBrad Bishop    else
77*403e2635SBrad Bishop        hostnamectl set-hostname $(hostname)-${BMC_SN}
78*403e2635SBrad Bishop    fi
79*403e2635SBrad Bishop
80*403e2635SBrad Bishop}
81*403e2635SBrad Bishop
82*403e2635SBrad Bishopsync_hostname
83*403e2635SBrad Bishop
84*403e2635SBrad Bishop# Prevent start at next boot time
85*403e2635SBrad Bishoptouch "/var/lib/first-boot-set-hostname"
86