Monitor snap changes

1. Introduction

There’s a lot more to snap packaging than the simple installation and removal of snaps; you can grant or revoke an application’s access to system resources, reconfigure internal parameters, make a local copy of s snap and check a snap’s provenance.

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.

:information_source: We recommend you familiarise yourself with basic snap usage before reading this tutorial. See Getting started for further details.

You’ll learn:

  • what a change is, in snapd terminology
  • how changes are viewed
  • how changes relate to snapd activity
  • how to monitor and abort actions

What you’ll need

  • GNU/Linux with snap installed (see Installing snapd)
  • some basic command line knowledge

How will you use this tutorial?

What is your current level of experience?


2. Environment setup

Snap provides plenty of background information about what’s happening when a snap is install and removed, but we’re first going to login and install a test snap to work with.

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 via the command line:

Personal information is handled as per our privacy notice at
https://www.ubuntu.com/legal/dataprivacy/snap-store

E-mail address: 
Password of <user>:
Two-factor code: 155717
Login successful

:information_source: 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.

Typing snap logout will log the current user out.

Install a test snap

Let’s install the chuck-norris-webserver snap, which a good test-case for a variety of snap functionality:

$ snap install chuck-norris-webserver
chuck-norris-webserver (stable) 1.0 from 'didrocks' installed

Though it’s not obvious, installation involves many small steps. You get some impression of these with the messages that briefly appear.

In the next step, we’ll decipher those steps. But to set the scene, we first need to remove the snap:

$ snap remove chuck-norris-webserver
chuck-norris-webserver removed

3. Viewing changes

Let’s try to reinstall the same snap again, but cancel it right away with Ctrl+c:

$ snap install chuck-norris-webserver
Setup snap "chuck-norris-webserver" (16) security profiles                                                           -Automatically connect eligible plugs and slots of snap "chuck-norris-webserver"                                      \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 fine out happened on the system. These can be shown by running running snap changes:

$ snap changes
ID   Status  Spawn               Ready               Summary
406  Done    today at 12:07 GMT  today at 12:07 GMT  Install "chuck-norris-webserver" snap
407  Done    today at 12:07 GMT  today at 12:07 GMT  Remove "chuck-norris-webserver" snap
408  Undone  today at 12:15 GMT  today at 12:15 GMT  Install "chuck-norris-webserver" snap

The last line shows us clearly that the transaction install “chuck-norris-webserver” snap was Undone. The status would have been Doing while the transaction was happening (just before we pressed Ctrl+x).

If we ran snap changes during installation, we’d get something like the following:

$ snap changes
ID   Status  Spawn               Ready               Summary
406  Done    today at 12:07 GMT  today at 12:07 GMT  Install "chuck-norris-webserver" snap
407  Done    today at 12:07 GMT  today at 12:07 GMT  Remove "chuck-norris-webserver" snap
408  Doing   today at 12:15 GMT  today at 12:15 GMT  Install "chuck-norris-webserver" snap
409  Doing   today at 12:20 GMT  -                   Install "chuck-norris-webserver" snap

The snap changes command displays a summary of recent system changes. But we can also dig into even more details…


4. 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 408. We can see what the transaction was supposed to run, and what failed by issuing the following command:

$ snap change 408
Status  Spawn               Ready               Summary
Done    today at 12:15 GMT  today at 12:15 GMT  Ensure prerequisites for "chuck-norris-webserver" are available
Undone  today at 12:15 GMT  today at 12:15 GMT  Download snap "chuck-norris-webserver" (16) from channel "stable"
Done    today at 12:15 GMT  today at 12:15 GMT  Fetch and check assertions for snap "chuck-norris-webserver" (16)
Undone  today at 12:15 GMT  today at 12:15 GMT  Mount snap "chuck-norris-webserver" (16)
Hold    today at 12:15 GMT  today at 12:15 GMT  Copy snap "chuck-norris-webserver" data
Hold    today at 12:15 GMT  today at 12:15 GMT  Setup snap "chuck-norris-webserver" (16) security profiles
Hold    today at 12:15 GMT  today at 12:15 GMT  Make snap "chuck-norris-webserver" (16) available to the system
Hold    today at 12:15 GMT  today at 12:15 GMT  Automatically connect eligible plugs and slots of snap "chuck-norris-webserver"
Hold    today at 12:15 GMT  today at 12:15 GMT  Set automatic aliases for snap "chuck-norris-webserver"
Hold    today at 12:15 GMT  today at 12:15 GMT  Setup snap "chuck-norris-webserver" aliases
Hold    today at 12:15 GMT  today at 12:15 GMT  Run install hook of "chuck-norris-webserver" snap if present
Hold    today at 12:15 GMT  today at 12:15 GMT  Start snap "chuck-norris-webserver" (16) services
Hold    today at 12:15 GMT  today at 12:15 GMT  Run configure hook of "chuck-norris-webserver" snap if present
Hold    today at 12:15 GMT  today at 12:15 GMT  Run health check of "chuck-norris-webserver" snap

The output to a specific change details every processed step (Done), the cancelled step (Undone) and the steps that didn’t get a chance to execute (Hold).


5. 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

6. Probing a change

Let’s now install chuck-norris-webserver again and check each step.

$ snap install chuck-norris-webserver
chuck-norris-webserver (stable) 1.0 from 'didrocks' installed

$ snap change 419
Status  Spawn               Ready               Summary
Done    today at 16:03 GMT  today at 16:03 GMT  Ensure prerequisites for "chuck-norris-webserver" are available
Done    today at 16:03 GMT  today at 16:03 GMT  Download snap "chuck-norris-webserver" (16) from channel "stable"
Done    today at 16:03 GMT  today at 16:03 GMT  Fetch and check assertions for snap "chuck-norris-webserver" (16)
Done    today at 16:03 GMT  today at 16:03 GMT  Mount snap "chuck-norris-webserver" (16)
Done    today at 16:03 GMT  today at 16:03 GMT  Copy snap "chuck-norris-webserver" data
Done    today at 16:03 GMT  today at 16:03 GMT  Setup snap "chuck-norris-webserver" (16) security profiles
Done    today at 16:03 GMT  today at 16:03 GMT  Make snap "chuck-norris-webserver" (16) available to the system
Done    today at 16:03 GMT  today at 16:03 GMT  Automatically connect eligible plugs and slots of snap "chuck-norris-webserver"
Done    today at 16:03 GMT  today at 16:03 GMT  Set automatic aliases for snap "chuck-norris-webserver"
Done    today at 16:03 GMT  today at 16:03 GMT  Setup snap "chuck-norris-webserver" aliases
Done    today at 16:03 GMT  today at 16:03 GMT  Run install hook of "chuck-norris-webserver" snap if present
Done    today at 16:03 GMT  today at 16:03 GMT  Start snap "chuck-norris-webserver" (16) services
Done    today at 16:03 GMT  today at 16:03 GMT  Run configure hook of "chuck-norris-webserver" snap if present
Done    today at 16:03 GMT  today at 16:03 GMT  Run health check of "chuck-norris-webserver" snap
Done    today at 16:03 GMT  today at 16:03 GMT  Connect chuck-norris-webserver:network-bind to core:network-bind
Done    today at 16:03 GMT  today at 16:03 GMT  Connect chuck-norris-webserver:network to core:network
Done    today at 16:03 GMT  today at 16:03 GMT  Setup snap "chuck-norris-webserver" (16) security profiles for auto-connections

Here’s a breakdown of what’s happening when we install chuck-norris-webserver:

  • 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.

:information_source: The above only covers an installation phase. However, changes are similar while doing refresh, remove and revert. Try them out and take a look at what’s different.


7. Next steps

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:

Finally, you can find our friendly and welcoming community at https://forum.snapcraft.io.