Beyond PiLFSLinux From Scratch on the Raspberry Pi

1 The PiLFS Bootscripts

The PiLFS Bootscripts tarball is a small collection of scripts and fixes from various sources that are specific to the Raspberry Pi. The following scripts can be installed:

networkfixThis little fix tells the kernel to keep 8MB of RAM free at all times for incoming network traffic. Without this fix, it's easy to hang the Pi when doing a lot of work over SSH. All it does is add vm.min_free_kbytes=8192 to /etc/sysctl.conf.

swapfixThe original LFS swap init script will try to enable swap space before the root file system is properly mounted. This won't work if you're using a swap file instead of a dedicated swap partition. This fix simply adjusts the timing of that init script.

fake-hwclockThe Pi doesn't have a way of keeping time between reboots. This script adds a save/restore function to set an approximate date until the exact date can be obtained via NTP.

rngdStart/stop script for the Random Number Generator Daemon for use with the hardware RNG device present on the Pi.

switch-cpu-governorSwitches the cpufreq governor from the default "powersave" to "ondemand" which allows for overclocking the Pi.

2 Adding a Swap File

To create a swap file where count is the number of megabytes you'd like:

Issue the following sed command if you want the kernel to appear exactly as the official one (with a 4.9.X+ version string):

sed -i 's/EXTRAVERSION =.*/EXTRAVERSION = +/' Makefile

Create a default kernel .config file for the Raspberry Pi:

make bcmrpi_defconfig

Or for the Raspberry Pi 2:

make bcm2709_defconfig

If you want you can customize the kernel configuration:

make menuconfig

Now we can build our kernel, then install the kernel modules:

makemake modules_install

Finally copy the compressed kernel to the boot partition:

cp -v arch/arm/boot/zImage /boot/kernel.img

Or for the Raspberry Pi 2:

cp -v arch/arm/boot/zImage /boot/kernel7.img

8 Compiling External Kernel Modules

What if you just want to compile a third-party kernel module compatible with the latest official Raspberry Pi kernel? In my case I wanted to compile the DirectFB Fusion module without having to build a complete kernel.

You'll still need the complete kernel sources so grab them first:

wget https://github.com/raspberrypi/linux/archive/rpi-4.9.y.tar.gz

After unpacking, ensure the sources are in pristine condition:

make mrproper

Issue the following sed command so that your module ends up in the correct place (/lib/modules/4.9.X+):

sed -i 's/EXTRAVERSION =.*/EXTRAVERSION = +/' Makefile

Create a default kernel .config file for the Raspberry Pi:

make bcmrpi_defconfig

Or for the Raspberry Pi 2:

make bcm2709_defconfig

Now we need to grab an important file called Module.symvers from the Firmware repo and put that in the root of the source tree:

10 Building With QEMU User Mode Emulation

QEMU has two operating modes, User mode and System mode emulation. In user mode, only the ARM CPU is emulated and your real file system is used as storage. This is the fastest method but requires that you run a Linux system (real or virtual). To run under Windows/Mac, try the System Emulation.

The running kernel needs to have support for CONFIG_BINFMT_MISC and have /proc/sys/fs/binfmt_misc mounted:

11 Building With QEMU System Emulation

What if you're stuck with Windows or Mac OS X and can't use the more elegant QEMU user mode emulation? Maybe you just prefer to prepare a complete SD card in a sandboxed virtual machine with limited access to your precious file system?

What we can do is emulate hardware very similar to the Raspberry Pi, running a similar kernel and the same LFS base system you would find on the SD card.

Disclaimer: Your emulated Pi will most likely be many times slower than a real Pi. It's probably faster to run QEMU user mode emulation in a virtual machine.

I have verified everything working in QEMU v2.4.0 on Yosemite built with the standard Homebrew formula.

Sometimes the background console shines through while playing a video, or equally annoying, stays black after playing a video. This can be fixed with the fbset utility and a simple wrapper function in your ~/.profile:

13 Adding yt - The YouTube Console Client

yt is a command-line front-end to YouTube which allows you to browse YouTube videos and play them directly from the command-line. It uses youtube-dl and OMXPlayer to actually play the videos. The combination of a text based interface and OMXPlayer makes yt a great YouTube client for the Raspberry Pi.

Now you just have to decide where to install your Quake3 binaries, I chose /root/quake3 here:

cp -rv build/release-linux-arm ~/quake3

And the final step is to copy the pak#.pk3 files from the original Quake3 CD:

cp -v pak{0..8}.pk3 ~/quake3/baseq3

Start the game with ~/quake3/ioquake3.arm - Happy fragging!

15 Building Accelerated SDL + DGen Genesis Emulator

With all the fun software out there depending on the SDL multimedia library, wouldn't it be nice if SDL could take better advantage of the Pi's GPU?

Vanfanel thought so too and wrote a Dispmanx backend for SDL, which takes a small resolution (320x240) output from any SDL application and hardware-scales it to your monitor's native fullscreen resolution (like 1920x1080).

I tried it with DGen and really enjoyed watching Sonic frolic about in 1080p at 60FPS (with a bit of overclock) - smooth as silk!

Building SDL:

For SDL, the only prerequisites are ALSA-lib and ALSA-utils (to be able to adjust the volume with alsamixer). Let's build!

That's all there is to it really, but you'll need to ensure that your SDL application outputs to a small (320x240) plain resolution without any fancy scaling effects etc. so that when you start the application you should see something like this:

Paste the resulting output anywhere in /etc/retroarch.cfg or ~/.config/retroarch/retroarch.cfg. You can optionally bind a button to input_exit_emulator_btn to quit the emulator with the joypad. I also like to bind input_menu_toggle_btn to the joypad so that I can change games through a nifty menu system without having to exit RetroArch.

Now you'll want to grab one or more emulator cores, most of them can be found in the libretro github. Here's a list of some nice ones:

For every compiled core you'll end up with a file called libretro.so something. If we do find . -name '*libretro.so' for FCEU we'll find fceumm_libretro.so. You should copy this file somewhere in an organized fashion that makes sense to you, for example:

mkdir -p ~/retroarch/corescp -v fceumm_libretro.so ~/retroarch/cores

Next we'll want to add some info files. These are required for RetroArch to pick a suitable core automatically for any given ROM file.

Time to try out the emulation! Just type retroarch followed by the ROM file name to start. You can also specify a specific core to load using the -L argument. Or type retroarch --menu to browse ROMs through the spiffy menu system.

17 Building Wayland / Weston

The GPU accelerated desktop on the Pi that we've all been waiting for is inching ever closer each day.

We have the DirectFB project making good progress, and we have the Wayland / Weston & QT5 camps making some really impressive headway. I'll personally be watching all these projects very closely but let's start with Wayland.

These instructions will get you a "pure" Wayland / Weston environment free of X11 dependencies, which obviously means that the XWayland compatibility layer will not be built. If you need the ability to run X apps inside Weston, follow the Xorg BLFS guide to get your Xorg server up and running first.

First ensure that pkgconfig will be able to locate a few Raspberry Pi specific graphics libraries:

Now go build some cool stuff on top of Wayland and let me know about it!

18 Building Kodi (XBMC)

Let's begin with the dependencies that are required for Kodi to build.

First of all, you need a Java runtime to build the interface files. Note that Java is not required to run Kodi, it's only used during the build. Head on over to the Java SE Embedded Downloads page and trade your soul in for ejdk-8u65-linux-arm-sflt.gz.

Untar and put it somewhere nice and make sure that the java executable is in your path:

Phew! So those are the bare minimum dependencies for building Kodi. You may want to add additional software that Kodi can make use of, like Samba and Avahi etc. In particular, I would recommend that you also install libgpg-error, libgcrypt and libmicrohttpd and remove --disable-webserver from the configure line below. You'll be able to remote-control Kodi from your browser or your phone which is a very convenient feature to have on the Pi.

If your TV supports CEC, you'll also want to build libCEC so that you can use your TV remote to control Kodi: