Yocto and Pi

Masa
5 min readJun 24, 2017

--

I’ve spent the last week playing around with the yoctoproject. The Yocto Project is a system for building embedded Linux “distributions”. And, if you didn’t know what it was before, the RaspberryPi is a little Linux computer. It’s not really an embedded device, but, for a beginner like me, it’s pretty close. I went through the quick start, then built my own image for my Raspberry Pi. I have an image that I can run Qt5 applications, but that will have to be in another post, to keep this from getting too long.

Preface

To provide some background, Linux generally runs on PC class hardware. It has a pretty cool background, and you should read Linus Torvald’s “Just For Fun”. I first used it in the early nineties, but it was quite a hassle to get windowing working. That’s definitely not something that will help build a strong user base if you’re competing against Windows or Mac OS. I eventually got it working, and it helped me get a pretty good understanding of how Linux configuration works. You can get pretty deep into tweaking the kernel and configuring the OS. Nowadays, different companies and organizations provide DVD’s and you can be running in about 15 minutes after downloading the the installation media. I personally go with Ubuntu. It’s gotten so much easier, but I sometimes miss getting my hands deep into the guts.

The Raspberry Pi runs Linux too. It’s an ARM chip, instead of an Intel, and isn’t going to beat your Macbook Pro or Lenovo at anything. It’s more of a smartphone class chip. Well, maybe not the one you just bought. But, it’s a 64-bit, multi-core processor, so, about the same generation as the iPhone 6 or 6S. The main Linux distribution is called Raspbian, but Ubuntu also makes one, although I haven’t tried it. Those both work great, but I thought, I miss mucking about with the core Linux bits, so why don’t I make my own!

(Really, I wanted to run some Qt applications on the Raspberry, and then my mental exercise took a laborious turn into building a whole linux install.)

Yocto

The PC/Desktop distributions of Linux usually start with booting from a DVD (originally, it was a floppy, then CD). With an embedded device, you probably have a micro SD card if you’re lucky. And you don’t often have enough memory to run X or a fancy display. This makes a raspberry pi good practice for building an embedded system since it’s often headless, and the boot media (and entire OS) is from a micro SD card. The YoctoProject is basically a bunch of knobs and switches that will spit out an OS “image”, which you copy, bit for bit, onto a micro SD card, which your embedded device can use as its OS. The thing is, there are A LOT of knobs and switches. Luckily, they provide a lot of documentation. But, it’s best taken in baby steps.

The quick start. Yocto is a bunch of things, and you can get it all from git. (No, not github, but their own repository). But for this quick start, all you need is one bit: Poky. It’s pronounced like the Japanese chocolate cookie stick, Pocky. Incidentally, I have a bunch of hand sanitizer sticks from Pocky if you want some. Why hand sanitizer? Because Pocky is from Osaka, and the local baseball team is the Hanshin Tigers. Get it? Han(d) s(h)an(i)tizers. If you say it just the right way, they sound sort of the same…

Anyway, the guide is pretty straightforward, and you’ll end up with an image in 6 commands. You’ll want to start here. The documentation is pretty good, so you can use their Quick Start, and refer here if you have questions. Say, you create a directory called ‘yoctoproject’:

% mkdir ~/yoctoproject
% cd ~/yoctoproject% git clone http://git.yoctoproject.org/cgit.cgi/poky/
% mkdir build
% source poky/oe-init-build-env build
# This script has thrown you into the build directory you just
# created, setting your path and other environment variables for you
% bitbake core-image-sato
# This will take a while. On the order of hours
% runqemu qemux86

What did you just do? You got Poky, which contained bitbake, your main interaction with Poky. You ran the environment script, which created the two essential conf files: local.conf and bblayer.conf, then you ran bitbake, which used those files, with their defaults, to create the ‘core-image-sato’ image for the qemux86. That’s a basic image, with the sato GUI for the qemux86 emulator.

Next Step: Build an image for a real piece of hardware. To do this, you need to get a few more things from the Yocto repository. More specifically, you need to get some other “layers”. Layers are packages of recipes, and the order by which those recipes will run. For the Quick Start, we had 3 layers: meta, meta-poky, and meta-yocto-bsp. That last one is what specified the qemux86 emulator. We want to substitute that with the raspberrypi layer. That’s found in another yocto meta-raspberrypi repository. That actually has a dependency on Open embedded, so we’ll have to get that repository too. That repository has some layers in it too, so we’ll add those:

# go to the directory where you cloned poky. Note, we're playing
# fast and loose and not sticking with a branch. This is not
# recommended
% cd poky
% git clone git://git.openembedded.org/meta-openembedded
% git clone git://git.yoctoproject.org/meta-raspberrypi

You can use the basic conf files from last time, but you’ll have to change the machine:

local.conf
----------
# MACHINE ??= "qemux86"
MACHINE ??= "raspberrypi2" # You can omit the 2 or change it to 3, depending on your raspberry.
bblayers.conf
-------------
BBLAYERS ?= " \
/home/<user>/yoctoproject/poky/meta \
/home/<user>/yoctoproject/poky//meta-poky \
/home/<user>/yoctoproject/poky//meta-yocto-bsp \
"
should become:
/home/<user>/yoctoproject/poky/meta \
/home/<user>/yoctoproject/poky/meta-poky \
/home/<user>/yoctoproject/poky/meta-openembedded/meta-oe \
/home/<user>/yoctoproject/poky/meta-openembedded/meta-multimedia \
/home/<user>/yoctoproject/poky/meta-openembedded/meta-networking \
/home/<user>/yoctoproject/poky/meta-openembedded/meta-python \
/home/<user>/yoctoproject/poky/meta-raspberrypi \

Then, just run a new bitbake command, building rpi-hwup-image

% bitbake rpi-hwup-image

This will take several hours, depending on your machine. You’ll have an image that you can ‘dd’ onto an SD card and boot off of. Congratulations!! You’ve built yourself a raspberrypi linux image! You can run:

# If you don't know which device your SD card is, you can run lsblk
% sudo dd if=~/yoctoproject/build/tmp/deploy/images/raspberrypi<2>/rpi-hwup-image-raspberrypi2.rpi-sdimg of=/dev/<your micro SD card>

Conclusion

We didn’t actually build anything useful. Just the basics, from which we can start from. We’ll add Qt to our image, an SDK and cross-compiling system, and then add a simple app to our custom image.

--

--