Rolling your own minimal embedded Linux for the Raspberry Pi

Unicode logo The Raspberry Pi is a clever little gadget, to be sure; but it suffers from the limitation that all the official Linux releases that are available for it (Raspian, NOOBS) are desktop-oriented. None is really intended for true embedded applications -- they are full-sized distributions with systemd, dbus, and all that stuff. Worse, they provide no straightforward method to run with a read-only root filesystem. Being able to shut down an embdedded Linux appliance safely simply by switching it off is a necessity, and running the root filesystem read-only is a way to help make this practicable.

The complexity of the standard distributions also makes the Pi slow to boot, not to mention slow to shut down if you want to avoid the hazards of a hard power-off.

To the best of my knowledge, and to my surprise, there is no official documentation even on the minimal requirements for a bootable Raspberry Pi boot SD card. I wouldn't expect the Raspberry Pi Foundation to provide guidance on building a Linux distribution but, in a device that is promoted as suitable for experimenters, I would have thought that they would release at least one truly minimal, base image that could be used for customization. To be fair, there are Pi Linux distributions derived from Raspbian that are less bloated than the official releases, like DietPi, but even these are far too heavy for a true embedded appliance.

The purpose of these articles is to describe how to build, from the official Raspbian Linux release, a truly minimal Linux, with a read-only root filessytem. The first article describes how to boot to a root shell. No systemd, no init, no package manager, no users -- nothing. Just a # prompt. This set-up will be used as the basis for a more useful Linux installation, including lightweight X support, in due course.

I will point out right from the start that I'm writing this article for people who are experienced Linux users, and I'm assuming that you'll be doing the set-up on a Linux desktop system. I'm assuming that you have some knowledge of the Raspberry Pi -- otherwise, why would you be doing this in the first place?

But I'm making another assumption which is likely to be contentious: the assumption that it's a good idea to start with the Raspian binaries, rather than building everything from source. To some extent that's a necessary assumption, since the Pi firmware is not open source and cannot, so far as I know, be substituted for anything else. You certainly can build your own kernel from source, if you have the patience. If you want sub-second boot times, you'll certainly have to. However, I'm assuming that the official Raspberry Pi kernel is good enough for present purposes. It typically boots in 3-4 seconds on a Pi 3B, depending what hardware is attached.

Similarly, I'm assuming that it's productive to plunder the official Raspbian repositories using scripts, since no official package manager is likely to be at all useful. Again, this seems a reasonable assumption for an embedded system, since you won't need very much of the stock software anyway.

Let's get started.

Part 1: Booting to a root shell
Installing a enough of an operating system to boot the kernel and run a root shell.

Part 2: Early initialization
Installing essential utilities and configuration files.

Part 3: Enabling remote access services
Moving from a single-user system to a network-enabled system with a service management framework.

Part 4: Audio
Installing the minimum software needed to play audio using the ALSA system.

Part 5: X
Running simple X applications, without the weight and complexity of a graphical desktop and its associated baggage.