-
Notifications
You must be signed in to change notification settings - Fork 23
/
host_reset_recovery.cpp
185 lines (151 loc) · 5.57 KB
/
host_reset_recovery.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "config.h"
#include <unistd.h>
#include <phosphor-logging/elog.hpp>
#include <phosphor-logging/lg2.hpp>
#include <sdbusplus/bus.hpp>
#include <sdbusplus/exception.hpp>
#include <xyz/openbmc_project/Logging/Create/client.hpp>
#include <xyz/openbmc_project/Logging/Entry/client.hpp>
#include <xyz/openbmc_project/State/Boot/Progress/client.hpp>
#include <cstdlib>
#include <format>
#include <fstream>
#include <string>
namespace phosphor
{
namespace state
{
namespace manager
{
PHOSPHOR_LOG2_USING;
using BootProgress =
sdbusplus::client::xyz::openbmc_project::state::boot::Progress<>;
using LoggingCreate =
sdbusplus::client::xyz::openbmc_project::logging::Create<>;
using LoggingEntry = sdbusplus::client::xyz::openbmc_project::logging::Entry<>;
constexpr auto HOST_STATE_SVC = "xyz.openbmc_project.State.Host";
constexpr auto HOST_STATE_PATH = "/xyz/openbmc_project/state/host0";
constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties";
constexpr auto BOOT_PROGRESS_PROP = "BootProgress";
constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
constexpr auto HOST_STATE_QUIESCE_TGT = "[email protected]";
bool wasHostBooting(sdbusplus::bus_t& bus)
{
try
{
using ProgressStages = BootProgress::ProgressStages;
auto method = bus.new_method_call(HOST_STATE_SVC, HOST_STATE_PATH,
PROPERTY_INTERFACE, "Get");
method.append(BootProgress::interface, BOOT_PROGRESS_PROP);
auto response = bus.call(method);
std::variant<ProgressStages> bootProgressV;
response.read(bootProgressV);
auto bootProgress = std::get<ProgressStages>(bootProgressV);
if (bootProgress == ProgressStages::Unspecified)
{
info("Host was not booting before BMC reboot");
return false;
}
info("Host was booting before BMC reboot: {BOOTPROGRESS}",
"BOOTPROGRESS", bootProgress);
}
catch (const sdbusplus::exception_t& e)
{
error("Error reading BootProgress, error {ERROR}, service {SERVICE}, "
"path {PATH}",
"ERROR", e, "SERVICE", HOST_STATE_SVC, "PATH", HOST_STATE_PATH);
throw;
}
return true;
}
void createErrorLog(sdbusplus::bus_t& bus)
{
try
{
// Create interface requires something for additionalData
std::map<std::string, std::string> additionalData;
additionalData.emplace("_PID", std::to_string(getpid()));
static constexpr auto errorMessage =
"xyz.openbmc_project.State.Error.HostNotRunning";
auto method = bus.new_method_call(LoggingCreate::default_service,
LoggingCreate::instance_path,
LoggingCreate::interface, "Create");
method.append(errorMessage, LoggingEntry::Level::Error, additionalData);
auto resp = bus.call(method);
}
catch (const sdbusplus::exception_t& e)
{
error(
"sdbusplus D-Bus call exception, error {ERROR}, objpath {OBJPATH}, "
"interface {INTERFACE}",
"ERROR", e, "OBJPATH", LoggingCreate::instance_path, "INTERFACE",
LoggingCreate::interface);
throw std::runtime_error(
"Error in invoking D-Bus logging create interface");
}
catch (const std::exception& e)
{
error("D-bus call exception: {ERROR}", "ERROR", e);
throw e;
}
}
// Once CHASSIS_ON_FILE is removed, the [email protected] has
// completed and the phosphor-chassis-state-manager code has processed it.
bool isChassisTargetComplete()
{
auto chassisFile = std::format(CHASSIS_ON_FILE, 0);
std::ifstream f(chassisFile);
return !f.good();
}
void moveToHostQuiesce(sdbusplus::bus_t& bus)
{
try
{
auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
SYSTEMD_INTERFACE, "StartUnit");
method.append(HOST_STATE_QUIESCE_TGT);
method.append("replace");
bus.call_noreply(method);
}
catch (const sdbusplus::exception_t& e)
{
error("sdbusplus call exception starting quiesce target: {ERROR}",
"ERROR", e);
throw std::runtime_error(
"Error in invoking D-Bus systemd StartUnit method");
}
}
} // namespace manager
} // namespace state
} // namespace phosphor
int main()
{
using namespace phosphor::state::manager;
PHOSPHOR_LOG2_USING;
auto bus = sdbusplus::bus::new_default();
// Chassis power is on if this service starts but need to wait for the
// [email protected] to complete before potentially initiating
// another systemd target transition (i.e. Quiesce->Reboot)
while (!isChassisTargetComplete())
{
debug("Waiting for chassis on target to complete");
std::this_thread::sleep_for(std::chrono::seconds(1));
// There is no timeout here, wait until it happens or until system
// is powered off and this service is stopped
}
info("Chassis power on has completed, checking if host is "
"still running after the BMC reboot");
// Check the last BootProgeress to see if the host was booting before
// the BMC reboot occurred
if (!wasHostBooting(bus))
{
return 0;
}
// Host was booting before the BMC reboot so log an error and go to host
// quiesce target
createErrorLog(bus);
moveToHostQuiesce(bus);
return 0;
}