summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuka Pivk <luka.pivk@toradex.com>2020-03-06 16:01:52 +0100
committerLuka Pivk <luka.pivk@toradex.com>2020-03-19 10:36:04 +0100
commit523aea31bb7a705860f488f3431dc20745c31c48 (patch)
treecfe5e1f6550b2a732ff6311692fd6bf0d5871a2b
parentaaf2b59a0bd69c1cfea6d2fc0df30ab49e16e031 (diff)
hostname: generate a uniqe hostname on boot
This will generate a hostname based on machine name and serial number. (E.g. colibri-imx7-031111777) If serial number is not found it will say "no-serial-number" in host name. If machine is not supported hostname will include "unsupported-device" string. If /dev/device-tree does not exist it will say "recovery-mode" as it assumes its in recovery mode of Toradex Easy Installer To prevent openembedded from creating /etc/hostname file add hostname_pn-base-files = "" to configration Related-to: TEI-100, ELB-1322 Signed-off-by: Luka Pivk <luka.pivk@toradex.com>
-rw-r--r--recipes-core/set-hostname/files/set-hostname.service13
-rw-r--r--recipes-core/set-hostname/files/sethostname19
-rw-r--r--recipes-core/set-hostname/set-hostname_1.0.bb30
3 files changed, 62 insertions, 0 deletions
diff --git a/recipes-core/set-hostname/files/set-hostname.service b/recipes-core/set-hostname/files/set-hostname.service
new file mode 100644
index 0000000..306528b
--- /dev/null
+++ b/recipes-core/set-hostname/files/set-hostname.service
@@ -0,0 +1,13 @@
+[Unit]
+ConditionPathExists=|!/etc/hostname
+Wants=network-pre.target
+Before=network-pre.target
+After=local-fs.target
+After=sys-subsystem-net-devices-eth0.device
+
+[Service]
+Type=oneshot
+ExecStart=/usr/bin/sethostname
+
+[Install]
+WantedBy=network.target
diff --git a/recipes-core/set-hostname/files/sethostname b/recipes-core/set-hostname/files/sethostname
new file mode 100644
index 0000000..87e6320
--- /dev/null
+++ b/recipes-core/set-hostname/files/sethostname
@@ -0,0 +1,19 @@
+#!/bin/sh
+if [ ! -f /proc/device-tree/serial-number ]; then
+ serial="recovery-mode"
+else
+ serial=$(tr -d '\0' </proc/device-tree/serial-number)
+
+ #if serial number is empty we append no-serial-number string
+ if [ -z "$serial" -a "$serial" != " " ]; then
+ serial="no-serial-number"
+ fi
+fi
+
+hostname=@@MACHINE@@"-"${serial}
+
+if [ -f /usr/bin/hostnamectl ]; then
+ /usr/bin/hostnamectl set-hostname ${hostname}
+else
+ hostname ${hostname}
+fi
diff --git a/recipes-core/set-hostname/set-hostname_1.0.bb b/recipes-core/set-hostname/set-hostname_1.0.bb
new file mode 100644
index 0000000..15fa66b
--- /dev/null
+++ b/recipes-core/set-hostname/set-hostname_1.0.bb
@@ -0,0 +1,30 @@
+LICENSE = "MIT"
+LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
+
+PACKAGE_ARCH = "${MACHINE_ARCH}"
+
+inherit systemd
+
+SRC_URI += " \
+ file://sethostname \
+ file://set-hostname.service \
+"
+
+FILES_${PN} = " \
+ ${bindir} \
+ ${systemd_system_unitdir} \
+"
+
+SYSTEMD_SERVICE_${PN} = " \
+ set-hostname.service \
+"
+
+do_install () {
+ install -d ${D}${bindir}
+ install -m 0755 ${WORKDIR}/sethostname ${D}${bindir}
+ sed -i "s/@@MACHINE@@/${MACHINE}/g" ${D}${bindir}/sethostname
+ if ${@bb.utils.contains('DISTRO_FEATURES','systemd','true','false',d)}; then
+ install -d ${D}${systemd_system_unitdir}
+ install -m 0644 ${WORKDIR}/set-hostname.service ${D}${systemd_system_unitdir}
+ fi
+}