Trying and packing a snap
the edit and test loop for app developers
Three routes take a snap from a build directory onto a system. snap try bind-mounts the directory itself so file edits are live at once. snap pack validates the directory and builds one squashfs file. snap install --dangerous installs a file that has no signatures and gives it a local revision. The flags --devmode and --jailmode and --classic must agree with the confinement the snap declares.
On the map: developing snaps district
01 Three routes from a directory to an installed snap
snap try <dir> installs the directory in place. snap pack <dir> builds a file and snap install --dangerous <file> installs it without signatures. Both give the snap a local revision such as x1.
You've just built acme-agent. It's a directory on your laptop, version 2.1. How do you get from there to a snap you can actually run? The store is one way, but uploading and waiting for review is slow when you're testing a fix. There are two quicker routes. Snap try uses the directory as it is; nothing gets built. Snap pack turns the directory into one file, and installing that file needs --dangerous, because nobody signed it. Both leave a snap with a local revision, x1, that the store knows nothing about. The rest of this deck walks those two routes.
02 snap try mounts the directory itself for app developers for snapd developers
The "file" acme-agent_x1.snap is a symlink to the directory and /snap/acme-agent/x1 is a bind mount of it. snap list shows try in Notes. The docs call the directory an unpacked snap.
A normal install mounts a file. But with snap try there's no file. So what does snapd mount? Your directory. Where the snap file would sit, snapd puts a symlink to your directory. And the mount unit for revision x1 is a bind mount of that directory, not a squashfs mount. From there on it's an ordinary local install, in a change of kind try-snap: profiles, services, commands on your path. Snap list marks it with try in Notes.
03 What is live, and what needs another try for app developers
Changes to files are live at once. Changes to meta/snap.yaml need another snap try, which creates the next local revision. A second try after adding a pre-refresh hook fails; snapd records this as a known issue in a test.
You fix a bug in the agent's binary and rebuild. Do you need to tell snapd? No, and that's the whole point of try. Revision x1 is your directory, so whatever's in it now is what runs next. snap.yaml is different. Snapd read it once, when it wrote the profiles, the units and the commands. So when you add a daemon line, a plug or an app, run snap try again. That makes x2, and the install tasks run again; that's when the service unit appears. One rough edge: both revisions are the same directory, so after you add a pre-refresh hook the next try fails.
04 snap pack checks before it builds for app developers for Ubuntu Core developers
snap pack parses and validates snap.yaml, checks that commands exist and are executable and that completers are readable, validates gadget and kernel snaps, then builds <name>_<version>_<arch>.snap and prints built:. Bad plugs and slots do not stop the build; --check-skeleton runs the checks only and lists them.
Try is quick, but you can't hand a directory to a colleague. Snap pack makes a file, and it checks a few things first, so a broken snap fails on your laptop, not at install. First it reads snap.yaml and adds any hooks it finds in meta/hooks. Plugs and slots get the real interface checks here, though a bad one is only set aside. Then the validation an install runs. Then every app's command must be there and executable. A gadget or kernel snap gets its own checks. Only then is the squashfs file built and named. The --check-skeleton option runs the checks and builds nothing.
05 Installing a file nobody signed
Without --dangerous snapd needs a signed snap-revision assertion for the file. With it, the file's own metadata is trusted, the revision is local, and refreshes skip the snap until snap refresh --amend. --devmode implies --dangerous.
A colleague sends you acme-agent as a file, and snap install refuses it. Why? When snapd installs a file, it looks for a signed statement from the store that matches the file's hash. A store download comes with one. A file packed a minute ago has none. --dangerous tells snapd to go ahead and take the name, the version and everything else from the file itself. The snap gets revision x1. The store has no record of it, so refreshes leave it alone; snap refresh --amend moves it to the store later. Dangerous is the right word: nobody vouched for this file.
06 Three flags that must agree with the snap for app developers for snapd developers
seccomp starts with @complain when devmode is on and jailmode is off, and with @unrestricted for classic without jailmode. AppArmor gets the complain flag for devmode or classic without jailmode. A snap declaring devmode or classic is refused without the matching flag. --jailmode overrides both; a classic snap needs it with --classic. The docs call devmode "developer mode".
The agent works under snap try with the --devmode flag and fails without it. What did that flag change? A strict snap installed with it keeps its profiles, but seccomp starts with @complain and AppArmor gets the complain flag. Denials are logged, not blocked, so you can find the plugs it needs. A snap that declares devmode needs that flag on every install, or the --jailmode flag to force enforcement. A classic snap needs the --classic flag and gets @unrestricted; on Ubuntu Core it's refused. Jail mode wins over both; a classic snap takes it alongside the --classic flag. Whichever you used shows in Notes.
07 The directory went away for app developers for administrators
A tried snap whose directory is gone shows broken in Notes and - as its version. It can be removed, and its /snap/bin entries and unit files go with it.
You clean your build tree and forget a snap was mounted from it. What does snapd do with a snap whose files just vanished? With a packed snap, snapd holds the file. With try, it holds a symlink to something of yours. Delete prime and revision x1 has nothing behind it. Nothing crashes. Snap list still answers, shows a dash for the version and marks the snap broken. You can still remove it, and its commands and unit files go with it. Try in Notes means someone developed on this machine; broken means snapd can't read the snap's files. Remove it, then try or install again.
08 Where to go from here
Roads to the other developing snaps topics, to snap packaging, confinement, interface policy for local snaps, and components.
So acme-agent is installed, one way or the other. Where next depends on what it does when you run it. Run and debug shows how it starts and how to look inside its sandbox. Apps and environment covers the entries pack just checked. Layouts and system usernames are the two snap.yaml features you're likely to need next. Snap packaging has the file format and how local revisions are numbered. Confinement says what devmode relaxes. When snapd asks explains why a snap without a snap-id is judged differently for interfaces. And components have their own pack and install.
Read more
Commands
| Command | What it does |
|---|---|
snap try [<snap-dir>] | installs the directory in place; flags --devmode, --jailmode, --classic; prints <name> <version> mounted from <path> |
snap pack [<snap-dir>] [<target-dir>] | validates and builds a snap file; both default to .; prints built: <path> |
snap pack --check-skeleton <snap-dir> | validation only; prints a summary of bad plugs and slots; a result of only missing commands exits 0 |
snap pack --filename=<name> | sets the file name; an absolute name cannot be combined with a target directory |
snap pack --compression=<xz or lzo> | chooses the squashfs compression |
snap install --dangerous <file> | installs a file without signatures |
snap install --devmode <file> | uses signatures when present and accepts their absence |
snap refresh --amend <snap> | refreshes a local snap, one unknown to the store, from the store |
snap try without an argument uses prime when snap/snapcraft.yaml, snapcraft.yaml or .snapcraft.yaml exists and prime is a directory; otherwise the current directory when it has meta/snap.yaml; otherwise it fails with "error: the <snap-dir> argument was not provided and couldn't be inferred".
Without --dangerous (or --devmode) an install of a file fails with cannot find signatures with metadata for snap/component "<file name>" unless snapd has a snap-revision assertion for the file's hash.
What snap pack checks, in order
1. meta/snap.yaml parses. While parsing, plugs and slots are checked against the real interfaces; a bad one is recorded and dropped, and does not stop the build. Hooks found in meta/hooks/ are added. 2. The snap validates: cannot validate snap "<name>": .... 3. . and meta are readable and searchable, meta/snap.yaml is readable, every app command exists and is executable, every completer is readable. A missing command is snap is unusable due to missing files: path "<path>" does not exist. 4. For bases other than core, core18, core20, core22 and core24: content plug targets and layout sources under $SNAP exist in the tree. See layouts. 5. meta/snapshots.yaml parses, if present. 6. Gadget snaps: gadget.yaml and the gadget content. Kernel snaps: the kernel validation.
A failure is printed as cannot pack "<snap-dir>": <reason>. Unreadable files stop the build with "cannot access the following locations in the snap source directory:" and one line per path with its owner and mode.
Files left out of the snap
| Where | Names |
|---|---|
| top level | DEBIAN, .git, .gitignore, .bzr, .hg, .svn, CVS, .snapignore and similar |
| anywhere | *.snap, *.click, .*.sw?, *~, ,,*, .[#~]* |
File names
| Input | Default name |
|---|---|
a snap, no architectures | <name>_<version>_all.snap |
| a snap, one architecture | <name>_<version>_<arch>.snap |
| a snap, several architectures | <name>_<version>_multi.snap |
a directory with meta/component.yaml and no meta/snap.yaml | <snap>+<comp>_<version>.comp or <snap>+<comp>.comp; see components |
The confinement flags
| Snap declares | Flag | Result |
|---|---|---|
| strict | none | enforced |
| strict | --devmode | seccomp @complain, AppArmor complain |
| strict | --classic | snap "<name>" is not a classic confined snap |
| devmode | none | snap "<name>" requires devmode or confinement override |
| devmode | --devmode | seccomp @complain, AppArmor complain |
| devmode | --jailmode | enforced |
| classic | none | snap "<name>" requires classic confinement |
| classic | --classic | seccomp @unrestricted, AppArmor complain |
| classic | --classic --jailmode | regular, non-classic confinement, enforced |
| classic, on Ubuntu Core | any | snap "<name>" requires classic confinement which is only available on classic systems |
Refused combinations: cannot use devmode and jailmode flags together, cannot use classic and devmode flags together, and this system cannot honour the jailmode flag on a system that forces devmode.
AppArmor complain is the flag complain added to the profile's flag list, after attach_disconnected and mediate_deleted. seccomp @complain and @unrestricted are the first directives of the profile, after its version comment.
Notes in snap list
| Note | Meaning |
|---|---|
try | installed with snap try |
broken | the snap cannot be read, for example the tried directory is gone; its version then shows as - |
devmode, jailmode, classic | the flag the snap was installed with |
A tried snap and a snap installed with --dangerous show - for Tracking and Publisher.
for snapd developers
In the code
| What | Where |
|---|---|
snap try in the daemon, change kind try-snap | daemon/api_sideload_n_try.go:971-1002 |
snapstate.TryPath sets TryMode | overlord/snapstate/snapstate.go:851-856 |
| the symlink in place of the snap file | snap/snapdir/snapdir.go:68-72 |
| the bind mount unit | systemd/systemd.go:1587-1591 |
TryMode kept across undo as old-trymode | overlord/snapstate/handlers.go:1915-1916, 2117, 2530-2531, 2634 |
| pack validation | snap/pack/pack.go:244-293 |
real plug and slot sanitization in snap pack | cmd/snapd/cli/cmd_pack.go:87-89, snap/info_snap_yaml.go:284 |
flags against confinement | overlord/snapstate/check_snap.go:93-129 |
| try-mode and id-less snaps left out of refresh | overlord/snapstate/storehelpers.go:331-347 |
| a broken snap's info, built from its side info | overlord/snapstate/snapmgr.go:632-650 |
A known limitation, kept as a test: adding meta/hooks/pre-refresh to a directory that is already tried makes the next snap try fail, because both revisions are the same directory (tests/main/try-with-hooks).
Further reading
- Test snaps with snap try: the how-to, including which changes need a reinstall.
- Install modes:
--dangerous,--devmode,--jailmodeand--classicfrom the user's side. - Revisions: store revisions and local
x<n>revisions. - The snap format: the
meta/skeleton thatsnap packchecks. - Snap components:
snap packfor a component and the.compfile name. - Test snapd fixes: a real
snap install --dangerousof a locally built snapd snap.
No published page explains snap pack as a topic; this deck and snap pack --help are the description.
Roads from here
START HERE
- snapd — back to the overview
GETTING SNAPS
- Snap packaging — what is inside the file and how local revisions x1 and x2 are numbered
INTERFACES AND CONFINEMENT
- Confinement — what strict and devmode and classic mean for a running process
POLICY · who may install and connect what
- When snapd asks — how interface policy treats a snap without a snap-id
ADMINISTRATION · managing a system of snaps
- Components — packing and installing a component file
DEVELOPING SNAPS · try, pack, run, debug
- Running and debugging an app — run the snap you just installed and look inside its sandbox
- Apps, command lines and environment — the app entries that pack checks and what they become
- Layouts — make fixed paths appear inside the snap
- System usernames — give a daemon a user to drop to