SNAP_COMMON looks different from snap shell

I have a configure hook that writes to a log file in SNAP_COMMON.

The log file is not visible from the normal shell in /var/snap/mysnap/common/

It is is visible from the snap shell (sudo snap run --shell mysnap) in SNAP_COMMON.

Is this expected? (If memory serves, some time ago there was no difference in the files and their contents based on whether one was in snap shell or normal terminal.)

2 Likes

Same here. I have added a configure hook:

#!/bin/sh -e

OPENFORTIVPN_TEMPLATE="${SNAP}/share/openfortivpn/config.template"
OPENFORTIVPN_CONFIG="${SNAP_COMMON}/config"

if [ -f "$OPENFORTIVPN_CONFIG" ]; then
	echo "Existing configuration file will not be overwritten"
	exit 0
else
	if [ ! -d "$SNAP_COMMON" ]; then
		mkdir "$SNAP_COMMON"
	fi
	sed -e '3,$ s/^/# /' "$OPENFORTIVPN_TEMPLATE" > "$OPENFORTIVPN_CONFIG"
	chmod 600 "$OPENFORTIVPN_CONFIG"
fi

Just after installing the snap, I can the config in $SNAP_COMMON from within the snap):

# sudo snap run --shell openfortivpn
# ls -l $SNAP_COMMON 
total 4
-rw------- 1 root root 147 Apr  7 14:53 config
# 

The $SNAP_COMMON does not exist outside the snap:

$ ls -l /snap/openfortivpn
total 0
lrwxrwxrwx 1 root root  2 avril  7 14:53 current -> x1
drwxr-xr-x 8 root root 85 avril  7 13:29 x1
$ 

Sometimes $SNAP_COMMON appears with reason, I cannot find what triggers this. It is extremely confusing, I’ve spent more than a day trying to understand why the configure hook appears not to be working. Any clue? How can we explain or debug this?

I had forgotten about my original post above and I’ve used configure hooks many times since then and they work.

Couple of points, just in case you are not already aware:

You should not ever need to create SNAP_COMMON directory. This directory is auto created by snapd on snap install and should always exist. So this is not appropriate mkdir "$SNAP_COMMON"

Also, a configure hook script (which is in the snap project source dir as snap/hooks/configure) is never called manually as you may be doing above. It is executed by snapd when you run snap set SNAPNAME KEY=VALUE. You then use snapctl get in the configure hook to get the key value (see https://snapcraft.io/docs/using-snapctl).

1 Like

Also just a note that configure hooks are run when the snap is installed or refreshed too

My wrong, I have been mixing up the following two paths - for days!

/snap/openfortivpn/common/
/var/snap/openfortivpn/common/

As already noted elsewhere the word snap appearing everywhere and describing different things (multiple directories, a command, a package) is confusing.

In any case it would be nice to improve the hooks documentation. While I understand the configure script is called differently at install/refresh time (without arguments) and when called using snapctl set, this is not explained clearly and the examples lack a complete configure script.

FWIW, here’s a super simple configure script: https://github.com/knitzsche/test-configure/blob/master/snap/hooks/configure

Ah but that’s the point, I’m not interested (for now) in the set/get case, only the install/refresh case. A script combining both use cases would be great.

If you aren’t using the configure script for the set/get purpose, I recommend you consider using the more appropriate post-refresh and install hooks instead.

1 Like

Ah right, will do. In my case the install and post-refresh are identical: they will install the config file if it does not already exist. Then I will have a look at the configure hook as it might be of interest.

Still, the current documentation is confusing. I was looking for a hook that would be called upon both install and refresh: configure fits the bill.