Yocto Project : Build your custom GNU/Linux Distribution ( Raspberry Pi Sample)
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.
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.
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”.
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
Go to “poky” directory.
Now we have to get meta layer of the Raspberry Pi. Use this command.
git clone git://git.yoctoproject.org/meta-raspberrypi -b dunfell
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.
git clone https://git.openembedded.org/meta-openembedded -b dunfell
Now we have to initialize the build environment
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”.
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"
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 ;
This process lasts hours depending on your computer hardware and speed of internet connection. You need to wait until building is finished.
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.
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.