1# OpenBMC & Systemd
2
3OpenBMC uses [systemd](https://www.freedesktop.org/wiki/Software/systemd/) to
4manage all processes. It has its own set of target and service units to control
5which processes are started. There is a lot of documentation on systemd and to
6do OpenBMC state work, you're going to have to read some of it. Here's the
7highlights:
8
9[Unit](https://www.freedesktop.org/software/systemd/man/systemd.unit.html#) -
10Units are the basic framework of all systemd work.
11[Service](https://www.freedesktop.org/software/systemd/man/systemd.service.html) -
12Services are a type of unit, that define the processes to be run and execute.
13[Target](https://www.freedesktop.org/software/systemd/man/systemd.target.html) -
14Targets are another type of unit, they have two purposes:
15
161. Define synchronization points among services.
172. Define the services that get run for a given target.
18
19On an OpenBMC system, you can go to /lib/systemd/system/ and see all of the
20systemd units on the system. You can easily cat these files to start looking at
21the relationships among them. Service files can also be found in
22/etc/systemd/system and /run/systemd/system as well.
23
24[systemctl](https://www.freedesktop.org/software/systemd/man/systemctl.html) is
25the main tool you use to interact with systemd and its units.
26
27---
28
29## Initial Power
30
31When an OpenBMC system first has power applied, it starts the "default.target"
32unless an alternate target is specified on the kernel command line. In Phosphor
33OpenBMC, there is a link from `default.target` to `multi-user.target`.
34
35You'll find all the phosphor services associated with `multi-user.target`.
36
37## Server Power On
38
39When OpenBMC is used within a server, the
40[obmc-host-start@.target](https://github.com/openbmc/phosphor-state-manager/blob/master/target_files/obmc-host-start%40.target)
41is what drives the boot of the system.
42
43To start it you would run `systemctl start obmc-host-start@0.target`.
44
45If you dig into its .requires relationship, you'll see the following in the file
46system
47
48```
49ls -1 /lib/systemd/system/obmc-host-start@0.target.requires/
50obmc-host-startmin@0.target
51phosphor-reset-host-reboot-attempts@0.service
52```
53
54The
55[obmc-host-startmin@.target](https://github.com/openbmc/phosphor-state-manager/blob/master/target_files/obmc-host-startmin%40.target)
56represents the bare minimum of services and targets required to start the host.
57This target is also utilized in host reboot scenarios. This distinction of a
58host-start and a host-startmin target allows the user to put services in the
59`obmc-host-start@.target` that should only be run on an initial host boot (and
60not run on host reboots). For example, in the output above you can see the user
61only wants to run the `phosphor-reset-host-reboot-attempts@0.service` on a fresh
62host boot attempt.
63
64Next if we look at the `obmc-host-startmin@0.target`, we see this:
65
66```
67ls -1 /lib/systemd/system/obmc-host-startmin@0.target.requires/
68obmc-chassis-poweron@0.target
69start_host@0.service
70```
71
72You can see within `obmc-host-startmin@0.target` that we have another target in
73there, `obmc-chassis-poweron@0.target`, along with a service aptly named
74`start_host@0.service`.
75
76The `obmc-chassis-poweron@0.target` has corresponding services associated with
77it:
78
79```
80ls -1 /lib/systemd/system/obmc-chassis-poweron@0.target.requires/
81op-power-start@0.service
82op-wait-power-on@0.service
83```
84
85If you run `systemctl start obmc-host-start@0.target` then systemd will start
86execution of all of the above associated target and services.
87
88The services have dependencies within them that control the execution of each
89service (for example, the op-power-start.service will run prior to the
90op-wait-power-on.service). These dependencies are set using targets and the
91Wants,Before,After keywords.
92
93## Server Power Off (Soft)
94
95The soft server power off function is encapsulated in the
96`obmc-host-shutdown@.target`. This target is soft in that it notifies the host
97of the power off request and gives it a certain amount of time to shut itself
98down.
99
100## Server Power Off (Hard)
101
102The hard server power off is encapsulated in the
103`obmc-chassis-hard-poweroff@.target`. This target will force the stopping of the
104soft power off service if running, and immediately cut power to the system.
105
106## Server Reboot
107
108The reboot of the server is encapsulated in the `obmc-host-reboot@.target`. This
109target will utilize the soft power off target and then, once that completes,
110start the host power on target.
111
112## Server Quiesce
113
114The `obmc-host-quiesce@.target` is utilized in host error scenarios. When the
115`obmc-host-quiesce@0.target` is entered, it puts the host state D-Bus
116[object](https://github.com/openbmc/phosphor-dbus-interfaces/blob/master/yaml/xyz/openbmc_project/State/Host.interface.yaml)
117in a `Quiesced` state.
118
119## Server Emergency Power Off due to Error
120
121The `obmc-chassis-emergency-poweroff@.target` is a wrapper target around the
122`obmc-chassis-hard-poweroff@.target` and `obmc-host-quiesce@.target`. It is
123utilized by applications that have detected critical thermal or power errors
124which require an immediate shutdown of the system. It will turn the chassis off
125and put the host into Quiesce (if the host is running). Certain non-critical
126services in the shutdown path can conflict with this target to ensure only the
127most critical services are run in this path.
128
129Automated error recovery (i.e. host reboot) will not be done if this target is
130started. User intervention is required to exit from it. The user could request a
131power on if they wished or a host stop / power off if they wanted to get out of
132quiesce.
133
134## Systemd Control in OpenBMC
135
136There are a collection of services within OpenBMC that interact with systemd and
137its unit files, providing somewhat of an abstraction layer from the user of the
138OpenBMC system and systemd. See the
139[state](https://github.com/openbmc/phosphor-dbus-interfaces/tree/master/yaml/xyz/openbmc_project/State)
140interfaces for more information on this function.
141
142For example, if you wanted to execute the server power on function, you would do
143the following:
144
145> busctl set-property xyz.openbmc_project.State.Host
146> /xyz/openbmc_project/state/host0 xyz.openbmc_project.State.Host
147> RequestedHostTransition s xyz.openbmc_project.State.Host.Transition.On
148
149Underneath the covers, this is calling systemd with the server power on target.
150
151## Systemd Services or Monitoring Applications
152
153A common question when creating new OpenBMC applications which need to execute
154some logic in the context of systemd targets is whether they should be triggered
155by systemd services or by monitoring for the appropriate D-Bus signal indicating
156the start/stop of the target they are interested in.
157
158The basic guidelines for when to create a systemd service are the following:
159
160- If your application logic depends on other systemd based services then make it
161  a systemd service and utilize the Wants/After/Before service capabilities.
162- If other applications depend on your application logic then it should be a
163  systemd service.
164- If your application failing during the target start could impact targets or
165  services that run after it, then it should be a systemd service. This ensures
166  dependent targets are not started if your application fails.
167
168## Error Handling of Systemd
169
170With great numbers of targets and services, come great chances for failures. To
171make OpenBMC a robust and productive system, it needs to be sure to have an
172error handling policy for when services and their targets fail.
173
174When a failure occurs, the OpenBMC software needs to notify the users of the
175system and provide mechanisms for either the system to automatically retry the
176failed operation (i.e. reboot the system) or to stay in a quiesced state so that
177error data can be collected and the failure can be investigated.
178
179There are two main failure scenarios when it comes to OpenBMC and systemd usage:
180
1811. A service within a target fails
182
183- If the service is a "oneshot" type, and the service is required (not wanted)
184  by the target then the target will fail if the service fails - Define a
185  behavior for when the target fails using the "OnFailure" option (i.e. go to a
186  new failure target if any required service fails)
187- If the service is not a "oneshot", then it can not fail the target (the target
188  only knows that it started successfully) - Define a behavior for when the
189  service fails (OnFailure) option. - The service can not have
190  "RemainAfterExit=yes" otherwise, the OnFailure action does not occur until the
191  service is stopped (instead of when it fails) - \*See more information below
192  on [RemainAfterExit](#RemainAfterExit)
193
1942. A failure outside of a normal systemd target/service (host watchdog expires,
195   host checkstop detected)
196
197- The service which detects this failure is responsible for logging the
198  appropriate error, and instructing systemd to go to the appropriate target
199
200Within OpenBMC, there is a host quiesce target. This is the target that other
201host related targets should go to when they hit a failure. Other software within
202OpenBMC can then monitor for the entry into this quiesce target and will handle
203the halt vs. automatic reboot functionality.
204
205Targets which are not host related, will need special thought in regards to
206their error handling. For example, the target responsible for applying chassis
207power, `obmc-chassis-poweron@0.target`, will have a
208`OnFailure=obmc-chassis-poweroff@%i.target` error path. That is, if the chassis
209power on target fails then power off the chassis.
210
211The above info sets up some general **guidelines** for our host related targets
212and services:
213
214- All targets should have a `OnFailure=obmc-quiesce-host@.target`
215- All services which are required for a target to achieve its function should be
216  RequiredBy that target (not WantedBy)
217- All services should first try to be "Type=oneshot" so that we can just rely on
218  the target failure path
219- If a service can not be "Type=oneshot", then it needs to have a
220  `OnFailure=obmc-quiesce-host@.target` and ideally set "RemainAfterExit=no"
221  (but see caveats on this below)
222- If a service can not be any of these then it's up to the service application
223  to call systemd with the `obmc-quiesce-host@.target` on failures
224
225### RemainAfterExit
226
227This is set to "yes" for most OpenBMC services to handle the situation where
228someone starts the same target twice. If the associated service with that target
229is not running (i.e. RemainAfterExit=no), then the service will be executed
230again. Think about someone accidentally running the
231`obmc-chassis-poweron@.target` twice. If you execute it when the operating
232system is up and running, and the service which toggles the pgood pin is
233re-executed, you're going to crash your system. Given this info, the goal should
234always be to write "oneshot" services that have RemainAfterExit set to yes.
235
236## Target and Service Dependency Details
237
238There are some tools available out there to visualize systemd service and target
239dependencies (systemd-analyze) but due to the complexity of our design, they do
240not generate anything very useful.
241
242For now, document the current dependencies on a witherspoon system for
243reference.
244
245```
246R = Requires
247W = Wants
248A = After
249B = Before
250S = Start (runs a command to start another target or service)
251(S) = Synchronization Target
252```
253
254### Soft Power Off
255
256```
257obmc-host-shutdown.target
258  R: xyz.openbmc_project.Ipmi.Internal.SoftPowerOff.service
259     W: obmc-host-stopping.target (S)
260     B: obmc-host-stopping.target (S)
261  R: obmc-chassis-poweroff.target
262     R: obmc-host-stop.target
263        R: op-occ-disable.service
264           B: obmc-host-stop-pre.target
265     R: op-power-stop.service
266        W: obmc-power-stop.target (S)
267        B: obmc-power-stop.target (S)
268        W: obmc-power-stop-pre.target (S)
269        A: obmc-power-stop-pre.target (S)
270        W: mapper-wait@-org-openbmc-control-power.service
271        A: mapper-wait@-org-openbmc-control-power.service
272     R: op-wait-power-off.service
273        B: obmc-power-off.target (S)
274        W: obmc-power-stop.target (S)
275        B: obmc-power-stop.target (S)
276        W: obmc-power-stop-pre.target (S)
277        A: obmc-power-stop-pre.target (S)
278        W: mapper-wait@-org-openbmc-control-power.service
279        A: mapper-wait@-org-openbmc-control-power.service
280     R: op-powered-off.service
281        A: op-wait-power-off.service
282        R: op-wait-power-off.service
283        S: obmc-chassis-powered-off.target
284     W: pcie-poweroff.service
285        B: op-power-stop.service
286        A: obmc-power-stop-pre@.target
287```
288
289#### Synchronization Target Dependencies
290
291```
292obmc-power-stop.target
293  W: obmc-power-stop-pre.target
294  A: obmc-power-stop-pre.target
295
296obmc-power-stop-pre.target
297  W: obmc-host-stopped.target
298  A: obmc-host-stopped.target
299
300obmc-host-stopped.target
301  W: obmc-host-stopping.target
302  A: obmc-host-stopping.target
303  B: obmc-power-stop-pre.target
304
305obmc-host-stopping.target
306  W: obmc-host-stop-pre.target
307  A: obmc-host-stop-pre.target
308  B: obmc-host-stopped.target
309
310obmc-host-stop-pre.target
311  B: obmc-host-stopping.target
312```
313