Skip to main content Logo

Quis custodiet ipsos custodes?
Home | About | All pages | Cluster Status | RSS Feed | Gopher

Execute a script in a Yocto package on every image build

Published: 22-02-2022 | Author: Remy van Elst | Text only version of this article

This is a small snippet showing a Yocto recipe that executes a script on every build of an image that includes that recipe. I use it to write the build hosts date/time to a file on the image, but you could do anything you want inside the script. It's not recommended to do this, for example, if you want to place a binary on your image you should version it correctly. Yocto can build from a git repo, no need to copy binaries. If you include the buildinfo class your image or the os-release recipe, build info is also written to your image.

In my case I had issues with the SD cards that I was using to flash the image on. Vague errors, sometimes corruption, sometimes leftover data from a previous write, all kinds of weird stuff. Including this recipe in my image was an extra measurement to be sure that the SD card write succeeded, but I would not use this in an actual production image.

I'm developing a desktop monitoring app, Leaf Node Monitoring, open source, but paid. For Windows, Linux & Android, go check it out.

Consider sponsoring me on Github. It means the world to me if you show your appreciation and you'll help pay the server costs.

You can also sponsor me by getting a Digital Ocean VPS. With this referral link you'll get $100 credit for 60 days.

The recipe writes the current date and time to the file /build-timestamp. Here's how it looks once flashed, the three commands below were each of a different image:

root@b2qt-raspberrypi4:~# cat /build-timestamp

root@b2qt-raspberrypi4:~# cat /build-timestamp

root@b2qt-raspberrypi4:~# cat /image-version 

For one reason or another, I didn't look in to in further, the boot2qt image had a date/time of 2018 in /etc/version.

This is the recipe. I assume you know how to make your own layer and add this recipe to, including adding it to your own image. If you include it in your image, due to the two lines at the bottom, the script will be executed every time that image is built.

DESCRIPTION = "Writes yocto image build datetime to image filesystem"
PR = "r2"

S = "${WORKDIR}"

INSANE_SKIP_${PN} = "installed-vs-shipped"
FILES_${PN} += " /"

do_install() {            
        date_2=$(date "+%Y%m%d%H%M%S")

        echo $date_1$date_2 > ${WORKDIR}/build-timestamp
        install -m 0644 ${WORKDIR}/build-timestamp ${D}/build-timestamp

# These two lines force the recipe to be built every time.
do_compile[nostamp] = "1"
do_install[nostamp] = "1"
Tags: arm , bitbake , embedded , hardware , openembedded , snippets , yocto