Can you access the gadget's defaults section dynamically

Can the defaults section of a gadget snap be accessed and modified dynamically?

i.e.

<gadget-snap-id>.defaults.<snap-id>.key = "some value obtained at run time"

Perhaps an explanation of what I am trying to achieve would help.

I have a snap that has an install hook that will set values based on keys in the gadget file defaults section.

...
defaults:
  <snap_id>:
    passphrase: hardcoded_passphrase
...

This work fine if I want to use a hardcoded value. And the same value for every device. However, I want this value to NOT be hardcoded and be based on a dynamic device value - in this case the mac id of eth0, which is stored in /sys/class/net/eth0/address.

So, I was hoping that in the prepare_device hook, I could read this value and then set the appropriate defaults key something like:

<gadget-name>.defaults.<snapid>.passphrase = “$(cat /sys/class/net/eth0/address)”

This would effectively let me dynamically set the default passphrase for each individual device with nothing hardcoded or having to set that value manually in each gadget file and creating individual images for every device.

Thanks for any help or suggestions

No they cannot. However you can just set the config item you want from the install hook, i.e. something like

#!/bin/sh

snapctl set passphrase="$(cat /sys/class/net/eth0/address)"

Would that work for your use case?

Also, just as an FYI I would recommend moving this topic to either the #devices or the #snapd categories where it will get more views from relevant folks.

Thanks for the quick reply.

I started there - part of the install hook - however I cannot get that to work. I’ll explain my results and perhaps you could offer a solution?

I had added the code to the install hook. And, since I am accessing the /sys/ directory and a network device, I have added the requisite hardware-observe and network-observe plugs. However, during install, I still get an access denied error:

= AppArmor =
Time: May 29 21:49:29
Log: apparmor=“DENIED” operation=“open” profile=“snap.pi-wifi-connect.daemon” name="/sys/devices/pci0000:00/0000:00:13.1/0000:02:00.0/net/eth0/address" pid=6840 comm=“cat” requested_mask=“r” denied_mask=“r” fsuid=1000 ouid=0
File: /sys/devices/pci0000:00/0000:00:13.1/0000:02:00.0/net/eth0/address (read)
Suggestions:

  • adjust program to not access ‘/sys/devices/pci0000:00/0000:00:13.1/0000:02:00.0/net/eth0/address’
  • adjust program to not access ‘/sys/devices/pci[0-9]:[0-9]/[0-9]:[0-9]:[0-9].[0-9]/[0-9]:[0-9]:[0-9].[0-9]/net/eth[0-9]*/address’

I have read numerous places that all that is required to access the /sys/class/net/* files is hardware-observe and network-observe (I have even added system-observe) but to no avail.

Any thoughts, suggestions, ideas??

Thanks!

Did you add the plugs to the install hook with somethign like:

hooks:
    install:
        plugs:
            - hardware-observe
            - network-observe

?

yes.

hooks:
  install:
    plugs: [network-observe, hardware-observe, system-observe]

did you make sure to have a store declaration defined in your brand store for these interfaces ?

none of them do autoconnect IIRC and i’m not sure a connection statement in the gadget.yaml is sufficient for getting them connected …