Accessing various files within a snap

Hi,
I am new to snaps. I have been fiddling with creating a snap for openscap
and have hit a problem I am not able to readily figure out… I am using
snapcraft, version 2.42.1.

The oscap command in my snap requires schema files that usually live in
/share/openscap/schemas. This directory path is hardcoded in the oscap code using the prefix
set by configure script. When I attempt to run my snap, I get the error,

OpenSCAP Error: Schema file ‘xccdf/1.2/xccdf_1.2.xsd’ not found in path ‘/share/openscap/schemas’ …

I do see this file in the directory in my prime/share/openscap/schemas. I also see it in my /snap/openscap/current directory. But how do I direct the command to this path in snap? I took a look at command-oscap.wrapper… I am not sure what it is I am missing. Any guidance on how to handle this is much appreciated. Hopefully I have given enough info, if not, please let me know. Thanks!

regards,
Joy

By default snapcraft set’s the installation prefix to empty_string, that’s why the regular hardware-independent data files is searched from /share instead of /usr/local/share (the usual default /usr/local is stripped). If you install the part without any configuration, it should install hardware-independent data to /snap/snap_name/current/share, which is $SNAP/share in runtime.

If the snapped application itself supports relocation (i.e. not using hardcoded paths), it should locate it’s data files from any of the XDG_DATA_DIRS, which should be set properly by the desktop-launch script of the snapcrafters/snapcraft-desktop-helpers:

apps:
  audio-recorder:
    command: desktop-launch audio-recorder

REFER: Prepend $SNAP/share to XDG_DATA_DIRS by Lin-Buo-Ren · Pull Request #121 · ubuntu/snapcraft-desktop-helpers

If the snapped application, like most of them, unfortunately, does not support relocation, you can configure it to hardcode it’s --datarootdir via the configure script to let it use /snap/snap_name/current/share as the --datarootdir as long as the build system is autotools:

parts:
  audio-recorder:
    plugin: autotools
    configflags:
    - --datarootdir=/snap/audio-recorder/current/share

This will have an adverse effect that the autotools installation recipe will then install its data files under $SNAPCRAFT_PART_INSTALL/snap/_snap_name_/current/share which will in turn become $SNAPCRAFT_STAGE/snap/_snap_name_/current/share and $SNAP/snap/_snap_name_/current/share id. est. /snap/_snap_name_/current/snap/_snap_name_/current/share, which can be fixed by using the organize keyword:

parts:
  audio-recorder:
    organize:
      snap/audio-recorder/current/: /

This should be enough to make the snapped application locate it’s hardware-independent data files, enjoy!

Hello,

  Thanks for this explanation, it helped my understanding. I must mention that the code generates the path to the schemas using "${prefix}/share/openscap/schemas". Thus it is using the prefix set by configure. Since snapcraft sets prefix to the empty string (I did observe this in my config.log), the code generated the path "/share/openscap/schemas" to the schemas. Unfortunately, it does not use datarootdir.

  So, I am thinking perhaps the only way to get the code to generate the paths I need is to perhaps set "prefix" the way "datarootdir" is set below. But I am thinking changing "prefix" might be messy... but perhaps I could use organize keyword to "fix" things...?

regards,

Joy

My snap is now working. I used,
configflags:
prefix=/snap/openscap/current

organize:
snap/openscap/current: /

However, I am curious about the shared libraries I see pulled into prime/usr/lib/x86_64-linux-gnu/ directory. I did use “stage-packages” for a few library packages and I understand these would become part of the final snap. But I am somewhat confused as to why I see libraries that are in the core also in my prime directory? I thought snaps were built and run against the core?

Thanks for all help!

1 Like