Yocto Project : Build your custom GNU/Linux Distribution ( Raspberry Pi Sample)

Enes ALTUN
5 min readDec 11, 2021

Raspberry Pi is a very popular single board computer that you can practice programming and develop lots of embedded projects. You can use it as a computer by connecting a monitor via HDMI and keyboard-mouse via USB. Also it has GPIOs that you can control as physically input or output on board. It supports running several OS. You can use raspberry’s own OS( Raspbian ) or a custom-made distribution. On this tutorial we will build our own GNU/Linux distribution and load it to Raspberry Pi.

1-) The Yocto Project

Yocto Project is an open-source community project that allows you to build your own Linux system. There are alternatives like Buildroot, OpenWRT etc. But Yocto is best choice of these projects. Because it is a collaborative project of the Linux foundation. Additionally, Yocto has a large and vibrant developer community and you can have supports on your projects. There are many sources that are created by Yocto on web. Also most of important firms use Yocto for their products.

To say disadvantages of Yocto, the biggest disadvantage is learning time. Cause Yocto is not proper for beginners. Users need to know a fair amount of operating systems coding and Linux. Other disadvantage is building time. You have to have fast internet connection and personal computer with strong hardware. My building time took 4 hours!

There are 3 core components of the Yocto Project. Lets check them out.

1.1-) BitBake

Bitbake is a task scheduler build engine that helps us to create our custom images. We will use it on command line to build after customize our other steps.

1.2-) OpenEmbedded-Core

The Yocto projects are made upon meta-layers. Meta-layers are composed of basic recipes, classes, and related files. We will use correct layers according to projects and used hardware. For example i used “meta-raspberrypi” because our Linux system will run on raspberry pi. If i used TI hardware, i should have to use “meta-ti”.

1.3-) Poky

Poky is a reference distribution of the Yocto Project. It provides meta data divided into layers and recipes for building various packages and images.

2-) Get Started

Firstly before setting up Yocto environment, you have to use a personal computer with GNU/Linux distribution like Ubuntu, Debian etc. My computer has Ubuntu 20.04.

Open command terminal and enter command below. This command installs the host packages based on an Ubuntu distribution.

The constant meta layer for our Yocto projects is poky. We will always use it. Use the following commands to clone the Poky repository.

git clone git://git.yoctoproject.org/poky -b dunfell

Output of git clone git://git.yoctoproject.org/poky -b dunfell command

Go to “poky” directory.

cd poky

Now we have to get meta layer of the Raspberry Pi. Use this command.

git clone git://git.yoctoproject.org/meta-raspberrypi -b dunfell

Output of git clone git://git.yoctoproject.org/meta-raspberrypi -b dunfell command

Meta-raspberrypi needs two main layers. One of them is poky. We installed poky already. Other is meta-openembedded layers. We can get them by this command.

README section of meta-raspberrypi

git clone https://git.openembedded.org/meta-openembedded -b dunfell

Now we have to initialize the build environment

source oe-init-build-env

Output of source oe-init-build-env command

As you see on photo above, we need to edit two file. One of them is bblayers.conf in build/conf Open that file edit it like below.

Other file is local.conf in build/conf. Open it and go to 37. line. You have to edit line after MACHINE??= with name of your hardware. It is selected MACHINE ??= “qemux86–64” as default. Here we will change it with MACHINE ??= “raspberrypi”.

Default view of local.conf file. Change this line with MACHINE ??= “raspberrypi”

To help conserve disk space during builds, you can add the following statement to your project’s local.conf configuration file found in the Build Directory. Adding this statement deletes the work directory used for building a recipe once the recipe is built.

INHERIT += "rm_work"

Last line of local.conf file. Add here INHERIT += “rm_work” command

Now we are ready to build our image. Here we will use BitBake command with types we want. There are a few types. You can check them all on mega manual . But on this tutorial, i will use ;

bitbake core-image-base

This process lasts hours depending on your computer hardware and speed of internet connection. You need to wait until building is finished.

A screenshot during building process

After this process is finished, our image files will exist in build/tmp/deploy/images/raspberrypi directory.

3-) Flash SD Card

There a few ways to upload our image file to SD card. You can use bmaptool or dd commands to write image file to SD card. But here i will use faster and easier tool, Raspberry Pi Imager. You can install it by command below.

sudo apt install rpi-imager

Now insert your SD card to your computer. Open RP Imager and follow CHOOSE OS > Use custom. Choose file ending with … .rootfs.wic.bz2. Then choose your SD card from storage section. Click WRITE. Wait untill process is done.

4-) Testing

Now you can insert SD card to the Raspberry Pi and plug in HDMI, power connectors.

Congratulations! You have your custom GNU/Linux distribution. After this tutorial you will edit your meta layers and recipes as you want. Then will do same steps to build.

Sources :

https: //docs.yoctoproject.org/index.html

https://www.yoctoproject.org/docs/3.1/mega-manual/mega-manual.html

https: //bootlin.com/doc/training/yocto/yocto-labs.pdf

--

--

Enes ALTUN

“Learn from yesterday, live for today, hope for tomorrow.”