swtpm and Secure Boot firmware in the snap
a component, an app for users for CI operators
What the snap supplies for the four Ubuntu Core +tpm systems. The qemu-x86-64 component carries the Secure Boot OVMF pair, the swtpm component carries swtpm and the libraries the base lacks, make stops before the download when that component is missing, and the image-garden.swtpm app serves launchers run from a host shell.
On the map: snap
▶ Watch this deck — 6 scenes, ~4 min
Worth seeing first
- Ubuntu Core with Secure Boot and a TPM — what the +tpm systems are and why they need Secure Boot and a TPM
01 Two pieces for a TPM system
The Secure Boot firmware comes with qemu-x86-64. swtpm is its own component: snap install image-garden+swtpm.
Say you want ubuntu-core-24+tpm from the snap. Outside the snap, your host would need two packages for it: swtpm, the TPM emulator, and ovmf, for the Secure Boot firmware. The snap brings both, but in different places. The firmware rides along in qemu-x86-64, the QEMU component a PC already has. swtpm is a component of its own, which the snap doesn't install for you, because no other system needs it. So one command gets you ready: snap install image-garden+swtpm. This deck shows what each piece holds, and how the snap uses them.
Details and sources
Details
qemu-x86-64"contains the 64-bit x86 system emulator (QEMU) as well as the corresponding EFI firmware (EDK2) and the Secure Boot EFI firmware of Ubuntu (OVMF)" (snapcraft.yaml:59-66); the install hook installs it on x86_64 hosts (install:6-14).swtpm: "the TPM emulator behind the virtual TPM of the Ubuntu Core +tpm systems, which also need the qemu-x86-64 component. Other systems do not need it" (snapcraft.yaml:109-119). "It is not installed automatically:snap install image-garden+swtpm" (README.md:322-334).- The snap's news for this release: the Secure Boot firmware in
qemu-x86-64, theimage-garden.swtpmapp, and swtpm as a separate component (NEWS.snappy:26-30). - Outside the snap the host needs the swtpm and ovmf packages; see what your host needs.
Read more
02 The Secure Boot pair, in the QEMU component
OVMF_CODE_4M.secboot.fd and OVMF_VARS_4M.ms.fd in qemu-x86-64/share/ovmf, after the host's pair; copied to efi-secboot-code.x86_64.img and efi-secboot-vars.x86_64.img.
Start with the firmware. When the snap is built, it takes Ubuntu's ovmf-generic package and keeps two files: the Secure Boot build of the code, and the variable store with Microsoft's keys enrolled, which belongs with it. Both go into the qemu-x86-64 component, under share/ovmf, next to the QEMU that loads them. make lists that pair after the host's own, so when your host has the ovmf package, its pair wins. Either way, the two are copied once into the directory make runs in, and every +tpm system starts from those copies.
Details and sources
Details
- The
ovmf-x86-64part stages Ubuntu'sovmf-genericpackage, keepsOVMF_CODE_4M.secboot.fdandOVMF_VARS_4M.ms.fd("OVMF_VARS_4M.ms.fd is for the Secure Boot code") and its copyright file, and organizes them into(component/qemu-x86-64)/share/ovmf(snapcraft.yaml:645-665). - make's Secure Boot lists:
/usr/share/OVMF/OVMF_CODE_4M.secboot.fdwith/usr/share/OVMF/OVMF_VARS_4M.ms.fdfirst, the component's pair second; the Nth entries go together and the first pair where both exist wins (010-qemu-x86_64.mk:282-296). - The pair is copied to
efi-secboot-code.x86_64.imgandefi-secboot-vars.x86_64.img, aftercheck-qemu-x86-64-component(010-qemu-x86_64.mk:298-308); each +tpm system converts them to its own.efi-code.qcow2and.efi-vars.qcow2(050-ubuntu-core.mk:57-61). - Without a pair:
please install x86_64 edk2 firmware with Secure Boot and Microsoft keys (the ovmf package) - not in … with …(010-qemu-x86_64.mk:300-308).
Read more
03 What the swtpm component holds
bin/swtpm and lib/swtpm/ (libtpms, libswtpm_libtpms, json-glib), versioned from Ubuntu's package. The stub sets LD_LIBRARY_PATH.
The swtpm component is made the same way, from Ubuntu's swtpm package. It keeps the swtpm program, and the libraries it needs that the snap's base lacks, such as libtpms. Its version is the package's version, read from its changelog, so it moves with Ubuntu's updates rather than with QEMU. Inside the snap, swtpm is a stub: it puts those libraries on the library path and runs the program. And there's no swtpm_setup. An empty state directory is enough: swtpm creates the TPM state there by itself, on the first boot.
Details and sources
Details
- The
swtpmpart stages Ubuntu'sswtpmpackage, unpinned, and keepsbin/swtpm, the librarieslibswtpm_libtpms.so.0*,libtpms.so.0*andlibjson-glib-1.0.so.0*inlib/swtpm, and the copyright files; everything else is deleted (snapcraft.yaml:608-644). - The component's version is read from the first line of the package's
changelog.Debian.gz, without the epoch; the build fails if it cannot be found (snapcraft.yaml:621-632). - "swtpm_setup is not needed: swtpm creates the TPM state on its own when the directory is empty" (snapcraft.yaml:614-616).
- The stub prepends
lib/swtpmtoLD_LIBRARY_PATHand execsbin/swtpm(swtpm:34-39).
Read more
04 Stopped before the download
install the swtpm component to use systems with a TPM: snap install image-garden+swtpm, before any download. A missing qemu-x86-64 is reported before the firmware is copied.
What if you forget the component? make checks before it does anything costly. A +tpm system's download is a link to the plain Ubuntu Core image, and making that link depends on a check that the swtpm component is there. So make stops before it downloads anything, and says install the swtpm component to use systems with a TPM, with the snap install command. If you remove the component later, a launcher stops at the stub instead, showing a message with the same command. And on a machine without qemu-x86-64, make asks for that component first.
Details and sources
Details
check-swtpm-componentruns only in the snap and fails withinstall the swtpm component to use systems with a TPM: snap install image-garden+swtpm(010-qemu-x86_64.mk:121-131).- Each +tpm download is a link to the plain image,
ubuntu-core-24+tpm.x86_64.qcow2toubuntu-core-24-amd64.qcow2for example, withcheck-swtpm-componentas its first order-only prerequisite, so make stops before the download; the.qcow2rule has it too (050-ubuntu-core.mk:74-79, 050-ubuntu-core.mk:279-289). - The swtpm stub shows a message headed Notice and
snap install image-garden+swtpmand exits 1 when the component is missing (swtpm:11-32). - The Secure Boot copies depend on
check-qemu-x86-64-component:install qemu-x86-64 component to use x86_64 emulation(010-qemu-x86_64.mk:298, 010-qemu-x86_64.mk:310-316, 010-qemu.mk:50-55).
Read more
05 A launcher run from your shell
Outside the snap the launcher runs image-garden.swtpm and image-garden.qemu-system-x86-64, with swtpm's socket in $XDG_RUNTIME_DIR/snap.image-garden/image-garden/.
Now the launcher. Inside the snap, as under allocate, the .run script calls swtpm and QEMU by their plain names, which are the stubs. Run it from an ordinary shell, and it calls the snap's apps instead: image-garden.swtpm and image-garden.qemu-system-x86-64. Both are still confined, and they reach only the snap's own part of your runtime directory. So the script puts swtpm's socket there, under snap.image-garden, rather than in image-garden's usual place. The swtpm app plugs just home, for the TPM state next to your disk, and network-bind, for its socket.
Details and sources
Details
SWTPMin a launcher made by the snap is a shell expression:image-garden.swtpmwhenSNAP_INSTANCE_NAMEis unset at run time,swtpminside the snap (010-qemu-x86_64.mk:115-119); QEMU is chosen the same way,image-garden.qemu-system-x86-64orqemu-system-x86_64(010-qemu-x86_64.mk:4).- In that case the launcher puts swtpm's run directory under
${XDG_RUNTIME_DIR:-/run/user/UID}/snap.image-garden/image-garden: "Outside the snap its confined swtpm and QEMU reach only the runtime directory of the snap" (010-qemu-x86_64.mk:173-189). Under allocate,GARDEN_TPM_RUN_DIRnames the directory instead. - The
swtpmapp:command: usr/bin/swtpm, plugshomeandnetwork-bind("Binding swtpm's UNIX sockets needs it") (snapcraft.yaml:132-139). - The launcher must still be run from its own directory, which must be under your home directory for the confined apps to reach it; see three things that belong together.
06 Where to go from here
Next: the +tpm systems, each launch of swtpm, the firmware search, the components, confinement, and the snapd map's encryption deck.
So the snap brings both pieces: the Secure Boot pair in its x86_64 QEMU component, and swtpm in a component you add yourself. From here, the Secure Boot and TPM deck shows the four systems and what their first boot seals. The runtime deck walks through each launch of swtpm. The firmware deck shows how make picks the Secure Boot pair. The components deck has the rest of the snap's optional parts, and the confinement deck the other things a host shell launch changes. The snapd map explains what the sealed keys protect.
Details and sources
Read more
- Secure Boot and a TPM, a TPM launch, firmware, snap components, confinement.
- On the snapd map: what is encrypted and who decides.
Read more
Basis of the statements on this page
Read in the image-garden source at release v0.8.0 ("code"): snap/snapcraft.yaml, qemu-stubs/swtpm, mk/010-qemu-x86_64.mk and mk/050-ubuntu-core.mk. Nothing was run for this page; swtpm was not installed.
What the snap supplies for a +tpm system
| Piece | Where in the snap | From | Installed |
|---|---|---|---|
OVMF_CODE_4M.secboot.fd, OVMF_VARS_4M.ms.fd | qemu-x86-64 component, share/ovmf/ | Ubuntu's ovmf-generic | with qemu-x86-64, by the install hook on x86_64 hosts |
swtpm and lib/swtpm/ | swtpm component | Ubuntu's swtpm | by you: snap install image-garden+swtpm |
usr/bin/swtpm stub | the snap | qemu-stubs/swtpm | always |
image-garden.swtpm app | the snap | runs the stub | always; plugs home, network-bind |
Sources: snapcraft.yaml:109-139, snapcraft.yaml:608-665, swtpm:6-39.
Names a launcher uses
| Launcher run | swtpm | QEMU | swtpm's run directory |
|---|---|---|---|
inside the snap (image-garden allocate, image-garden make) | swtpm (the stub) | qemu-system-x86_64 (the stub) | GARDEN_TPM_RUN_DIR, or $XDG_RUNTIME_DIR/image-garden/tpm.XXXXXXXX |
| from a host shell | image-garden.swtpm | image-garden.qemu-system-x86-64 | ${XDG_RUNTIME_DIR:-/run/user/UID}/snap.image-garden/image-garden/tpm.XXXXXXXX |
Sources: 010-qemu-x86_64.mk:4, 010-qemu-x86_64.mk:115-119, 010-qemu-x86_64.mk:173-189.
Messages
| When | Message |
|---|---|
| make of a +tpm system without the swtpm component | install the swtpm component to use systems with a TPM: snap install image-garden+swtpm |
| make without the qemu-x86-64 component | install qemu-x86-64 component to use x86_64 emulation |
| make without any Secure Boot pair | please install x86_64 edk2 firmware with Secure Boot and Microsoft keys (the ovmf package) - not in … with … |
| the swtpm stub without its component | a message headed Notice, then snap install image-garden+swtpm |
| a launcher that finds no swtpm command | swtpm, the TPM emulator, is required: install the swtpm package |
Roads from here
UBUNTU CORE · all snaps
- Ubuntu Core with Secure Boot and a TPM — the four +tpm systems and what their first boot seals
ARCHITECTURES · machines and firmware
- UEFI firmware per system — how make finds the Secure Boot pair among the host's firmware
RUNTIME · make rules and the CLI
- How a TPM system is launched — how each launch starts swtpm, step by step
SNAP · snap-only features
- Snap components — the other components and how they arrive
- Living with strict confinement — launchers run from a host shell, and the rest of confinement
Snapd map ↗
- What is encrypted and who decides — what the sealed keys protect, on the snapd map