DEV Community

Erick Andrés Obregón Fonseca
Erick Andrés Obregón Fonseca

Posted on • Updated on

Yocto Project Tutorial: Baking a Minimal Linux Image from Scratch

Introduction

Even though I am still a student, I always wanted to share a little of the knowledge that I have acquired throughout my university studies. This is why I’ve decided to write this, my first blog. In this case, I bring a tool that I learned to use in the Introduction to Embedded Systems course and that I found very interesting. This is a copy and paste of a post that I did in Medium.

What is the Yocto Project?

The Yocto Project is an open-source collaboration project that helps you to create custom Linux-based systems regardless of the hardware architecture [1].

As they say on their page, the Yocto Project is not an embedded Linux distribution, instead, it creates a custom one for you.

The Yocto Project has several releases. I’ll mention the last five releases but you can find all in [2].

Codename Version Release Date Current Version Poky Version
Hardknott 3.3 April 2021 - 25.0
Gatesgarth 3.2 October 2020 - 24.0
Dunfell 3.1 April 2020 3.1.3 23
Zeus 3.0 October 2019 3.0.4 22.0.3
Warrior 2.7 April 2019 2.7.4 21.0

As you can see, the Hardknott version has yet to be released (at least by the time I wrote this post).

The Yocto Project through the OpenEmbedded build system provides an open-source development environment targeting the ARM, MIPS, PowerPC and x86 architectures for a variety of platforms including x86–64 and emulated ones. You can use components from the Yocto Project to design, develop, build, debug, simulate, and test the complete software stack using Linux, the X Window System, GTK+ frameworks, and Qt frameworks [3].

The Yocto Project Development Environment


The Yocto Project Development Environment. Taken from [3]

For this tutorial, we will use Poky Dunfell because I have worked with it before and had no problems.

Why should I use my own custom Linux image?

General-purpose operating systems, such as Windows, macOS, or most Linux distributions, have several tasks to run in the background, as well as multiple installed applications that we don’t need or will never use on our embedded systems. All these reasons, together with the limited resources of our systems, make these operating systems inefficient or excessive for our poor hardware. Think that you don’t need a text editor, an office package, or a PDF reader in your embedded system if you will work with an IoT device.

Building our own operating systems allows us to install only the dependencies and software that we need. This makes it easier for us to better control and take advantage of our limited hardware and make our system more efficient by executing only the tasks it needs.

Before we start

Environment

Before starting to work with Yocto, we must make sure that we meet some requirements:

  • A supported Linux version (like Fedora, OpenSUSE, CentOS, Debian, or Ubuntu). You can check them in [4].
  • The OpenEmbedded build system should be able to run on any modern distribution that has the following versions for Git 1.7.8 or greater, tar 1.24 or greater, and Python 2.7.3 or greater [4].
  • At least 50 GB of free disk space for image creation.

Installing dependencies

First, we need to install the necessary packages to run Yocto. Let’s open a terminal and copy and paste the next line:

$ sudo apt-get install gawk wget git-core diffstat unzip texinfo gcc-multilib build-essential chrpath socat libsdl1.2-dev xterm
Enter fullscreen mode Exit fullscreen mode

Img1

In my case, I already had all packages installed.

Installing QEmu

This is the easiest part, you only need to run:

$ sudo apt-get install qemu
Enter fullscreen mode Exit fullscreen mode

Img2

Downloading Poky Dunfell

There are two options to download Poky Dunfell. The first one is to go on this link and download the tar.bz compressed file or type in a terminal:

$ wget http://downloads.yoctoproject.org/releases/yocto/yocto-3.1.2/poky-dunfell-23.0.2.tar.bz2
Enter fullscreen mode Exit fullscreen mode

I will download it to my user folder. It could take a while depending on your connection speed, so be patient.

Img3

Now, we need to unzip the downloaded file.

$ tar -xvf poky-dunfell-23.0.2.tar.bz2
Enter fullscreen mode Exit fullscreen mode

This will create a new folder called poky-dunfell-23.0.2 (you can change the name if you want, but it is not necessary). Now let’s enter the directory and show its content.

Img4

Baking our Image

Now, we will create a couple of directories to work with. The first is a folder where Yocto will download all our packages and dependencies (downloads folder), and the second one will be used for our image (qemu-arm folder). You can name them as you want.

$ mkdir downloads
$ mkdir qemu-arm
Enter fullscreen mode Exit fullscreen mode

Img5

We need to execute the script to configure the environment necessary for variables and commands of the Yocto Project, this is the oe-init-build-env file. This will go into the qemu-arm folder.

$ . oe-init-build-env qemu-arm
Enter fullscreen mode Exit fullscreen mode

Make sure to run this command every time you need to build or rebuild the image.

Img6

If we take a glance inside the conf folder, we can see some configuration files. For this tutorial, we are only interested in the local.conf file where we can set the machine architecture and the download folder path. In this, we need to add the following lines to the end of the file changing <user> for your user:

DL_DIR ?= “/home/<user>/poky-dunfell-23.0.2/downloads”
INHERIT += “rm_work”
MACHINE ?= “qemuarm”
Enter fullscreen mode Exit fullscreen mode

Img7

You could use other test architectures that come in the local.conf file like:

Img8

Comments are written with # at the beginning. Another setting you can do is to use the default directory for downloads.

Img9

Finally, we can build our image. This process can take a while… 2–6 hours… depending on your internet connection and the processing power of your workstation (in this case, your computer). But don’t be overwhelmed, thankfully, every time you make a change or add new recipes (set of instructions for building packages [5]), Yocto will simply rebuild the changes and not the entire project, making this process less time-consuming.

$ bitbake core-image-minimal
Enter fullscreen mode Exit fullscreen mode

Img10

You can rerun the command to see that it will only take a couple of minutes to build.

Img11

Testing our Image

Now, let’s test our image. Since our image is not graphic but console, we must specify it so that it does not produce an error.

$ runqemu qemuarm nographic
Enter fullscreen mode Exit fullscreen mode

Img12

Img13

Now, type root, and we are done.

Img14

Bibliographic References

[1] "Yocto Project — It's not an embedded Linux distribution — it creates a custom one for you", Yoctoproject.org. [Online]. Available: https://www.yoctoproject.org/. [Accessed: 23- Oct- 2020].

[2] "Releases — Yocto Project", Wiki.yoctoproject.org. [Online]. Available: https://wiki.yoctoproject.org/wiki/Releases. [Accessed: 23- Oct- 2020].

[3] "Yocto Project Quick Start", Yoctoproject.org. [Online]. Available: https://www.yoctoproject.org/docs/1.8/yocto-project-qs/yocto-project-qs.html. [Accessed: 25- Oct- 2020].

[4] "Yocto Project Reference Manual", Yoctoproject.org. [Online]. Available: https://www.yoctoproject.org/docs/1.8/ref-manual/ref-manual.html. [Accessed: 25- Oct- 2020].

[5] "Yocto Project Development Manual", Yoctoproject.org. [Online]. Available: https://www.yoctoproject.org/docs/1.6/dev-manual/dev-manual.html. [Accessed: 26- Oct- 2020].

Top comments (3)

Collapse
 
legorock profile image
Ahmet Erdem

Thanks a lot, I managed to run within a docker container :)
Besides the tricky Dockerfile, I had to select slirp as network device. So I ran runqemu qemuarm slirp nographic. The TUN control device require sudo modprobe tun which requires more complicated configuration for the docker image.

Collapse
 
rusty_sys_dev profile image
Scott

Very nice! I will definitely reference this in the future ( If I work with IoT again ).

Collapse
 
erickof profile image
Erick Andrés Obregón Fonseca

Great! I hope it is useful.