This is a text-only version of the following page on https://raymii.org:
---
Title       : 	Execute a script in a Yocto package on every image build
Author      : 	Remy van Elst
Date        : 	22-02-2022
URL         : 	https://raymii.org/s/snippets/Execute_a_script_in_a_Yocto_package_on_every_image_build.html
Format      : 	Markdown/HTML
---



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.

<p class="ad"> <b>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:</b><br><br> <a href="https://leafnode.nl">I'm developing an open source monitoring app called  Leaf Node Monitoring, for windows, linux & android. Go check it out!</a><br><br> <a href="https://github.com/sponsors/RaymiiOrg/">Consider sponsoring me on Github. It means the world to me if you show your appreciation and you'll help pay the server costs.</a><br><br> <a href="https://www.digitalocean.com/?refcode=7435ae6b8212">You can also sponsor me by getting a Digital Ocean VPS. With this referral link you'll get $100 credit for 60 days. </a><br><br> </p>



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"

---

License:
All the text on this website is free as in freedom unless stated otherwise. 
This means you can use it in any way you want, you can copy it, change it 
the way you like and republish it, as long as you release the (modified) 
content under the same license to give others the same freedoms you've got 
and place my name and a link to this site with the article as source.

This site uses Google Analytics for statistics and Google Adwords for 
advertisements. You are tracked and Google knows everything about you. 
Use an adblocker like ublock-origin if you don't want it.

All the code on this website is licensed under the GNU GPL v3 license 
unless already licensed under a license which does not allows this form 
of licensing or if another license is stated on that page / in that software:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

Just to be clear, the information on this website is for meant for educational 
purposes and you use it at your own risk. I do not take responsibility if you 
screw something up. Use common sense, do not 'rm -rf /' as root for example. 
If you have any questions then do not hesitate to contact me.

See https://raymii.org/s/static/About.html for details.