Gadget snaps are an essential part of Ubuntu Core. They’re responsible for defining and manipulating device-specific system properties and configuration and, as such, they’re part of both the installation image and operating environment.
See Gadget snaps for information on what they can contain and how they’re configured, and see Types of snap for details on the other types of snap that make up an Ubuntu Core image.
Building a gadget snap enables specific device and customisation options to be set and integrated into an Ubuntu Core image.
The following gadget repositories contain the gadget snap definitions for amd64 (64 bit PC Gadget Snap) and the Raspberry Pi family of devices supported by Ubuntu Core:
There are typically two methods for building a gadget snap, cross building and native building. Cross building is likely the most convenient and performant as the gadget is built within a container on the host machine.
See below for examples of both to build the Raspberry Pi “Universal” Gadget Snap.
Cross building
Cross building allows a gadget to be built on an architecture different from the target. In the following example, we build for armhf on an amd64 host.
Prerequisites
- An Ubuntu host (20.04 or newer is recommended)
- Snapcraft
First, clone the appropriate gadget repository, switch to the appropriate branch, make whatever modifications are necessary, and simply run the snapcraft
command:
$ git clone https://github.com/snapcore/pi-gadget
$ cd pi-gadget
$ git checkout 20-armhf
[edit the gadget]
$ snapcraft
[...]
Snapped pi_20-1_armhf.snap
By default, snapcraft attempts to build the gadget snap in either an LXD (default) or Multipass container, isolating the host system from the build system. Both Multipass and LXD allow for the build architecture to differ from the run-on architecture, as defined by the architecture
stanza in the snapcraft.yaml
file for the gadget snap:
architecture
- build-on: [amd64, armhf]
run-on: armhf
See Architectures for more details on defining architectures and Image building for instructions on how to build a bootable image that includes the gadget snap.
Native building
This method allows for the gadget snap to be built on the same hardware the gadget is intended for.
Prerequisites
- A supported Raspberry Pi with UC20+ installed
- An SSH connection to the Raspberry Pi
- Raspberry Pi internet access
To build the gadget snap:
- Install and set up LXD
- Launch a fresh instance of Ubuntu 20.04
- Within the instance:
- Install snapcraft
- Clone the repo, switch to the appropriate build and arch branch
- Build the gadget with snapcraft
- Exit the instance and obtain the snap from within the container
Running the following commands on the Raspberry Pi will perform the above process:
$ sudo snap install lxd
$ sudo lxd init --auto
$ sudo lxc launch ubuntu:20.04 focal
$ sudo lxc shell focal
# snap install snapcraft --classic
# git clone https://github.com/snapcore/pi-gadget/
# cd pi-gadget
# snapcraft --destructive-mode
[...]
Snapped pi_20-1_arm64.snap
# exit
$ lxc file pull focal/root/pi-gadget/pi_20-1_arm64.snap .
See Image building for instructions on how to build a bootable image that includes the gadget snap.