Reference to unknown variable "$SNAP_USER_DATA" on layout

I’m trying to follow Olivier’s recipe to address smart card bug in Chromium but I’m having issues with

  • add a layout for /usr/lib/pkcs11 pointing to a writeable area of the snap (e.g. $SNAP_USER_DATA/.local/lib)

Added to snapcraft.yaml:

  /usr/lib/$SNAPCRAFT_ARCH_TRIPLET/pkcs11:
    bind: $SNAP_USER_DATA/.local/lib

Got:

error: cannot pack “/build/chromium/prime”: cannot validate snap “chromium”: layout “/usr/lib/x86_64-linux-gnu/pkcs11” uses invalid bind mount source “$SNAP_USER_DATA/.local/lib”: reference to unknown variable “$SNAP_USER_DATA”

Did I misunderstand the instruction or did I miscarry it?

$SNAP_USER_DATA is only set at runtime, not at build time … better use a symlink from an upgrade or install hook or even a command-chain wrapper script for this …

1 Like

Thanks ogra, I’ll try that.

I indeed jumped to fast to the question, for the record this is documented:

symlink: <source-path>
bind: <source-path>
bind-file: <source-path>

<source-path> must refer to either $SNAP, $SNAP_DATA or $SNAP_COMMON.

But those three are /snap/hello-world/27, /var/snap/hello-world/27, /var/snap/hello-world/common. (So as you said, not possible at build time.)

That seems to work, but with this install hook:

#!/bin/sh -u
ln -s "/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/pkcs11/" "$SNAP_USER_DATA/.local"

I get

ERROR run hook “install”: /snap/tali/x1/snap/hooks/install: 2: SNAPCRAFT_ARCH_TRIPLET: parameter not set

well, $SNAPCRAFT_* variables are build time vars … it will not carry any value at runtime …

you can deduct the arch from the $SNAP_ARCH variable, but it is a bit awkward since you need to craft the triplet itself from i.e. a case function in the script.

or you could dynamically sed the hook content at build time using the SNAPCRAFT_ARCH_TRIPLET variable …

1 Like

I wrote the hook and in snap change ID I see it runs:

Run install hook of “chromium” snap if present

However, it actually did nothing:

% ls -l ~/snap/chromium
total 0
lrwxrwxrwx 1 nteodosio nteodosio 4 fev  7 15:12 current -> 2312

Probably because current is a dangling symlink.

Only after launching Chromium for the first time is the target created and populated.

All in all, if $SNAP_USER_DATA is empty during install, a install hook no good here?

hmm, indeed, i didn’t think about that but the hooks run as root and not as the user executing the install (you might find the link in /root), i guess the link should rather come from some startup wrapper.

1 Like