add first boot set hostname

Copy this script and systemd unit file from meta-openpower.

Change-Id: I98aed26efb2be00f1fe3d703f83fd201f18059db
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
diff --git a/firstboot/first-boot-set-hostname.service b/firstboot/first-boot-set-hostname.service
new file mode 100644
index 0000000..22bb80c
--- /dev/null
+++ b/firstboot/first-boot-set-hostname.service
@@ -0,0 +1,14 @@
+[Unit]
+Description=Init BMC Hostname
+ConditionPathExists=!/var/lib/first-boot-set-hostname
+Wants=op-vpd-parser.service
+After=op-vpd-parser.service
+
+[Service]
+ExecStart=/usr/bin/first-boot-set-hostname.sh
+Type=oneshot
+RemainAfterExit=Yes
+
+[Install]
+WantedBy=multi-user.target
+
diff --git a/firstboot/first-boot-set-hostname.sh b/firstboot/first-boot-set-hostname.sh
new file mode 100644
index 0000000..eaabb0a
--- /dev/null
+++ b/firstboot/first-boot-set-hostname.sh
@@ -0,0 +1,85 @@
+#!/bin/sh -eu
+
+show_error() {
+    if [ -n "${JOURNAL_STREAM-}" ]; then
+        echo "$@" | systemd-cat -t first-boot-set-hostname -p emerg
+    else
+        echo "$@" >&2
+    fi
+}
+
+sync_hostname() {
+    MAPPER_IFACE='xyz.openbmc_project.ObjectMapper'
+    MAPPER_PATH='/xyz/openbmc_project/object_mapper'
+    INVENTORY_PATH='/xyz/openbmc_project/inventory'
+
+    BMC_ITEM_IFACE='xyz.openbmc_project.Inventory.Item.Bmc'
+    INV_ASSET_IFACE='xyz.openbmc_project.Inventory.Decorator.Asset'
+    BMC_SN=''
+    BMC_ITEM_PATH=$(busctl --no-pager --verbose call \
+                            ${MAPPER_IFACE} \
+                            ${MAPPER_PATH} \
+                            ${MAPPER_IFACE} \
+                            GetSubTree sias \
+                            ${INVENTORY_PATH} 0 1 ${BMC_ITEM_IFACE} \
+                        2>/dev/null | grep ${INVENTORY_PATH} || true)
+
+    # '     STRING "/xyz/openbmc_project/inventory/system/chassis/bmc";'
+    BMC_ITEM_PATH=${BMC_ITEM_PATH#*\"}
+    BMC_ITEM_PATH=${BMC_ITEM_PATH%\"*}
+
+    BMC_ITEM_SERVICE=$(mapper get-service \
+                                ${BMC_ITEM_PATH} 2>/dev/null || true)
+
+    if [[ -n "${BMC_ITEM_SERVICE}" ]]; then
+        BMC_SN=$(busctl get-property ${BMC_ITEM_SERVICE} \
+                            ${BMC_ITEM_PATH} \
+                            ${INV_ASSET_IFACE} SerialNumber)
+        # 's "002B0DH1000"'
+        BMC_SN=${BMC_SN#*\"}
+        BMC_SN=${BMC_SN%\"*}
+    else
+        show_error "No BMC item found in the Inventory. Is VPD EEPROM empty?"
+    fi
+
+    if [[ -z "${BMC_SN}" ]] ; then
+        show_error "BMC Serial Number empty! Setting Hostname as 'hostname + mac address' "
+
+        NETWORK_ITEM_IFACE='xyz.openbmc_project.Inventory.Item.NetworkInterface'
+        NETWORK_ITEM_PATH=$(busctl --no-pager --verbose call \
+                           ${MAPPER_IFACE} \
+                           ${MAPPER_PATH} \
+                           ${MAPPER_IFACE} \
+                           GetSubTree sias \
+                                ${INVENTORY_PATH} 0 1 ${NETWORK_ITEM_IFACE} \
+                    2>/dev/null | grep ${INVENTORY_PATH} || true)
+
+        NETWORK_ITEM_PATH=${NETWORK_ITEM_PATH#*\"}
+        NETWORK_ITEM_PATH=${NETWORK_ITEM_PATH%\"*}
+
+        NETWORK_ITEM_OBJ=$(mapper get-service ${NETWORK_ITEM_PATH} 2>/dev/null || true)
+
+        if [[ -z "${NETWORK_ITEM_OBJ}" ]]; then
+            show_error 'No Ethernet interface found in the Inventory. Unique hostname not set!'
+            exit 1
+        fi
+
+        MAC_ADDR=$(busctl get-property ${NETWORK_ITEM_OBJ} \
+                               ${NETWORK_ITEM_PATH} \
+                               ${NETWORK_ITEM_IFACE} MACAddress)
+
+        # 's "54:52:01:02:03:04"'
+        MAC_ADDR=${MAC_ADDR#*\"}
+        MAC_ADDR=${MAC_ADDR%\"*}
+
+        hostnamectl set-hostname $(hostname)-${MAC_ADDR}
+    else
+        hostnamectl set-hostname $(hostname)-${BMC_SN}
+    fi
+
+}
+
+sync_hostname
+
+# Prevent start at next boot time
+touch "/var/lib/first-boot-set-hostname"
diff --git a/firstboot/meson.build b/firstboot/meson.build
index d2d09d7..06a139e 100644
--- a/firstboot/meson.build
+++ b/firstboot/meson.build
@@ -1,4 +1,5 @@
 set_mac = dependency('systemd', required: get_option('first-boot-set-mac'))
+set_hostname = dependency('systemd', required: get_option('first-boot-set-hostname'))
 
 if set_mac.found()
     install_data(
@@ -15,3 +16,19 @@
             set_mac.get_pkgconfig_variable('systemdsystemunitdir'),
     )
 endif
+
+if set_hostname.found()
+    install_data(
+        'first-boot-set-hostname.sh',
+        install_mode: 'rwxr-xr-x',
+        install_dir: get_option('bindir'),
+    )
+
+    configure_file(
+        input: 'first-boot-set-hostname.service',
+        output: 'first-boot-set-hostname.service',
+        copy: true,
+        install_dir:
+            set_hostname.get_pkgconfig_variable('systemdsystemunitdir'),
+    )
+endif
diff --git a/meson_options.txt b/meson_options.txt
index a946e82..af2e039 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -1,3 +1,6 @@
 option(
     'first-boot-set-mac', type: 'feature', description: 'Set MAC address on first boot.',
 )
+option(
+    'first-boot-set-hostname', type: 'feature', description: 'Set hostname on first boot.',
+)