Snap creating a daemon that reboots Ubuntu Core

Hi, I’m trying to create a snap which launches a daemon that gets activated upon reboot and, after sleeping for some minutes, reboots the Ubuntu Core once again.

I want to flash a custom ubuntu core image, containing the snap at issue, onto an USB drive, so as to have the daemon launched upon boot on the host machine. The goal is to make the machine reboot automatically and periodically to carry out a sort of stress test.

I found this topic in the forum: Shutdown Ubuntu Core running in chroot (snap classic) . However, since I am newbie to bash, I am not sure whether the solution is right for my application.

Can you give me some insights about the matter please?

Thank you in advance

For permissions you can use the shutdown interface but this interface is privileged and you will need additional help to grant your snap access to use it.

Once you have the interface you need to make a dbus call to the org.freedesktop.systemd1.Manager.Reboot method, on to the object /org/freedesktop/systemd1 on the system bus.

You need to decide when to reboot, as to avoid reboot loops or other possible mistakes.

I see… What do you mean by “additional help”? About the dbus call, instead, should that be embedded in a bash script? Could you please provide an example of code?

This is here (and in a bunch of other posts you can find simply searching for “reboot”):

Yes I saw this, however I don’t really understand whether I need the netplan.yaml file for my purposes. If the answer is yes, what should it contain?

No, it is just example code, back when it was created netplan was not easily able to apply new configs without reboot (when the config change came from a snap) … i solely pointed to it for the few lines of the reboot code, ignore the rest

I tried creating a simple bash snap containing only the command:

dbus-send --system --print-reply \
                    --dest=org.freedesktop.login1 /org/freedesktop/login1 \
                    "org.freedesktop.login1.Manager.Reboot" boolean:true

Then I installed it on my ubuntu desktop machine so as to try it before including it into an ubuntu core image. However the daemon is not able to reboot the machine, stating: “Permission denied”.
How to solve this?

Did you add the shutdown interface in your snapcraft.yaml and connected it after installing the snap?

Okay, thank you, now it’s working! I manually connected the snap to the shutdown interface. However I’m wandering: should I establish this connection manually also in ubuntu core? Or is it automatically connected?

1 Like

If you have a brand store, you should be able to set a snap declaration for the snap to have it auto-connect at package install time (or request it via a store support tixket)

Is this the only way? Is it possible to declare it in the snapcraft.yaml?

you could build your own gadget (and image) and use the connections: statement in gadget.yaml for this … but that hard-requires that your app is uploaded to the store since this mechanism operates by snap ID (which you only get for officially uploaded snaps) …

beyond this, you could hack up your own (un-snapped) shell script and systemd unit to make that connection from the cmdline …