What’s in a snap?
by Igor Ljubuncic on 26 March 2021
Snaps are several things, all at once. They are confined, standalone Linux applications that bundle all their necessary dependencies, which means they do not need to rely on the underlying system, and can run independently of it. Snaps are also packaged as compressed Squashfs filesystems, using the .snap extension. For most users, they are an abstraction to get software on their Linux distro, in a simple, straightforward manner. But you may wonder, what lurks inside?
Unpacking the squashFS filesystem
If you want to check the contents of a snap, you can. First, download it, then extract the snap using the unsquashfs tool on the command line. A large number of Linux distributions should have this utility installed, or available in the archives, and should support the compression methods used for snaps (xz and lzo).
snap download “snap name”
unsquashfs “snap name”
Without any other arguments provided, the snap (with the .snap extension) will be uncompressed into a folder named squashfs-root in the current working directory.
Parallel unsquashfs: Using 8 processors
563 inodes (697 blocks) to write
[=========================================|] 697/697 100%
created 500 files
created 166 directories
created 62 symlinks
created 0 devices
created 0 fifos
The contents of a snap
To allow applications to run independently of the underlying system, snaps create an isolated environment for the contained applications. For strictly confined snaps, the application will see the base snap as its root filesystem, and thus expect to see the traditional layout of folders inside its environment. Typically, the squashfs-root will contain different folders like /bin, /lib, /usr, and others.
drwxr-xr-x 9 igor igor 4096 Mar 9 14:49 ./
drwxrwxrwx 11 igor igor 4096 Mar 23 12:21 ../
drwxr-xr-x 5 igor igor 4096 Mar 9 14:48 etc/
drwxr-xr-x 2 igor igor 4096 Mar 9 14:48 kf5/
-rwxr-xr-x 1 igor igor 8428 Mar 9 14:43 kf5-launch*
-rwxr-xr-x 1 igor igor 3783 Mar 9 14:43 kf5-locale-gen*
-rwxr-xr-x 1 igor igor 191 Mar 9 14:43 kf5-portal-launch*
drwxr-xr-x 3 igor igor 4096 Mar 9 14:48 lib/
drwxr-xr-x 2 igor igor 4096 Mar 9 14:48 lib64/
drwxr-xr-x 3 igor igor 4096 Mar 9 14:49 meta/
drwxr-xr-x 3 igor igor 4096 Mar 9 14:49 snap/
drwxr-xr-x 5 igor igor 4096 Mar 9 14:48 usr
Some of the paths will be obvious and familiar. Some, including various files, will be different.
Application entry point
When developers package snaps, they need to declare the application name and command in the snapcraft.yaml file. For the most part, there is no strict requirement how these should be specified, and the developers can use any variation they like. The application command does not have to be identical to what it could look like using the traditional Linux packaging.
Furthermore, snapcraft also helps the developers create some of the necessary application runtime environment elements, including different variables and relative paths. You can inspect what this setup looks like by checking any script available inside the snap.
This folder contains various setup and declarative files that describe the snap’s behavior. It can include several components: snap.yaml file that contains the basic snap details, a list of plugs and slots, any daemons, and more; hooks that are triggered on specific events; GUI elements like icon files and desktop files for the snap.
The snap directory (if present) can contain the command-chain declaration, a list of commands that are run before the actual application(s) bundled inside the snap. This can help set up the environment that snaps need, without having to resort to convoluted use of wrapper scripts.
Your snap (or a snap) could have only a handful of folders, or a whole range of them, including custom paths and structure. The contents will differ from one application to another, but at the end of the day, there is no great mystery about what snaps are. Anyone can download, unpack and inspect any snap, and see exactly what they do. In many cases, you will have an exact trail of environment variables and commands that run. If you want to learn a bit more about what else to expect inside a snap, you may want to check the snap format documentation for other useful details.
Finally, if you want to experiment, you can try the handy snap try command – to make quick, live changes to snaps, and test modified behavior. This can be quite useful if you want to speed up your development. That’s all we had today. If you have any comments or suggestions, please join our forum for a discussion.