Prepare-device hook questions

I have built my first gadget snap for a custom image, and I want to utilize the prepare-device hook to set up some things that I (think I) cannot achieve through gadget.yaml declarations.

My requirements:

  1. Set the hostname through nmcli (or hostnamectl)
  2. copy over a custom miral-kiosk.display config file to use with mir-kiosk (the file has additional layouts)
  3. Edit the swapfile to enable 1GB swap on the system

The hook code:

#! /bin/sh -e

#/usr/bin/hostnamectl set-hostname 'myhost'
nmcli g hostname 'myhost'
nmcli d set wlan0 autoconnect yes

# Enable swap with 1GB
cp /etc/default/swapfile .
sed -i.bak -e "s/^SIZE=0$/SIZE=1024/"swapfile
cp swapfile /etc/default/swapfile
rm swapfile swapfile.bak
systemctl enable --now swapfile.service

cp miral-kiosk.display /var/snap/mir-kiosk/current/

I’ve added this stanza to my gadget’s snap/snapcraft.yaml:

hooks:
  prepare-device:
    plugs:
      -  hostname-control
      - network-manager

All examples I can find for prepare-device deal with serial registration. Nothing from the hook seems to have any effect. Is it possible to do the above in this hook at all? To understand this better, I’d like to know:

  • When is the hook called during setup? (have all snaps been installed, interfaces connected …)
  • Which privileges does it have (root)?
  • Are there better ways to achieve things that would normally require system-user commands on a TTY after the device has been set up?

i’d actualy go with a configuration snap for the devicename bits, there are some examples linked from my latest blog post about building self-configuring appliances at:

particulary check the links to dashkiosk-image-config and dashkiosk-client-image-config …

for enabling swap we should probably have an interface that allows access to /etc/default/swapfile (core-swapfile-control ??) you should not need to do anything but adding the SIZE= to it, a swapfile should be created on next boot automatically … i’m not sure how hard it would be to add such an interface, @jdstrand might be able to judge that, but given it would only need to allow access to that one file i imagine it would be simple and straightforward …

Thanks, your blog was missing in my feed reader :slight_smile: The hostname stuff seems doable from within my snap, and swap is not crucial right now – but how would I go about the Mir kiosk config file that needs to be copied into the mir-kiosk snap’s current directory? If I’m not mistaken, by design only mir-kiosk (and and the system-user via sudo) would have access to its $SNAP_* writable directories.

if you actually need to go beyond the Mir autodetection and supply a specific display configuration, i think there is a way to feed the content of the whole file as a string into a config key, probably @alan_g or @greyback can point you to the right documentation for this feature.

There does seem to be a gap in the documentation. Most of the display configuration options are described in Display Configuration for mir-kiosk

The thing that is not mentioned is the ability to supply the configuration as a configuration key:

snap set mir-kiosk display-config=...

Putting the whole display configuration file into a string is left as “an exercise for the reader”.

2 Likes

Belated reply to whomever has the same use-case :smile: While my initial experiments with the suggested configuration key failed (back in late 2019), passing the display config via gadget.yaml defaults works fine in a core20 gadget with the current stable snapd + mir-kiosk.

Alan’s docs for mir-kiosk configuration were also updated with example code, thanks @alan_g and @kyleN !

1 Like