There’s a lot more to snap packaging than the simple installation and removal of snaps. This is one of several tutorials that cover more advanced snap usage and covers how to monitor the internal state of changes within snapd, the daemon that manages the snap packaging system.
Requirements
This tutorial is suitable for anyone running Ubuntu (or any other system that supports snaps).
You should also be familiar with the concepts covered in the Quickstart guide, and also with basic terminal and command line functions.
Login for convenience
By first logging in to your online snap account you remove the need to use sudo
with privileged snap commands like install, refresh, remove and revert.
If you haven’t already, create a snap account at https://login.ubuntu.com/ and login with the snap login
command:
$ snap login
Personal information is handled as per our privacy notice at
https://www.ubuntu.com/legal/dataprivacy/snap-store
E-mail address: <your-email-address>
Password of <user>: <your-password>
Two-factor code: <your-two-factor-code>
Login successful
You will only be asked for a two-factor code if you have it enabled on your account.
Another advantage of being logged in is that you get access to your private snaps. Private snaps allow developers to share snaps with a small group of people, which is useful for beta-testing, for instance.
You can use snap logout
to log the current user out.
Install a test snap
Let’s install the hello snap, which a good test-case for a variety of snap functionality:
snap install hello
Though it’s not obvious, installation involves many small steps. You get some impression of these with the messages that briefly appear before a snap is finally installed.
In the next step, we’ll decipher those steps. But to set the scene, we first need to remove the snap:
snap remove hello
Viewing changes
Let’s try to reinstall the same snap again, but cancel it right away by pressing ctrl+c:
$ snap install hello
error: change finished in status "Undone" with no error message
Snapd cancelled the whole installation transaction, ensuring your system is in the same state as it was before.
Exactly the same? Not totally true because we have access to snap-specific logs to help us find out happened on the system. These can be shown by running snap changes
:
$ snap changes
ID Status Spawn Ready Summary
434 Done today at 16:13 BST today at 16:13 BST Install "hello" snap
435 Done today at 16:24 BST today at 16:24 BST Remove "hello" snap
436 Undone today at 16:24 BST today at 16:24 BST Install "hello" snap
The last line shows us clearly that the transaction install “hello” snap
was Undone. The status would have been Doing while the transaction was happening (just before we pressed Ctrl+c).
If we ran snap changes
during installation, we’d get something like the following:
ID Status Spawn Ready Summary
434 Done today at 16:13 BST today at 16:13 BST Install "hello" snap
435 Done today at 16:24 BST today at 16:24 BST Remove "hello" snap
436 Undone today at 16:24 BST today at 16:24 BST Install "hello" snap
437 Doing today at 16:24 GMT - Install "hello" snap
The snap changes
command displays a summary of recent system changes. But we can also dig into even more details.
Change identifiers
The ID column in the output to snap changes shows the change id_ for each change.
The change cancelled in the previous example has an id of 436. We can see what the transaction was supposed to run, and what failed by issuing the following command:
$ snap change 436
Status Spawn Ready Summary
Done today at 16:24 BST today at 16:24 BST Ensure prerequisites for "hello" are available
Undone today at 16:24 BST today at 16:24 BST Download snap "hello" (43) from channel "stable"
Done today at 16:24 BST today at 16:24 BST Fetch and check assertions for snap "hello" (43)
Undone today at 16:24 BST today at 16:24 BST Mount snap "hello" (43)
Hold today at 16:24 BST today at 16:24 BST Copy snap "hello" data
Hold today at 16:24 BST today at 16:24 BST Setup snap "hello" (43) security profiles
Hold today at 16:24 BST today at 16:24 BST Make snap "hello" (43) available to the system
Hold today at 16:24 BST today at 16:24 BST Automatically connect eligible plugs and slots of snap "hello"
Hold today at 16:24 BST today at 16:24 BST Set automatic aliases for snap "hello"
Hold today at 16:24 BST today at 16:24 BST Setup snap "hello" aliases
Hold today at 16:24 BST today at 16:24 BST Run install hook of "hello" snap if present
Hold today at 16:24 BST today at 16:24 BST Start snap "hello" (43) services
Hold today at 16:24 BST today at 16:24 BST Run configure hook of "hello" snap if present
Hold today at 16:24 BST today at 16:24 BST Run health check of "hello" snap
The output to a specific change details every processed step (Done), the cancelled steps (Undone) and the steps that didn’t get a chance to execute (Hold).
Abort in-progress changes
For changes that may take a long time, such as those requiring a large download across a slow network, you can monitor in-progress transactions with the watch command:
$ snap watch 416
chuck-norris-webserver 2.03 MB / 13.10 MB [=====>------------------] 21.24% 1.75 MB/s 14
[…]
The snap abort command can be used to cancel an in-progress change:
$ snap abort 416
Examining a change
Let’s now install hello
again and check each step.
Status Spawn Ready Summary
Done today at 16:24 BST today at 16:24 BST Ensure prerequisites for "hello" are available
Undone today at 16:24 BST today at 16:24 BST Download snap "hello" (43) from channel "stable"
Done today at 16:24 BST today at 16:24 BST Fetch and check assertions for snap "hello" (43)
Undone today at 16:24 BST today at 16:24 BST Mount snap "hello" (43)
Hold today at 16:24 BST today at 16:24 BST Copy snap "hello" data
Hold today at 16:24 BST today at 16:24 BST Setup snap "hello" (43) security profiles
Hold today at 16:24 BST today at 16:24 BST Make snap "hello" (43) available to the system
Hold today at 16:24 BST today at 16:24 BST Automatically connect eligible plugs and slots of snap "hello"
Hold today at 16:24 BST today at 16:24 BST Set automatic aliases for snap "hello"
Hold today at 16:24 BST today at 16:24 BST Setup snap "hello" aliases
Hold today at 16:24 BST today at 16:24 BST Run install hook of "hello" snap if present
Hold today at 16:24 BST today at 16:24 BST Start snap "hello" (43) services
Hold today at 16:24 BST today at 16:24 BST Run configure hook of "hello" snap if present
Hold today at 16:24 BST today at 16:24 BST Run health check of "hello" snap
Here’s a breakdown of what’s happening when we install hello
:
- the snap file is downloaded from the server.
- snapd downloads and checks for assertions. An assertion is a set of signed permissions to allow installation, interface auto-connection and availability rules.
- the mount step is an internal snapd mechanism that presents the snap as a traditional Linux directory tree structure to your system.
- security profiles are set before snapd checks which interfaces require auto-connection.
- finally, the snap is made available to the system:
- adds binaries to your $PATH and other similar operations
- sets up command aliases.
- starts any services defined for the snap.
- runs any defined configure hooks to dynamically configure services/applications.
The above only covers an installation phase. However, changes are similar while doing
refresh
,remove
andrevert
. Try them out and take a look at what’s different.
Every change that snapd makes is a transaction, and being able to monitor and detail specific changes to your system is a powerful part of the snap system.
To find out more about snap:
- take a look at our Snap documentation
- or try building your own snap with Snapcraft