Setting up a Raspberry Pi perfectly on the first boot

Bytemark’s offices have long been missing that vital component of any tech company – wall-hanging status displays! I want to show off some vital statistics: support satisfaction statistics for our support team, the railcams, sales figures – all that movie stuff!

Easy, right? We’ll use Raspberry Pis to do the display – they’re cheap and easily hidden behind TVs or old monitors. But working with Pis is frustrating if you want to set them up in bulk.

There’s no way around the fact that you need to write your own SD cards to install Raspbian, the Raspberry Pi’s OS. But Raspbian is made for tinkering – it’s well tested, but a toy box, not a single-purpose appliance.

If you want your Raspbery Pi to be a movie player, or web kiosk, or file server, you need to follow a tutorial. And inevitably those tutorials say “now plug in the Pi in and type…”. Arrrgh, annoying! You have to wait for them to boot, type at a strange keyboard tucked at the side of your desk, wait ages for packages to install … it takes patience. I wanted to set up lots of Pis from the same image without having to manually configure each one.

I could set it up perfectly once, then take a copy of the SD. But copying the SD takes 10-15 minutes each time, it takes up a lot of hard drive space, and if I make a mistake I have to go back to each image and fix it up. I can buy Raspbian SD cards – so why can’t I just copy the setup commands onto each one, rather than the whole image?

Using the boot partition

Here’s how:

Inspired by the approach that Docker takes to creating server appliances, I created a first “appliance” (groan) that turns your Raspberry Pi into a full screen web browser, completely hands-free.

To try it out:

  1. buy or write yourself a standard Raspbian SD card, insert it into your computer;
  2. download the first release of the full screen browser appliance (less than 1MB);
  3. unzip it into the boot partition of your Raspbian SD card, letting it overwrite what’s there;
  4. edit two files files if you want:
    1. it you want to change the URL that the Raspberry Pi displays on the screen, change the last line of appliance/home/pi/graphical-startup.sh
    2. if you’re using a wireless USB widget for your Raspberry Pi’s network connectivity, put your wireless access credentials into appliance/etc/wpa_supplicant/wpa_supplicant.conf
  5. eject the SD card from your computer, and put it into your Raspberry Pi;
  6. plug in your Raspberry Pi, and wait 5 minutes – on the first boot it looks like nothing is happening (it’s quicker on a Raspberry Pi 2);
  7. (if you get bored wondering what it’s doing at the login prompt, type “top” to see how busy it is, but there’s no need to plug in a keyboard if you don’t want to).

When it’s finished, your Pi will display a full screen web browser.

If you want to create three more “kiosks”, you can write three more SD cards from the standard Raspbian image and just use the same small zip file over and over – no need to boot each system to adjust it.

Why this was too hard

All I wanted to do was jam in some setup instructions – install this package, overwrite this file, start this program when you turn on, etc.

But the Raspbian image is divided into two partitions (shown here with some unused space at the end of the  SD card):

Rasperry Pi disk layoutThe second partition is the root partition and it contains almost the whole Raspbian system – web browser, startup scripts, package lists and so on. Unless you use Linux on your desktop to set up your Raspberry Pis (and even then…), it’s inconvenient to alter Raspbian from the outside, and needs extra software on Windows & Mac OS.

The first partition is the one we can access conveniently, the boot partition. This is written as FAT32, the (Microsoft-originated) disk format now understood by pretty much any computer. The Raspberry Pi stores the Linux kernel & some boot parameters – the rest of the boot process happens in the second partition.

How this works (for developers)

I found the opportunity to make changes to the live system just by only altering the boot partition.

I wrote a shim to Linux’s init process which I’ve called pi-init2. We then add three items to the boot partition to get what we want:

  1. the pi-init2 program;
  2. a new cmdline.txt which tells Linux to use pi-init2 instead of its normal boot process;
  3. a folder full of files that we want to overwrite what’s in Raspbian, which is the job we’re trying to do, I’ve just called it “appliance”;

When the Pi boots up, it reads the new command line and tells Linux to call our new init program.

pi-init2 then takes care of  goes through our folder, overwriting any files it finds on the main partition with links to the /boot folder. It then gets out of the way and calls the normal Linux init process, less than a second later. But we’ve then achieved our aim of getting changes onto Raspbian without booting it.

Using links instead of copying the files means we can easily change the wireless network name & password, or the web address, or any other parameters we want to expose to the “user” of the SD card image we’ve prepared.

The code for pi-init2 is available on Github and includes all the “appliance” files that turn the Pi into a full-screen web browser. I’ve used Go rather than C because 1) it supports all the low-level Linux programming needed to make this work, and 2) it supports building Raspberry Pi programs on any Go-supported system. On the weekend I wrote it, I’d left my laptop charger at the Bytemark office, and was working on Windows, so this was by far the easier option. It certainly doesn’t read any worse than the equivalent C.

What’s next

This does everything I want, and the pi-init2 program is MIT-licensed (i.e. free as the wind), for anyone to do what they want with.

What I’d hope for is that it becomes unnecessary – the Raspbian developers could choose to implement something similar in their boot process, because I think this workflow is going to be important for more people than just me.

At that point, the  Raspberry Pi tutorials of the world can be rewritten to say “here’s the small zip files that’ll do the job” rather than making you type everything out! Or there’s scope to build an Raspberry Pi “appliance repository” in the same way Docker have done for their Docker Hub.

Bytemark are hiring

If you enjoy this sort of hacking, and live near York or Manchester, Bytemark are recruiting system administrators where there’s plenty more of this to do.

You don’t even need to give your name, age, gender or CV to apply. We interview you over the internet, and it’s completely anonymous (until the third round at least). Your interest & skills can shine before we know your name. Apply over at https://careers.bytemark.co.uk/ where you can read more about the process.