[systemd-devel] System power control

Lennart Poettering lennart at poettering.net
Sun Aug 12 16:09:43 PDT 2012


On Sun, 12.08.12 17:55, David Lambert (dave at lambsys.com) wrote:

> I am developing a battery backed up cape for a Beaglebone project
> which uses systemd under Angstrom. In order to conserve battery
> life, I would like to signal my battery control hardware via a GPIO
> when system shutdown is complete and all file systems have been
> dismounted. Is there a "hook" in systemd which will allow me to call
> a small user helper program at this point. Looking at the shutdown.c
> source, I see the following code:
> 
>         if (access("/run/initramfs/shutdown", X_OK) == 0) {
> 
>                 if (prepare_new_root() >= 0 &&
>                     pivot_to_new_root() >= 0) {
>                         execv("/shutdown", argv);
>                         log_error("Failed to execute shutdown binary: %m");
>                 }
>         }
> 
> Is the intention of this code to provide such a hook? Can I cheat by
> creating /run/initramfs/ just prior to shutdown and copy a small
> shutdown executable of my own here. This sounds like it may work,
> but seems like a terrible hack. I feel that there should be a more
> elegant way to achieve this goal.

No, that's for the initrd to undo the storage setup it did at boot.

If you look a tiny bit above the code you quoted you will find the right
way to do this: right before shutdown we execute all executables from
/usr/lib/systemd/system-shutdown/. Just drop int your stuff thre, mark
it executable, and you are done.

This is documented in systemd-halt.service(8) in more detail.

Note that using this for powering down things is a racy and unsafe
though. Some things maintained by the kernel are only synced to disk
when the actual kernel system call reboot(2) is invoked. That means that
you should be careful only to shut off power after that system call has
been entered and finished this syncing bit. Effectively this means that
you need to do your power down logic in the kernel, as this cannot be
done safely and race-freely from usersapce.

Hope this is helpful.

Lennart

-- 
Lennart Poettering - Red Hat, Inc.


More information about the systemd-devel mailing list