Shutdown Ubuntu Core running in chroot (snap classic)

I’m running a Python notebook inside snap classic environment and I’m using a remote control (infrared) to shut down Ubuntu Core. However, when I run (inside Python notebook):

os.system(“sudo shutdown -h now”)

and

os.system(“init 0”)

Ubuntu Core ignores the request, as chroot does not have root privileges to shut the system down.

Is there a way to provide root privileges to snap classic or even activate a command in Ubuntu Core to do this task ?

This is not a matter of privileges, it is a matter of no init (systemd) running inside the chroot, so that shutdown signal simply doesnt get through to the actual ubuntu core system … one (ugly but working) thing you could do is to have your ssh key inside the chroot and then call something like ```ssh localhost “sudo shutdown”` so you would actually re-login to the undelying os and interact with it through ssh …

Yes, @ogra, I did that via ssh, but my idea was to really shut down Ubuntu Core from inside Python notebook. When I run os.system(“exit && sudo shutdown -h now”) inside py notebook the error “running at chroot. ignoring request” is gone, but it doesnt shut down Ubuntu Core. The problem with ssh is that the command line asks for a root password, so the process would take an additional step besides only pushing a button.

Hi all - this is an old thread, but wondering if there’s any updated way for a snap to initiate a hardware shutdown/restart?

Hitting a wall here with “Running in chroot, ignoring request.” returned on the official Ubuntu Core 18 image on a RasbPi3

Having an SSH key accessible within the snap isn’t an option

the very simple solution is to not use the classic snap anymore, it is not maintained and should actually be removed from the store … it has never been designed to allow more than doing snap development, so you could do snapcraft --destructive-mode in there in the past … since there is a working lxd setup since a few years now, lxd containers should be used for snap development …

regarding the shutdown, this really needs to be done from a snap using the shutdown interface with a simple dbus call like:

1 Like

Excellent, thanks @ogra. Is there any way to request a shutdown with a delay with dbus, similar to:

shutdown +1 “Hardware is going down in 1 minute…”

For anyone else interested, here are the dbus calls to reboot/shutdown from a confined snap:

Reboot:

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

Shutdown:

dbus-send --system --print-reply --dest=org.freedesktop.login1 /org/freedesktop/login1 “org.freedesktop.login1.Manager.PowerOff” boolean:true

Note that you’ll need to list “shutdown” as a plug for the relevant app in snapcraft.yaml and then connect it with:

snap connect your-app:shutdown core:shutdown

1 Like

there seems to be an “*.inhibit-delay-shutdown” as well as a “ScheduleShutdown” described on:

https://www.freedesktop.org/wiki/Software/systemd/logind/

i have not played with either though … not sure if the shutdown interface allows using them.

we’re seeing some differences with the shutdown-reboot d-bus commands between Ubuntu Core running on a Dell Edge Gateway 3002 and a Raspberry Pi 4…the Dell Edge Gateway is returning the following error - any ideas?!

process 18974: arguments to dbus_message_new_method_call() were incorrect, assertion “iface == NULL || _dbus_check_is_valid_interface (iface)” failed in file …/…/dbus/dbus-message.c line 1346.
This is normally a bug in some application using the D-Bus library.
process 18974: arguments to dbus_message_set_auto_start() were incorrect, assertion “message != NULL” failed in file …/…/dbus/dbus-message.c line 3065.
This is normally a bug in some application using the D-Bus library.
Couldn’t allocate D-Bus message

no, sorry, are the Ubuntu Core versions the same ?

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 @ogra . 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