It's Pi all the way down...


In the previous post we saw how to configure networking on first boot with cloud-init. If your Pi has a reliable network connection during that first boot (preferably Ethernet, given the current wifi issue described in LP: #1870346) you can also opt to have cloud-init install whatever packages you’re going to need (and configure them too!) so your system is completely ready for you when you login.


First, it’s useful to learn how to get the local package index up to date (especially if you’re dealing with an old image). This can be done by adding the following line to the user-data file (that one again!) on the boot partition:

package_update: true

If you also want to upgrade the system to the latest version of all installed packages on the initial boot, you can add:

package_upgrade: true


As you can tell, on Ubuntu these effectively run the “apt update” and “apt upgrade” commands, but cloud-init also supports several other distros, hence the rather generic naming of these options. How about installing extra packages? This is also simple to arrange:

# Install some things that Dave probably ought to consider including on
# the images by default...
- pi-bluetooth
- iw

You can specify a particular version of a package by using a list, but this generally isn’t a good idea (there’s no guarantee that version is going to be available unless you’re installing from, say, a repo you control like a PPA):

- [nginx-light, 1.17.9-0ubuntu3]

Speaking of which: what if you want to include a PPA as a source?

      source: "ppa:waveform/pibootctl"  # quote the source

This may seem like an overly convoluted series of structures (“source” within a label within “sources” within “apt”) but the reason for this is that there are a great many options for apt, and a number of different means for specifying third party repos (including importing of signing keys, and templates for deriving new repos from existing ones).


A couple more features are worth introducing in the context of installing packages. Firstly, the ability to write arbitrary files. This is particularly useful for providing configuration for packages you’ve installed:

- path: /etc/default/keyboard
  content: |
    XKBOPTIONS="ctrl: nocaps"

And finally, the ultimate blunt tool of “runcmd” which effectively lets you run arbitrary commands as root on the first boot; use with caution! In this case we can use it to set the wireless regulatory domain:

- iw reg set GB

Putting it all together

Here’s a user-data file I use when testing Pi daily images. It sets up the Pi so I can access it over SSH, importing my keys from GitHub, installs some extra packages for WiFi and Bluetooth, configures the WiFi regulatory domain, adds the PPA for pibootctl (a little project I’ve been working on for boot configuration manipulation):


 1 #cloud-config
 3 chpasswd:
 4   expire: false
 5   list:
 6   - ubuntu:ubuntu
 8 hostname: pitest
10 apt:
11   sources:
12     my-ppa:
13       source: "ppa:waveform/pibootctl"  # the quotes are mandatory
15 package_update: true
16 package_upgrade: true
17 packages:
18 - pi-bluetooth
19 - iw
20 - pibootctl
22 runcmd:
23 - iw reg set GB
25 ssh_pwauth: false
27 ssh_import_id:
28 - gh:waveform80
30 write_files:
31 - path: /etc/default/keyboard
32   content: |
34     XKBMODEL="pc105"
35     XKBLAYOUT="gb"
36     XKBVARIANT=""
37     XKBOPTIONS="ctrl: nocaps"
38     BACKSPACE="guess"
40 runcmd:
41 - reboot

At the moment this file lives on my desktop and, after flashing a fresh image, the first thing I do is copy that file onto the boot partition, replacing the existing user-data. Then I boot the Pi and leave it for a few minutes to sort everything out!

Hopefully that gives you some ideas for playing with. And remember, you can throw these same configurations at various cloud instances too (which can be quite handy if you ever need to scale things up temporarily!)

Coming Up

Next I’m going to take a short break and see if I can set up some form of comments here (probably via the lazy disqus route), and then I might delve into a series on pibootctl or something.