I use an oldish notebook with a retired monitor and an amplifier I picked up from kerbside junk to watch TV („consume visual media“, if you prefer), and I want all that hardware to be switched on and off using a single power switch (see, um… Figure 1).
Given that the notebook's battery still is good for many minutes, it's a perfectly reasonable stand-in for a UPS. Hence, my problem is quite like the one in the ancient days when big-iron servers had UPSes with just enough juice to let them orderly shut down when the power grid was failing. This used to involve daemons watching a serial line coming in from the UPS. Today, with ACPI in almost every x86 box and batteries in many of them, it's quite a bit simpler.
This post shows how to automatically power (up and) down with acpid. If you run systemd, you probably will have to do a few extra tweaks to keep it from interfering. Please write in if you figure them out or if things just work.
Make the Box Wake Up On Power
The first half is to configure the machine to wake up when mains power returns. Notebooks typically don't do that out of the box, but most ACPI firmwares can be configured that way. On my box, a Thinkpad X230 controlled by a legacy BIOS rather than UEFI, it is a setting in the BIOS setup pages. If you boot through UEFI, you may be able to do this configuration from within the Linux (please write in if you can provide details on that).
Having said that, let me, perhaps only loosely relatedly, mention /proc/acpi/wakeup, which may play a role in this for you (although it does not on the X230). If you cat this file, you will see something like:
LID S4 *enabled platform:PNP0C0D:00 SLPB S3 *enabled platform:PNP0C0E:00 IGBE S4 *enabled pci:0000:00:19.0 EXP3 S4 *disabled pci:0000:00:1c.2 XHCI S3 *enabled pci:0000:00:14.0 EHC1 S3 *enabled pci:0000:00:1d.0 EHC2 S3 *enabled pci:0000:00:1a.0 HDEF S4 *disabled pci:0000:00:1b.0
Whatever is enabled here will wake the machine up, sometimes depending on whether it is hibernating or just suspended. There are various events that could cause a wakeup, such as when the lid is opened (in the ACPI lingo used here, LID), when a Wake-on-LAN packet arrives (IGBE), when the sleep/power button is pressed (SLPB) or when someone puts in a signal via USB (XHCI, EHC1, ECH2; typically, that would be a keyboard). To change this, you echo the respective string into the file, which toggles the enabledness:
$ echo LID | sudo tee /proc/acpi/wakeup LID $ cat /proc/acpi/wakeup | grep LID LID S4 *disabled platform:PNP0C0D:00
If there's no obvious BIOS setting for waking up the machine on power, look for something like PWR in /proc/acpi/wakeup. Incidentally, disabling wakeup sources here may actually conserve battery power when hibernating.
Make the Box Hibernate on Mains Loss
The second half is that the machine should go into hibernation when I flip the central power switch. A straightforward way to get there is to talk to the acpid. It seems it is still standard on PC-style hardware even when there is systemd.
So, let us configure it to call an appropriate script when it switches to battery mode (i.e., the power has gone). You can do that sufficiently well by writing:
# /etc/acpi/events/battery # Called when AC power goes away and we switch to battery event=battery.* action=/etc/acpi/to-battery.sh
to /etc/acpi/events/battery. The Debian-distributed acpid already has that file, but it calls the script power.sh, which, as delivered, does something entirely different; you could modify power.sh to your liking, but it's cleaner to use a different, custom script, for instance, because it is less hassle on dist-upgrades. Disclaimer: This will fire too often, namely both on power up and down. However, at least on my hardware that doesn't hurt, and it doesn't seem acpid generates different events for battery in/out.
Then create the script /etc/acpi/to-battery.sh. I've written this there:
#!/bin/sh sleep 2 if [ `cat /sys/class/power_supply/AC/online` -eq 1 ]; then exit fi # x230 specials; you probably won't need them buslist="pci i2c" for bus in $buslist; do for i in /sys/bus/$bus/devices/*/power/control; do echo on > $i done done logger "powerbutton-acpi-support enter" sync sync echo platform > /sys/power/disk echo disk > /sys/power/state logger "powerbutton-acpi-support leave" (sleep 12; ntpdate pool.ntp.org) & # this is just an example of an extra hack for resetting a TV # card that would be broken after the wakeup. (sleep 2; logger reloading tv; /usr/local/bin/uhubctl -l 1-1 -a cycle) &
This thing first waits two seconds and then ensures AC is really gone before doing anything else; this is because on my box I occasionally received spurious power loss notifications, and hibernating the box just when something interesting was on TV has interrupted the rare moments of enjoyable programming a few times too often. Besides, this will catch cases where the battery event is generated by power coming back.
After that, I'm running a few specials where I enable power management on the PCI and I²C busses of the machine. That has been broken for some reason or another at least on one kernel version or another on this particular box. I've left it the script above in as an inspiration for how you could intervene if something doesn't quite work and needs some fiddling.
It then proceeds to sync the disk, just in case something goes wrong on suspend or resume and eventually does a low-level hibernation. You could probably use pm-hibernate or some systemd facility just as well, but I personally have found the direct operation of /sys/power to be at the same time the least hassle and the least fragile option (at least if you're prepared to write a few lines of script like the bus loop in my example).
The last two commands – an NTP update and a hack to reset a USB device that is confused after a wakeup – are executed as part of the wakeup (but in background shells so the box is quickly responsive again). Adapt to your needs.
Enjoy – and conserve energy by centrally pulling power from all the greedy little wall plug transformers.
|||On the X230, to change it I had to press Enter immediately after power-up, then F1, and then navigate to “Power On with AC Attach“ in the Config pane – but regrettably, there's nothing even resembling a standard there, and given this is tech supposedly obsolete since, what, 15 years, I don't think there will ever be one.|
|||In case you're wondering what HDEF is: Well, it's audio, according to other things ACPI. What I don't know is how to make the audio hardware send a wakeup sinal. I've tried plugging in a headphone, and that didn't work. If you know more… well, again, totally feel free to write in.|