Turning off balenaOS from container

Is there a way how to halt from a container the whole balenaOS? It runs on raspberryPi 3. I have a simple python script, that needs to turn off raspberryPi when a condition is true. The container runs in privileged mode.

Hi,
Yes, you should be able to. If you connect to the systemd daemon running on the hostOS via dbus (https://www.balena.io/docs/learn/develop/runtime/#dbus-communication-with-host-os), you can trigger the service named systemd-halt which will halt the entire system.
Kind regards,
Theodor

Ok, I added to my Dockerfile ENV DBUS_SYSTEM_BUS_ADDRESS=unix:path=/host/run/dbus/system_bus_socket
No it only needs to call “systemd-halt” in container?

Hello, you need to use dbus to comunicate with the hostOS systemd. Something like

DBUS_SYSTEM_BUS_ADDRESS=unix:path=/host/run/dbus/system_bus_socket \
      dbus-send \
      --system \
      --print-reply \
      --dest=org.freedesktop.systemd1 \
      /org/freedesktop/systemd1 \
      org.freedesktop.systemd1.Manager.Halt

should do the trick.
Remember that the dbus-send command might not be available in your OS (it is included in the dbus package) you might need to install that from your Dockerfile if you are using the Debian image, or check under what name does your chosen operating system supply the dbus-send executable.

Ok, and which command should I use in container to Halt system?

Going byhttps://www.freedesktop.org/wiki/Software/systemd/dbus/ it should be Halt()

Hey did you manage to get this working? The dbus-send command I pasted above should work from the application container (you also don’t need to redeclare the DBUS_SYSTEM_ADDRESS env var if its already exported)

Yep, it works …