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