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' |
asmithakarun | 173965e | 2019-06-26 02:04:53 -0500 | [diff] [blame] | 15 | |
Alexander Filippov | 89bd384 | 2019-05-22 11:07:30 +0300 | [diff] [blame] | 16 | BMC_ITEM_IFACE='xyz.openbmc_project.Inventory.Item.Bmc' |
asmithakarun | 57f0a3e | 2019-06-26 02:31:07 -0500 | [diff] [blame] | 17 | INV_ASSET_IFACE='xyz.openbmc_project.Inventory.Decorator.Asset' |
asmithakarun | 173965e | 2019-06-26 02:04:53 -0500 | [diff] [blame] | 18 | BMC_SN='' |
Alexander Filippov | 89bd384 | 2019-05-22 11:07:30 +0300 | [diff] [blame] | 19 | 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 | |
asmithakarun | 173965e | 2019-06-26 02:04:53 -0500 | [diff] [blame] | 34 | if [[ -n "${BMC_ITEM_SERVICE}" ]]; then |
| 35 | BMC_SN=$(busctl get-property ${BMC_ITEM_SERVICE} \ |
Alexander Filippov | 89bd384 | 2019-05-22 11:07:30 +0300 | [diff] [blame] | 36 | ${BMC_ITEM_PATH} \ |
asmithakarun | 57f0a3e | 2019-06-26 02:31:07 -0500 | [diff] [blame] | 37 | ${INV_ASSET_IFACE} SerialNumber) |
asmithakarun | 173965e | 2019-06-26 02:04:53 -0500 | [diff] [blame] | 38 | # '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 Filippov | 89bd384 | 2019-05-22 11:07:30 +0300 | [diff] [blame] | 44 | |
asmithakarun | 173965e | 2019-06-26 02:04:53 -0500 | [diff] [blame] | 45 | 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 Filippov | 89bd384 | 2019-05-22 11:07:30 +0300 | [diff] [blame] | 80 | } |
| 81 | |
asmithakarun | 173965e | 2019-06-26 02:04:53 -0500 | [diff] [blame] | 82 | sync_hostname |
Alexander Filippov | 89bd384 | 2019-05-22 11:07:30 +0300 | [diff] [blame] | 83 | |
Alexander Filippov | 2e78e09 | 2019-10-18 15:10:30 +0300 | [diff] [blame] | 84 | # Prevent start at next boot time |
| 85 | touch "/var/lib/first-boot-set-hostname" |