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