-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystemd.go
More file actions
322 lines (272 loc) · 8.29 KB
/
Copy pathsystemd.go
File metadata and controls
322 lines (272 loc) · 8.29 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
SPDX-FileCopyrightText: Copyright 2024 SAP SE or an SAP affiliate company and cobaltcore-dev contributors
SPDX-License-Identifier: Apache-2.0
Licensed under the Apache License, LibVirtVersion 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package systemd
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"strconv"
"syscall"
v1 "github.com/cobaltcore-dev/openstack-hypervisor-operator/api/v1"
systemd "github.com/coreos/go-systemd/v22/dbus"
"github.com/godbus/dbus/v5"
logger "sigs.k8s.io/controller-runtime/pkg/log"
)
const (
ACTIVE = "active"
ACTIVATING = "activating"
INACTIVE = "inactive"
FAILED = "failed"
)
type SystemdConn struct {
// go-systemd dbus connection
conn *systemd.Conn
// godbus dbus connection for poweroff inhibition
login1conn *dbus.Conn
// godbus dbus object for poweroff inhibition
login1obj dbus.BusObject
// channel for shutdown signal
prepareForShutdownSignal chan *dbus.Signal
// channel for shutdown goroutine
shutdownCh chan bool
// file descriptor for inhibition
fd int
}
var systemdConn *SystemdConn
func dialBus() (*dbus.Conn, error) {
conn, err := dbus.SystemBusPrivate()
if err != nil {
return nil, err
}
methods := []dbus.Auth{
dbus.AuthExternal("0"),
dbus.AuthExternal(strconv.Itoa(os.Getuid())),
dbus.AuthAnonymous(),
}
if err = conn.Auth(methods); err != nil {
_ = conn.Close()
return nil, err
}
if err = conn.Hello(); err != nil {
_ = conn.Close()
return nil, err
}
return conn, nil
}
func NewSystemd(ctx context.Context) (*SystemdConn, error) {
if systemdConn != nil {
return systemdConn, nil
}
log := logger.FromContext(ctx)
log.Info("Connecting to systemd")
conn, err := systemd.NewConnection(dialBus)
if err != nil {
return nil, err
}
// separate connection for systemd inhibition since go-systemd doesn't support it
dbusConn, err := dialBus()
if err != nil {
return nil, fmt.Errorf("failed to connect to dbus: %w", err)
}
systemdConn = &SystemdConn{
conn: conn,
login1conn: dbusConn,
login1obj: dbusConn.Object("org.freedesktop.login1", "/org/freedesktop/login1"),
prepareForShutdownSignal: make(chan *dbus.Signal, 1),
shutdownCh: make(chan bool),
fd: -1,
}
return systemdConn, nil
}
// EnableShutdownInhibit blocks shutdown by using systemd inhibition lock,
// and registers a shutdown callback
func (s *SystemdConn) EnableShutdownInhibit(ctx context.Context, cb func(context.Context) error) error {
if s.fd != -1 {
return errors.New("shutdown inhibition already enabled")
}
log := logger.Log.WithName("systemd")
log.Info("enabling shutdown inhibition")
// List inhibitors
var inhibitors [][]any
if err := s.login1obj.CallWithContext(
ctx,
"org.freedesktop.login1.Manager.ListInhibitors",
0,
).Store(&inhibitors); err != nil {
return fmt.Errorf("failed to list inhibitors: %w", err)
}
log.Info("existing inhibitors", "inhibitors", inhibitors)
// create inhibitor
if err := s.login1obj.CallWithContext(
ctx,
"org.freedesktop.login1.Manager.Inhibit",
0,
"sleep:shutdown",
"kvm-node-agent",
"Emergency evacuation of host node.",
"delay",
).Store(&s.fd); err != nil {
// ignore error if not running in k8s, so we can debug remotely
return fmt.Errorf("error storing file descriptor: %w", err)
}
log.Info("registering shutdown callback")
go func() {
for {
select {
case <-s.shutdownCh:
log.Info("stopping shutdown callback goroutine")
return
case signal, ok := <-s.prepareForShutdownSignal:
if !ok {
log.Info("prepareForShutdownSignal channel closed")
return
}
log.Info("received shutdown signal", "signal", signal)
// execute the shutdown callback
if err := cb(ctx); err != nil {
log.Error(err, "failed to execute shutdown callback")
}
log.Info("releasing shutdown inhibition")
// release the inhibition lock to continue shutdown
if err := s.DisableShutdownInhibit(); err != nil {
log.Error(err, "failed to release shutdown inhibition")
}
return
}
}
}()
// register signal handler
if err := s.login1conn.AddMatchSignal(
dbus.WithMatchInterface("org.freedesktop.login1.Manager"),
dbus.WithMatchObjectPath("/org/freedesktop/login1"),
dbus.WithMatchMember("PrepareForShutdown"),
); err != nil {
return fmt.Errorf("failed to add match signal: %w", err)
}
s.login1conn.Signal(s.prepareForShutdownSignal)
return nil
}
// DisableShutdownInhibit releases the systemd inhibition lock
func (s *SystemdConn) DisableShutdownInhibit() error {
log := logger.Log.WithName("systemd")
log.Info("disabling shutdown inhibition")
if s.fd == -1 {
// nothing to do
return nil
}
// remove signal handler
s.login1conn.RemoveSignal(s.prepareForShutdownSignal)
// stopping the shutdown callback goroutine
s.shutdownCh <- true
err := syscall.Close(s.fd)
if err != nil {
return fmt.Errorf("failed to close file descriptor: %w", err)
}
s.fd = -1
return nil
}
func (s *SystemdConn) Close() {
s.conn.Close()
_ = s.login1conn.Close()
}
func (s *SystemdConn) IsConnected() bool {
return s.conn.Connected() && s.login1conn.Connected()
}
func (s *SystemdConn) ListUnitsByNames(ctx context.Context, units []string) ([]systemd.UnitStatus, error) {
return s.conn.ListUnitsByNamesContext(ctx, units)
}
func (s *SystemdConn) GetUnitByName(ctx context.Context, unit string) (systemd.UnitStatus, error) {
units, err := s.ListUnitsByNames(ctx, []string{unit})
if err != nil {
return systemd.UnitStatus{}, err
}
if len(units) == 0 {
return systemd.UnitStatus{}, nil
}
return units[0], nil
}
func (s *SystemdConn) StartUnit(ctx context.Context, unit string) (int, error) {
return s.conn.StartUnitContext(ctx, unit, "replace", nil)
}
func (s *SystemdConn) ReloadUnit(ctx context.Context, unit string) (int, error) {
return s.conn.ReloadUnitContext(ctx, unit, "replace", nil)
}
var ErrFailed = errors.New("update has failed")
// ReconcileSysUpdate orchestrates a systemd-sysupdate via the systemd-sysupdate@.service unit.
func (s *SystemdConn) ReconcileSysUpdate(ctx context.Context, hv *v1.Hypervisor) (bool, error) {
version := hv.Spec.OperatingSystemVersion
log := logger.FromContext(ctx, "systemd", "reconcileSysUpdate", "version", version)
// Needs to be connected to systemd
if !s.IsConnected() {
return false, errors.New("not connected to systemd")
}
unit := fmt.Sprintf("systemd-sysupdate@%s.service", version)
if version == "latest" {
unit = "systemd-sysupdate.service"
}
status, err := s.GetUnitByName(ctx, unit)
if err != nil {
return false, err
}
// Check if the update is already running
if hv.Status.Update.InProgress {
switch status.ActiveState {
case ACTIVE, ACTIVATING:
log.Info("update is running")
case FAILED:
log.Info("Update has failed")
return false, fmt.Errorf("%s %w", version, ErrFailed)
case INACTIVE:
// Update has finished successfully
if hv.Spec.Reboot {
if _, err = s.StartUnit(ctx, "systemd-sysupdate-reboot.target"); err != nil {
return false, err
}
}
}
} else {
if status.ActiveState == ACTIVE {
log.Info("An update is already running, ignoring")
} else {
// Start the update
log.Info("starting update")
if _, err = s.StartUnit(ctx, unit); err != nil {
return false, err
}
return true, nil
}
}
return status.ActiveState == ACTIVE, nil
}
func (s *SystemdConn) Describe(ctx context.Context) (*Descriptor, error) {
// Get descriptor
var res []byte
if err := s.login1conn.
Object("org.freedesktop.hostname1", "/org/freedesktop/hostname1").
CallWithContext(
ctx,
"org.freedesktop.hostname1.Describe",
0,
).Store(&res); err != nil {
return nil, fmt.Errorf("failed to fetch hostname descriptor: %w", err)
}
// Parse descriptor
var desc Descriptor
if err := json.Unmarshal(res, &desc); err != nil {
return nil, fmt.Errorf("failed to unmarshal hostname descriptor: %w", err)
}
return &desc, nil
}