Search code examples
gityoctobitbake

How to include git revision of image layer in boot output or /etc/issue?


I'm creating a Poky image for an SBC, and I want to have a way for a user to look up the SHA1 ID of the recipe file used to create the image.

Recipe contents are as follows:

SUMMARY = "Toradex Embedded Linux Console Sporian Yocto version"
DESCRIPTION = "A Yocto Poky build derived from core-image-minimal"

LICENSE = "MIT"

#start of the resulting deployable tarball name
export IMAGE_BASENAME = "Sporian-Console-Image-Yocto"
IMAGE_NAME_apalis-imx6 = "Apalis-iMX6_${IMAGE_BASENAME}"

require /home/rdepew/workspace/oe-core3/poky/meta/recipes-core/images/core-image-minimal.bb

IMAGE_INSTALL += " \
    packagegroup-core-ssh-openssh \
    sqlite3 \
    avro-c \
"

Here's the console output when the SBC boots:

Poky (Yocto Project Reference Distro) 2.4.3 apalis-imx6 /dev/ttymxc0

apalis-imx6 login: root
root@apalis-imx6:~# uname -a
Linux apalis-imx6 4.1.44-2.7.4+gb1555bfbf388 #1 SMP Tue Oct 9 17:35:02 UTC 2018 armv7l GNU/Linux
root@apalis-imx6:~#

Here are the contents of /etc/issue. Note that these are the default contents:

Poky (Yocto Project Reference Distro) 2.4.3 \n \l

Suppose the SHA1 ID of the repository containing the recipe is ea4c5bb42e7542... . I want to print the SHA1 ID during bootup or in response to a user command (similar to 'uname'). How can I do that?

I thought that ${SRCPV} might be the solution to my problem, but I can't bend it to my will.


Solution

  • Linux and U-Boot git hashes are the ones from the Linux/U-Boot git repository. This is how it is commonly done with OpenEmbedded. There is certainly a way to pass the git hash from OE to the U-Boot/Kernel build system, but I would not recommend doing that since it is not how it is commonly done.

    As for the /etc/issue file, this typically gets generated in the meta/recipes-core/base-files/base-files_3.0.14.bb recipe. It should be fairly straight forward to add a bbappend to your layer and extend the task, e.g. something like this:

    def get_layer_rev(d):
        return bb.process.run('git rev-parse HEAD')
    
    LAYER_REV="${@get_layer_rev(d)}"
    
    do_install_basefilesissue_append() {
        # Overwrite /etc/issue with a custom version of it
        printf "${DISTRO_NAME} " > ${D}${sysconfdir}/issue
        printf "${LAYER_REV}" >> ${D}${sysconfdir}/issue
    }