Modular system for customized embedded distributions
Author: Simon Egli, bbv Software Services
Contribution – Embedded Software Engineering Congress 2015
The Yocto build system is a large and powerful tool. For beginners, it's often difficult to understand how the system works, making it challenging to start their own project. This article shows, step by step, how to build a simple distribution using custom recipes. Readers of this article will be equipped to tackle more advanced topics independently.
The Yocto project provides embedded product manufacturers with tools and metadata to develop their own hardware-independent Linux-based distributions. Yocto allows all the properties and characteristics of a system to be defined, tested, and, if necessary, simulated on a virtual system from the outset.
Typically, custom-developed programs, along with custom configuration files and standard programs (SSH, vim, etc.), are integrated into the finished image.
Individual units are divided into so-called recipes, which describe the steps to "bake" a program unit using the Bitbake program. The recipes are in turn grouped into layers, which help to manage the complexity of the many recipes.
A small example distribution will be created below. [1]
All files created here can also be downloaded from GitHub; see further links.
Initializing a Yocto project
The following commands create a new Yocto project and prepare the terminal for subsequent commands. [2]
~/ $ git clone git://git.yoctoproject.org/poky.git ~/yocto/poky
~/ $ source ~/yocto/poky/oe-init-build-env
The Source command initializes the terminal and switches to the directory that is automatically generated for building.
The following steps assume that these commands have been executed previously and that this initialized terminal is being used.
Create a separate layer
Creating a custom layer is easiest using the provided script. Each layer has an adjustable priority, allowing it to extend or override recipes from lower-priority collections. The example layer in this article is named "ese".
~/yocto/build$ ../poky/scripts/yocto-layer create ese -o ../poky/meta-ese
Please enter the layer priority you'd like to use for the layer: [default: 6]
Would you like to have an example recipe created? (y/n) [default: n]
Would you like to have an example bbappend file created? (y/n) [default: n]
New layer created in meta-ese.
After the script's required information has been provided, the directory structure and the necessary configuration files are created in a Yocto-compliant manner.
In order for the newly created layer to be integrated into the system, it must be entered in the file «conf/bblayers.conf».
~/yocto/build $ vi ./conf/bblayers.conf
BBLAYERS ?= “ \
/home/dead/yocto/poky/meta \
/home/dead/yocto/poky/meta-yocto \
/home/dead/yocto/poky/meta-yocto-bsp \
/home/dead/yocto/poky/meta-ese \
„
This entry integrates the new layer, and it will be included in the next distribution build.
A recipe of my own
With the newly created layer, you can now add a recipe for your own program. In the "meta-ese" directory, create a directory called "recipes-example" and within that, a subfolder named "hello-world". This is the main folder for your custom recipe. It's a small hello-world program (written in C), which runs under /usr/bin[3] is to be installed. Recipes in a layer are always grouped under one term. By default, Bitbake only searches for recipes in such collections. A folder named „ is created in the hello-world directory.„hello-world-0.1″ created for the recipe's source code. This contains the C file to be compiled.„helloworld.c created (see also Figure 1, PDF).
#include
int main(int argc, char **argv)
{
printf(„Hello World!„);
return 0;
}
Contrary to this example, the source code is often automatically downloaded from the internet using the corresponding recipe. Bitbake automatically finds the source code subfolder and uses the files it contains to execute the steps in the recipe. In the directory „meta-ese/recipes-example/hello-world"“ The actual recipe "hello-world_0.1.bb" is created. The numbers after the underscore represent the version number and are required for every recipe.
SUMMARY = „Simple helloworld application“
SECTION = „examples“
LICENSE = „WITH“
LIC_FILES_CHKSUM = „file:///MIT;md5=0835ade698e0bcf8506ecda2f7b4f302“
SRC_URI = „file://helloworld.c“
S = „“
do_compile() {
helloworld.c -o helloworld
}
do_install() {
install -d
install -m 0755 helloworld
}
Each recipe requires a license description. Here, the MIT license from the Standard Licenses Directory is used. Every recipe consists of several extensible standard steps, called tasks, which are designated with "do_". In very few cases do the individual steps need to be written manually: many programs use automated build processes, such as cmake or qmake. Bitbake can automatically detect and execute the compilation steps for these. The steps of the example recipe compile the source code in the "compile" task. In "do_install", the resulting executable file is placed in the "bindir" of the "D" directory – that is, in the directory. /usr/bin-Directory of the finished image – installed and equipped with the necessary rights.
Once the recipe is complete, this compilation can be started. In the Yocto environment, this is called "baking a recipe".
~/yocto/build$ Bitbake hello-world
Depending on the recipe, processing can take some time, as all dependent recipes must also be processed. The recipe output can be viewed under
~/yocto/build/tmp/work/i586-poky-linux/hello-world/0.1-r0/image/
can be viewed.
Patching other people's recipes
Often, the recipes of other layers need to be modified. Typically, for example, a different configuration file might be used, or a few lines of a standard program might need to be modified for the local distribution.
In the following example, the configuration file of the Linux scheduler "cron" will be replaced by a custom file.
The recipe containing cron is called "Cronie" on Yocto and is located at meta/recipes-extended/cronie to find.
To extend it, a new "bbappend" file is created. Files with this extension are automatically used to patch a recipe of the same name.
This time, the creation of this file and the associated recipe structure will be done by a tool included with Yocto.
~/yocto/build$ recipetool newappend ../poky/meta-ese cronie
The script searches the recipes and automatically creates a new bbappend file in the correct location with the current version of the cronie recipe:
/home/dead/yocto/poky/meta-ese/recipes-extended/cronie/cronie_1.5.0.bbappend
To prepare the recipe for all future versions, the version number (1.5.0) is manually replaced with "%"—a wildcard character that allows all versions. The file is therefore renamed... cronie_%.bbappend.
The file's content is very short:
FILESEXTRAPATHS_prepend := „/files:“
SRC_URI += "file://crontab"
These lines simply instruct Bitbake to consider the folder and copy the file it contains to the build directory; there it overwrites the default file of the Cronie recipe.
The actual recipe, located in the external layer, handles the compilation and all other necessary steps.
To separate the configuration file from the recipe, a new subfolder is created in ~/yocto/poky/meta-ese/recipes-extended/cronie named «files» and a modified crontab file was created within it.
# /etc/crontab: system-wide crontab
# Example crontab file for the meta-ese layer
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
*/5 * * * * root echo „Hello world“ > /var/log/syslog &
This recipe can also be baked with this recipe:
This in turn can take a while – as not only cronie, but also all the recipes that cronie depends on need to be built.
The result can finally be found under ~/yocto/build/tmp/work/i586-poky-linux/cronie/1.5.0-r0/image The crontab file should be inspected – it should be correctly placed under /etc/crontab and will be included with the finished image.
Building an image
The goal is to create an image containing both the original and the patched recipe.
A sample image of the standard poky layer «core-image-base» is used as the basis.
There are several ways to achieve the goal – attaching the recipes to this image recipe. Here, creating a separate recipe file is chosen.
The recipe is in ~/yocto/poky/meta-ese/recipes-core/images/ese-image.bb created with the following content.
# Base this image on core-image-minimal
include recipes-core/images/core-image-base.bb
IMAGE_INSTALL += „hello-world cronie“
The recipe collection «recipes-core» normally includes the recipes essential for the system, which is suitable for this main recipe.
The image recipe instructions instruct Bitbake to install "hello-world" and the modified cronie. However, it should also follow the instructions of the "core-image-base" image.
The image can now be built.
~/yocto/poky$ Bitbake ese-image
The build process takes a considerable amount of time because all the tools, programs, and components for the finished system need to be built. Afterward, the image can be started, for example, on a virtual machine.
To use the recipes productively, a manufacturer layer would need to be downloaded and included. The simplest solution would then be to rewrite the ese-image so that it includes a manufacturer-provided image instead of the core-image-base.
conclusion
This article describes how to create your own distribution using Yocto. A fully functional image can be generated in a short amount of time.
The reader can follow along step by step and thus gain an insight into working with Yocto. Only a small part of Yocto's functionality is shown, and many possibilities and topics are left out (see Figure 2)., PDF).
The reader is encouraged to read the further documentation from Bitbake and Yocto to deepen their knowledge.
Further links
Open Source – our training & coaching
Do you want to bring yourself up to date with the latest technology?
Then find out more here MircoConsult offers training courses/seminars/workshops and individual coaching on the topic of Open Source / Embedded Software Engineering.
Training & coaching on the other topics in our portfolio can be found here. here.
Open Source – Expertise
Valuable expertise in modeling/embedded and real-time software development is available. here Available for you to download free of charge.
You can find expertise on other topics in our portfolio here. here.
