Skip to main content

Raymii.org Raymii.org Logo

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

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 post is over two years old. It may no longer be up to date. Opinions may have changed.

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.

Recently I removed all Google Ads from this site due to their invasive tracking, as well as Google Analytics. Please, if you found this content useful, consider a small donation using any of the options below:

I'm developing an open source monitoring app called Leaf Node Monitoring, 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 $200 credit for 60 days. Spend $25 after your credit expires and I'll get $25!

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
build-timestamp-20220221181616

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

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

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"
AUTHOR = "yocto@relst.nl"
LICENSE = "CLOSED"
PR = "r2"

S = "${WORKDIR}"

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

do_install() {            
        date_1=build-timestamp-
        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