Alexander Filippov | 89bd384 | 2019-05-22 11:07:30 +0300 | [diff] [blame] | 1 | #!/bin/sh -eu |
| 2 | |
| 3 | show_error() { |
Alexander Filippov | c2102a4 | 2019-06-10 18:44:59 +0300 | [diff] [blame] | 4 | if [ -n "${JOURNAL_STREAM-}" ]; then |
| 5 | echo "$@" | systemd-cat -t first-boot-set-hostname -p emerg |
| 6 | else |
| 7 | echo "$@" >&2 |
| 8 | fi |
Alexander Filippov | 89bd384 | 2019-05-22 11:07:30 +0300 | [diff] [blame] | 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 | 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 Filippov | c2102a4 | 2019-06-10 18:44:59 +0300 | [diff] [blame] | 33 | show_error "No BMC item found in the Inventory. Is VPD EEPROM empty?" |
Alexander Filippov | 89bd384 | 2019-05-22 11:07:30 +0300 | [diff] [blame] | 34 | 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 | |
| 49 | systemctl disable first-boot-set-hostname.service |