Build your own Linux Image for the Raspberry Pi

Michael Seidel
Nerd For Tech
Published in
6 min readJan 31, 2021

The Raspberry Pi and similar single board computers are very popular nowadays. The possibilities are almost endless. From a Home Server to a Media Station to IoT Projects, there’s everything. The two things that make it so easy to use are probably the big community and the ready to start SD-Card Images like Raspian. Grabbing the newest Image from the web and flashing it on an SD-Card is the easiest and fastest way to get started. And there is nothing wrong with it if you just want to play with your Raspberry or doing a one time project on your own. But if you ever tried to replicate your system on different or even multiple systems, it gets complicated. And if you care about the size of the image and security it gets even more complex. Don’t think that I’m able to present to you the solution for a secure Linux system. But building your own Image and knowing exactly what’s in there is a good starting point. And last but not least it’s interesting to learn a little bit more about how Linux works and being able to say, that you’ve built your own Linux from scratch.

In this first article, we’ll not go into too much depth. There is a lot more to know if you want to understand the whole system and customize it to your needs. Instead, we want a fast success and just get going with your very first Image. Stay tuned for more articles if you’re interested in to a deep-dive.

The Yocto Project

We’re gonna use Yocto for building our own Linux Distribution.

“The Yocto Project. It’s not an embedded Linux Distribution, It creates a custom one for you.” This statement on the official website might the best and shortest description of what Yocto is. You have to look at Yocto as a collection of useful tools and configuration artifacts that really help you to create your own Linux Image. There are alternatives, but Yocto is probably one of the most popular.

For an in-depth look at the different parts of the Yocto Project, you can have a look at their website.

The things we get in touch with for our first Image are meta layers and BitBake. These two things form the core of the system.

Meta Layers

Yocto is built upon different layers, mostly containing recipes and configuration data. A recipe is used to describe how stuff is built. So for example it contains from where to download the source code of the Linux Kernel and which commands and tools to use for compiling it correctly. The configuration data is for example used, to describe which architecture the Raspberry Pi uses so that the recipes knows which target to compile for. This might be an oversimplification but it’s probably enough to know for know that you need the right meta layers for your target system. Inside these everything the build system needs is available.

BitBake

BitBake is the central command-line tool for the build. It was originally part of the OpenEmbeded Project but is now a standalone tool maintained by the Yocto Project and the OpenEmbedded Project.

Setting up the Build system

Now it’s time four your first own Image. The steps for setting up the build environment are fairly simple, but you’ll need some time for the build to complete. That’s the downside of building a Linux Image from scratch. Everything has to be build and this is CPU, RAM, and HDD intensive. On ordinary computers, it can take up to 8 hours. But don’t worry, after the first build is finished the good caching algorithms of Yocto kicks in, and only new and changed stuff is built. The minimal free disk space on your computer has to be at least 50 GB. (For reference: I’ve built this image on a 16-Core AWS Cloud Instance in around 1 hour. The built time is heavily dependent on CPU power and download speed. More on the topic of building Yocto Images in the cloud might be in following articles.)

You can find a list of officially tested Linux distribution as the host for the build system in the Mega Manual of the yocto Project. It will work on other distributions too, but you might expect unexpected issues. I’ll use Ubuntu 18.04 LTS for all my examples in this article.

The first step is to install the prerequisite for Yocto. This command installs all packages on Ubuntu or Debian. You’ll find other examples in the Mega Manual.

Getting the meta layers

All meta layers are usually available via git. Don’t worry for now, if you have no clue what git means. We’re just using one command for now. The git clone command gets the repository from the internet. The -b dunfell switch we're using specifies which version to get. At the time of writing the dunfell version is the most recent version with long-term support. See https://wiki.yoctoproject.org/wiki/Releases for an overview of the most recent releases.

Before getting any meta layers we’re gonna create a project folder named yocto and a folder named source for all the meta layers inside.

mkdir yocto
cd yocto
mkdir sources

The meta layer we’re always needing is poky. It contains all the basic stuff for yocto to work. We get it by executing the following git command.

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

For the Raspberry Pi, there is a nicely made meta layer with all the definitions needed to run the Raspberry Pi. We get it with this command.

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

Meta layers always state on which meta layers they depend. The readme of meta-raspberrypi states that we need poky, which we already have, and meta-openembedded. This meta-layer itself is divided into multiple layers. We get them all by this command.

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

Those are all the meta layers we need. Let’s go back to our project folder and initialize the build environment.

cd .. 
. sources/poky/oe-init-build-env

We’re almost ready to build our first image. We only need to edit two configuration files.

The bblayers.conf file in the conf folder contains all the path to the meta layers we're gonna use.

nano conf/bblayers.conf

Last but not least, the local.conf file in the conf folder does contain some basic configurations and, most important for us, the name of the machine to build for. If we want to build for the Raspberry Pi 4, we're using raspberrypi4, for the Raspberry Pi 3, raspberrypi3. This is one of the big benefits of using Yocto. It's just a matter of changing one configuration line to build the same Image for another system.

nano conf/local.conf[...]MACHINE ?= "raspberrypi4"[...]

With all these steps done, it’s time to start the actual build and get some (or multiple) coffee(s) while we wait for it to be finished.

bitbake core-image-base

Congratulations! You’ve built your first Linux Image for the Raspberry Pi. You’ll find the completed image under tmp/deploy/images/repberrypi4/core-image-base-raspberrypi4.wic.bz2 This file is compressed. To decompress it, use the bzip2 command or a tool like 7zip.

bzip2 -d -f tmp/deploy/images/raspberrypi4/core-image-base-raspberrypi4.wic.bz2

You only have to flash it to an SD-Card like any other Raspian image, and you’re ready to boot. https://www.raspberrypi.org/documentation/installation/installing-images/

By default, the username is root with an empty password.

Next Steps

I hope you’re proud of yourself for archiving this milestone. Yocto is a powerful tool and there is a lot more to learn. If you want to learn more follow me so that you won’t miss the next article on Yocto.

Until then I’ll try to give you a starting point for searching to continue learning about Yocto yourself. If you need an additional piece of software in your Image, the first place to start is on the OpenEmbedded Layer Index. There you can search for meta layers containing a recipe for the software. Download the layer and add it to your bblayers.conf, if not already there. Then add the name of the recipe to IMAGE_INSTALL_append in your local.conf file.

IMAGE_INSTALL_append = " nano"

Rebuild your Image, flash it, and you’re ready to go.

--

--