I need to run a script on a target OS built by Yocto.
This script needs to be ran as part of the install and thus must be ran only once (either after the entire OS install or on the first boot). It cannot be ran on the host system, as it depends on the hardware IO which exists only on the target.
An additional, minor, constraint is that the rootfs is mounted as read only, but I guess that can be avoided by having the script re-mount as rw and again remount as r after the execution or something along those lines.
Any help is appreciated.
I ended up doing what shibley had written. Here's a detailed howto:
Create a new layer
Put the desired layer wherever your other layers are. Mine are in
stuffdirectory, next to the build directory.Make the following files/directories:
meta_mylayeris the name of your new layer.Let's define the layer in
conf/layer.confand tell it where to search for the recipes:The recipes are defined by the name of the .bb file. This layer only has one recipe, named initscript.
initscript.bbcontains the recipe information. The following recipe will add our initscript service and put the actual install script,initscript.sh, into/usr/sbin/install -dwill create any directories needed for the specified path, whileinstall -m 0644will copy the specified file with 644 permissions.${D}is the destination directory, by default it's${WORKDIR}/imageCreate the systemd service definition
I won't go into much details about how systemd works, but will rather paste the service definition:
Do note the script location at
/usr/sbin/- that's where it will be copied by the last line of ourdo_installfunction above.Lastly, our
initscript.shscript itself:Register the layer
We need to register our new layer, so that bitbake knows it's there. Edit the
build/conf/bblayers.conffile and add the following line to theBASELAYERSvariable:Now that the bitbake recognizes our layer, we need to add our recipe to the image. Edit the
build/conf/local.confand add the initscript recipe to theIMAGE_INSTALL_appendvariable. Here's how it looks like when added next to the python.Run the build
Run the build like you usually do. For example:
After you install the build and boot for the first time, your
initscript.shwill be executed.