How it works...

Power management is part of the Linux kernel; that is why we cannot use a Docker container to work with it. Docker virtualization is lightweight and uses the kernel of the host operating system.

We cannot use the real Raspberry Pi board either, because it does not provide any sleep modes at all because of hardware limitations. QEMU, however, provides full virtualization, including power management in the kernel we use to emulate Raspberry Pi.

Linux provides access to its power management functions through the sysfs interface. Applications can read and write text files in the /sys/power directory. Access to power-management functions is limited for the root user; that is why we need to get the root shell once we log into the system:

$ sudo bash

Now we can get the list of supported sleep modes. To do this, we read the /sys/power/state file: 

$ cat /sys/power/state

The file consists of a single line of text. Each word represents a sleep mode that is supported, with the modes separated by spaces. We can see that the QEMU kernel supports two modes: freeze and mem:

Freeze represents the S2I state we discussed in the preceding section. The meaning of mem is defined by the content of the /sys/power/mem_sleep file. In our system, it contains only [s2idle], representing the same S2I state as freeze.

Let's switch our emulator to freeze mode. We write the word freeze to /sys/power/state, and immediately the QEMU window turns black and frozen:

We were able to put the emulated Linux system to sleep, but cannot wake it up—there are no sources of interrupts that it can understand. We learned about different sleep modes and the kernel API to work with them. Based on the requirements of your embedded system, you can use these modes to reduce power consumption.