config: introduce hostname generation hooks

Overriding hostname in a .conf file, via base-files:

  HOST_NAME="k3s-host"
  hostname_pn-base-files = "${HOST_NAME}"

Is always a valid option, but if it is not configured, we can easily
have two hosts with the same name on the network, confusing adddress
assignement, etc.

This commit introduces a way to generate a unique hostname based
on the uuid of the build host, and the machine being built.

If virt-unique-hostname is added to IMAGE_FEATURES, like the following:

  IMAGE_FEATURES += "virt-unique-hostname"
  IMAGE_FEATURES[validitems] += "virt-unique-hostname"

Then a rootfs postprocessing hook will override hostnae to something
unique.

Note: this means your image will be reproducible on a single builder,
but not between them.

Signed-off-by: Bruce Ashfield <bruce.ashfield@gmail.com>
This commit is contained in:
Bruce Ashfield 2021-11-22 20:28:27 +00:00
parent 6fe326b680
commit 491a385ca6
2 changed files with 30 additions and 0 deletions

View File

@ -0,0 +1,29 @@
# This doesn't work, since it seems to be too late for sanity checking.
# IMAGE_FEATURES[validitems] += ' ${@bb.utils.contains("DISTRO_FEATURES", "virtualization", "virt-unique-hostname; ", "",d)}'
ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains("IMAGE_FEATURES", "virt-unique-hostname", "virt_gen_hostname; ", "",d)}'
ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains("IMAGE_FEATURES", "virt-unique-hostname", "virt_set_hostname; ", "",d)}'
python virt_gen_hostname() {
import uuid
targetname = d.getVar("VIRT_TARGETNAME")
if targetname != None:
return
status, date = oe.utils.getstatusoutput("date +%d-%m-%y")
if status:
bb.warn("Can't get the date string for target hostname")
uuid = ':'.join(['{:02x}'.format((uuid.getnode() >> ele) & 0xff) for ele in range(0,8*6,8)][::-1])
if uuid:
targetname = "%s-%s" % (d.getVar("MACHINE"), uuid.split(":")[0])
else:
targetname = "%s-%s" % (d.getVar("MACHINE"), date)
d.setVar("VIRT_TARGETNAME", targetname)
}
virt_set_hostname() {
echo "${VIRT_TARGETNAME}" > ${IMAGE_ROOTFS}/etc/hostname
}

View File

@ -57,5 +57,6 @@ K8S_CONFIG_PATH = "${LAYERDIR}/conf/distro/include/k8s-versions.inc"
USER_CLASSES:append = " meta-virt-cfg"
USER_CLASSES:append = " meta-virt-k8s-cfg"
USER_CLASSES:append = " meta-virt-xen-cfg"
USER_CLASSES:append = " meta-virt-hosts"
HOSTTOOLS_NONFATAL += "getent"