Building GNU/Linux Distribution for Raspberry Pi Using the Yocto Project

Introduction: Building GNU/Linux Distribution for Raspberry Pi Using the Yocto Project

About: Software engineer and open source enthusiast

Raspberry Pi is probably the most popular low cost single board computer on the market. It is ofter used for Internet of Things and other embedded projects. Several GNU/Linux distributions have excellent support for Raspberry Pi and there is even Microsoft Windows for Raspberry Pi 2.

If you want to create Internet of Things (IoT) based on Raspberry Pi and if you want to do it professionally it is recommended to build an embedded Linux distribution that fits the exact needs of your device. To boot a Linux distribution on Raspberry Pi you need a bootloader, Linux kernel and various applications in the user space.

One of the most popular ways for building custom embedded Linux distribution is using the Yocto Project. Yocto is a collaborative project of the Linux foundation that uses the Openembedded framework and bitbake build engine. Poky is the reference system of the Yocto Project with a six month release cycle. It provides meta data divided into layers and recipes for building various packages and images.

This tutorial provides the exact steps for building an image with minimal GNU/Linux distribution for Raspberry Pi, that includes systemd and connman.

Step 1: Getting Ready

For this tutorial you will need:

  • Personal computer with GNU/Linux distribution, for example Ubuntu, on which you will build the GNU/Linux distribution for Raspberry Pi.
  • Raspberry Pi
  • microSD or SD card depending on the version of your Raspberry Pi
  • HDMI cable and a monitor
  • USB keyboard
  • Power supply

Install the required packages on your personal computer depending on your GNU/Linux distribution: http://www.yoctoproject.org/docs/2.0/yocto-projec...

Notes (by Yunus EmreI):

  • You cannot bake an image if your home directory is encrypted because it wont allow long file names
  • Initial build takes very long if you have a slow connection
  • While flashing the image to SD card make sure you select the main /dev/sdX, not the /dev/sdXp1 or other variation of internal part

Step 2: Get the Source Code

Perform the actions below on your personal computer:

  • Clone Poky using Git (in this case I am downloading release Jethro (2.0) of the Yocto Project):
    git clone -b jethro git://git.yoctoproject.org/poky
  • Go to directory poky:
    cd poky
  • Clone meta-raspberrypi:
    git clone -b jethro git://git.yoctoproject.org/meta-raspberrypi

Step 3: Configure

  • Initialize the build environment
    source oe-init-build-env
  • Add meta-raspberrypi to BBLAYERS in conf/bblayers.conf, after that it should be similar (but in your case with different paths) to:
    BBLAYERS ?= " \
      /home/leon/poky/meta \
      /home/leon/poky/meta-yocto \
      /home/leon/poky/meta-yocto-bsp \
      /home/leon/poky/meta-raspberrypi \
      "
    
  • Open conf/local.conf and change MACHINE to raspberrypi, raspberrypi0,raspberrypi2 or raspberrypi3 depending on your Raspberry Pi model, for example:
    MACHINE ??= "raspberrypi2"
  • Append the following line to conf/local.conf to set GPU memory in megabytes:
    GPU_MEM = "16"
  • Optionally, append the following line to conf/local.conf to replace System V with systemd:
    DISTRO_FEATURES_append = " systemd"
    VIRTUAL-RUNTIME_init_manager = "systemd"
    DISTRO_FEATURES_BACKFILL_CONSIDERED = "sysvinit"
    VIRTUAL-RUNTIME_initscripts = ""
    
  • Optionally, append the following line to conf/local.conf to include the command line network manager connman in the image:
    IMAGE_INSTALL_append = " connman connman-client "

Step 4: Build an Image

  • Execute the following command to build a minimal image of a GNU/Linux distribution for your Raspberry Pi:
    bitbake rpi-basic-image

Please note that the build may take up to several hours depending on the hardware of your personal computer and the speed of the Internet connection.

Step 5: Flash SD Card

When the build completes the image will be located at following path in your build directory: tmp/deploy/images/raspberrypi2/rpi-basic-image-raspberrypi2.rpi-sdimg.

Plug a microSD card to your computer, replace X with its corresponding ID (which can be found through commands like lsblk or fdisk -l) and execute the following commands to flash the image on it:
sudo umount /dev/sdX
sudo dd if=tmp/deploy/images/raspberrypi2/core-image-weston-raspberrypi2.rpi-sdimg of=/dev/sdX
sync
sudo umount /dev/sdX

Alternatively, if you prefer you can use bmaptool instead of dd.

Be very careful while flashing the SD card and make sure you are using the right letter for the path to the device. Do it on you own risk and keep in mind that a mistake might damage a drive on your personal computer!

Step 6: Boot and Test

Boot Raspberry Pi and execute the following steps to verify that the image is working successfully:
  • Login as root without any password
  • If connman is installed, verify that its systemd services has been loaded:
    systemctl status -l connman
Raspberry Pi Contest 2016

Participated in the
Raspberry Pi Contest 2016

Digital Life 101 Challenge

Participated in the
Digital Life 101 Challenge

2 People Made This Project!

Recommendations

  • Make it Glow Contest

    Make it Glow Contest
  • First Time Author Contest

    First Time Author Contest
  • PCB Challenge

    PCB Challenge

19 Discussions

you dont explain anything, these are just steps to build an image with yocto

0
it.feel.filipov
it.feel.filipov

1 year ago on Step 6

Excellent info! Exactly what is needed, without unnecessary tips/tricks! The build (32 bits), working fine on Raspberry PI 3, Model B.The DHCP client is active and ssh configured, so you can access board with ssh root@<Board IP>

0
pajogdand
pajogdand

1 year ago

I am getting error:

ERROR: ExpansionError during parsing /home/sharadanand/Desktop/PRAVIN/poky/meta-raspberrypi/recipes-kernel/linux/linux-raspberrypi_3.18.bb
Traceback (most recent call last):
File "Var <CMDLINE_DEBUG>", line 1, in <module>
bb.data_smart.ExpansionError: Failure expanding variable CMDLINE_DEBUG, expression was ${@base_conditional("DISTRO_TYPE", "release", "quiet", "debug", d)} which triggered exception NameError: name 'base_conditional' is not defined

0
wildling
wildling

1 year ago

Thanks for this demo, exactly what I needed !

0
adewolf1
adewolf1

1 year ago

How to enable the serial console?

0
sanathkumar767
sanathkumar767

Question 1 year ago on Step 4

I am getting this error
ERROR: OE-core's config sanity checker detected a potential misconfiguration.
Either fix the cause of this error or at your own risk disable the checker (see sanity.conf).
Following is the list of potential problems / advisories:

Please set a valid MACHINE in your local.conf or environment
I have made the specified entries in local.conf as well as bblayers.conf Still facing this issue

0
geshaftw32
geshaftw32

Answer 1 year ago

I had similar issue. The branch jethro is outdated. Try to checkout to the newest branch (in the current time it's warrior). Refer to the yocto wiki for this information.

0
karthi_k
karthi_k

1 year ago

am getting this error ERROR: ParseError at /home/cj/poky/meta-raspberrypi/recipes-bsp/rpi-u-boot-scr/rpi-u-boot-scr.bb:19: Could not inherit file classes/nopackages.bbclass

0
christian1972
christian1972

Question 2 years ago on Introduction

Hi Leon, thank you for your great tutorial about Yocto on Raspberry Pi. I have one question, which is the cross compiler Im using in this environment? Since I use Debian Linux and I have no cross compiler installed, which are the tools I used while building the Yocto Image?

Thank you

BR Christian

0
leon-anavi
leon-anavi

Answer 2 years ago

Hi Christian,

The OpenEmbedded build system creates the necessary cross compiler toolchains for you. The exact versions depend on the releases of the Yocto Project that you are using. For more details please have a look at "4.4. Cross-Development Toolchain Generation" from the current Yocto Project Mega Manual: https://www.yoctoproject.org/docs/current/mega-man...

Best regards, Leon

0
DeepSouthT
DeepSouthT

2 years ago

Thanks, I got it working on Raspberry pi :)

0
UsmanK46
UsmanK46

Answer 2 years ago

Update poky. If you have the latest version then check if you have some mistake in poky/configure file.

0
leon-anavi
leon-anavi

Answer 2 years ago

Use the latest stable release of Poky and meta-updater, aka at the moment it is Sumo.

0
PrateekG33
PrateekG33

Question 2 years ago on Step 6

How do we make our application to auto start at boot? Can I get a step-by-step procedure?

0
leon-anavi
leon-anavi

Answer 2 years ago

Write your own Yocto/OE recipe and add a systemd service (if you are using systemd).

0
fran黃
fran黃

4 years ago

very good